Class: Umami::Runner

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/chef-umami/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Constructor Details

#initialize(policyfile_lock_file = nil, policyfile = nil) ⇒ Runner

TODO: Build the ability to specify a custom policy lock file name.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef-umami/runner.rb', line 34

def initialize(policyfile_lock_file = nil, policyfile = nil)
  @cookbook_dir = Dir.pwd
  @policyfile_lock_file = 'Policyfile.lock.json'
  @policyfile = policyfile || 'Policyfile.rb'
  @exporter = exporter
  @chef_zero_server = chef_zero_server
  # If we load the uploader or client now, they won't see the updated
  # Chef config!
  @uploader = nil
  @chef_client = nil
end

Instance Attribute Details

#cookbook_dirObject (readonly)

Returns the value of attribute cookbook_dir.



30
31
32
# File 'lib/chef-umami/runner.rb', line 30

def cookbook_dir
  @cookbook_dir
end

#policyfileObject (readonly)

Returns the value of attribute policyfile.



32
33
34
# File 'lib/chef-umami/runner.rb', line 32

def policyfile
  @policyfile
end

#policyfile_lock_fileObject (readonly)

Returns the value of attribute policyfile_lock_file.



31
32
33
# File 'lib/chef-umami/runner.rb', line 31

def policyfile_lock_file
  @policyfile_lock_file
end

Instance Method Details

#chef_clientObject



68
69
70
# File 'lib/chef-umami/runner.rb', line 68

def chef_client
  @chef_client ||= Umami::Client.new
end

#chef_zero_serverObject



64
65
66
# File 'lib/chef-umami/runner.rb', line 64

def chef_zero_server
  @chef_zero_server ||= Umami::Server.new
end

#exporterObject



56
57
58
# File 'lib/chef-umami/runner.rb', line 56

def exporter
  @exporter ||= Umami::Policyfile::Exporter.new(policyfile_lock_file, cookbook_dir, policyfile)
end

#runObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef-umami/runner.rb', line 72

def run
  validate_lock_file!
  puts "\nExporting the policy, related cookbooks, and a valid client configuration..."
  exporter.export
  Chef::Config.from_file("#{exporter.chef_config_file}")
  chef_zero_server.start
  puts "\nUploading the policy and related cookbooks..."
  uploader.upload
  puts "\nExecuting chef-client compile phase..."
  # Define Chef::Config['config_file'] lest Ohai complain.
  Chef::Config['config_file'] = exporter.chef_config_file
  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}"
    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(exporter.export_root) if exporter.export_root.match(re_export_path)

  puts "\nGenerating a set of unit tests..."
  unit_tester = Umami::Test::Unit.new
  unit_tester.generate(recipe_resources)

  puts "\nGenerating a set of integration tests..."
  integration_tester = Umami::Test::Integration.new
  integration_tester.generate(recipe_resources)

end

#uploaderObject



60
61
62
# File 'lib/chef-umami/runner.rb', line 60

def uploader
  @uploader ||= Umami::Policyfile::Uploader.new(policyfile_lock_file)
end

#validate_lock_file!Object



46
47
48
49
50
51
52
53
54
# File 'lib/chef-umami/runner.rb', line 46

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