Skip to content

Commit

Permalink
Merge pull request #4 from chef-cookbooks/jtimberman/issue-3
Browse files Browse the repository at this point in the history
Fixes #3, allow latest and remove package_name
  • Loading branch information
jtimberman committed Jun 23, 2015
2 parents bde563e + 315cc91 commit 00d6523
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 45 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ A "chef ingredient" is the core package itself, or products or add-on components

#### Properties
- `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.
- `package_name`: The name of the package in the repository. Should correspond to the published package names (`chef-server-core`, `opscode-manage`, etc).
- `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.
- `options`: Options passed to the `package` resource used for installation.
- `version`: Package version, e.g., `12.0.4`. Do not use if specifying `package_source`. Default `nil`.
- `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.
- `package_source`: Full path to a location where the package is located. If present, this file is used for installing the package. Default `nil`.
- `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.

Expand Down
18 changes: 6 additions & 12 deletions libraries/chef_ingredient_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def whyrun_supported?
action :install do
# We need Mixlib::Versioning in the library helpers for
# parsing the version string.
chef_gem "#{new_resource.product_name}-mixlib-versioning" do
chef_gem "#{new_resource.product_name}-mixlib-versioning" do #~FC009 foodcritic needs an update
package_name 'mixlib-versioning'
compile_time true
end
Expand All @@ -48,35 +48,29 @@ def whyrun_supported?

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

pkg_name = if new_resource.package_name
new_resource.package_name
else
product_lookup(new_resource.product_name, new_resource.version)['package-name']
end

declare_resource package_resource, new_resource.product_name do
package_name pkg_name
package_name ingredient_package_name
options new_resource.options
version install_version if Mixlib::Versioning.parse(new_resource.version) > '0.0.0'
version install_version if Mixlib::Versioning.parse(version_string(new_resource.version)) > '0.0.0'
source new_resource.package_source
timeout new_resource.timeout
end
end

action :uninstall do
package new_resource.package_name do
package ingredient_package_name do
action :remove
end
end

action :remove do
package new_resource.package_name do
package ingredient_package_name do
action :remove
end
end

action :reconfigure do
execute "#{new_resource.package_name}-reconfigure" do
execute "#{ingredient_package_name}-reconfigure" do
command "#{ctl_command} reconfigure"
end
end
Expand Down
3 changes: 1 addition & 2 deletions libraries/chef_ingredient_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ChefIngredient < Chef::Resource::LWRPBase
state_attrs :installed

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

# Attributes for package
attribute :options, kind_of: String
attribute :version, kind_of: String, default: '0.0.0'
attribute :version, kind_of: [String, Symbol], default: :latest
attribute :timeout, kind_of: [Integer, String, NilClass], default: nil
end
end
Expand Down
51 changes: 30 additions & 21 deletions libraries/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ module ChefIngredientCookbook
module Helpers
def chef_ctl_command(product)
if new_resource.respond_to?(:version)
product_lookup(product, new_resource.version)['ctl-command']
product_lookup(product, version_string(new_resource.version))['ctl-command']
else
product_lookup(product)['ctl-command']
end
end

def version_string(vers)
return '0.0.0' if vers.to_sym == :latest
vers
end

def ingredient_package_name
product_lookup(new_resource.product_name, version_string(new_resource.version))['package-name']
end

def local_package_resource
return :dpkg_package if node['platform_family'] == 'debian'
return :rpm_package if node['platform_family'] == 'rhel'
Expand All @@ -43,7 +52,7 @@ def rhel_major_version

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

def ctl_command
new_resource.ctl_command || chef_ctl_command(new_resource.package_name)
new_resource.ctl_command || chef_ctl_command(new_resource.product_name)
end

def reconfigure
ctl_cmd = ctl_command
execute "#{new_resource.package_name}-reconfigure" do
execute "#{new_resource.product_name}-reconfigure" do
command "#{ctl_cmd} reconfigure"
end
end

# When updating this, also update PRODUCT_MATRIX.md
def product_matrix
{
'analytics' => {'package-name' => 'opscode-analytics', 'ctl-command' => 'opscode-analytics-ctl' },
'chef' => {'package-name' => 'chef', 'ctl-command' => nil },
'chef-ha' => {'package-name' => 'chef-ha', 'ctl-command' => nil },
'chef-server' => {'package-name' => 'chef-server-core', 'ctl-command' => 'chef-server-ctl' },
'chef-sync' => {'package-name' => 'chef-sync', 'ctl-command' => 'chef-sync-ctl' },
'chefdk' => {'package-name' => 'chefdk', 'ctl-command' => nil },
'delivery' => {'package-name' => 'delivery', 'ctl-command' => 'delivery-ctl' },
'delivery-cli' => {'package-name' => 'delivery-cli', 'ctl-command' => nil },
'manage' => {'package-name' => 'chef-manage', 'ctl-command' => 'chef-manage-ctl' },
'private-chef' => {'package-name' => 'private-chef', 'ctl-command' => 'private-chef-ctl' },
'push-client' => {'package-name' => 'chef-push-client', 'ctl-command' => nil },
'push-server' => {'package-name' => 'chef-push-server', 'ctl-command' => 'chef-push-ctl' },
'reporting' => {'package-name' => 'opscode-reporting', 'ctl-command' => 'opscode-reporting-ctl' },
'supermarket' => {'package-name' => 'supermarket', 'ctl-command' => 'supermarket-ctl' },
'analytics' => { 'package-name' => 'opscode-analytics', 'ctl-command' => 'opscode-analytics-ctl' },
'chef' => { 'package-name' => 'chef', 'ctl-command' => nil },
'chef-ha' => { 'package-name' => 'chef-ha', 'ctl-command' => nil },
'chef-server' => { 'package-name' => 'chef-server-core', 'ctl-command' => 'chef-server-ctl' },
'chef-sync' => { 'package-name' => 'chef-sync', 'ctl-command' => 'chef-sync-ctl' },
'chefdk' => { 'package-name' => 'chefdk', 'ctl-command' => nil },
'delivery' => { 'package-name' => 'delivery', 'ctl-command' => 'delivery-ctl' },
'delivery-cli' => { 'package-name' => 'delivery-cli', 'ctl-command' => nil },
'manage' => { 'package-name' => 'chef-manage', 'ctl-command' => 'chef-manage-ctl' },
'private-chef' => { 'package-name' => 'private-chef', 'ctl-command' => 'private-chef-ctl' },
'push-client' => { 'package-name' => 'chef-push-client', 'ctl-command' => nil },
'push-server' => { 'package-name' => 'chef-push-server', 'ctl-command' => 'chef-push-ctl' },
'reporting' => { 'package-name' => 'opscode-reporting', 'ctl-command' => 'opscode-reporting-ctl' },
'supermarket' => { 'package-name' => 'supermarket', 'ctl-command' => 'supermarket-ctl' }
}
end

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

require 'mixlib/versioning'
v = Mixlib::Versioning.parse(version)
v = Mixlib::Versioning.parse(version_string(version))

data = product_matrix[product]

Expand All @@ -121,7 +130,7 @@ def product_lookup(product, version = '0.0.0')
# bottom version is something valid. If we don't have the check
# in the elsif, it will say that 0.0.0 is not a valid version.
if (product == 'chef-server')
if (v < Mixlib::Versioning.parse('12.0.0') && v > Mixlib::Versioning.parse('11.0.0'))
if (v < Mixlib::Versioning.parse('12.0.0')) && (v > Mixlib::Versioning.parse('11.0.0'))
data['package-name'] = 'chef-server'
elsif (v < Mixlib::Versioning.parse('11.0.0')) && (v > Mixlib::Versioning.parse('1.0.0'))
Chef::Log.fatal("Invalid version specified, '#{version}' for #{product}!")
Expand Down
11 changes: 6 additions & 5 deletions libraries/omnibus_service_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ def whyrun_supported?
true
end

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

private
def get_ctl_command
new_resource.ctl_command || chef_ctl_command(get_service_properties.first)

def omnibus_ctl_command
new_resource.ctl_command || chef_ctl_command(omnibus_service_name.first)
end

def get_service_properties
def omnibus_service_name
new_resource.service_name.split('/')
end
end
Expand Down
4 changes: 2 additions & 2 deletions libraries/omnibus_service_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class Resource
class OmnibusService < Chef::Resource::LWRPBase
self.resource_name = 'omnibus_service'

actions %i[start stop restart hup int kill graceful_kill once]
actions %i(start stop restart hup int kill graceful_kill once)
default_action :nothing

attribute :ctl_command, kind_of: String
attribute :service_name, kind_of: String, regex: %r{[\w-]+/[\w-]+}, name_attribute: true
attribute :service_name, kind_of: String, regex: /[\w-]+\/[\w-]+/, name_attribute: true
end
end
end
40 changes: 40 additions & 0 deletions spec/centos_65_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,46 @@
)
end
end

context '`latest` is specified for the version as a symbol' do
cached(:centos_65) do
ChefSpec::SoloRunner.new(
platform: 'centos',
version: '6.5',
step_into: ['chef_ingredient']
) do |node|
node.set['test']['chef-server-core']['version'] = :latest
end.converge('test::repo')
end

it 'installs chef_ingredient[chef-server]' do
expect(centos_65).to install_chef_ingredient('chef-server')
end

it 'installs yum_package[chef-server]' do
expect(centos_65).to install_yum_package('chef-server-core')
end
end

context '`latest` is specified for the version as a string' do
cached(:centos_65) do
ChefSpec::SoloRunner.new(
platform: 'centos',
version: '6.5',
step_into: ['chef_ingredient']
) do |node|
node.set['test']['chef-server-core']['version'] = 'latest'
end.converge('test::repo')
end

it 'installs chef_ingredient[chef-server]' do
expect(centos_65).to install_chef_ingredient('chef-server')
end

it 'installs yum_package[chef-server]' do
expect(centos_65).to install_yum_package('chef-server-core')
end
end
end

describe 'test::local on centos' do
Expand Down
2 changes: 1 addition & 1 deletion spec/omnibus_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
).converge(described_recipe)
end

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

it 'allows the chef-server-core/rabbitmq service to restart' do
expect(chef_run).to restart_omnibus_service('chef-server/rabbitmq')
Expand Down
40 changes: 40 additions & 0 deletions spec/ubuntu_1404_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,46 @@
)
end
end

context '`latest` is specified for the version as a symbol' do
cached(:ubuntu_1404) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '14.04',
step_into: ['chef_ingredient']
) do |node|
node.set['test']['chef-server-core']['version'] = :latest
end.converge('test::repo')
end

it 'installs chef_ingredient[chef-server]' do
expect(ubuntu_1404).to install_chef_ingredient('chef-server')
end

it 'installs yum_package[chef-server]' do
expect(ubuntu_1404).to install_apt_package('chef-server-core')
end
end

context '`latest` is specified for the version as a string' do
cached(:ubuntu_1404) do
ChefSpec::SoloRunner.new(
platform: 'ubuntu',
version: '14.04',
step_into: ['chef_ingredient']
) do |node|
node.set['test']['chef-server-core']['version'] = 'latest'
end.converge('test::repo')
end

it 'installs chef_ingredient[chef-server]' do
expect(ubuntu_1404).to install_chef_ingredient('chef-server')
end

it 'installs yum_package[chef-server]' do
expect(ubuntu_1404).to install_apt_package('chef-server-core')
end
end
end

describe 'test::local on ubuntu' do
Expand Down

0 comments on commit 00d6523

Please sign in to comment.