knife-cloudstack 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,88 @@
1
+ #
2
+ # Author:: Ryan Holmes (<rholmes@edmunds.com>)
3
+ # Copyright:: Copyright (c) 2011 Edmunds, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+
21
+ module KnifeCloudstack
22
+ class CsStackDelete < Chef::Knife
23
+
24
+ deps do
25
+ require 'chef/json_compat'
26
+ require 'chef/mash'
27
+ require 'knife-cloudstack/connection'
28
+ KnifeCloudstack::CsServerDelete.load_deps
29
+ end
30
+
31
+ banner "knife cs stack delete JSON_FILE (options)"
32
+
33
+ option :cloudstack_url,
34
+ :short => "-U URL",
35
+ :long => "--cloudstack-url URL",
36
+ :description => "The CloudStack endpoint URL",
37
+ :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }
38
+
39
+ option :cloudstack_api_key,
40
+ :short => "-A KEY",
41
+ :long => "--cloudstack-api-key KEY",
42
+ :description => "Your CloudStack API key",
43
+ :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }
44
+
45
+ option :cloudstack_secret_key,
46
+ :short => "-K SECRET",
47
+ :long => "--cloudstack-secret-key SECRET",
48
+ :description => "Your CloudStack secret key",
49
+ :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
50
+
51
+ def run
52
+ file_path = File.expand_path(@name_args.first)
53
+ unless File.exist?(file_path) then
54
+ ui.error "Stack file '#{file_path}' not found. Please check the path."
55
+ exit 1
56
+ end
57
+
58
+ data = File.read file_path
59
+ stack = Chef::JSONCompat.from_json data
60
+ delete_stack stack
61
+
62
+ end
63
+
64
+ def delete_stack(stack)
65
+ current_stack = Mash.new(stack)
66
+ current_stack[:servers].each do |server|
67
+ if server[:name]
68
+
69
+ # delete server(s)
70
+ names = server[:name].split(/[\s,]+/)
71
+ names.each do |name|
72
+ delete_server(name)
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+ end
79
+
80
+ def delete_server(server_name)
81
+ cmd = KnifeCloudstack::CsServerDelete.new([server_name])
82
+ cmd.config[:yes] = true
83
+ cmd.run_with_pretty_exceptions
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,100 @@
1
+ #
2
+ # Author:: Ryan Holmes (<rholmes@edmunds.com>)
3
+ # Copyright:: Copyright (c) 2011 Edmunds, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+
21
+ module KnifeCloudstack
22
+ class CsTemplateList < Chef::Knife
23
+
24
+ MEGABYTES = 1024 * 1024
25
+
26
+ deps do
27
+ require 'knife-cloudstack/connection'
28
+ end
29
+
30
+ banner "knife cs template list (options)"
31
+
32
+ option :filter,
33
+ :short => "-L FILTER",
34
+ :long => "--filter FILTER",
35
+ :description => "The template search filter. Default is 'featured'",
36
+ :default => "featured"
37
+
38
+ option :cloudstack_url,
39
+ :short => "-U URL",
40
+ :long => "--cloudstack-url URL",
41
+ :description => "The CloudStack endpoint URL",
42
+ :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }
43
+
44
+ option :cloudstack_api_key,
45
+ :short => "-A KEY",
46
+ :long => "--cloudstack-api-key KEY",
47
+ :description => "Your CloudStack API key",
48
+ :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }
49
+
50
+ option :cloudstack_secret_key,
51
+ :short => "-K SECRET",
52
+ :long => "--cloudstack-secret-key SECRET",
53
+ :description => "Your CloudStack secret key",
54
+ :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
55
+
56
+ def run
57
+
58
+ connection = CloudstackClient::Connection.new(
59
+ locate_config_value(:cloudstack_url),
60
+ locate_config_value(:cloudstack_api_key),
61
+ locate_config_value(:cloudstack_secret_key)
62
+ )
63
+
64
+ template_list = [
65
+ ui.color('Name', :bold),
66
+ ui.color('Size', :bold),
67
+ ui.color('Zone', :bold),
68
+ ui.color('Public', :bold),
69
+ ui.color('Created', :bold),
70
+ ]
71
+
72
+ filter = config['filter']
73
+ templates = connection.list_templates(filter)
74
+ templates.each do |t|
75
+ template_list << t['name']
76
+ template_list << (human_file_size(t['size']) || 'Unknown')
77
+ template_list << t['zonename']
78
+ template_list << t['ispublic'].to_s
79
+ template_list << t['created']
80
+ end
81
+ puts ui.list(template_list, :columns_across, 5)
82
+
83
+ end
84
+
85
+ def human_file_size n
86
+ count = 0
87
+ while n >= 1024 and count < 4
88
+ n /= 1024.0
89
+ count += 1
90
+ end
91
+ format("%.2f", n) + %w(B KB MB GB TB)[count]
92
+ end
93
+
94
+ def locate_config_value(key)
95
+ key = key.to_sym
96
+ Chef::Config[:knife][key] || config[key]
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,78 @@
1
+ #
2
+ # Author:: Ryan Holmes (<rholmes@edmunds.com>)
3
+ # Copyright:: Copyright (c) 2011 Edmunds, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/knife'
20
+
21
+ module KnifeCloudstack
22
+ class CsZoneList < Chef::Knife
23
+
24
+ deps do
25
+ require 'knife-cloudstack/connection'
26
+ end
27
+
28
+ banner "knife cs zone list (options)"
29
+
30
+ option :cloudstack_url,
31
+ :short => "-U URL",
32
+ :long => "--cloudstack-url URL",
33
+ :description => "The CloudStack endpoint URL",
34
+ :proc => Proc.new { |url| Chef::Config[:knife][:cloudstack_url] = url }
35
+
36
+ option :cloudstack_api_key,
37
+ :short => "-A KEY",
38
+ :long => "--cloudstack-api-key KEY",
39
+ :description => "Your CloudStack API key",
40
+ :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_api_key] = key }
41
+
42
+ option :cloudstack_secret_key,
43
+ :short => "-K SECRET",
44
+ :long => "--cloudstack-secret-key SECRET",
45
+ :description => "Your CloudStack secret key",
46
+ :proc => Proc.new { |key| Chef::Config[:knife][:cloudstack_secret_key] = key }
47
+
48
+ def run
49
+
50
+ connection = CloudstackClient::Connection.new(
51
+ locate_config_value(:cloudstack_url),
52
+ locate_config_value(:cloudstack_api_key),
53
+ locate_config_value(:cloudstack_secret_key)
54
+ )
55
+
56
+ zone_list = [
57
+ ui.color('Name', :bold),
58
+ ui.color('Network Type', :bold),
59
+ ui.color('Security Groups', :bold)
60
+ ]
61
+
62
+ zones = connection.list_zones
63
+ zones.each do |z|
64
+ zone_list << z['name']
65
+ zone_list << z['networktype']
66
+ zone_list << z['securitygroupsenabled'].to_s
67
+ end
68
+ puts ui.list(zone_list, :columns_across, 3)
69
+
70
+ end
71
+
72
+ def locate_config_value(key)
73
+ key = key.to_sym
74
+ Chef::Config[:knife][key] || config[key]
75
+ end
76
+
77
+ end
78
+ end