Class: Umami::Policyfile::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-umami/policyfile/exporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policyfile_lock_file = nil, cookbook_dir = nil, policyfile = nil) ⇒ Exporter

Returns a new instance of Exporter.



30
31
32
33
34
35
36
37
38
39
# File 'lib/chef-umami/policyfile/exporter.rb', line 30

def initialize(policyfile_lock_file = nil, cookbook_dir = nil, policyfile = nil)
  @policyfile = policyfile
  @export_root = Dir.mktmpdir('umami-')
  # We need the target dir named the same as the source dir so that `chef` commands
  # work as happily programatically as they would via the command line.
  # This is because the commands assume they're being run from within a cookbook
  # directory.
  @export_path = File.join(export_root, cookbook_dir)
  @chef_config_file = "#{export_path}/.chef/config.rb"
end

Instance Attribute Details

#chef_config_fileObject (readonly)

Returns the value of attribute chef_config_file.



23
24
25
# File 'lib/chef-umami/policyfile/exporter.rb', line 23

def chef_config_file
  @chef_config_file
end

#cookbook_dirObject (readonly)

Returns the value of attribute cookbook_dir.



24
25
26
# File 'lib/chef-umami/policyfile/exporter.rb', line 24

def cookbook_dir
  @cookbook_dir
end

#export_pathObject (readonly)

Returns the value of attribute export_path.



26
27
28
# File 'lib/chef-umami/policyfile/exporter.rb', line 26

def export_path
  @export_path
end

#export_rootObject (readonly)

Returns the value of attribute export_root.



25
26
27
# File 'lib/chef-umami/policyfile/exporter.rb', line 25

def export_root
  @export_root
end

#policyfileObject (readonly)

Returns the value of attribute policyfile.



27
28
29
# File 'lib/chef-umami/policyfile/exporter.rb', line 27

def policyfile
  @policyfile
end

#policyfile_lock_fileObject

Returns the value of attribute policyfile_lock_file.



28
29
30
# File 'lib/chef-umami/policyfile/exporter.rb', line 28

def policyfile_lock_file
  @policyfile_lock_file
end

Instance Method Details

#cp_fake_client_keyObject



61
62
63
64
65
# File 'lib/chef-umami/policyfile/exporter.rb', line 61

def cp_fake_client_key
  # Create a fake client cert based on a dummy cert we have laying around.
  fake_client_key_src = File.join(File.dirname(__FILE__), %w(.. .. .. support umami.pem))
  FileUtils.cp(fake_client_key_src, fake_client_key)
end

#exportObject

Export the cookbook and prepare a chef-zero-compatible directory. We’ll use this as a temporary launch pad for things, as needed, akin to test-kitchen’s sandbox.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef-umami/policyfile/exporter.rb', line 79

def export
  install_policy
  export_service = ChefDK::PolicyfileServices::ExportRepo.new(
    policyfile: policyfile_lock_file,
    export_dir: export_path
  )
  begin
    export_service.run
  rescue ChefDK::PolicyfileExportRepoError => e
    puts "\nFAILED TO EXPORT POLICYFILE: #{e.message} (#{e.class})"
    puts "CAUSE: #{e.cause}"
    puts 'BACKTRACE:'
    e.backtrace.each do |line|
      puts "\t#{line}"
    end
    exit(1)
  end
  cp_fake_client_key
  update_chef_config
end

#fake_client_keyObject



57
58
59
# File 'lib/chef-umami/policyfile/exporter.rb', line 57

def fake_client_key
  "#{export_path}/umami.pem"
end

#install_policyObject

Execute ‘chef install` to ensure we get a fresh, clean Policyfile lock file on each run.



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

def install_policy
  puts "Generating a new Policyfile from '#{policyfile}'..."
  install_service = ChefDK::PolicyfileServices::Install.new(
    policyfile: policyfile,
    ui: ui
  )
  @policyfile_lock_file = install_service.storage_config.policyfile_lock_filename
  install_service.run
end

#uiObject



41
42
43
# File 'lib/chef-umami/policyfile/exporter.rb', line 41

def ui
  @ui ||= ChefDK::UI.new
end

#update_chef_configObject



67
68
69
70
71
72
73
74
# File 'lib/chef-umami/policyfile/exporter.rb', line 67

def update_chef_config
  File.open(chef_config_file, 'a') do |f|
    f.puts "chef_server_url 'http://127.0.0.1:8889'"
    f.puts "cookbook_path ['#{export_path}/cookbook_artifacts']"
    f.puts "client_key '#{fake_client_key}'"
    f.puts "node_name 'umami-node'"
  end
end