From f5d9fe64ccd257aaa690550a3698df6155fa9d70 Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Tue, 19 Apr 2016 08:52:30 -0700 Subject: [PATCH 1/2] Add accept_license property to chef-ingredient. --- README.md | 1 + libraries/chef_ingredient_provider.rb | 15 +++++++++++++++ libraries/chef_ingredient_resource.rb | 3 +++ libraries/helpers.rb | 2 +- resources/ingredient_config.rb | 2 +- spec/unit/recipes/test_config_spec.rb | 4 ++++ test/fixtures/cookbooks/test/recipes/config.rb | 1 + 7 files changed, 26 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b3d6e8a..8f98254 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ By default, `chef_ingredient` will install using the `packages.chef.io` stable r - `channel`: Channel to install the products from. It can be `:stable` (default), `:current` or `:unstable`. - `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. +- `accept_license`: A boolean value that specifies if license should be accepted if it is asked for during `reconfigure`action. This option is applicable to only these products: manage, analytics, reporting and compliance. Default: `false`. ### omnibus_service diff --git a/libraries/chef_ingredient_provider.rb b/libraries/chef_ingredient_provider.rb index 026b596..cd7410f 100644 --- a/libraries/chef_ingredient_provider.rb +++ b/libraries/chef_ingredient_provider.rb @@ -81,6 +81,21 @@ def initialize(name, run_context = nil) not_if { get_config(new_resource.product_name).empty? } end + # If accept_license is set, drop .license.accepted file so that + # reconfigure does not prompt for license acceptance. This is + # the backwards compatible way of accepting a Chef license. + if new_resource.accept_license && %w(analytics manage reporting compliance).include?(new_resource.product_name) + # The way we construct the data directory for a product, that looks + # like /var/opt/ is to get the config file path that + # looks like /etc//.rb and do path + # manipulation. + product_data_dir = ::File.basename(::File.dirname(ingredient_config_file(new_resource.product_name))) + + file ::File.join('/var/opt', product_data_dir, '.license.accepted') do + action :touch + end + end + execute "#{ingredient_package_name}-reconfigure" do command "#{ingredient_ctl_command} reconfigure" end diff --git a/libraries/chef_ingredient_resource.rb b/libraries/chef_ingredient_resource.rb index 2f06366..0345748 100644 --- a/libraries/chef_ingredient_resource.rb +++ b/libraries/chef_ingredient_resource.rb @@ -38,6 +38,9 @@ class ChefIngredient < Chef::Resource::LWRPBase # Attributes for package resources used on rhel and debian platforms attribute :options, kind_of: String attribute :timeout, kind_of: [Integer, String, NilClass], default: nil + + # Attribute to accept the license when applicable + attribute :accept_license, kind_of: [TrueClass, FalseClass], default: false end end end diff --git a/libraries/helpers.rb b/libraries/helpers.rb index fd16be0..b9882c5 100644 --- a/libraries/helpers.rb +++ b/libraries/helpers.rb @@ -53,7 +53,7 @@ def ingredient_ctl_command # # Returns the ctl-command for a chef_ingredient resource # - def ingredient_config_file + def ingredient_config_file(product_name) ensure_mixlib_install_gem_installed! PRODUCT_MATRIX.lookup(product_name).config_file diff --git a/resources/ingredient_config.rb b/resources/ingredient_config.rb index f61b81c..22c01f4 100644 --- a/resources/ingredient_config.rb +++ b/resources/ingredient_config.rb @@ -28,7 +28,7 @@ property :config, String, default: nil action :render do - target_config = ingredient_config_file + target_config = ingredient_config_file(product_name) return if target_config.nil? directory ::File.dirname(target_config) do diff --git a/spec/unit/recipes/test_config_spec.rb b/spec/unit/recipes/test_config_spec.rb index fd33920..535ec8d 100644 --- a/spec/unit/recipes/test_config_spec.rb +++ b/spec/unit/recipes/test_config_spec.rb @@ -37,6 +37,10 @@ expect(chef_run).to reconfigure_chef_server_ingredient('manage') end + it 'creates the license acceptance file' do + expect(chef_run).to touch_file('/var/opt/chef-manage/.license.accepted') + end + it 'does not render config file using ingredient_config' do expect(chef_run).to_not render_ingredient_config('manage') end diff --git a/test/fixtures/cookbooks/test/recipes/config.rb b/test/fixtures/cookbooks/test/recipes/config.rb index 3918c36..ef6467a 100644 --- a/test/fixtures/cookbooks/test/recipes/config.rb +++ b/test/fixtures/cookbooks/test/recipes/config.rb @@ -12,5 +12,6 @@ # Management Console - using the compat shim resource chef_server_ingredient 'manage' do + accept_license true action :reconfigure end From e4d04b1b6e7f00630f7d498d7a2ba97d17b89aad Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Wed, 20 Apr 2016 10:43:34 -0700 Subject: [PATCH 2/2] Create the directory for the .license.accepted file. --- libraries/chef_ingredient_provider.rb | 10 ++++++++-- spec/unit/recipes/test_config_spec.rb | 4 ++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/libraries/chef_ingredient_provider.rb b/libraries/chef_ingredient_provider.rb index cd7410f..16d6175 100644 --- a/libraries/chef_ingredient_provider.rb +++ b/libraries/chef_ingredient_provider.rb @@ -89,9 +89,15 @@ def initialize(name, run_context = nil) # like /var/opt/ is to get the config file path that # looks like /etc//.rb and do path # manipulation. - product_data_dir = ::File.basename(::File.dirname(ingredient_config_file(new_resource.product_name))) + product_data_dir_name = ::File.basename(::File.dirname(ingredient_config_file(new_resource.product_name))) + product_data_dir = ::File.join('/var/opt', product_data_dir_name) - file ::File.join('/var/opt', product_data_dir, '.license.accepted') do + directory product_data_dir do + recursive true + action :create + end + + file ::File.join(product_data_dir, '.license.accepted') do action :touch end end diff --git a/spec/unit/recipes/test_config_spec.rb b/spec/unit/recipes/test_config_spec.rb index 535ec8d..eec0b41 100644 --- a/spec/unit/recipes/test_config_spec.rb +++ b/spec/unit/recipes/test_config_spec.rb @@ -37,6 +37,10 @@ expect(chef_run).to reconfigure_chef_server_ingredient('manage') end + it 'creates the directory for the license acceptance file' do + expect(chef_run).to create_directory('/var/opt/chef-manage').with(recursive: true) + end + it 'creates the license acceptance file' do expect(chef_run).to touch_file('/var/opt/chef-manage/.license.accepted') end