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

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

Instance Method Summary collapse

Methods included from Guest

#get_guest_key_path, #windows?

Instance Method Details

#cache_dir(env) ⇒ Object



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

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
    env[:ui].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



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

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
    env[:ui].error "Failed to create #{cache_dir(env)}/#{key_filename(env)}: #{e.class} - #{e}"
  end
end

#create_cache_dir(env) ⇒ Object



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

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

#grab_key_from_guest(env) ⇒ Object



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

def grab_key_from_guest(env)
  create_cache_dir(env)
  unless windows?(env)
    machine(env).communicate.execute "chmod 0644 #{guest_key_path(env)}", :sudo => true
  end
  machine(env).communicate.download(guest_key_path(env), "#{cache_dir(env)}/#{key_filename(env)}")
  env[:ui].info "Saved client key to #{cache_dir(env)}/#{key_filename(env)}"
end

#guest_key_path(env) ⇒ Object



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

def guest_key_path(env)
  @guest_key_path ||= get_guest_key_path(env)
end

#key_filename(env) ⇒ Object



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

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