knife-cloudstack-fog 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,275 @@
1
+ # Author:: Jeff Moody (<jmoody@datapipe.com>)
2
+ # Copyright:: Copyright (c) 2012 Datapipe
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'chef/knife/bootstrap'
19
+ Chef::Knife::Bootstrap.load_deps
20
+ require 'socket'
21
+ require 'net/ssh/multi'
22
+ require 'chef/json_compat'
23
+ require 'chef/knife/cloudstack_base'
24
+
25
+ class Chef
26
+ class Knife
27
+ class CloudstackServerCreate < Knife
28
+
29
+ include Knife::CloudstackBase
30
+
31
+ banner "knife cloudstack server create (options)"
32
+
33
+ option :cloudstack_serviceid,
34
+ :short => "-s SERVICEID",
35
+ :long => "--serviceid SERVICEID",
36
+ :description => "The CloudStack service offering ID."
37
+
38
+ option :cloudstack_templateid,
39
+ :short => "-t TEMPLATEID",
40
+ :long => "--templateid TEMPLATEID",
41
+ :description => "The CloudStack template ID for the server."
42
+
43
+ option :cloudstack_zoneid,
44
+ :short => "-z ZONEID",
45
+ :long => "--zoneid ZONE",
46
+ :description => "The CloudStack zone ID for the server."
47
+
48
+ option :cloudstack_networknames,
49
+ :short => "-W NETWORKNAMES",
50
+ :long => "--networknames NETWORKNAMES",
51
+ :description => "Comma separated list of CloudStack network names. Each group name must be encapuslated in quotes if it contains whitespace.",
52
+ :proc => lambda { |n| n.split(/[\s,]+/) },
53
+ :default => []
54
+
55
+ option :cloudstack_networkids,
56
+ :short => "-w NETWORKIDS",
57
+ :long => "--networkids NETWORKIDS",
58
+ :description => "Comma separated list of CloudStack network IDs.",
59
+ :proc => lambda { |n| n.split(/[\s,]+/) },
60
+ :default => []
61
+
62
+ option :cloudstack_groupids,
63
+ :short => "-g SECURITYGROUPIDS",
64
+ :long => "--groupids SECURITYGROUPIDS",
65
+ :description => "Comma separated list of CloudStack Security Group IDs.",
66
+ :proc => lambda { |n| n.split(/[\s,]+/) },
67
+ :default => []
68
+
69
+ option :cloudstack_groupnames,
70
+ :short => "-G SECURITYGROUPNAMES",
71
+ :long => "--groupnames SECURITYGROUPNAMES",
72
+ :description => "Comma separated list of CloudStack Security Group names. Each group name must be encapuslated in quotes if it contains whitespace.",
73
+ :proc => lambda { |n| n.split(/[\s,]+/) },
74
+ :default => []
75
+
76
+ option :distro,
77
+ :short => "-d DISTRO",
78
+ :long => "--distro DISTRO",
79
+ :description => "Bootstrap a distro using a template; default is 'ubuntu10.04-gems'",
80
+ :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
81
+ :default => "ubuntu10.04-gems"
82
+
83
+ option :template_file,
84
+ :long => "--template-file TEMPLATE",
85
+ :description => "Full path to location of template to use",
86
+ :proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
87
+ :default => false
88
+
89
+ option :run_list,
90
+ :short => "-r RUN_LIST",
91
+ :long => "--run-list RUN_LIST",
92
+ :description => "Comma separated list of roles/recipes to apply",
93
+ :proc => lambda { |o| o.split(/[\s,]+/) },
94
+ :default => []
95
+
96
+ option :ssh_user,
97
+ :short => "-x USERNAME",
98
+ :long => "--ssh-user USERNAME",
99
+ :description => "The ssh username",
100
+ :default => 'root'
101
+
102
+ option :ssh_password,
103
+ :short => "-P PASSWORD",
104
+ :long => "--ssh-password PASSWORD",
105
+ :description => "The ssh password"
106
+
107
+ option :server_name,
108
+ :short => "-N NAME",
109
+ :long => "--server-name NAME",
110
+ :description => "The server name"
111
+
112
+ option :keypair,
113
+ :short => "-k KEYPAIR",
114
+ :long => "--keypair KEYPAIR",
115
+ :description => "The CloudStack Key Pair to use for password generation/storage"
116
+
117
+ option :diskoffering,
118
+ :short => "-D DISKOFFERINGID",
119
+ :long => "--diskoffering DISKOFFERINGID",
120
+ :description => "Specifies either the Disk Offering ID for the ROOT disk for an ISO template, or a DATA disk."
121
+
122
+ def bootstrap_for_node(host, user, password)
123
+ Chef::Log.debug("Bootstrap host: #{host}")
124
+ Chef::Log.debug("Bootstrap user: #{user}")
125
+ Chef::Log.debug("Bootstrap pass: #{password}")
126
+ bootstrap = Chef::Knife::Bootstrap.new
127
+ bootstrap.name_args = host
128
+ bootstrap.config[:run_list] = config[:run_list]
129
+ bootstrap.config[:ssh_user] = user
130
+ bootstrap.config[:ssh_password] = password
131
+ bootstrap.config[:identity_file] = config[:identity_file]
132
+ bootstrap.config[:chef_node_name] = config[:server_name] if config[:server_name]
133
+ bootstrap.config[:prerelease] = config[:prerelease]
134
+ bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
135
+ bootstrap.config[:distro] = locate_config_value(:distro)
136
+ bootstrap.config[:use_sudo] = true
137
+ bootstrap.config[:template_file] = locate_config_value(:template_file)
138
+ bootstrap.config[:environment] = config[:environment]
139
+ # may be needed for vpc_mode
140
+ bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
141
+ bootstrap
142
+ end
143
+
144
+ def tcp_test_ssh(hostname)
145
+ tcp_socket = TCPSocket.new(hostname, 22)
146
+ readable = IO.select([tcp_socket], nil, nil, 5)
147
+ if readable
148
+ Chef::Log.debug("\nsshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}\n")
149
+ yield
150
+ true
151
+ else
152
+ false
153
+ end
154
+
155
+ rescue Errno::ETIMEDOUT
156
+ false
157
+ rescue Errno::EPERM
158
+ false
159
+ rescue Errno::ECONNREFUSED
160
+ sleep 2
161
+ false
162
+ rescue Errno::EHOSTUNREACH
163
+ sleep 2
164
+ false
165
+ ensure
166
+ tcp_socket && tcp_socket.close
167
+ end
168
+
169
+ def run
170
+ $stdout.sync = true
171
+
172
+ options = {}
173
+
174
+ options['zoneid'] = locate_config_value(:cloudstack_zoneid)
175
+ options['templateid'] = locate_config_value(:cloudstack_templateid)
176
+
177
+ if locate_config_value(:cloudstack_serviceid) != nil
178
+ options['serviceofferingid'] = locate_config_value(:cloudstack_serviceid)
179
+ end
180
+
181
+ if locate_config_value(:server_name) != nil
182
+ options['displayname'] = locate_config_value(:server_name)
183
+ end
184
+
185
+ security_groups = []
186
+ if locate_config_value(:cloudstack_groupids) != []
187
+ cs_groupids = locate_config_value(:cloudstack_groupids)
188
+ cs_groupids.each do |id|
189
+ security_groups.push(id)
190
+ end
191
+ options['securitygroupids'] = security_groups
192
+ else
193
+ cs_groupnames = locate_config_value(:cloudstack_groupnames)
194
+ cs_groupnames.each do |name|
195
+ security_groups.push(name)
196
+ end
197
+ options['securitygroupnames'] = security_groups
198
+ end
199
+
200
+ if locate_config_value(:keypair) != nil
201
+ options['keypair'] = locate_config_value(:keypair)
202
+ end
203
+
204
+ if locate_config_value(:diskoffering) != nil
205
+ options['diskofferingid'] = locate_config_value(:diskoffering)
206
+ end
207
+
208
+ Chef::Log.debug("Options: #{options} \n")
209
+
210
+ server = connection.deploy_virtual_machine(options)
211
+ jobid = server['deployvirtualmachineresponse'].fetch('jobid')
212
+
213
+ server_start = connection.query_async_job_result('jobid'=>jobid)
214
+ print "#{ui.color("Waiting for server", :magenta)}"
215
+ while server_start['queryasyncjobresultresponse'].fetch('jobstatus') != 1
216
+ print "#{ui.color(".", :magenta)}"
217
+ sleep(1)
218
+ server_start = connection.query_async_job_result('jobid'=>jobid)
219
+ end
220
+ puts "\n\n"
221
+
222
+ Chef::Log.debug("Job ID: #{jobid} \n")
223
+ Chef::Log.debug("Options: #{options} \n")
224
+ server_start = connection.query_async_job_result('jobid'=>jobid)
225
+ Chef::Log.debug("Server_Start: #{server_start} \n")
226
+
227
+ server_info = server_start['queryasyncjobresultresponse']['jobresult']['virtualmachine']
228
+
229
+ server_name = server_info['displayname']
230
+ server_id = server_info['hostname']
231
+ server_serviceoffering = server_info['serviceofferingname']
232
+ server_template = server_info['templatename']
233
+ if server_info['password'] != nil
234
+ ssh_password = server_info['password']
235
+ else
236
+ ssh_password = locate_config_value(:ssh_password)
237
+ end
238
+
239
+ ssh_user = locate_config_value(:ssh_user)
240
+
241
+ public_ip = nil
242
+
243
+ if server_info['nic'].size > 0
244
+ public_ip = server_info['nic'].first['ipaddress']
245
+ end
246
+
247
+ puts "\n\n"
248
+ puts "#{ui.color("Name", :cyan)}: #{server_name}"
249
+ puts "#{ui.color("Public IP", :cyan)}: #{public_ip}"
250
+ puts "#{ui.color("Username", :cyan)}: #{ssh_user}"
251
+ puts "#{ui.color("Password", :cyan)}: #{ssh_password}"
252
+
253
+ print "\n#{ui.color("Waiting for sshd", :magenta)}"
254
+
255
+ print("#{ui.color(".", :magenta)}") until tcp_test_ssh(public_ip) { sleep @initial_sleep_delay ||= 10; puts("done") }
256
+
257
+ bootstrap_for_node(public_ip, ssh_user, ssh_password).run
258
+
259
+ puts "\n"
260
+ puts "#{ui.color("Instance Name", :green)}: #{server_name}"
261
+ puts "#{ui.color("Instance ID", :green)}: #{server_id}"
262
+ puts "#{ui.color("Service Offering", :green)}: #{server_serviceoffering}"
263
+ puts "#{ui.color("Template", :green)}: #{server_template}"
264
+ puts "#{ui.color("Public IP Address", :green)}: #{public_ip}"
265
+ puts "#{ui.color("User", :green)}: #{ssh_user}"
266
+ puts "#{ui.color("Password", :green)}: #{ssh_password}"
267
+ puts "#{ui.color("Environment", :green)}: #{config[:environment] || '_default'}"
268
+ puts "#{ui.color("Run List", :green)}: #{config[:run_list].join(', ')}"
269
+
270
+
271
+ end
272
+
273
+ end
274
+ end
275
+ end
@@ -0,0 +1,53 @@
1
+ # Author:: Jeff Moody (<jmoody@datapipe.com>)
2
+ # Copyright:: Copyright (c) 2012 Datapipe
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ require 'chef/knife/cloudstack_base'
20
+
21
+ class Chef
22
+ class Knife
23
+ class CloudstackServerDelete < Knife
24
+
25
+ include Knife::CloudstackBase
26
+ banner "knife cloudstack server delete INSTANCE_ID [INSTANCE_ID] (options)"
27
+
28
+ def run
29
+
30
+ if @name_args.nil?
31
+ puts #{ui.color("Please provide an Instance ID.", :red)}
32
+ end
33
+
34
+ @name_args.each do |instance_id|
35
+ response = connection.list_virtual_machines('name' => instance_id)
36
+ instance_name = response['listvirtualmachinesresponse']['virtualmachine'].first['name']
37
+ instance_ip = response['listvirtualmachinesresponse']['virtualmachine'].first['nic'].first['ipaddress']
38
+ real_instance_id = response['listvirtualmachinesresponse']['virtualmachine'].first['id']
39
+ puts "#{ui.color("Name", :red)}: #{instance_name}"
40
+ puts "#{ui.color("Public IP", :red)}: #{instance_ip}"
41
+ puts "\n"
42
+ confirm("#{ui.color("Do you really want to delete this server", :red)}")
43
+ connection.destroy_virtual_machine('id' => real_instance_id)
44
+ ui.warn("Deleted server #{instance_name}")
45
+ end
46
+ end
47
+
48
+
49
+
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,91 @@
1
+ # Author:: Chirag Jog (<chirag@clogeny.com>)
2
+ # Copyright:: Copyright (c) 2011 Clogeny Technologies.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Author:: Jeff Moody (<jmoody@datapipe.com>)
6
+ # Copyright:: Copyright (c) 2012 Datapipe
7
+ # License:: Apache License, Version 2.0
8
+ #
9
+ # Author:: Seth Chisamore (<schisamo@opscode.com>)
10
+ # Copyright:: Copyright (c) 2011 Opscode, Inc.
11
+ # License:: Apache License, Version 2.0
12
+ #
13
+ # Licensed under the Apache License, Version 2.0 (the "License");
14
+ # you may not use this file except in compliance with the License.
15
+ # You may obtain a copy of the License at
16
+ #
17
+ # http://www.apache.org/licenses/LICENSE-2.0
18
+ #
19
+ # Unless required by applicable law or agreed to in writing, software
20
+ # distributed under the License is distributed on an "AS IS" BASIS,
21
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ # See the License for the specific language governing permissions and
23
+ # limitations under the License.
24
+ #
25
+
26
+ require 'chef/knife/cloudstack_base'
27
+
28
+ class Chef
29
+ class Knife
30
+ class CloudstackServerList < Knife
31
+
32
+ include Knife::CloudstackBase
33
+
34
+ banner "knife cloudstack server list (options)"
35
+
36
+ def run
37
+ $stdout.sync = true
38
+
39
+ validate!
40
+
41
+ instance_list = [
42
+ ui.color('Instance ID', :bold),
43
+ ui.color('Display Name', :bold),
44
+ ui.color('IP Address', :bold),
45
+ ui.color('Security Group', :bold),
46
+ ui.color('Instance Zone', :bold),
47
+ ui.color('Service Offering', :bold),
48
+ ui.color('Template', :bold),
49
+ ui.color('State', :bold)
50
+ ]
51
+
52
+ response = connection.list_virtual_machines['listvirtualmachinesresponse']
53
+ if virtual_machines = response['virtualmachine']
54
+ virtual_machines.each do |instance|
55
+ instance_list << instance['name'].to_s
56
+ instance_list << instance['displayname'].to_s
57
+ ip_list = []
58
+ instance['nic'].each do |nic|
59
+ ip_list << nic['ipaddress'].to_s
60
+ end
61
+ instance_list << ip_list.join(", ")
62
+ sg_list = []
63
+ instance['securitygroup'].each do |group|
64
+ sg_list << group['name'].to_s
65
+ end
66
+ instance_list << sg_list.join(", ")
67
+
68
+ instance_list << instance['zonename'].to_s
69
+ instance_list << instance['serviceofferingname'].to_s
70
+ instance_list << instance['templatedisplaytext'].to_s
71
+
72
+ instance_list << begin
73
+ state = instance['state'].to_s.downcase
74
+ case state
75
+ when 'shutting-down','terminated','stopping','stopped'
76
+ ui.color(state, :red)
77
+ when 'pending', 'starting'
78
+ ui.color(state, :yellow)
79
+ else
80
+ ui.color(state, :green)
81
+ end
82
+ end
83
+
84
+ end
85
+
86
+ puts ui.list(instance_list, :columns_across, 8)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,53 @@
1
+ # Author:: Jeff Moody (<jmoody@datapipe.com>)
2
+ # Copyright:: Copyright (c) 2012 Datapipe
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ require 'chef/knife/cloudstack_base'
20
+
21
+ class Chef
22
+ class Knife
23
+ class CloudstackKeypairList < Knife
24
+
25
+ include Knife::CloudstackBase
26
+
27
+ banner "knife cloudstack keypair list (options)"
28
+
29
+ def run
30
+ $stdout.sync = true
31
+
32
+ validate!
33
+
34
+ sshkeypair_list = [
35
+ ui.color('Name', :bold),
36
+ ui.color('Fingerprint', :bold),
37
+ ui.color('Private Key', :bold)
38
+ ]
39
+ response = connection.list_ssh_key_pairs['listsshkeypairsresponse']
40
+ if sshkeypairs = response['sshkeypair']
41
+ sshkeypairs.each do |sshkeypair|
42
+ sshkeypair_list << sshkeypair['name'].to_s
43
+ sshkeypair_list << sshkeypair['fingerprint'].to_s
44
+ sshkeypair_list << sshkeypair['privatekey'].to_s
45
+ end
46
+ end
47
+ puts ui.list(sshkeypair_list, :columns_across, 3)
48
+
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,67 @@
1
+ # Author:: Jeff Moody (<jmoody@datapipe.com>)
2
+ # Copyright:: Copyright (c) 2012 Datapipe
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ require 'chef/knife/cloudstack_base'
20
+
21
+ class Chef
22
+ class Knife
23
+ class CloudstackNetworksList < Knife
24
+
25
+ include Knife::CloudstackBase
26
+
27
+ banner "knife cloudstack networks list (options)"
28
+
29
+ def run
30
+ $stdout.sync = true
31
+
32
+ validate!
33
+
34
+ network_list = [
35
+ ui.color('ID', :bold),
36
+ ui.color('Name', :bold),
37
+ ui.color('Zone ID', :bold),
38
+ ui.color('VLAN', :bold),
39
+ ui.color('State', :bold)
40
+ ]
41
+ response = connection.list_networks['listnetworksresponse']
42
+ if networks = response['network']
43
+ networks.each do |network|
44
+ network_list << network['id'].to_s
45
+ network_list << network['name'].to_s
46
+ network_list << network['zoneid'].to_s
47
+ network_list << network['vlan'].to_s
48
+ network_list << begin
49
+ state = network['state'].to_s.downcase
50
+ case state
51
+ when 'allocated'
52
+ ui.color(state, :red)
53
+ when 'pending'
54
+ ui.color(state, :yellow)
55
+ else
56
+ ui.color(state, :green)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ puts ui.list(network_list, :columns_across, 5)
62
+
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,124 @@
1
+ # Author:: Jeff Moody (<jmoody@datapipe.com>)
2
+ # Copyright:: Copyright (c) 2012 Datapipe
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+
19
+ require 'chef/knife/cloudstack_base'
20
+
21
+ class Chef
22
+ class Knife
23
+ class CloudstackSecuritygroupList < Knife
24
+
25
+ include Knife::CloudstackBase
26
+
27
+ banner "knife cloudstack securitygroup list (options)"
28
+ option :groupid,
29
+ :short => "-g GROUPID",
30
+ :long => "--groupid GROUPID",
31
+ :description => "List the rules contained within a Security Group, specified by ID",
32
+ :default => 'none'
33
+ option :groupname,
34
+ :short => "-G GROUPNAME",
35
+ :long => "--groupname GROUPNAME",
36
+ :description => "List the rules contained within a Security Group, specified by name.",
37
+ :default => 'none'
38
+
39
+ def sg_details_list(securitygroup_list, securitygroup_details, groups, options={})
40
+ temp = groups
41
+ if groupid = options[:groupid]
42
+ temp.reject!{|g| g['id'] != groupid.to_i}
43
+ end
44
+ if groupname = options[:groupname]
45
+ temp.reject!{|g| g['name'] != groupname}
46
+ end
47
+
48
+ temp.each do |securitygroup|
49
+ securitygroup_list << securitygroup['id'].to_s
50
+ securitygroup_list << securitygroup['name'].to_s
51
+ securitygroup_list << securitygroup['description'].to_s
52
+ if securitygroup['ingressrule'].nil?
53
+ securitygroup_details << ' '
54
+ else
55
+ securitygroup['ingressrule'].each do |ingressrule|
56
+ rule_details = []
57
+ securitygroup_details << ingressrule['protocol'].to_s
58
+ securitygroup_details << ingressrule['startport'].to_s
59
+ securitygroup_details << ingressrule['endport'].to_s
60
+ if ingressrule['cidr'].nil?
61
+ rule_details << ingressrule['securitygroupname'].to_s
62
+ rule_details << ingressrule['account'].to_s
63
+ else
64
+ rule_details << ingressrule['cidr'].to_s
65
+ end
66
+ securitygroup_details << rule_details.join(", ")
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ def run
73
+ $stdout.sync = true
74
+
75
+ validate!
76
+ groupid = locate_config_value(:groupid)
77
+ groupname = locate_config_value(:groupname)
78
+
79
+ if (groupid == 'none' and groupname == 'none')
80
+ securitygroup_list = [
81
+ ui.color('ID', :bold),
82
+ ui.color('Name', :bold),
83
+ ui.color('Description', :bold)
84
+ ]
85
+ response = connection.list_security_groups['listsecuritygroupsresponse']
86
+ if securitygroups = response['securitygroup']
87
+ securitygroups.each do |securitygroup|
88
+ securitygroup_list << securitygroup['id'].to_s
89
+ securitygroup_list << securitygroup['name'].to_s
90
+ securitygroup_list << securitygroup['description'].to_s
91
+ end
92
+ end
93
+ puts ui.list(securitygroup_list, :columns_across, 3)
94
+ else
95
+ securitygroup_details = [
96
+ ui.color('Protocol', :bold),
97
+ ui.color('Start Port', :bold),
98
+ ui.color('End Port', :bold),
99
+ ui.color('Restricted To', :bold)
100
+ ]
101
+ securitygroup_list = [
102
+ ui.color('ID', :bold),
103
+ ui.color('Name', :bold),
104
+ ui.color('Description', :bold)
105
+ ]
106
+
107
+ if response = connection.list_security_groups['listsecuritygroupsresponse']
108
+ if securitygroups = response['securitygroup']
109
+ filters = {}
110
+ filters[:groupid] = groupid unless groupid == 'none'
111
+ filters[:groupname] = groupname unless groupname == 'none'
112
+ sg_details_list(securitygroup_list, securitygroup_details, securitygroups, filters)
113
+
114
+ puts ui.list(securitygroup_list, :columns_across, 3)
115
+ puts ui.list(securitygroup_details, :columns_across, 4)
116
+ end
117
+ end
118
+
119
+ end
120
+ end
121
+
122
+ end
123
+ end
124
+ end