Class: KnifeCloudstack::CsServiceList

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

Constant Summary collapse

MEGABYTES =
1024 * 1024

Instance Method Summary collapse

Instance Method Details

#human_memory(n) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/chef/knife/cs_service_list.rb', line 78

def human_memory n
  count = 0
  while  n >= 1024 and count < 2
    n /= 1024.0
    count += 1
  end
  format("%.2f", n) + %w(MB GB TB)[count]
end

#locate_config_value(key) ⇒ Object



87
88
89
90
# File 'lib/chef/knife/cs_service_list.rb', line 87

def locate_config_value(key)
  key = key.to_sym
  Chef::Config[:knife][key] || config[key]
end

#runObject



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

def run

  connection = CloudstackClient::Connection.new(
      locate_config_value(:cloudstack_url),
      locate_config_value(:cloudstack_api_key),
      locate_config_value(:cloudstack_secret_key)
  )

  service_list = [
      ui.color('Name', :bold),
      ui.color('Memory', :bold),
      ui.color('CPUs', :bold),
      ui.color('CPU Speed', :bold),
      ui.color('Created', :bold)
  ]

  services = connection.list_service_offerings
  services.each do |s|
    service_list << s['name']
    service_list << (human_memory(s['memory']) || 'Unknown')
    service_list << s['cpunumber'].to_s
    service_list << s['cpuspeed'].to_s + ' Mhz'
    service_list << s['created']
  end
  puts ui.list(service_list, :columns_across, 5)

end