Skip to content

Commit 00d6523

Browse files
committed
Merge pull request #4 from chef-cookbooks/jtimberman/issue-3
Fixes #3, allow latest and remove package_name
2 parents bde563e + 315cc91 commit 00d6523

9 files changed

+127
-45
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ A "chef ingredient" is the core package itself, or products or add-on components
4040

4141
#### Properties
4242
- `product_name`: (name attribute) The product name. See the [PRODUCT_MATRIX.md](https://github.com/chef-cookbooks/chef-ingredient/blob/master/PRODUCT_MATRIX.md). For example, `chef-server`, `analytics`, `delivery`, `manage`, etc.
43-
- `package_name`: The name of the package in the repository. Should correspond to the published package names (`chef-server-core`, `opscode-manage`, etc).
4443
- `ctl_command`: The "ctl" command, e.g., `chef-server-ctl`. This should be automatically detected by the library helper method `chef_ctl_command`, but may need to be specified if something changes, like a new add-on is made available.
4544
- `options`: Options passed to the `package` resource used for installation.
46-
- `version`: Package version, e.g., `12.0.4`. Do not use if specifying `package_source`. Default `nil`.
45+
- `version`: Package version to install. Can be specified in various semver-alike ways: `12.0.4`, `12.0.3-rc.3`, and also `:latest`/`'latest'`. Do not use this property when specifying `package_source`. Default is `:latest`, which will install the latest package from the repository.
4746
- `package_source`: Full path to a location where the package is located. If present, this file is used for installing the package. Default `nil`.
4847
- `timeout`: The amount of time (in seconds) to wait to fetch the installer before timing out. Default: default timeout of the Chef package resource - `900` seconds.
4948

libraries/chef_ingredient_provider.rb

+6-12
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def whyrun_supported?
3636
action :install do
3737
# We need Mixlib::Versioning in the library helpers for
3838
# parsing the version string.
39-
chef_gem "#{new_resource.product_name}-mixlib-versioning" do
39+
chef_gem "#{new_resource.product_name}-mixlib-versioning" do #~FC009 foodcritic needs an update
4040
package_name 'mixlib-versioning'
4141
compile_time true
4242
end
@@ -48,35 +48,29 @@ def whyrun_supported?
4848

4949
package_resource = new_resource.package_source.nil? ? :package : local_package_resource
5050

51-
pkg_name = if new_resource.package_name
52-
new_resource.package_name
53-
else
54-
product_lookup(new_resource.product_name, new_resource.version)['package-name']
55-
end
56-
5751
declare_resource package_resource, new_resource.product_name do
58-
package_name pkg_name
52+
package_name ingredient_package_name
5953
options new_resource.options
60-
version install_version if Mixlib::Versioning.parse(new_resource.version) > '0.0.0'
54+
version install_version if Mixlib::Versioning.parse(version_string(new_resource.version)) > '0.0.0'
6155
source new_resource.package_source
6256
timeout new_resource.timeout
6357
end
6458
end
6559

6660
action :uninstall do
67-
package new_resource.package_name do
61+
package ingredient_package_name do
6862
action :remove
6963
end
7064
end
7165

7266
action :remove do
73-
package new_resource.package_name do
67+
package ingredient_package_name do
7468
action :remove
7569
end
7670
end
7771

7872
action :reconfigure do
79-
execute "#{new_resource.package_name}-reconfigure" do
73+
execute "#{ingredient_package_name}-reconfigure" do
8074
command "#{ctl_command} reconfigure"
8175
end
8276
end

libraries/chef_ingredient_resource.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class ChefIngredient < Chef::Resource::LWRPBase
2424
state_attrs :installed
2525

2626
attribute :product_name, kind_of: String, name_attribute: true
27-
attribute :package_name, kind_of: String
2827
attribute :installed, kind_of: [TrueClass, FalseClass, NilClass], default: false
2928
attribute :reconfigure, kind_of: [TrueClass, FalseClass], default: false
3029
attribute :config, kind_of: [Hash, Mash], default: {}
@@ -37,7 +36,7 @@ class ChefIngredient < Chef::Resource::LWRPBase
3736

3837
# Attributes for package
3938
attribute :options, kind_of: String
40-
attribute :version, kind_of: String, default: '0.0.0'
39+
attribute :version, kind_of: [String, Symbol], default: :latest
4140
attribute :timeout, kind_of: [Integer, String, NilClass], default: nil
4241
end
4342
end

libraries/helpers.rb

+30-21
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,21 @@ module ChefIngredientCookbook
1919
module Helpers
2020
def chef_ctl_command(product)
2121
if new_resource.respond_to?(:version)
22-
product_lookup(product, new_resource.version)['ctl-command']
22+
product_lookup(product, version_string(new_resource.version))['ctl-command']
2323
else
2424
product_lookup(product)['ctl-command']
2525
end
2626
end
2727

28+
def version_string(vers)
29+
return '0.0.0' if vers.to_sym == :latest
30+
vers
31+
end
32+
33+
def ingredient_package_name
34+
product_lookup(new_resource.product_name, version_string(new_resource.version))['package-name']
35+
end
36+
2837
def local_package_resource
2938
return :dpkg_package if node['platform_family'] == 'debian'
3039
return :rpm_package if node['platform_family'] == 'rhel'
@@ -43,7 +52,7 @@ def rhel_major_version
4352

4453
def install_version
4554
require 'mixlib/versioning'
46-
v = Mixlib::Versioning.parse(new_resource.version)
55+
v = Mixlib::Versioning.parse(version_string(new_resource.version))
4756
version = "#{v.major}.#{v.minor}.#{v.patch}"
4857
version << "~#{v.prerelease}" if v.prerelease? && !v.prerelease.match(/^\d$/)
4958
version << "+#{v.build}" if v.build?
@@ -69,33 +78,33 @@ def cleanup_old_repo_config
6978
end
7079

7180
def ctl_command
72-
new_resource.ctl_command || chef_ctl_command(new_resource.package_name)
81+
new_resource.ctl_command || chef_ctl_command(new_resource.product_name)
7382
end
7483

7584
def reconfigure
7685
ctl_cmd = ctl_command
77-
execute "#{new_resource.package_name}-reconfigure" do
86+
execute "#{new_resource.product_name}-reconfigure" do
7887
command "#{ctl_cmd} reconfigure"
7988
end
8089
end
8190

8291
# When updating this, also update PRODUCT_MATRIX.md
8392
def product_matrix
8493
{
85-
'analytics' => {'package-name' => 'opscode-analytics', 'ctl-command' => 'opscode-analytics-ctl' },
86-
'chef' => {'package-name' => 'chef', 'ctl-command' => nil },
87-
'chef-ha' => {'package-name' => 'chef-ha', 'ctl-command' => nil },
88-
'chef-server' => {'package-name' => 'chef-server-core', 'ctl-command' => 'chef-server-ctl' },
89-
'chef-sync' => {'package-name' => 'chef-sync', 'ctl-command' => 'chef-sync-ctl' },
90-
'chefdk' => {'package-name' => 'chefdk', 'ctl-command' => nil },
91-
'delivery' => {'package-name' => 'delivery', 'ctl-command' => 'delivery-ctl' },
92-
'delivery-cli' => {'package-name' => 'delivery-cli', 'ctl-command' => nil },
93-
'manage' => {'package-name' => 'chef-manage', 'ctl-command' => 'chef-manage-ctl' },
94-
'private-chef' => {'package-name' => 'private-chef', 'ctl-command' => 'private-chef-ctl' },
95-
'push-client' => {'package-name' => 'chef-push-client', 'ctl-command' => nil },
96-
'push-server' => {'package-name' => 'chef-push-server', 'ctl-command' => 'chef-push-ctl' },
97-
'reporting' => {'package-name' => 'opscode-reporting', 'ctl-command' => 'opscode-reporting-ctl' },
98-
'supermarket' => {'package-name' => 'supermarket', 'ctl-command' => 'supermarket-ctl' },
94+
'analytics' => { 'package-name' => 'opscode-analytics', 'ctl-command' => 'opscode-analytics-ctl' },
95+
'chef' => { 'package-name' => 'chef', 'ctl-command' => nil },
96+
'chef-ha' => { 'package-name' => 'chef-ha', 'ctl-command' => nil },
97+
'chef-server' => { 'package-name' => 'chef-server-core', 'ctl-command' => 'chef-server-ctl' },
98+
'chef-sync' => { 'package-name' => 'chef-sync', 'ctl-command' => 'chef-sync-ctl' },
99+
'chefdk' => { 'package-name' => 'chefdk', 'ctl-command' => nil },
100+
'delivery' => { 'package-name' => 'delivery', 'ctl-command' => 'delivery-ctl' },
101+
'delivery-cli' => { 'package-name' => 'delivery-cli', 'ctl-command' => nil },
102+
'manage' => { 'package-name' => 'chef-manage', 'ctl-command' => 'chef-manage-ctl' },
103+
'private-chef' => { 'package-name' => 'private-chef', 'ctl-command' => 'private-chef-ctl' },
104+
'push-client' => { 'package-name' => 'chef-push-client', 'ctl-command' => nil },
105+
'push-server' => { 'package-name' => 'chef-push-server', 'ctl-command' => 'chef-push-ctl' },
106+
'reporting' => { 'package-name' => 'opscode-reporting', 'ctl-command' => 'opscode-reporting-ctl' },
107+
'supermarket' => { 'package-name' => 'supermarket', 'ctl-command' => 'supermarket-ctl' }
99108
}
100109
end
101110

@@ -104,14 +113,14 @@ def product_matrix
104113
# "latest", but "latest" is not a value that is valid for
105114
# mixlib/versioning.
106115
def product_lookup(product, version = '0.0.0')
107-
unless product_matrix.has_key?(product)
116+
unless product_matrix.key?(product)
108117
Chef::Log.fatal("We don't have a product, '#{product}'. Please specify a valid product name:")
109118
Chef::Log.fatal(product_matrix.keys.join(' '))
110119
fail
111120
end
112121

113122
require 'mixlib/versioning'
114-
v = Mixlib::Versioning.parse(version)
123+
v = Mixlib::Versioning.parse(version_string(version))
115124

116125
data = product_matrix[product]
117126

@@ -121,7 +130,7 @@ def product_lookup(product, version = '0.0.0')
121130
# bottom version is something valid. If we don't have the check
122131
# in the elsif, it will say that 0.0.0 is not a valid version.
123132
if (product == 'chef-server')
124-
if (v < Mixlib::Versioning.parse('12.0.0') && v > Mixlib::Versioning.parse('11.0.0'))
133+
if (v < Mixlib::Versioning.parse('12.0.0')) && (v > Mixlib::Versioning.parse('11.0.0'))
125134
data['package-name'] = 'chef-server'
126135
elsif (v < Mixlib::Versioning.parse('11.0.0')) && (v > Mixlib::Versioning.parse('1.0.0'))
127136
Chef::Log.fatal("Invalid version specified, '#{version}' for #{product}!")

libraries/omnibus_service_provider.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ def whyrun_supported?
2929
true
3030
end
3131

32-
%i[start stop restart hup int kill graceful-kill once].each do |sv_command|
32+
%i(start stop restart hup int kill graceful-kill once).each do |sv_command|
3333
action sv_command do
34-
execute "#{get_ctl_command} #{sv_command} #{get_service_properties.last}"
34+
execute "#{omnibus_ctl_command} #{sv_command} #{omnibus_service_name.last}"
3535
end
3636
end
3737

3838
private
39-
def get_ctl_command
40-
new_resource.ctl_command || chef_ctl_command(get_service_properties.first)
39+
40+
def omnibus_ctl_command
41+
new_resource.ctl_command || chef_ctl_command(omnibus_service_name.first)
4142
end
4243

43-
def get_service_properties
44+
def omnibus_service_name
4445
new_resource.service_name.split('/')
4546
end
4647
end

libraries/omnibus_service_resource.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class Resource
1919
class OmnibusService < Chef::Resource::LWRPBase
2020
self.resource_name = 'omnibus_service'
2121

22-
actions %i[start stop restart hup int kill graceful_kill once]
22+
actions %i(start stop restart hup int kill graceful_kill once)
2323
default_action :nothing
2424

2525
attribute :ctl_command, kind_of: String
26-
attribute :service_name, kind_of: String, regex: %r{[\w-]+/[\w-]+}, name_attribute: true
26+
attribute :service_name, kind_of: String, regex: /[\w-]+\/[\w-]+/, name_attribute: true
2727
end
2828
end
2929
end

spec/centos_65_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,46 @@
102102
)
103103
end
104104
end
105+
106+
context '`latest` is specified for the version as a symbol' do
107+
cached(:centos_65) do
108+
ChefSpec::SoloRunner.new(
109+
platform: 'centos',
110+
version: '6.5',
111+
step_into: ['chef_ingredient']
112+
) do |node|
113+
node.set['test']['chef-server-core']['version'] = :latest
114+
end.converge('test::repo')
115+
end
116+
117+
it 'installs chef_ingredient[chef-server]' do
118+
expect(centos_65).to install_chef_ingredient('chef-server')
119+
end
120+
121+
it 'installs yum_package[chef-server]' do
122+
expect(centos_65).to install_yum_package('chef-server-core')
123+
end
124+
end
125+
126+
context '`latest` is specified for the version as a string' do
127+
cached(:centos_65) do
128+
ChefSpec::SoloRunner.new(
129+
platform: 'centos',
130+
version: '6.5',
131+
step_into: ['chef_ingredient']
132+
) do |node|
133+
node.set['test']['chef-server-core']['version'] = 'latest'
134+
end.converge('test::repo')
135+
end
136+
137+
it 'installs chef_ingredient[chef-server]' do
138+
expect(centos_65).to install_chef_ingredient('chef-server')
139+
end
140+
141+
it 'installs yum_package[chef-server]' do
142+
expect(centos_65).to install_yum_package('chef-server-core')
143+
end
144+
end
105145
end
106146

107147
describe 'test::local on centos' do

spec/omnibus_service_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
).converge(described_recipe)
88
end
99

10-
let(:log_message) { chef_run.log('I tell nginx to stop')}
10+
let(:log_message) { chef_run.log('I tell nginx to stop') }
1111

1212
it 'allows the chef-server-core/rabbitmq service to restart' do
1313
expect(chef_run).to restart_omnibus_service('chef-server/rabbitmq')

spec/ubuntu_1404_spec.rb

+40
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,46 @@
102102
)
103103
end
104104
end
105+
106+
context '`latest` is specified for the version as a symbol' do
107+
cached(:ubuntu_1404) do
108+
ChefSpec::SoloRunner.new(
109+
platform: 'ubuntu',
110+
version: '14.04',
111+
step_into: ['chef_ingredient']
112+
) do |node|
113+
node.set['test']['chef-server-core']['version'] = :latest
114+
end.converge('test::repo')
115+
end
116+
117+
it 'installs chef_ingredient[chef-server]' do
118+
expect(ubuntu_1404).to install_chef_ingredient('chef-server')
119+
end
120+
121+
it 'installs yum_package[chef-server]' do
122+
expect(ubuntu_1404).to install_apt_package('chef-server-core')
123+
end
124+
end
125+
126+
context '`latest` is specified for the version as a string' do
127+
cached(:ubuntu_1404) do
128+
ChefSpec::SoloRunner.new(
129+
platform: 'ubuntu',
130+
version: '14.04',
131+
step_into: ['chef_ingredient']
132+
) do |node|
133+
node.set['test']['chef-server-core']['version'] = 'latest'
134+
end.converge('test::repo')
135+
end
136+
137+
it 'installs chef_ingredient[chef-server]' do
138+
expect(ubuntu_1404).to install_chef_ingredient('chef-server')
139+
end
140+
141+
it 'installs yum_package[chef-server]' do
142+
expect(ubuntu_1404).to install_apt_package('chef-server-core')
143+
end
144+
end
105145
end
106146

107147
describe 'test::local on ubuntu' do

0 commit comments

Comments
 (0)