class Object
Public Instance Methods
_run_command(cmd, base_dir)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 50 def _run_command(cmd, base_dir) banner("Running '#{cmd}' in #{base_dir}...") Dir.chdir base_dir do run_command(cmd, true) end end
berks_update(base_dir)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 57 def berks_update(base_dir) _run_command('berks install', base_dir) _run_command('berks update', base_dir) end
berks_vendor(base_dir, target_folder)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 62 def berks_vendor(base_dir, target_folder) _run_command("berks vendor #{target_folder}/cookbooks", base_dir) end
cache_path_config(target_folder)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 110 def cache_path_config(target_folder) %( # To enable chef-client without sudo. # https://docs.chef.io/ctl_chef_client.html#run-as-non-root-user cache_path "#{target_folder}/.chef" ) end
chefdk_export(base_dir, target_folder)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 70 def chefdk_export(base_dir, target_folder) _run_command("chef export #{target_folder} --force", base_dir) end
chefdk_update(base_dir)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 66 def chefdk_update(base_dir) _run_command('chef update', base_dir) end
clean(target_folder)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 188 def clean(target_folder) cmd = "rm -rf #{target_folder}/cookbooks" banner("Cleanning up the cookbooks cache folder with '#{cmd}' ...") run_command(cmd, true) end
copy_attributes_file(base_dir, target_folder)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 153 def copy_attributes_file(base_dir, target_folder) attributes_file = File.join(base_dir, 'attributes.json') return unless File.exist?(attributes_file) banner("Copying attributes file from #{attributes_file} to #{target_folder}...") FileUtils.cp_r(attributes_file, target_folder) end
copy_data_bags(base_dir, target_folder)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 145 def copy_data_bags(base_dir, target_folder) data_bags_directory = File.join(base_dir, 'data_bags') return unless File.directory?(data_bags_directory) banner("Copying data_bags folder from #{data_bags_directory} to #{target_folder}...") FileUtils.cp_r(data_bags_directory, target_folder) end
create_custom_client_rb(target_folder, configuration_file, copy_original)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 118 def create_custom_client_rb(target_folder, configuration_file, copy_original) banner("Creating custom 'client.rb' in #{target_folder} ...") original_client_rb = File.join(target_folder, 'client.rb') custom_client_rb = File.join(target_folder, 'custom_client.rb') config = read_configuration(configuration_file) begin FileUtils.copy_file(original_client_rb, custom_client_rb) if copy_original open_mode = copy_original ? 'a' : 'w' File.open(custom_client_rb, open_mode) do |output| output.write(cache_path_config(target_folder)) if config.nil? puts 'No configuration file found: custom_client.rb will not have any user custom configuration.' else output.write(prepare_handlers(config[:handlers])) end end rescue Errno::EACCES => e puts "Problem creating #{custom_client_rb} - #{e}" end puts "Writed a custom client.rb to '#{custom_client_rb}" end
foodcritic(exit_on_error = true)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/test_tasks.rb, line 39 def foodcritic(exit_on_error = true) foodcritic_rules = File.join(File.dirname(__FILE__), '../../foodcritic/rules') cmd = "chef exec foodcritic -f any -P --include #{foodcritic_rules} ." banner("Running '#{cmd}' ...") run_command(cmd, exit_on_error) end
help_bump()
click to toggle source
# File lib/cookbook_sdk/rake_tasks/helper_tasks.rb, line 14 def help_bump title = 'Help: Bumping cookbooks'.gray.bold body = <<BODY To bump your cookbook run the following command: chef exec knife spork bump -z Note: -z stands for local mode (chef-zero) For more about 'knife spork' go to: http://jonlives.github.io/knife-spork/ BODY output_help(title, body) end
help_kitchen()
click to toggle source
# File lib/cookbook_sdk/rake_tasks/helper_tasks.rb, line 29 def help_kitchen title = 'Help: Test Kitchen'.gray.bold body = <<BODY Test kitchen is an awsome testing tool. It enables you to test your cookbook against some virtual machine, container, or an instance in several cloud virtualization tecnologies. Run rhe folling command to have the real kitchen help chef exec knife --help Note: You don't get nothing when run chef-provisioning cookbooks with kitchen. For more about 'kitchen' go to: http://kitchen.ci/ BODY output_help(title, body) end
output_help(title, body)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/helper_tasks.rb, line 46 def output_help(title, body) puts title puts body end
prepare_handlers(handlers)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 90 def prepare_handlers(handlers) config_rb = '' handlers[:enabled].each do |enabled_handler| handler_config = handlers[:config][enabled_handler.to_sym] config_rb += %( # #{enabled_handler} handler configuration require 'cookbook_sdk/handlers/#{enabled_handler}' #{enabled_handler}_handler_options = #{handler_config} #{enabled_handler}_handler = Chef::Handler::Slack.new(#{enabled_handler}_handler_options) start_handlers << #{enabled_handler}_handler report_handlers << #{enabled_handler}_handler exception_handlers << #{enabled_handler}_handler ) end config_rb end
read_configuration(configuration_file)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 74 def read_configuration(configuration_file) file = nil if File.exist?(configuration_file) file = File.read(configuration_file) elsif File.exist?("../#{configuration_file}") file = File.read(configuration_file) end return nil if file.nil? data_hash = JSON.parse(file, :symbolize_names => true) data_hash rescue Errno::ENOENT, Errno::EACCES, JSON::ParserError => e puts "Problem reading #{configuration_file} - #{e}" end
rspec(exit_on_error = true)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/test_tasks.rb, line 46 def rspec(exit_on_error = true) files = FileList[File.join(Dir.pwd, 'test', 'unit', '**/test_*.rb')] cmd = "chef exec rspec #{files} --format doc" banner("Running '#{cmd}' ...") run_command(cmd, exit_on_error) end
rubocop(exit_on_error = true)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/test_tasks.rb, line 53 def rubocop(exit_on_error = true) cmd = 'chef exec rubocop' banner("Running '#{cmd}' ...") run_command(cmd, exit_on_error) end
run_chef_zero(target_folder, custom_named_run_list = nil, run_list = nil, debug = false)
click to toggle source
# File lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb, line 161 def run_chef_zero(target_folder, custom_named_run_list = nil, run_list = nil, debug = false) named_run_list = custom_named_run_list.nil? ? '' : "-n #{custom_named_run_list}" run_list = run_list.nil? ? '' : "-o #{run_list}" debug = !debug ? '' : '-l debug' log_format = '--force-formatter' attributes_file_path = File.join(target_folder, 'attributes.json') attributes_flag = File.exist?(attributes_file_path) ? '-j attributes.json' : '' timestamp = Time.now.to_i cache_pid_file = "#{target_folder}/.chef/cache/chef-client-running_#{timestamp}.pid" lockfile = "--lockfile=#{cache_pid_file}" cmd = 'chef exec chef-client -c custom_client.rb -z ' cmd += "#{named_run_list} #{run_list} #{debug} #{attributes_flag} #{lockfile} #{log_format}" if attributes_flag != '' file = File.read(attributes_file_path) data_hash = JSON.pretty_generate(JSON.parse(file)) banner('Attributes are:') banner(data_hash) end _run_command(cmd, target_folder) end
run_command(cmd, exit_on_error = false)
click to toggle source
Runs a shell command. If all good, return false. If error, return true. If error and exit_on_error, exit with the error code.
# File lib/cookbook_sdk/rake_tasks.rb, line 13 def run_command(cmd, exit_on_error = false) Bundler.with_clean_env do system(cmd) status = $CHILD_STATUS.exitstatus return false unless status != 0 return true unless exit_on_error exit status end end