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
|