Information Technology Reference
In-Depth Information
Remember that you have the full power of Ruby classes and methods available to you in
your Chef recipes, so don't be afraid to use it. For example, you can use the
::File.basename()
method to extract the package name from the URL. The package name
is the last component of the URL after the forward slash (“/”):
private-
chef-11.1.8-1.el6.x86_64.rpm
. Refer to the
Ruby core API documentation
for more in-
formation on the
::File
class:
package_name
= ::
File
.
basename
(
package_url
)
Unfortunately, the
package
resource does not work with URLs, so we're introducing a new
resource, the
remote_file
resource. The
remote_file
resource will download files from a
remote location. Rather than hardcoding a path like “/tmp” for the package download, Chef
provides a variable you should use instead:
Chef::Config[:file_cache_path]
. Let Chef
choose the best place to store temporary files for you. Pass the local path where you want to
store the file as a string parameter to
remote_file
or as a
name
attribute; in this case, we use
the
package_local_path
variable. The download URL should be passed to
remote_file
as
the
source
attribute.
In order to execute the
chef-server-ctl reconfigure
, we need to introduce another new
resource, the
execute
resource. When you fail to find a resource that meets your needs, you
can use the
execute
resource to run arbitrary shell commands. Pass the shell command you
want to execute as a string parameter to the
execute
resource.
Here's the full code listing shown in
Example 9-3
.
Example 9-3. chefdk/chef-repo/cookbooks/enterprise-chef/recipes/default.rb
#
# Cookbook Name:: enterprise-chef
# Recipe:: default
#
# Copyright (C) 2014
#
#
#
package_url
=
node
[
'enterprise-chef'
][
'url'
]
package_name
= ::
File
.
basename
(
package_url
)
package_local_path
=
"
#{
Chef
:
:Config
[
:file_cache_path
]
}
/
#{
package_name
}
"