Class: Knifecosmic::CosmicServerList

Inherits:
Chef::Knife show all
Includes:
Chef::Knife::KnifecosmicBaseList
Defined in:
lib/chef/knife/cosmic_server_list.rb

Instance Method Summary collapse

Methods included from Chef::Knife::KnifecosmicBaseList

included, #list_object, #list_object_fields, #output_format

Instance Method Details

#confirm_action(question) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/chef/knife/cosmic_server_list.rb', line 156

def confirm_action(question)
  return true if locate_config_value(:yes)
  result = ui.ask_question(question, :default => "Y" )
  if result == "Y" || result == "y" then 
    return true 
  else 
    return false
  end
end

#runObject



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
101
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
# File 'lib/chef/knife/cosmic_server_list.rb', line 58

def run
  validate_base_options
 
  columns = [
    'Name       :name',
    'Public IP  :ipaddress',
    'Service    :serviceofferingname',
    'Template   :templatename',
    'State      :state',
    'Instance   :instancename',
    'Hypervisor :hostname'
  ]

  params = { 'command' => "listVirtualMachines" }
  params['filter']  = locate_config_value(:filter)  if locate_config_value(:filter)
  params['listall'] = locate_config_value(:listall) if locate_config_value(:listall)
  params['keyword'] = locate_config_value(:keyword) if locate_config_value(:keyword)
  params['name']    = locate_config_value(:name)    if locate_config_value(:name)
  params['expunge'] = locate_config_value(:expunge) if locate_config_value(:expunge)
  params['expunge'] = false if params['expunge'].nil?
 
  ##
  # Get the public IP address if possible, except when the option --no-public-ip is given.

  rules       = connection.list_port_forwarding_rules(nil, true)
  public_list = connection.list_public_ip_addresses(true)
  result      = connection.list_object(params, "virtualmachine")
  result.each do |n|
    public_ip = connection.get_server_public_ip(n, rules, public_list) if locate_config_value(:public_ip)
    private_ip = (n['nic'].select { |k| k['isdefault'] }).first
    public_ip ? n['ipaddress'] = public_ip : n['ipaddress'] = private_ip['ipaddress'] || "N/A"
  end

  list_object(columns, result)
  
  ## 
  # Executing actions against the list results that are returned.

  if locate_config_value(:action)
    result.each do |r|
      hostname = r['name'] 
      case locate_config_value(:action).downcase
      when "start" then
        show_object_details(r, connection, rules)
        result = confirm_action("Do you really want to start this server ")
        if result then 
          print "#{ui.color("Waiting for startup", :magenta)}"
   connection.start_server(hostname)
          puts "\n"
          ui.msg("Started server #{hostname}")
        end 
      when "stop" then 
        show_object_details(r, connection, rules)
        result = confirm_action("Do you really want to stop this server ")
        if result then 
          print "#{ui.color("Waiting for shutdown", :magenta)}"
          connection.stop_server(hostname)
          puts "\n"
          ui.msg("Shutdown server #{hostname}")
        end 
      when "destroy" then 
        show_object_details(r, connection, rules)
        result = confirm_action("Do you really want to destroy this server ")
        if result then
          print "#{ui.color("Waiting for demolition", :magenta)}"
          connection.delete_server(hostname, params['expunge'])
          puts "\n"
          ui.msg("Destroyed server #{hostname}")
        end
      end
    end
  end
end

#show_object_details(s, connection, rules) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/knife/cosmic_server_list.rb', line 132

def show_object_details(s, connection, rules)
  return if locate_config_value(:yes)

  object_fields = []
  object_fields << ui.color("Name:", :cyan)
  object_fields << s['name'].to_s
  object_fields << ui.color("Public IP:", :cyan)
  object_fields << (connection.get_server_public_ip(s, rules) || '')
  object_fields << ui.color("Service:", :cyan)
  object_fields << s['serviceofferingname'].to_s
  object_fields << ui.color("Template:", :cyan)
  object_fields << s['templatename']
  object_fields << ui.color("Domain:", :cyan)
  object_fields << s['domain']
  object_fields << ui.color("Zone:", :cyan)
  object_fields << s['zonename']
  object_fields << ui.color("State:", :cyan)
  object_fields << s['state']

  puts "\n"
  puts ui.list(object_fields, :uneven_columns_across, 2)
  puts "\n"
end