This video comes from The Joy of Automating series, hosted by Franklin Webber.
In this episode we focus on working with converting a script to a recipe using Chef community cookbooks. Community cookbooks are powerful. Learning how to leverage them will help increase the speed at which you work. We will create a cookbook that deploys Jenkins.
In this episode we focus on taking the manual installation instructions found in this tutorial and create a recipe that does the work for us.
The instructions followed in this exercise and the resulting code will no longer work for the current versions of Jenkins (2.89.3). Jenkins requires a Java 8. To install Java 8 on Ubuntu 14.04 requires additional setup that is described below the video.
The apt_repository resource became part of core Chef in Chef Client 12.9. So you will not see this custom resource defined in the apt cookbook.
Installing Java 8 on Ubuntu 14.04
Ubuntu 14.04 does not provide Java 8 through existing package repositories so you need to add a new package repository, update the repository, and then install the package.
Editor: Installation of Java 8
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:openjdk-r/ppa
$ sudo apt-get update
$ sudo apt-get install openjdk-8-jre-headless
Refactoring the Java installation
When you are done with the initial implementation, we encourage you to refactor your initial implementation for installing java.
First, ensure that the resources take action only when they need to which will require resources to work together through notifications and for a resource to define guards.
This command to add the openjdk repository to the package manager,
sudo add-apt-repository ppa:openjdk-r/ppa, translates directly into Chef as
execute 'add-apt-repository ppa:openjdk-r/ppa'. The
execute resource takes the :run action by default so this command will happen each time the system is converged. Adding a guard, defines a test that the resource will make, before attempting to repair the state of the system. This command
add-apt-repository ppa:openjdk-r/ppa results in the creation of this file
/etc/apt/sources.list.d/openjdk-r-ppa-trusty.list. Define a not_if guard within this resource to check for the existence of this file to prevent it from taking action.
With the guard defined you will want to next have this resource notify the
execute 'apt-get update' when it takes action. This means you want the
execute 'apt-get update' resource to by default not take any action. This could leave you with a recipe that contains two identical resources
execute 'apt-get update', the first one being used to update the package manager after adding the Jenkins repository. If that is the case, choose one of them and place it before the Java and Jenkins packages are installed.
Second, the java installation can also be refactored to use the java community cookbook. Add to your cookbook's metadata a dependency on the java cookbook. Define the attributes to install the appropriate install flavor, packages, and jdk version. Then replace all the resources installing Java within your recipe with the Java cookbook's default recipe.
default['java']['install_flavor'] = 'openjdk'
default['java']['openjdk_packages'] = [ 'openjdk-8-jre-headless' ]
default['java']['jdk_version'] = '8'
Provide configuration for Jenkins
We started to explore the Jenkins implementation in the community cookbook. We copied the implementation except for the remaining template resource that allows us to configure Jenkins.
Find that original file on the test instance or use the template provided in the community cookbook and bring it into the cookbook you developed. Start with the hard-coded values and then slowly start to replace them template variables.