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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/ssp/application/office_node.rb', line 28
def create(fqdn, *roles)
ip = "192.168.1.#{options[:iplastpart]}"
disk_size = options[:memory] * 20 / 1024
hostname = fqdn.split(".").first
run_list = roles.map { |r| r =~ /recipe\[(.*)\]/ ? $1 : "role[#{r}]" }
unless run_list.include? "role[chef-client]"
run_list.unshift "role[chef-client]"
end
vncpass = pwgen
rootpass = pwgen
say_status "Name: ", fqdn, :cyan
say_status "Memory: ", "#{options[:memory]}MB", :cyan
say_status "Disk: ", "#{disk_size}GB", :cyan
say_status "IP: ", ip, :cyan
say_status "VNC password: ", vncpass, :cyan
say_status "root password: ", rootpass, :cyan
say "\nRequesting server", :magenta
command = <<EOH
bash -c '
/root/bin/mkdomu.sh -m #{options[:memory]} -d #{disk_size} -v #{vncpass} -p #{rootpass} #{hostname} #{options[:iplastpart]}
xm create /etc/xen/#{hostname}.cfg
'
EOH
ssh_run "vanilla", command
say "\nServer ready, waiting 15 seconds to bootstrap."
sleep 15
say "\nBootstrapping #{shell.set_color(fqdn, :bold)}..."
command = <<EOH
bash -c '
mkdir -p /etc/chef
(
cat <<'EOP'
127.0.0.1 localhost localhost.localdomain
#{ip} #{fqdn} #{fqdn.split(".").first}
192.168.1.82 basil basil.sspti.me chef.sspti.me
EOP
) > /etc/hosts
(
cat <<'EOP'
#{IO.read(chef_config[:validation_key])}
EOP
) > /tmp/validation.pem
awk NF /tmp/validation.pem > /etc/chef/validation.pem
rm /tmp/validation.pem
(
cat <<'EOP'
log_level :info
log_location STDOUT
chef_server_url "#{chef_config[:chef_server_url]}"
validation_client_name "#{chef_config[:validation_client_name]}"
EOP
) > /etc/chef/client.rb
(
cat <<'EOP'
#{{ "run_list" => run_list }.to_json}
EOP
) > /etc/chef/first-boot.json
apt-get -y update
apt-get -y upgrade
/usr/local/bin/chef-client -j /etc/chef/first-boot.json'
EOH
begin
ssh_run ip, command, "root", rootpass
rescue Net::SSH::HostKeyMismatch => key_ex
key_ex.remember_host!
retry
end
end
|