Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3, allow latest and remove package_name #4

Merged
merged 2 commits into from
Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ 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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to document the usage of :latest.

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: '0.0.0'
attribute :timeout, kind_of: [Integer, String, NilClass], default: nil
end
end
Expand Down
47 changes: 28 additions & 19 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 == :latest || vers == 'latest'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly cleaner version:

return '0.0.0' if vers.to_sym == :latest

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh 😄

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 Down Expand Up @@ -82,20 +91,20 @@ def reconfigure
# 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