Class: Chef::Knife::CloudstackServerCreate

Inherits:
Chef::Knife
  • Object
show all
Includes:
CloudstackBase
Defined in:
lib/chef/knife/cloudstack_server_create.rb

Instance Method Summary collapse

Methods included from CloudstackBase

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

Instance Method Details

#bootstrap_for_node(host, user, password) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/knife/cloudstack_server_create.rb', line 130

def bootstrap_for_node(host, user, password)
  Chef::Log.debug("Bootstrap host: #{host}")
  Chef::Log.debug("Bootstrap user: #{user}")
  Chef::Log.debug("Bootstrap pass: #{password}")
  bootstrap = Chef::Knife::Bootstrap.new
  bootstrap.name_args = host
  bootstrap.config[:run_list] = config[:run_list]
  bootstrap.config[:ssh_user] = user
  bootstrap.config[:ssh_password] = password
  bootstrap.config[:identity_file] = locate_config_value(:identity_file)
  bootstrap.config[:chef_node_name] = config[:server_name] if config[:server_name]
  bootstrap.config[:prerelease] = config[:prerelease]
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
  bootstrap.config[:distro] = locate_config_value(:distro)
  bootstrap.config[:use_sudo] = true
  bootstrap.config[:template_file] = locate_config_value(:template_file)
  bootstrap.config[:environment] = config[:environment]
  # may be needed for vpc_mode

  bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
  bootstrap
end

#runObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/chef/knife/cloudstack_server_create.rb', line 181

def run
  $stdout.sync = true

  options = {}

  options['zoneid'] = locate_config_value(:cloudstack_zoneid)
  options['templateid'] = locate_config_value(:cloudstack_templateid)

  if locate_config_value(:cloudstack_serviceid) != nil
    options['serviceofferingid'] = locate_config_value(:cloudstack_serviceid)
  end

  if locate_config_value(:server_name) != nil
    options['displayname'] = locate_config_value(:server_name)
  end

  if locate_config_value(:host_name) != nil
    options['name'] = locate_config_value(:host_name)
  end

  network_ids = []
  if locate_config_value(:cloudstack_networkids) != []
    cs_networkids = locate_config_value(:cloudstack_networkids)
    cs_networkids.each do |id|
      network_ids.push(id)
    end
    options['networkids'] = network_ids
  end

  security_groups = []
  if locate_config_value(:cloudstack_groupids) != []
    cs_groupids = locate_config_value(:cloudstack_groupids)
    cs_groupids.each do |id|
      security_groups.push(id)
    end
    options['securitygroupids'] = security_groups
  elsif locate_config_value(:cloudstack_groupnames) != []
    cs_groupnames = locate_config_value(:cloudstack_groupnames)
    cs_groupnames.each do |name|
      security_groups.push(name)
    end
    options['securitygroupnames'] = security_groups
  end

  if locate_config_value(:keypair) != nil
    options['keypair'] = locate_config_value(:keypair)
  end

  if locate_config_value(:diskoffering) != nil
    options['diskofferingid'] = locate_config_value(:diskoffering)
  end

  if locate_config_value(:size) != nil
    options['size'] = locate_config_value(:size)
  end

  Chef::Log.debug("Options: #{options} \n")

  server = connection.deploy_virtual_machine(options)
  jobid = server['deployvirtualmachineresponse'].fetch('jobid')

  server_start = connection.query_async_job_result('jobid'=>jobid)

  Chef::Log.debug("Job ID: #{jobid} \n")

  print "#{ui.color("Waiting for server", :magenta)}"
  while server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 0
    print "#{ui.color(".", :magenta)}"
    sleep(15)
    server_start = connection.query_async_job_result('jobid'=>jobid)
    Chef::Log.debug("Server_Start: #{server_start} \n")
  end
  puts "\n\n"

  if server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 2
    errortext = server_start['queryasyncjobresultresponse'].fetch('jobresult').fetch('errortext')
    puts "#{ui.color("ERROR! Job failed with #{errortext}", :red)}"
  end

  if server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 1

    Chef::Log.debug("Job ID: #{jobid} \n")
    Chef::Log.debug("Options: #{options} \n")
    server_start = connection.query_async_job_result('jobid'=>jobid)
    Chef::Log.debug("Server_Start: #{server_start} \n")

    server_info = server_start['queryasyncjobresultresponse']['jobresult']['virtualmachine']

    server_name = server_info['displayname']
    server_id = server_info['name']
    server_serviceoffering = server_info['serviceofferingname']
    server_template = server_info['templatename']
    if server_info['password'] != nil
      ssh_password = server_info['password']
    else
      ssh_password = locate_config_value(:ssh_password)
    end

    ssh_user = locate_config_value(:ssh_user)

    public_ip = nil

    if server_info['nic'].size > 0
      public_ip = server_info['nic'].first['ipaddress']
    end

    puts "\n\n"
    puts "#{ui.color("Name", :cyan)}: #{server_name}"
    puts "#{ui.color("Public IP", :cyan)}: #{public_ip}"
    puts "#{ui.color("Username", :cyan)}: #{ssh_user}"
    puts "#{ui.color("Password", :cyan)}: #{ssh_password}"

    print "\n#{ui.color("Waiting for sshd", :magenta)}"

    print("#{ui.color(".", :magenta)}") until tcp_test_ssh(public_ip) { sleep @initial_sleep_delay ||= 10; puts("done") }

    puts("#{ui.color("Waiting for password/keys to sync.", :magenta)}")
    sleep 15

    bootstrap_for_node(public_ip, ssh_user, ssh_password).run

    Chef::Log.debug("#{server_info}")

    puts "\n"
    puts "#{ui.color("Instance Name", :green)}: #{server_name}"
    puts "#{ui.color("Instance ID", :green)}: #{server_id}"
    puts "#{ui.color("Service Offering", :green)}: #{server_serviceoffering}"
    puts "#{ui.color("Template", :green)}: #{server_template}"
    puts "#{ui.color("Public IP Address", :green)}: #{public_ip}"
    puts "#{ui.color("User", :green)}: #{ssh_user}"
    puts "#{ui.color("Password", :green)}: #{ssh_password}"
    puts "#{ui.color("Environment", :green)}: #{config[:environment] || '_default'}"
    puts "#{ui.color("Run List", :green)}: #{config[:run_list].join(', ')}"
  end

end

#tcp_test_ssh(hostname) ⇒ Object



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
# File 'lib/chef/knife/cloudstack_server_create.rb', line 152

def tcp_test_ssh(hostname)
  print("#{ui.color(".", :magenta)}")
  tcp_socket = TCPSocket.new(hostname, 22)
  readable = IO.select([tcp_socket], nil, nil, 5)
  if readable
    Chef::Log.debug("\nsshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}\n")
    yield
    true
  else
    false
  end

  rescue Errno::ETIMEDOUT
    false
  rescue Errno::EPERM
    false
  rescue Errno::ECONNREFUSED
    sleep 2
    false
  rescue Errno::EHOSTUNREACH
    sleep 2
    false
  rescue Errno::ENETUNREACH
    sleep 30
    false
  ensure
  tcp_socket && tcp_socket.close
end