Module: VagrantPlugins::KeyManager::HostsFile

Included in:
Action::GetGuestKeys, Command, Provisioner
Defined in:
lib/vagrant-keymanager/hosts_file.rb

Instance Method Summary collapse

Instance Method Details

#get_guest_keys(machine) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vagrant-keymanager/hosts_file.rb', line 7

def get_guest_keys(machine)
  machines = get_machines

  sshkeys = Hash.new
  sshrootkeys = Hash.new        

  #puts "MACHINES"
  #pp machines
  machines.each do |curr_machine|
      #pp curr_machine
      curr_machine_name=curr_machine.name.to_s
      puts "Getting SSH keys from "+curr_machine_name
      sshkey=get_user_key(curr_machine)
      #puts "SSH key: "+sshkey
      sshkeys[curr_machine_name] = sshkey
      sshrootkey=get_root_key(curr_machine)
      #puts "SSH root key: "+sshrootkey
      sshrootkeys[curr_machine_name] = sshrootkey
  end

  machines.each do |curr_machine|
    curr_machine_name=curr_machine.name.to_s
    curr_machine.communicate.sudo("rm -f /tmp/.all_keys.txt /tmp/.all_root_keys.txt /tmp/add_ssh_keys.sh")

    puts "Saving public SSH keys to "+curr_machine_name
    ssh_keys_to_save=sshkeys.reject{|k,v| k == curr_machine_name}.values.join.gsub("\n\n", '\n')
    curr_machine.communicate.execute("echo '"+ssh_keys_to_save+"' >/tmp/.all_keys.txt")
    #puts "Saved /tmp/.all_keys.txt"

    ssh_root_keys_to_save=sshrootkeys.reject{|k,v| k == curr_machine_name}.values.join.gsub("\n\n", '\n')
    curr_machine.communicate.execute("echo '"+ssh_root_keys_to_save+"' >/tmp/.all_root_keys.txt")

    #puts "Saved /tmp/.all_root_keys.txt"

    # We must save locally a bash script that computes and applies diff and always exits with 0 or vagrant plugin will exit with an error
    curr_machine.communicate.execute("echo 'diff --changed-group-format=\"%>\" --unchanged-group-format=\"\" ~/.ssh/authorized_keys $1 >>~/.ssh/authorized_keys;exit 0' >/tmp/add_ssh_keys.sh")

    curr_machine.communicate.execute("sh /tmp/add_ssh_keys.sh /tmp/.all_keys.txt")
    curr_machine.communicate.execute("sh /tmp/add_ssh_keys.sh /tmp/.all_root_keys.txt")
    #puts "Saved user keys"
    
    curr_machine.communicate.sudo("sh /tmp/add_ssh_keys.sh /tmp/.all_keys.txt")
    curr_machine.communicate.sudo("sh /tmp/add_ssh_keys.sh /tmp/.all_root_keys.txt")
    #puts "Saved root keys"
  end

  machines.each do |curr_machine|
    call_extra_user_steps(curr_machine)
  end

  machines.each do |curr_machine|
    curr_machine.communicate.sudo("rm -f /tmp/.all_keys.txt /tmp/.all_root_keys.txt /tmp/add_ssh_keys.sh")
  end
end