knife-cloudstack 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/CHANGES.rdoc +50 -0
  2. data/README.rdoc +221 -42
  3. data/lib/chef/knife/cs_account_list.rb +130 -0
  4. data/lib/chef/knife/cs_base.rb +98 -0
  5. data/lib/chef/knife/cs_baselist.rb +81 -0
  6. data/lib/chef/knife/cs_cluster_list.rb +93 -0
  7. data/lib/chef/knife/cs_config_list.rb +85 -0
  8. data/lib/chef/knife/cs_disk_list.rb +89 -0
  9. data/lib/chef/knife/cs_domain_list.rb +83 -0
  10. data/lib/chef/knife/cs_firewallrule_list.rb +95 -0
  11. data/lib/chef/knife/cs_host_list.rb +95 -0
  12. data/lib/chef/knife/cs_hosts.rb +2 -2
  13. data/lib/chef/knife/cs_iso_list.rb +103 -0
  14. data/lib/chef/knife/cs_network_list.rb +56 -46
  15. data/lib/chef/knife/cs_oscategory_list.rb +78 -0
  16. data/lib/chef/knife/cs_ostype_list.rb +80 -0
  17. data/lib/chef/knife/cs_pod_list.rb +93 -0
  18. data/lib/chef/knife/cs_project_list.rb +92 -0
  19. data/lib/chef/knife/cs_router_list.rb +94 -0
  20. data/lib/chef/knife/cs_server_create.rb +185 -144
  21. data/lib/chef/knife/cs_server_delete.rb +62 -79
  22. data/lib/chef/knife/cs_server_list.rb +136 -57
  23. data/lib/chef/knife/cs_server_reboot.rb +50 -54
  24. data/lib/chef/knife/cs_server_start.rb +48 -52
  25. data/lib/chef/knife/cs_server_stop.rb +54 -55
  26. data/lib/chef/knife/cs_service_list.rb +62 -41
  27. data/lib/chef/knife/cs_stack_create.rb +2 -2
  28. data/lib/chef/knife/cs_stack_delete.rb +2 -2
  29. data/lib/chef/knife/cs_template_create.rb +121 -0
  30. data/lib/chef/knife/cs_template_extract.rb +104 -0
  31. data/lib/chef/knife/cs_template_list.rb +65 -63
  32. data/lib/chef/knife/cs_template_register.rb +180 -0
  33. data/lib/chef/knife/cs_user_list.rb +93 -0
  34. data/lib/chef/knife/cs_volume_list.rb +94 -0
  35. data/lib/chef/knife/cs_zone_list.rb +55 -36
  36. data/lib/knife-cloudstack/connection.rb +75 -22
  37. data/lib/knife-cloudstack/string_to_regexp.rb +32 -0
  38. metadata +93 -61
@@ -1,7 +1,9 @@
1
1
  #
2
2
  # Author:: Ryan Holmes (<rholmes@edmunds.com>)
3
3
  # Author:: KC Braunschweig (<kcbraunschweig@gmail.com>)
4
+ # Author:: Sander Botman (<sbotman@schubergphilis.com>)
4
5
  # Copyright:: Copyright (c) 2011 Edmunds, Inc.
6
+ # Copyright:: Copyright (c) 2013 Sander Botman.
5
7
  # License:: Apache License, Version 2.0
6
8
  #
7
9
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,37 +19,24 @@
17
19
  # limitations under the License.
18
20
  #
19
21
 
20
- require 'chef/knife'
22
+ require 'chef/knife/cs_base'
21
23
 
22
24
  module KnifeCloudstack
23
25
  class CsServerStart < Chef::Knife
24
26
 
27
+ include Chef::Knife::KnifeCloudstackBase
28
+
25
29
  deps do
30
+ require 'chef/knife'
26
31
  require 'knife-cloudstack/connection'
27
32
  require 'chef/api_client'
33
+ Chef::Knife.load_deps
28
34
  end
29
35
 
30
36
  banner "knife cs server start SERVER_NAME [SERVER_NAME ...] (options)"
31
37
 
32
- option :cloudstack_url,
33
- :short => "-U URL",
34
- :long => "--cloudstack-url URL",
35
- :description => "The CloudStack endpoint URL",
36
- :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }
37
-
38
- option :cloudstack_api_key,
39
- :short => "-A KEY",
40
- :long => "--cloudstack-api-key KEY",
41
- :description => "Your CloudStack API key",
42
- :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }
43
-
44
- option :cloudstack_secret_key,
45
- :short => "-K SECRET",
46
- :long => "--cloudstack-secret-key SECRET",
47
- :description => "Your CloudStack secret key",
48
- :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
49
-
50
38
  def run
39
+ validate_base_options
51
40
 
52
41
  @name_args.each do |hostname|
53
42
  server = connection.get_server(hostname)
@@ -57,47 +46,54 @@ module KnifeCloudstack
57
46
  next
58
47
  end
59
48
 
60
- puts "\n"
61
- msg("Name", server['name'])
62
- msg("Public IP", connection.get_server_public_ip(server) || '?')
63
- msg("Service", server['serviceofferingname'])
64
- msg("Template", server['templatename'])
65
- msg("Domain", server['domain'])
66
- msg("Zone", server['zonename'])
67
- msg("State", server['state'])
68
-
69
- puts "\n"
70
- ui.confirm("Do you really want to start this server")
71
-
72
- print "#{ui.color("Waiting for startup", :magenta)}"
73
- connection.start_server(hostname)
74
- puts "\n"
75
- ui.msg("Started server #{hostname}")
49
+ rules = connection.list_port_forwarding_rules
50
+
51
+ show_object_details(server, connection, rules)
52
+
53
+ result = confirm_action("Do you really want to start this server")
54
+ if result
55
+ print "#{ui.color("Waiting for startup", :magenta)}"
56
+ connection.start_server(hostname)
57
+ puts "\n"
58
+ ui.msg("Started server #{hostname}")
59
+ end
76
60
  end
77
61
 
78
62
  end
79
63
 
80
- def connection
81
- unless @connection
82
- @connection = CloudstackClient::Connection.new(
83
- locate_config_value(:cloudstack_url),
84
- locate_config_value(:cloudstack_api_key),
85
- locate_config_value(:cloudstack_secret_key)
86
- )
87
- end
88
- @connection
64
+ def show_object_details(s, connection, rules)
65
+ return if locate_config_value(:yes)
66
+
67
+ object_fields = []
68
+ object_fields << ui.color("Name:", :cyan)
69
+ object_fields << s['name'].to_s
70
+ object_fields << ui.color("Public IP:", :cyan)
71
+ object_fields << (connection.get_server_public_ip(s, rules) || '')
72
+ object_fields << ui.color("Service:", :cyan)
73
+ object_fields << s['serviceofferingname'].to_s
74
+ object_fields << ui.color("Template:", :cyan)
75
+ object_fields << s['templatename']
76
+ object_fields << ui.color("Domain:", :cyan)
77
+ object_fields << s['domain']
78
+ object_fields << ui.color("Zone:", :cyan)
79
+ object_fields << s['zonename']
80
+ object_fields << ui.color("State:", :cyan)
81
+ object_fields << s['state']
82
+
83
+ puts "\n"
84
+ puts ui.list(object_fields, :uneven_columns_across, 2)
85
+ puts "\n"
89
86
  end
90
87
 
91
- def msg(label, value)
92
- if value && !value.empty?
93
- puts "#{ui.color(label, :cyan)}: #{value}"
88
+ def confirm_action(question)
89
+ return true if locate_config_value(:yes)
90
+ result = ui.ask_question(question, :default => "Y" )
91
+ if result == "Y" || result == "y" then
92
+ return true
93
+ else
94
+ return false
94
95
  end
95
96
  end
96
97
 
97
- def locate_config_value(key)
98
- key = key.to_sym
99
- Chef::Config[:knife][key] || config[key]
100
- end
101
-
102
98
  end
103
99
  end
@@ -1,7 +1,9 @@
1
1
  #
2
2
  # Author:: Ryan Holmes (<rholmes@edmunds.com>)
3
3
  # Author:: KC Braunschweig (<kcbraunschweig@gmail.com>)
4
+ # Author:: Sander Botman (<sbotman@schubergphilis.com>)
4
5
  # Copyright:: Copyright (c) 2011 Edmunds, Inc.
6
+ # Copyright:: Copyright (c) 2013 Sander Botman.
5
7
  # License:: Apache License, Version 2.0
6
8
  #
7
9
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,42 +19,29 @@
17
19
  # limitations under the License.
18
20
  #
19
21
 
20
- require 'chef/knife'
22
+ require 'chef/knife/cs_base'
21
23
 
22
24
  module KnifeCloudstack
23
25
  class CsServerStop < Chef::Knife
24
26
 
27
+ include Chef::Knife::KnifeCloudstackBase
28
+
25
29
  deps do
26
30
  require 'knife-cloudstack/connection'
27
31
  require 'chef/api_client'
32
+ require 'chef/knife'
33
+ Chef::Knife.load_deps
28
34
  end
29
35
 
30
36
  banner "knife cs server stop SERVER_NAME [SERVER_NAME ...] (options)"
31
37
 
32
- option :cloudstack_url,
33
- :short => "-U URL",
34
- :long => "--cloudstack-url URL",
35
- :description => "The CloudStack endpoint URL",
36
- :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }
37
-
38
- option :cloudstack_api_key,
39
- :short => "-A KEY",
40
- :long => "--cloudstack-api-key KEY",
41
- :description => "Your CloudStack API key",
42
- :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }
43
-
44
- option :cloudstack_secret_key,
45
- :short => "-K SECRET",
46
- :long => "--cloudstack-secret-key SECRET",
47
- :description => "Your CloudStack secret key",
48
- :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
49
-
50
38
  option :cloudstack_force_stop,
51
39
  :long => "--force",
52
40
  :description => "Force stop the VM. The caller knows the VM is stopped.",
53
41
  :boolean => true
54
42
 
55
43
  def run
44
+ validate_base_options
56
45
 
57
46
  @name_args.each do |hostname|
58
47
  server = connection.get_server(hostname)
@@ -62,53 +51,63 @@ module KnifeCloudstack
62
51
  next
63
52
  end
64
53
 
65
- puts "\n"
66
- msg("Name", server['name'])
67
- msg("Public IP", connection.get_server_public_ip(server) || '?')
68
- msg("Service", server['serviceofferingname'])
69
- msg("Template", server['templatename'])
70
- msg("Domain", server['domain'])
71
- msg("Zone", server['zonename'])
72
- msg("State", server['state'])
54
+ rules = connection.list_port_forwarding_rules
55
+
56
+ show_object_details(server, connection, rules)
73
57
 
74
- puts "\n"
75
58
  if config[:cloudstack_force_stop]
76
- ui.confirm("Do you really want to force stop this server")
77
- print "#{ui.color("Forcefully stopping", :magenta)}"
78
- connection.stop_server(hostname,config[:cloudstack_force_stop])
59
+ result = confirm_action("Do you really want to force stop this server")
60
+ if result
61
+ print "#{ui.color("Forcefully stopping", :magenta)}"
62
+ connection.stop_server(hostname,config[:cloudstack_force_stop])
63
+ puts "\n"
64
+ ui.msg("Stopped server #{hostname}")
65
+ end
79
66
  else
80
- ui.confirm("Do you really want to stop this server")
81
- print "#{ui.color("Stopping", :magenta)}"
82
- connection.stop_server(hostname)
67
+ result = confirm_action("Do you really want to stop this server")
68
+ if result
69
+ print "#{ui.color("Stopping", :magenta)}"
70
+ connection.stop_server(hostname)
71
+ puts "\n"
72
+ ui.msg("Stopped server #{hostname}")
73
+ end
83
74
  end
84
-
85
- puts "\n"
86
- ui.msg("Stopped server #{hostname}")
87
75
  end
88
-
89
76
  end
90
77
 
91
- def connection
92
- unless @connection
93
- @connection = CloudstackClient::Connection.new(
94
- locate_config_value(:cloudstack_url),
95
- locate_config_value(:cloudstack_api_key),
96
- locate_config_value(:cloudstack_secret_key)
97
- )
98
- end
99
- @connection
78
+ def show_object_details(s, connection, rules)
79
+ return if locate_config_value(:yes)
80
+
81
+ object_fields = []
82
+ object_fields << ui.color("Name:", :cyan)
83
+ object_fields << s['name'].to_s
84
+ object_fields << ui.color("Public IP:", :cyan)
85
+ object_fields << (connection.get_server_public_ip(s, rules) || '')
86
+ object_fields << ui.color("Service:", :cyan)
87
+ object_fields << s['serviceofferingname'].to_s
88
+ object_fields << ui.color("Template:", :cyan)
89
+ object_fields << s['templatename']
90
+ object_fields << ui.color("Domain:", :cyan)
91
+ object_fields << s['domain']
92
+ object_fields << ui.color("Zone:", :cyan)
93
+ object_fields << s['zonename']
94
+ object_fields << ui.color("State:", :cyan)
95
+ object_fields << s['state']
96
+
97
+ puts "\n"
98
+ puts ui.list(object_fields, :uneven_columns_across, 2)
99
+ puts "\n"
100
100
  end
101
101
 
102
- def msg(label, value)
103
- if value && !value.empty?
104
- puts "#{ui.color(label, :cyan)}: #{value}"
102
+ def confirm_action(question)
103
+ return true if locate_config_value(:yes)
104
+ result = ui.ask_question(question, :default => "Y" )
105
+ if result == "Y" || result == "y" then
106
+ return true
107
+ else
108
+ return false
105
109
  end
106
110
  end
107
111
 
108
- def locate_config_value(key)
109
- key = key.to_sym
110
- Chef::Config[:knife][key] || config[key]
111
- end
112
-
113
112
  end
114
113
  end
@@ -1,6 +1,8 @@
1
1
  #
2
2
  # Author:: Ryan Holmes (<rholmes@edmunds.com>)
3
+ # Author:: Sander Botman (<sbotman@schubergphilis.com>)
3
4
  # Copyright:: Copyright (c) 2011 Edmunds, Inc.
5
+ # Copyright:: Copyright (c) 2013 Sander Botman.
4
6
  # License:: Apache License, Version 2.0
5
7
  #
6
8
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,63 +18,87 @@
16
18
  # limitations under the License.
17
19
  #
18
20
 
19
- require 'chef/knife'
21
+ require 'chef/knife/cs_base'
22
+ require 'chef/knife/cs_baselist'
20
23
 
21
24
  module KnifeCloudstack
22
25
  class CsServiceList < Chef::Knife
23
26
 
24
- MEGABYTES = 1024 * 1024
27
+ include Chef::Knife::KnifeCloudstackBase
28
+ include Chef::Knife::KnifeCloudstackBaseList
25
29
 
26
30
  deps do
31
+ require 'chef/knife'
27
32
  require 'knife-cloudstack/connection'
33
+ Chef::Knife.load_deps
28
34
  end
29
35
 
30
36
  banner "knife cs service list (options)"
31
37
 
32
- option :cloudstack_url,
33
- :short => "-U URL",
34
- :long => "--cloudstack-url URL",
35
- :description => "The CloudStack endpoint URL",
36
- :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }
38
+ option :name,
39
+ :long => "--name NAME",
40
+ :description => "Specify servicename to list"
37
41
 
38
- option :cloudstack_api_key,
39
- :short => "-A KEY",
40
- :long => "--cloudstack-api-key KEY",
41
- :description => "Your CloudStack API key",
42
- :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }
42
+ option :keyword,
43
+ :long => "--keyword NAME",
44
+ :description => "Specify part of servicename to list"
43
45
 
44
- option :cloudstack_secret_key,
45
- :short => "-K SECRET",
46
- :long => "--cloudstack-secret-key SECRET",
47
- :description => "Your CloudStack secret key",
48
- :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
46
+ option :index,
47
+ :long => "--index",
48
+ :description => "Add index numbers to the output",
49
+ :boolean => true
49
50
 
50
51
  def run
51
-
52
- connection = CloudstackClient::Connection.new(
53
- locate_config_value(:cloudstack_url),
54
- locate_config_value(:cloudstack_api_key),
55
- locate_config_value(:cloudstack_secret_key)
56
- )
57
-
58
- service_list = [
52
+ validate_base_options
53
+
54
+ object_list = []
55
+ object_list << ui.color('Index', :bold) if locate_config_value(:index)
56
+
57
+ if locate_config_value(:fields)
58
+ locate_config_value(:fields).split(',').each { |n| object_list << ui.color(("#{n}").strip, :bold) }
59
+ else
60
+ [
59
61
  ui.color('Name', :bold),
60
62
  ui.color('Memory', :bold),
61
63
  ui.color('CPUs', :bold),
62
64
  ui.color('CPU Speed', :bold),
63
65
  ui.color('Created', :bold)
64
- ]
65
-
66
- services = connection.list_service_offerings
67
- services.each do |s|
68
- service_list << s['name']
69
- service_list << (human_memory(s['memory']) || 'Unknown')
70
- service_list << s['cpunumber'].to_s
71
- service_list << s['cpuspeed'].to_s + ' Mhz'
72
- service_list << s['created']
66
+ ].each { |field| object_list << field }
73
67
  end
74
- puts ui.list(service_list, :columns_across, 5)
75
68
 
69
+ columns = object_list.count
70
+ object_list = [] if locate_config_value(:noheader)
71
+
72
+ connection_result = connection.list_object(
73
+ "listServiceOfferings",
74
+ "serviceoffering",
75
+ locate_config_value(:filter),
76
+ false,
77
+ locate_config_value(:keyword),
78
+ locate_config_value(:name)
79
+ )
80
+
81
+ output_format(connection_result)
82
+
83
+ index_num = 0
84
+ connection_result.each do |r|
85
+ if locate_config_value(:index)
86
+ index_num += 1
87
+ object_list << index_num.to_s
88
+ end
89
+
90
+ if locate_config_value(:fields)
91
+ locate_config_value(:fields).downcase.split(',').each { |n| object_list << ((r[("#{n}").strip]).to_s || 'N/A') }
92
+ else
93
+ object_list << r['name'].to_s
94
+ object_list << (r['memory'] ? human_memory(r['memory']) : 'Unknown')
95
+ object_list << r['cpunumber'].to_s
96
+ object_list << r['cpuspeed'].to_s + ' Mhz'
97
+ object_list << r['created']
98
+ end
99
+ end
100
+ puts ui.list(object_list, :uneven_columns_across, columns)
101
+ list_object_fields(connection_result) if locate_config_value(:fieldlist)
76
102
  end
77
103
 
78
104
  def human_memory n
@@ -81,12 +107,7 @@ module KnifeCloudstack
81
107
  n /= 1024.0
82
108
  count += 1
83
109
  end
84
- format("%.2f", n) + %w(MB GB TB)[count]
85
- end
86
-
87
- def locate_config_value(key)
88
- key = key.to_sym
89
- Chef::Config[:knife][key] || config[key]
110
+ format("%.0f", n) + %w(MB GB TB)[count]
90
111
  end
91
112
 
92
113
  end
@@ -16,8 +16,6 @@
16
16
  # limitations under the License.
17
17
  #
18
18
 
19
- require 'chef/knife'
20
-
21
19
  module KnifeCloudstack
22
20
  class CsStackCreate < Chef::Knife
23
21
 
@@ -31,6 +29,8 @@ module KnifeCloudstack
31
29
  require 'net/ssh'
32
30
  require 'net/ssh/multi'
33
31
  require 'knife-cloudstack/connection'
32
+ require 'chef/knife'
33
+ Chef::Knife.load_deps
34
34
  Chef::Knife::Ssh.load_deps
35
35
  Chef::Knife::NodeRunListRemove.load_deps
36
36
  KnifeCloudstack::CsServerCreate.load_deps