Class: Chef::Knife::GlesysServerCreate

Inherits:
Chef::Knife show all
Includes:
GlesysBase
Defined in:
lib/chef/knife/glesys_server_create.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GlesysBase

#color_state, #connection, included, #locate_config_value, #msg_pair, #validate!

Instance Attribute Details

#serverObject (readonly)

Returns the value of attribute server.



11
12
13
# File 'lib/chef/knife/glesys_server_create.rb', line 11

def server
  @server
end

Instance Method Details

#bootstrap_for_node(server, ssh_host) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/chef/knife/glesys_server_create.rb', line 207

def bootstrap_for_node(server,ssh_host)
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = [ssh_host]
  bootstrap.config[:run_list] = locate_config_value(:run_list) || []
  bootstrap.config[:ssh_user] = "root"
  bootstrap.config[:ssh_port] = config[:ssh_port]
  bootstrap.config[:ssh_password] = server.rootpassword
  bootstrap.config[:ssh_gateway] = config[:ssh_gateway]
  bootstrap.config[:use_sudo] = false
  # bootstrap.config[:identity_file] = config[:identity_file]
  bootstrap.config[:chef_node_name] = locate_config_value(:chef_node_name) || server.serverid
  bootstrap.config[:prerelease] = config[:prerelease]
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:first_boot_attributes] = locate_config_value(:json_attributes) || {}
  bootstrap.config[:distro] = locate_config_value(:distro) || "chef-full"
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = config[:environment]

  # knife-bootstrap
  Chef::Config[:knife][:hints] ||= {}
  Chef::Config[:knife][:hints]["glesys"] ||= {}
  bootstrap
end

#create_server_defObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/chef/knife/glesys_server_create.rb', line 190

def create_server_def
  default_server = {
    templatename: locate_config_value(:image),
    datacenter: locate_config_value(:datacenter),
    platform: locate_config_value(:platform),
    memorysize: locate_config_value(:memorysize),
    disksize: locate_config_value(:disksize),
    cpucores: locate_config_value(:cpucores),
    transfer: locate_config_value(:transfer),
    description: config[:description],
    hostname: config[:hostname],
    rootpassword: config[ :rootpassword ]
  }

  default_server
end

#runObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/chef/knife/glesys_server_create.rb', line 136

def run
  validate!

  default_server = create_server_def
  @server = connection.servers.create default_server

  # Show information about the new server
  msg_pair("Server ID", @server.serverid)
  msg_pair("State", ui.color(color_state(@server.state),:bold))
  msg_pair("Hostname", @server.hostname)
  msg_pair("Description", @server.description)
  puts "\n"
  msg_pair("IPv4", @server.iplist.select{|i| i["version"] == 4}.collect{|i| i["ipaddress"]}.join(", "))
  msg_pair("IPv6", @server.iplist.select{|i| i["version"] == 6}.collect{|i| i["ipaddress"]}.join(", "))
  puts "\n"
  msg_pair("CPU Cores", @server.cpucores)
  msg_pair("Memory", "#{@server.memorysize} MB")
  msg_pair("Disk", "#{@server.disksize} GB")
  puts "\n"
  msg_pair("Image", @server.templatename)
  msg_pair("Platform", @server.platform)
  msg_pair("Datacenter", @server.datacenter)

  # Waiting for server to boot
  print "\nBooting"
  @server.wait_for{ print "."; ready? }

  # Waiting for sshd to start
  wait_for_sshd(@server.public_ip_address)

  # Bootstrap the node
  bootstrap_for_node(@server,@server.public_ip_address).run

  msg_pair("Server ID", @server.serverid)
  msg_pair("State", ui.color(color_state(@server.state),:bold))
  msg_pair("Hostname", @server.hostname)
  msg_pair("Description", @server.description)
  puts "\n"
  msg_pair("IPv4", @server.iplist.select{|i| i["version"] == 4}.collect{|i| i["ipaddress"]}.join(", "))
  msg_pair("IPv6", @server.iplist.select{|i| i["version"] == 6}.collect{|i| i["ipaddress"]}.join(", "))
  puts "\n"
  msg_pair("CPU Cores", @server.cpucores)
  msg_pair("Memory", "#{@server.memorysize} MB")
  msg_pair("Disk", "#{@server.disksize} GB")
  puts "\n"
  msg_pair("Image", @server.templatename)
  msg_pair("Platform", @server.platform)
  msg_pair("Datacenter", @server.datacenter)
  puts "\n"
  msg_pair("Environment", config[:environment] || '_default')
  msg_pair("Run List", (config[:run_list] || []).join(', '))
  msg_pair("JSON Attributes", config[:json_attributes]) unless !config[:json_attributes] || config[:json_attributes].empty?
end

#ssh_connect_hostObject



266
267
268
# File 'lib/chef/knife/glesys_server_create.rb', line 266

def ssh_connect_host
  @server.public_ip_address
end

#tcp_test_ssh(hostname, ssh_port) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/knife/glesys_server_create.rb', line 117

def tcp_test_ssh(hostname, ssh_port)
  tcp_socket = TCPSocket.new(hostname, ssh_port)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
    yield
    true
  else
    false
  end
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
  sleep 2
  false
rescue Errno::EPERM, Errno::ETIMEDOUT
  false
ensure
  tcp_socket && tcp_socket.close
end

#tunnel_test_ssh(hostname, &block) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/chef/knife/glesys_server_create.rb', line 243

def tunnel_test_ssh(hostname, &block)
  gw_host, gw_user = config[:ssh_gateway].split('@').reverse
  gw_host, gw_port = gw_host.split(':')
  gateway = Net::SSH::Gateway.new(gw_host, gw_user, :port => gw_port || 22)
  status = false
  gateway.open(hostname, config[:ssh_port]) do |local_tunnel_port|
    status = tcp_test_ssh('localhost', local_tunnel_port, &block)
  end
  status
rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
  sleep 2
  false
rescue Errno::EPERM, Errno::ETIMEDOUT
  false
end

#wait_for_direct_sshd(hostname, ssh_port) ⇒ Object



259
260
261
262
263
264
# File 'lib/chef/knife/glesys_server_create.rb', line 259

def wait_for_direct_sshd(hostname, ssh_port)
  print "\nWaiting for ssh."
  print(".") until tcp_test_ssh(ssh_connect_host, ssh_port) {
    puts "done"
  }
end

#wait_for_sshd(hostname) ⇒ Object



231
232
233
# File 'lib/chef/knife/glesys_server_create.rb', line 231

def wait_for_sshd(hostname)
  config[:ssh_gateway] ? wait_for_tunnelled_sshd(hostname) : wait_for_direct_sshd(hostname, config[:ssh_port])
end

#wait_for_tunnelled_sshd(hostname) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/chef/knife/glesys_server_create.rb', line 235

def wait_for_tunnelled_sshd(hostname)
  print("\nWaiting for sshd tunnel.")
  print(".") until tunnel_test_ssh(ssh_connect_host) {
    sleep @initial_sleep_delay ||= (vpc_mode? ? 40 : 10)
    puts("done")
  }
end