# frozen_string_literal: true require_relative 'project' require_relative 'kitchen' require_relative 'inspectestor' require_relative 'rubygem' # DeployRubygem - deploy a gem using rake # Containing a class module DeployRubygem # Using Cookbook to deploy and manage cookbook class Cookbook < Project attr_reader :profiles, :kitchens, :execute_profiles def initialize(new_cookbook_info) super(new_cookbook_info) @profiles = cookbook_info[:compliance_profiles].map do |profile_name| input_file = File.join(%w[compliance inputs] + ["#{cookbook_name}.yml"]) waiver_file = File.join(%w[compliance waivers] + ["#{cookbook_name}.yml"]) InspecTestor.new(profile_name, input_file, waiver_file) end @kitchens = cookbook_info[:kitchens].map do |kitchen_name| Kitchen.new(kitchen_name, self) end @execute_profiles = cookbook_info[:execute_profiles].map do |profile_name| InspecTestor.new(profile_name) end end def cookbook_name project_name end def cookbook_info project_options end def cookbook_path ::File.join(chefrepo_path, 'cookbooks', cookbook_name.to_s) end def git cookbook_info[__method__] end def groups cookbook_info[__method__] end def connect_cookbook system("git submodule add #{git}") system("git submodule init cookbooks/#{cookbook_name}") system("git submodule sync cookbooks/#{cookbook_name}") system("git submodule foreach --recursive 'pwd && git pull'") end def switch_to_cookbook change_to_chefrepo connect_cookbook change_to_project_folder end def upload_cookbook system('git pull') system('chef clean-policy-cookbooks') # system('chef clean-policy-revisions') system("knife cookbook upload --cookbook-path '../' #{cookbook_name}") system('chef install Policyfile.rb') groups.each do |group| system("chef push #{group} Policyfile.lock.json") end end def prepare_test_environment profiles.each(&:update) kitchens.each(&:converge) end def destroy_test_environment kitchens.each(&:destroy) end def deploy save_progress switch_to_cookbook save_progress upload_cookbook prepare_test_environment verify save_progress end def verify kitchens.each do |kitchen| puts "Verify cookbook #{cookbook_name} on target #{kitchen.target}" kitchen.verify end execute_profiles.each do |inspec_profile| inspec_profile.update inspec_profile.apply end end def save_progress system('git add .') system("git commit -m \"Saving: hostname=#{ENV['HOSTNAME']}, cookbook_name=#{cookbook_name}\"") system('git push') end # system('sudo chef-client --override-runlist jimbo_management_site') end end