Class: Chef::Knife::CloudstackServerStop

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

Instance Method Summary collapse

Methods included from CloudstackBase

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

Instance Method Details

#runObject



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

def run

  if @name_args.nil? || @name_args.empty?
    puts "#{ui.color("Please provide an Instance ID.", :red)}"
  end

  jobs = {}
  batch_size = 5

  @name_args.each_slice(batch_size) do |batch|
    batch.each do |instance_id|
      response = connection.list_virtual_machines('name' => instance_id)
      instance_name = response['listvirtualmachinesresponse']['virtualmachine'].first['name']
      instance_ip = response['listvirtualmachinesresponse']['virtualmachine'].first['nic'].first['ipaddress']
      real_instance_id = response['listvirtualmachinesresponse']['virtualmachine'].first['id']
      puts "#{ui.color("Name", :red)}: #{instance_name}"
      puts "#{ui.color("Public IP", :red)}: #{instance_ip}"
      puts "\n"
      confirm("#{ui.color("Do you really want to stop this server", :red)}")


      if locate_config_value(:force)
        server = connection.stop_virtual_machine('id' => real_instance_id, 'forced' => true)
      else
        server = connection.stop_virtual_machine('id' => real_instance_id)
      end
      jobid = server['stopvirtualmachineresponse'].fetch('jobid')

      jobs[instance_id] = jobid
    end


    print "#{ui.color("Waiting for servers", :magenta)}"
    until jobs.empty?
      jobs.each do |instance_id, jobid|
        server_stop = connection.query_async_job_result('jobid'=>jobid)

        if server_stop['queryasyncjobresultresponse'].fetch('jobstatus') == 1
          jobs.delete(instance_id)

          puts "\n\n"
          ui.warn("Stopped server #{instance_id}")
        else
          print "#{ui.color(".", :magenta)}"
          sleep(1)
        end
      end
    end
  end
end