Class: Chef::Knife::CloudstackServerList

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

Instance Method Summary collapse

Methods included from CloudstackBase

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

Instance Method Details

#Commented out as sorting of VM list is a work-in-progress

option :sort,
       :short => "-o TRUE/FALSE",
       :long => "--sort TRUE/FALSE",
       :description => "Enable or disable sorting by Zone ID then VM ID. Defaults to sorted.",
       :default => true


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

def print_servers(server_list,servers,options={})
  server = servers
  Chef::Log.debug("Servers: #{server}")
  if zoneid = options[:zoneid]
    server.reject!{|t| t['zoneid'] != zoneid}
  end
  if state = options[:state]
    state.downcase!
    server.reject!{|t| t['state'].downcase != state}
  end

=begin
    #Commented out as sorting of VM list is a work-in-progress
  # Sorting to group by zone ID first, then VM ID
  if vmsort = options[:sort]
    sort1 = server.sort_by { |hsh| hsh["zoneid"] }
    sorted = sort1.sort_by { |hsh| hsh["id"] }
  else
    sorted = server
  end
=end

  server.each do |instance|
    server_list << instance['name'].to_s
    server_list << instance['displayname'].to_s
    ip_list = []
    instance['nic'].each do |nic|
      ip_list << nic['ipaddress'].to_s
    end
    server_list << ip_list.join(", ")
    sg_list = []
    instance['securitygroup'].each do |group|
      sg_list << group['name'].to_s
    end
    server_list << sg_list.join(", ")

    server_list << instance['zonename'].to_s
    server_list << instance['serviceofferingname'].to_s
    server_list << instance['templatedisplaytext'].to_s
    server_list << instance['hypervisor'].to_s

    server_list << begin
      state = instance['state'].to_s.downcase
      case state
        when 'shutting-down','terminated','stopping','stopped'
          ui.color(state, :red)
        when 'pending', 'starting'
          ui.color(state, :yellow)
        else
          ui.color(state, :green)
      end
    end
  end

end

#runObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/chef/knife/cloudstack_server_list.rb', line 102

def run
  $stdout.sync = true

  validate!

  server_list = [
    ui.color('Server ID', :bold),
    ui.color('Display Name', :bold),
    ui.color('IP Address', :bold),
    ui.color('Security Group', :bold),
    ui.color('Server Zone', :bold),
    ui.color('Service Offering', :bold),
    ui.color('Template', :bold),
    ui.color('Hypervisor', :bold),
    ui.color('State', :bold)
  ]

  zoneid = locate_config_value(:zoneid)
  state = locate_config_value(:state)
  # vmsort = locate_config_value(:sort)

  response = connection.list_virtual_machines['listvirtualmachinesresponse']
  Chef::Log.debug("API request: #{response}")
  if virtual_machines = response['virtualmachine']
    filters = {}
    filters[:zoneid] = zoneid unless zoneid == 'all'
    filters[:state] = state unless state == 'all'
    print_servers(server_list, virtual_machines, filters)
    puts ui.list(server_list, :uneven_columns_across, 9)
  end
end