Class: Umami::Runner
- Inherits:
-
Object
- Object
- Umami::Runner
- Defined in:
- lib/chef-umami/runner.rb
Instance Attribute Summary collapse
-
#cookbook_dir ⇒ Object
readonly
Returns the value of attribute cookbook_dir.
Instance Method Summary collapse
- #chef_client ⇒ Object
-
#chef_config ⇒ Object
Convenience to return the Chef::Config singleton.
- #chef_zero_server ⇒ Object
-
#initialize ⇒ Runner
constructor
A new instance of Runner.
- #policy_group ⇒ Object
- #policyfile ⇒ Object
-
#policyfile_lock_file ⇒ Object
Return the computed policyfile lock name.
- #push ⇒ Object
- #run ⇒ Object
- #ui ⇒ Object
-
#umami_config ⇒ Object
A hash of values describing the Umami config.
- #validate_lock_file! ⇒ Object
Methods included from Options
Methods included from Logger
Constructor Details
#initialize ⇒ Runner
Returns a new instance of Runner.
32 33 34 35 36 37 38 39 40 |
# File 'lib/chef-umami/runner.rb', line 32 def initialize @umami_config = umami_config @cookbook_dir = Dir.pwd ## If we load the pusher or client now, they won't see the updated ## Chef config! @push = nil @chef_client = nil @ui = ui end |
Instance Attribute Details
#cookbook_dir ⇒ Object (readonly)
Returns the value of attribute cookbook_dir.
31 32 33 |
# File 'lib/chef-umami/runner.rb', line 31 def cookbook_dir @cookbook_dir end |
Instance Method Details
#chef_client ⇒ Object
94 95 96 |
# File 'lib/chef-umami/runner.rb', line 94 def chef_client @chef_client ||= Umami::Client.new(policyfile) end |
#chef_config ⇒ Object
Convenience to return the Chef::Config singleton.
49 50 51 |
# File 'lib/chef-umami/runner.rb', line 49 def chef_config Chef::Config end |
#chef_zero_server ⇒ Object
90 91 92 |
# File 'lib/chef-umami/runner.rb', line 90 def chef_zero_server @chef_zero_server ||= Umami::Server.new end |
#policy_group ⇒ Object
61 62 63 |
# File 'lib/chef-umami/runner.rb', line 61 def policy_group chef_config['policy_group'] end |
#policyfile ⇒ Object
57 58 59 |
# File 'lib/chef-umami/runner.rb', line 57 def policyfile umami_config[:policyfile] end |
#policyfile_lock_file ⇒ Object
Return the computed policyfile lock name.
66 67 68 |
# File 'lib/chef-umami/runner.rb', line 66 def policyfile_lock_file policyfile.gsub(/\.rb$/, '.lock.json') end |
#push ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/chef-umami/runner.rb', line 80 def push # rubocop:disable Layout/AlignHash @push ||= Umami::PolicyfileServices::Push.new(policyfile: policyfile, ui: ui, policy_group: policy_group, config: chef_config, root_dir: cookbook_dir) # rubocop:enable Layout/AlignHash end |
#run ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/chef-umami/runner.rb', line 98 def run validate_lock_file! chef_client.apply_config! chef_zero_server.start puts "\nUploading the policy and related cookbooks..." push.run puts "\nExecuting chef-client compile phase..." chef_client.compile # Build a hash of all the recipes' resources, keyed by the canonical # name of the recipe (i.e. ohai::default). recipe_resources = {} chef_client.resource_collection.each do |resource| canonical_recipe = "#{resource.cookbook_name}::#{resource.recipe_name}" unless umami_config[:recipes].nil? || umami_config[:recipes].empty? # The user has explicitly requested that one or more recipes have # tests written, to the exclusion of others. # ONLY include the recipe if it matches the list. next unless umami_config[:recipes].include?(canonical_recipe) end if recipe_resources.key?(canonical_recipe) recipe_resources[canonical_recipe] << resource else recipe_resources[canonical_recipe] = [resource] end end # Remove the temporary directory using a naive guard to ensure we're # deleting what we expect. re_export_path = Regexp.new('/tmp/umami') FileUtils.rm_rf(chef_client.staging_dir) if chef_client.staging_dir.match(re_export_path) if umami_config[:unit_tests] puts "\nGenerating a set of unit tests..." unit_tester = Umami::Test::Unit.new(umami_config[:test_root]) unit_tester.generate(recipe_resources) end if umami_config[:integration_tests] puts "\nGenerating a set of integration tests..." integration_tester = Umami::Test::Integration.new(umami_config[:test_root]) integration_tester.generate(recipe_resources) end end |
#ui ⇒ Object
53 54 55 |
# File 'lib/chef-umami/runner.rb', line 53 def ui @ui ||= ChefDK::UI.new end |
#umami_config ⇒ Object
A hash of values describing the Umami config. Comprised of command line options. May (in the future) contain options read from a config file.
44 45 46 |
# File 'lib/chef-umami/runner.rb', line 44 def umami_config @umami_config ||= end |
#validate_lock_file! ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/chef-umami/runner.rb', line 70 def validate_lock_file! unless policyfile_lock_file.end_with?('lock.json') raise InvalidPolicyfileLockFilename, "Policyfile lock files must end in '.lock.json'. I received '#{policyfile_lock_file}'." end unless File.exist?(policyfile_lock_file) raise InvalidPolicyfileLockFilename, "Unable to locate '#{policyfile_lock_file}' You may need to run `chef install` to generate it." end end |