Class: SSP::App::OfficeNode

Inherits:
Thor
  • Object
show all
Defined in:
lib/ssp/application/office_node.rb

Instance Method Summary collapse

Instance Method Details

#create(fqdn, *roles) ⇒ Object



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

  # Convert roles array to proper run list
  run_list = roles.map { |r| r =~ /recipe\[(.*)\]/ ? $1 : "role[#{r}]" }

  # Always include the chef-client role in the run list
  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

#destroy(fqdn) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ssp/application/office_node.rb', line 124

def destroy(fqdn)
  hostname = fqdn.split(".").first

  if ask("Are you sure you want to destroy the `#{fqdn}' node? [yes/no]").downcase != "yes"
    raise Thor::Error, "Aborting."
  end

  command = <<EOH
bash -c '
xm shutdown #{hostname}
umount /mnt/#{hostname}
sleep 3
rm /etc/xen/#{hostname}.cfg
lvremove -f vg/#{hostname}-{root,swap}'
EOH

  ssh_run "vanilla", command

  say %x{knife node delete #{fqdn} -y -c #{options[:chef_config]} 2>/dev/null}
  say %x{knife client delete #{fqdn} -y -c #{options[:chef_config]} 2>/dev/null}
end

#listObject



118
119
120
# File 'lib/ssp/application/office_node.rb', line 118

def list
  ssh_run "vanilla", "xm list"
end