Module: Vagrant::Butcher::Helpers::KeyFiles

Included in:
Action::CopyGuestKey, Connection
Defined in:
lib/vagrant-butcher/helpers/key_files.rb

Instance Method Summary collapse

Instance Method Details

#cache_dir(env) ⇒ Object



8
9
10
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 8

def cache_dir(env)
  @cache_dir ||= File.expand_path(File.join(root_path(env), butcher_config(env).cache_dir))
end

#cleanup_cache_dir(env) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 41

def cleanup_cache_dir(env)
  unless @failed_deletions
    key_file = "#{cache_dir(env)}/#{key_filename(env)}"
    File.delete(key_file) if File.exists?(key_file)
    Dir.delete(cache_dir(env)) if (Dir.entries(cache_dir(env)) - %w{ . .. }).empty?
  else
    ui(env).warn "#{@failed_deletions} not butchered from the Chef Server. Client key was left at #{client_key_path(env)}"
  end
end

#client_key_path(env) ⇒ Object



20
21
22
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 20

def client_key_path(env)
  @client_key_path ||= butcher_config(env).client_key || "#{cache_dir(env)}/#{key_filename(env)}"
end

#copy_guest_key(env) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 51

def copy_guest_key(env)
  begin
    grab_key_from_guest(env)
  rescue ::Vagrant::Errors::VagrantError => e
    ui(env).error "Failed to create #{cache_dir(env)}/#{key_filename(env)}: #{e.class} - #{e}"
  end
end

#create_cache_dir(env) ⇒ Object



24
25
26
27
28
29
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 24

def create_cache_dir(env)
  unless File.exists?(cache_dir(env))
    ui(env).info "Creating #{cache_dir(env)}"
    FileUtils.mkdir_p(cache_dir(env))
  end
end

#grab_key_from_guest(env) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 31

def grab_key_from_guest(env)
  create_cache_dir(env)

  machine(env).communicate.execute("cat #{guest_key_path(env)}", sudo: true) do |type,data|
    File.open("#{cache_dir(env)}/#{key_filename(env)}", "w") { |f| f << data } if type == :stdout
  end

  ui(env).info "Saved client key to #{cache_dir(env)}/#{key_filename(env)}"
end

#guest_key_path(env) ⇒ Object



12
13
14
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 12

def guest_key_path(env)
  @guest_key_path ||= butcher_config(env).guest_key_path
end

#key_filename(env) ⇒ Object



16
17
18
# File 'lib/vagrant-butcher/helpers/key_files.rb', line 16

def key_filename(env)
  @key_filename ||= "#{env[:machine].name}-client.pem"
end