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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/vagrant-keymanager/hosts_file.rb', line 7
def get_guest_keys(machine)
machines = get_machines
running_machines = machines.select {|m| m.communicate.ready? }
machines.each do |curr_machine|
if ! curr_machine.communicate.ready?
puts "Skipping machine "+curr_machine.name.to_s+ ". It's not in running state"
end
end
sshkeys = Hash.new
required_users = machine.config.keymanager.user_list
puts "REQUIRED USERS:"
pp required_users
running_machines.each do |curr_machine|
curr_machine.communicate.sudo("rm -f /tmp/add_ssh_keys.sh /tmp/get_user_keys.sh")
curr_machine.communicate.execute("echo -e 'diff --changed-group-format=\"%>\" --unchanged-group-format=\"\" ~/.ssh/authorized_keys $1 >>~/.ssh/authorized_keys\nexit 0' >/tmp/add_ssh_keys.sh")
curr_machine.communicate.execute("echo -e 'if [ ! -e ~/.ssh/id_rsa ] || [ ! -e ~/.ssh/id_rsa.pub ]; then\n\tssh-keygen -q -f ~/.ssh/id_rsa -P \"\"\nfi\ncat ~/.ssh/id_rsa.pub' >/tmp/get_user_keys.sh")
curr_machine_name=curr_machine.name.to_s
puts "Getting SSH keys from "+curr_machine_name
required_users.each do |curr_user|
if !check_user_existence(curr_machine, curr_user)
create_user(curr_machine, curr_user)
end
if !sshkeys[curr_user]
sshkeys[curr_user] = Hash.new
end
sshkey=get_user_key(curr_machine, curr_user)
sshkeys[curr_user][curr_machine_name] = sshkey
end
end
running_machines.each do |curr_machine|
curr_machine_name=curr_machine.name.to_s
puts "Saving public SSH keys to "+curr_machine_name
required_users.each do |curr_user|
curr_machine.communicate.sudo("rm -f /tmp/.all_"+curr_user+"_keys.txt")
ssh_keys_to_save=sshkeys[curr_user].reject{|k,v| k == curr_machine_name}.values.join.gsub("\n\n", '\n')
curr_machine.communicate.execute("sudo -u "+curr_user+" -H echo '"+ssh_keys_to_save+"' >/tmp/.all_"+curr_user+"_keys.txt")
end
required_users.each do |curr_user|
required_users.each do |source_user|
curr_machine.communicate.execute("sudo -u "+curr_user+" -H sh /tmp/add_ssh_keys.sh /tmp/.all_"+source_user+"_keys.txt")
end
end
end
running_machines.each do |curr_machine|
(curr_machine)
end
running_machines.each do |curr_machine|
curr_machine.communicate.sudo("rm -f /tmp/add_ssh_keys.sh /tmp/get_user_keys.sh")
required_users.each do |curr_user|
curr_machine.communicate.sudo("rm -f /tmp/.all_"+curr_user+"_keys.txt")
end
end
end
|