knife-cloudstack-fog 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,141 +1,142 @@
1
- [![Gem Version](https://badge.fury.io/rb/knife-cloudstack-fog.png)](http://badge.fury.io/rb/knife-cloudstack-fog)
2
- [![Build Status](https://www.travis-ci.org/fifthecho/knife-cloudstack-fog.png?branch=master)](https://www.travis-ci.org/fifthecho/knife-cloudstack-fog)
3
-
4
- = Knife Cloudstack
5
-
6
- = DESCRIPTION:
7
-
8
- This is the unofficial Opscode Knife plugin for Apache CloudStack / Citrix CloudPlatform Clouds. This plugin gives knife the ability to create, bootstrap, and manage instances in Cloudstack Compute clouds.
9
-
10
- = INSTALLATION:
11
-
12
- Be sure you are running the latest version Chef. Versions earlier than 0.10.0 don't support plugins:
13
-
14
- $ gem install chef
15
-
16
- Also ensure you are running the latest version of Fog. Versions earlier than 1.3.1 don't fully support Cloudstack Cloud:
17
-
18
- $ gem update fog
19
-
20
- This plugin is distributed as a Ruby Gem. To install it, run:
21
-
22
- $ gem install knife-cloudstack-fog
23
-
24
- Depending on your system's configuration, you may need to run this command with root privileges.
25
-
26
- Also, all of these commands have been tested against CloudStack/CloudPlatform 2.2.14 and 3.0.6.
27
-
28
- = CONFIGURATION:
29
-
30
- In order to communicate with an Cloudstack Compute cloud's CLOUDSTACK API you will have to tell Knife about your Cloudstack Compute cloud API endpoint, Cloudstack Access Key and Secret Access Key. The easiest way to accomplish this is to create some entries in your <tt>knife.rb</tt> file:
31
-
32
- ### Note: You may need to append the :cloudstack_access_key_id with ":$PROJECT_NAME", if it differs from your Cloudstack Username.
33
- knife[:cloudstack_access_key_id] = "Your Cloudstack Access Key ID"
34
- knife[:cloudstack_secret_access_key] = "Your Cloudstack Secret Access Key"
35
- ### Note: If you are not proxying HTTPS to the Cloudstack CLOUDSTACK API port, the scheme should be HTTP, and the PORT is 8773.
36
- knife[:cloudstack_api_endpoint] = "https://cloud.mycompany.com/service/Cloud"
37
-
38
- If your knife.rb file will be checked into a SCM system (ie readable by others) you may want to read the values from environment variables:
39
-
40
- knife[:cloudstack_access_key_id] = "#{ENV['CLOUDSTACK_ACCESS_KEY']}"
41
- knife[:cloudstack_secret_access_key] = "#{ENV['CLOUDSTACK_SECRET_KEY']}"
42
- knife[:cloudstack_api_endpoint] = "#{ENV['CLOUDSTACK_URL']}"
43
-
44
- You also have the option of passing your Cloudstack API Key/Secret into the individual knife subcommands using the <tt>-A</tt> (or <tt>--cloudstack-access-key-id</tt>) <tt>-K</tt> (or <tt>--cloudstack-secret-access-key</tt>) command options
45
-
46
- # provision a new webserver
47
- knife cloudstack server create 'role[webserver]' -I <image template> -f <service offering> -A 'Your Cloudstack Access Key ID' --cloudstack-api-endpoint 'https://cloud.mycompany.com/v1.0'
48
-
49
- Additionally the following options may be set in your `knife.rb`:
50
-
51
- * flavor
52
- * image
53
- * availability_zone
54
-
55
- = SUBCOMMANDS:
56
-
57
- This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
58
-
59
- == knife cloudstack server create
60
-
61
- Provisions a new server in an Cloudstack Compute cloud and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the server is bootstrapped using the chef-full template. This can be overridden using the <tt>-d</tt> or <tt>--template-file</tt> command options.
62
-
63
- == knife cloudstack server delete
64
-
65
- Deletes an existing server in the currently configured Cloudstack Compute cloud account. <b>PLEASE NOTE</b> - this does not delete the associated node and client objects from the Chef server.
66
-
67
- == knife cloudstack server list
68
-
69
- Outputs a list of all servers in the currently configured Cloudstack Compute cloud account. <b>PLEASE NOTE</b> - this shows all instances associated with the account, some of which may not be currently managed by the Chef server.
70
-
71
- == knife cloudstack serviceoffering list
72
-
73
- Outputs a list of all available service offerings (available hardware configuration for a server) available to the currently configured Cloudstack Compute cloud account. Each service offering has a unique combination of disk space, memory capacity and priority for CPU time. This data can be useful when choosing a service offering ID to pass to the <tt>knife cloudstack server create</tt> subcommand.
74
-
75
- == knife cloudstack template list
76
-
77
- Outputs a list of all available templates available to the currently configured Cloudstack Compute cloud account. A template is a collection of files used to create or rebuild a server. This data can be useful when choosing an template ID to pass to the <tt>knife cloudstack server create</tt> subcommand.
78
-
79
- == knife cloudstack keypair create
80
-
81
- Create a new SSH keypair or register a public key to the currently configured Cloudstack Compute cloud account. This command is valuable to provide password recovery options as well as associating a Cloudstack keypair with an instance for any advanced authentication methods when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
82
-
83
- == knife cloudstack keypair delete
84
-
85
- Delete a keypair which is abailable to the currently configured Cloudstack Compute cloud account.
86
-
87
- == knife cloudstack keypair list
88
-
89
- Outputs a list of all available keypairs available to the currently configured Cloudstack Compute cloud account. This information is valuable to provide password recovery options as well as associating a Cloudstack keypair with an instance for any advanced authentication methods when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
90
-
91
- == knife cloudstack network list
92
-
93
- Outputs a list of all available network offerings available to the currently configured Cloudstack Compute cloud account. This information is necessary to locate a network ID when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand if your Cloudstack Compute cloud requires a server be associated with a particular network offering.
94
-
95
- == knife cloudstack portforwardingrule list
96
-
97
- Outputs a list of all port forwarding rules.
98
-
99
- == knife cloudstack publicip create
100
-
101
- Assign a new public ip address to the zone specified by -z option. This sub command is available only on advanced network (VLAN enabled) zone.
102
-
103
- == knife cloudstack publicip list
104
-
105
- Outputs a list of all public ip addresses assigned to the account. This sub command is available only on advanced network (VLAN enabled) zone.
106
-
107
- == knife cloudstack securitygroup list
108
-
109
- Outputs a list of all available security groups defined for the currently configured Cloudstack Compute cloud account. This information is necessary to locate a security group ID when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand if your Cloudstack Compute cloud requires a server be associated with a security group.
110
-
111
- == knife cloudstack zone list
112
-
113
- Outputs a list of all available zones available to the currently configured Cloudstack Compute cloud account. This information is necessary to locate a zone ID when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
114
-
115
- == knife cloudstack diskofferings list
116
-
117
- Outputs a list of all available disk offerings available to the currently configured Cloudstack Compute cloud account. This information is valuable if you are also seeking to provision a data disk alongside your root volume when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
118
-
119
- == knife cloudstack network create
120
-
121
- Creates networks to be used by the knife cloudstack server create command in zones that are configured in Advanced/VPC networking modes.
122
-
123
-
124
-
125
- = LICENSE:
126
-
127
- Author:: Chirag Jog (<chirag@clogeny.com>), Jeff Moody (<jmoody@datapipe.com>), Damien Fuentes, Takashi Kanai (<anikundesu@gmail.com>)
128
- Copyright:: Copyright (c) 2011 Clogeny, 2012 Datapipe, 2012 IDC Frontier Inc.
129
- License:: Apache License, Version 2.0
130
-
131
- Licensed under the Apache License, Version 2.0 (the "License");
132
- you may not use this file except in compliance with the License.
133
- You may obtain a copy of the License at
134
-
135
- http://www.apache.org/licenses/LICENSE-2.0
136
-
137
- Unless required by applicable law or agreed to in writing, software
138
- distributed under the License is distributed on an "AS IS" BASIS,
139
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
140
- See the License for the specific language governing permissions and
141
- limitations under the License.
1
+ {<img src="https://badge.fury.io/rb/knife-cloudstack-fog.png" alt="Gem Version" />}[http://badge.fury.io/rb/knife-cloudstack-fog]
2
+ {<img src="https://www.travis-ci.org/fifthecho/knife-cloudstack-fog.png?branch=master" alt="Build Status" />}[https://www.travis-ci.org/fifthecho/knife-cloudstack-fog]
3
+ {<img src="https://gemnasium.com/fifthecho/knife-cloudstack-fog.png" alt="Dependency Status" />}[https://gemnasium.com/fifthecho/knife-cloudstack-fog]
4
+
5
+ = Knife Cloudstack
6
+
7
+ = DESCRIPTION:
8
+
9
+ This is the unofficial Opscode Knife plugin for Apache CloudStack / Citrix CloudPlatform Clouds. This plugin gives knife the ability to create, bootstrap, and manage instances in Cloudstack Compute clouds.
10
+
11
+ = INSTALLATION:
12
+
13
+ Be sure you are running the latest version Chef. Versions earlier than 0.10.0 don't support plugins:
14
+
15
+ $ gem install chef
16
+
17
+ Also ensure you are running the latest version of Fog. Versions earlier than 1.3.1 don't fully support Cloudstack Cloud:
18
+
19
+ $ gem update fog
20
+
21
+ This plugin is distributed as a Ruby Gem. To install it, run:
22
+
23
+ $ gem install knife-cloudstack-fog
24
+
25
+ Depending on your system's configuration, you may need to run this command with root privileges.
26
+
27
+ Also, all of these commands have been tested against CloudStack/CloudPlatform 2.2.14 and 3.0.6.
28
+
29
+ = CONFIGURATION:
30
+
31
+ In order to communicate with an Cloudstack Compute cloud's CLOUDSTACK API you will have to tell Knife about your Cloudstack Compute cloud API endpoint, Cloudstack Access Key and Secret Access Key. The easiest way to accomplish this is to create some entries in your <tt>knife.rb</tt> file:
32
+
33
+ ### Note: You may need to append the :cloudstack_access_key_id with ":$PROJECT_NAME", if it differs from your Cloudstack Username.
34
+ knife[:cloudstack_access_key_id] = "Your Cloudstack Access Key ID"
35
+ knife[:cloudstack_secret_access_key] = "Your Cloudstack Secret Access Key"
36
+ ### Note: If you are not proxying HTTPS to the Cloudstack CLOUDSTACK API port, the scheme should be HTTP, and the PORT is 8773.
37
+ knife[:cloudstack_api_endpoint] = "https://cloud.mycompany.com/service/Cloud"
38
+
39
+ If your knife.rb file will be checked into a SCM system (ie readable by others) you may want to read the values from environment variables:
40
+
41
+ knife[:cloudstack_access_key_id] = "#{ENV['CLOUDSTACK_ACCESS_KEY']}"
42
+ knife[:cloudstack_secret_access_key] = "#{ENV['CLOUDSTACK_SECRET_KEY']}"
43
+ knife[:cloudstack_api_endpoint] = "#{ENV['CLOUDSTACK_URL']}"
44
+
45
+ You also have the option of passing your Cloudstack API Key/Secret into the individual knife subcommands using the <tt>-A</tt> (or <tt>--cloudstack-access-key-id</tt>) <tt>-K</tt> (or <tt>--cloudstack-secret-access-key</tt>) command options
46
+
47
+ # provision a new webserver
48
+ knife cloudstack server create 'role[webserver]' -I <image template> -f <service offering> -A 'Your Cloudstack Access Key ID' --cloudstack-api-endpoint 'https://cloud.mycompany.com/v1.0'
49
+
50
+ Additionally the following options may be set in your `knife.rb`:
51
+
52
+ * flavor
53
+ * image
54
+ * availability_zone
55
+
56
+ = SUBCOMMANDS:
57
+
58
+ This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
59
+
60
+ == knife cloudstack server create
61
+
62
+ Provisions a new server in an Cloudstack Compute cloud and then perform a Chef bootstrap (using the SSH protocol). The goal of the bootstrap is to get Chef installed on the target system so it can run Chef Client with a Chef Server. The main assumption is a baseline OS installation exists (provided by the provisioning). It is primarily intended for Chef Client systems that talk to a Chef server. By default the server is bootstrapped using the chef-full template. This can be overridden using the <tt>-d</tt> or <tt>--template-file</tt> command options.
63
+
64
+ == knife cloudstack server delete
65
+
66
+ Deletes an existing server in the currently configured Cloudstack Compute cloud account. <b>PLEASE NOTE</b> - this does not delete the associated node and client objects from the Chef server.
67
+
68
+ == knife cloudstack server list
69
+
70
+ Outputs a list of all servers in the currently configured Cloudstack Compute cloud account. <b>PLEASE NOTE</b> - this shows all instances associated with the account, some of which may not be currently managed by the Chef server.
71
+
72
+ == knife cloudstack serviceoffering list
73
+
74
+ Outputs a list of all available service offerings (available hardware configuration for a server) available to the currently configured Cloudstack Compute cloud account. Each service offering has a unique combination of disk space, memory capacity and priority for CPU time. This data can be useful when choosing a service offering ID to pass to the <tt>knife cloudstack server create</tt> subcommand.
75
+
76
+ == knife cloudstack template list
77
+
78
+ Outputs a list of all available templates available to the currently configured Cloudstack Compute cloud account. A template is a collection of files used to create or rebuild a server. This data can be useful when choosing an template ID to pass to the <tt>knife cloudstack server create</tt> subcommand.
79
+
80
+ == knife cloudstack keypair create
81
+
82
+ Create a new SSH keypair or register a public key to the currently configured Cloudstack Compute cloud account. This command is valuable to provide password recovery options as well as associating a Cloudstack keypair with an instance for any advanced authentication methods when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
83
+
84
+ == knife cloudstack keypair delete
85
+
86
+ Delete a keypair which is abailable to the currently configured Cloudstack Compute cloud account.
87
+
88
+ == knife cloudstack keypair list
89
+
90
+ Outputs a list of all available keypairs available to the currently configured Cloudstack Compute cloud account. This information is valuable to provide password recovery options as well as associating a Cloudstack keypair with an instance for any advanced authentication methods when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
91
+
92
+ == knife cloudstack network list
93
+
94
+ Outputs a list of all available network offerings available to the currently configured Cloudstack Compute cloud account. This information is necessary to locate a network ID when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand if your Cloudstack Compute cloud requires a server be associated with a particular network offering.
95
+
96
+ == knife cloudstack portforwardingrule list
97
+
98
+ Outputs a list of all port forwarding rules.
99
+
100
+ == knife cloudstack publicip create
101
+
102
+ Assign a new public ip address to the zone specified by -z option. This sub command is available only on advanced network (VLAN enabled) zone.
103
+
104
+ == knife cloudstack publicip list
105
+
106
+ Outputs a list of all public ip addresses assigned to the account. This sub command is available only on advanced network (VLAN enabled) zone.
107
+
108
+ == knife cloudstack securitygroup list
109
+
110
+ Outputs a list of all available security groups defined for the currently configured Cloudstack Compute cloud account. This information is necessary to locate a security group ID when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand if your Cloudstack Compute cloud requires a server be associated with a security group.
111
+
112
+ == knife cloudstack zone list
113
+
114
+ Outputs a list of all available zones available to the currently configured Cloudstack Compute cloud account. This information is necessary to locate a zone ID when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
115
+
116
+ == knife cloudstack diskofferings list
117
+
118
+ Outputs a list of all available disk offerings available to the currently configured Cloudstack Compute cloud account. This information is valuable if you are also seeking to provision a data disk alongside your root volume when instantiating a server with the <tt>knife cloudstack server create</tt> subcommand.
119
+
120
+ == knife cloudstack network create
121
+
122
+ Creates networks to be used by the knife cloudstack server create command in zones that are configured in Advanced/VPC networking modes.
123
+
124
+
125
+
126
+ = LICENSE:
127
+
128
+ Author:: Chirag Jog (<chirag@clogeny.com>), Jeff Moody (<jmoody@datapipe.com>), Damien Fuentes, Takashi Kanai (<anikundesu@gmail.com>)
129
+ Copyright:: Copyright (c) 2011 Clogeny, 2012 Datapipe, 2012 IDC Frontier Inc.
130
+ License:: Apache License, Version 2.0
131
+
132
+ Licensed under the Apache License, Version 2.0 (the "License");
133
+ you may not use this file except in compliance with the License.
134
+ You may obtain a copy of the License at
135
+
136
+ http://www.apache.org/licenses/LICENSE-2.0
137
+
138
+ Unless required by applicable law or agreed to in writing, software
139
+ distributed under the License is distributed on an "AS IS" BASIS,
140
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
141
+ See the License for the specific language governing permissions and
142
+ limitations under the License.
@@ -21,6 +21,8 @@ require 'socket'
21
21
  require 'net/ssh/multi'
22
22
  require 'chef/json_compat'
23
23
  require 'chef/knife/cloudstack_base'
24
+ require 'chef/knife/ssh'
25
+ Chef::Knife::Ssh.load_deps
24
26
 
25
27
  class Chef
26
28
  class Knife
@@ -107,7 +109,7 @@ class Chef
107
109
  :long => "--ssh-port PORT",
108
110
  :description => "The port which SSH should be listening on. If unspecified, will default to 22."
109
111
 
110
- option :server_name,
112
+ option :server_display_name,
111
113
  :short => "-N NAME",
112
114
  :long => "--display-name NAME",
113
115
  :description => "The instance display name"
@@ -157,7 +159,7 @@ class Chef
157
159
  bootstrap.config[:ssh_password] = password
158
160
  bootstrap.config[:ssh_gateway] = config[:ssh_gateway]
159
161
  bootstrap.config[:identity_file] = locate_config_value(:identity_file)
160
- bootstrap.config[:chef_node_name] = config[:server_name] if config[:server_name]
162
+ bootstrap.config[:chef_node_name] = config[:server_display_name] if config[:server_display_name]
161
163
  bootstrap.config[:prerelease] = config[:prerelease]
162
164
  bootstrap.config[:bootstrap_version] = locate_config_value(:bootstrap_version)
163
165
  bootstrap.config[:distro] = locate_config_value(:distro)
@@ -174,88 +176,6 @@ class Chef
174
176
  end
175
177
  end
176
178
 
177
- def vpc_mode?
178
- # Virtual Private Cloud / Isolated Networking requires a network id. If
179
- # present, do a few things differently
180
- !!locate_config_value(:cloudstack_networkids)
181
- end
182
-
183
- def wait_for_sshd(hostname)
184
- config[:ssh_gateway] ? wait_for_tunnelled_sshd(hostname) : wait_for_direct_sshd(hostname, @sshport)
185
- end
186
-
187
- def wait_for_tunnelled_sshd(hostname)
188
- Chef::Log.debug("Connecting to #{hostname} via wait_for_tunnelled_sshd")
189
- print("#{ui.color(".", :magenta)}")
190
- print("#{ui.color(".", :magenta)}") until tunnel_test_ssh(ssh_connect_host) {
191
- sleep @initial_sleep_delay ||= (vpc_mode? ? 40 : 10)
192
- puts("#{ui.color(". Done.", :magenta)}")
193
- }
194
- end
195
-
196
- def tunnel_test_ssh(hostname, &block)
197
- gw_host, gw_user = config[:ssh_gateway].split('@').reverse
198
- gw_host, gw_port = gw_host.split(':')
199
- Chef::Log.debug("Connecting to #{hostname} via #{gw_host} over port #{gw_port}.")
200
- gateway = Net::SSH::Gateway.new(gw_host, gw_user, :port => gw_port || 22)
201
- status = false
202
- gateway.open(hostname, config[:ssh_port]) do |local_tunnel_port|
203
- status = tcp_test_ssh('localhost', local_tunnel_port, &block)
204
- Chef::Log.debug "Opened local port #{local_tunnel_port} to tunnel the connection."
205
- end
206
- status
207
- rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
208
- sleep 2
209
- false
210
- rescue Errno::EPERM, Errno::ETIMEDOUT
211
- false
212
- rescue Errno::Disconnect
213
- sleep @initial_sleep_delay
214
- retry
215
- end
216
-
217
- def wait_for_direct_sshd(hostname, ssh_port)
218
- Chef::Log.debug("Connecting directly to #{hostname} over port #{ssh_port}")
219
- print("#{ui.color(".", :magenta)}") until tcp_test_ssh(ssh_connect_host, ssh_port) {
220
- sleep @initial_sleep_delay ||= (vpc_mode? ? 40 : 10)
221
- puts("#{ui.color(". Done.", :magenta)}")
222
- }
223
- end
224
-
225
- def ssh_connect_host
226
- @ssh_connect_host ||= if config[:server_connect_attribute]
227
- server.send(config[:server_connect_attribute])
228
- else
229
- Chef::Log.debug("Connecting to #{@primary_ip}")
230
- @primary_ip
231
- # vpc_mode? ? server.private_ip_address : server.dns_name
232
- end
233
- end
234
-
235
- def tcp_test_ssh(hostname, ssh_port)
236
- Chef::Log.debug("Conecting to #{hostname} on #{ssh_port}.")
237
- print("#{ui.color(".", :magenta)}")
238
- tcp_socket = TCPSocket.new(hostname, ssh_port)
239
- readable = IO.select([tcp_socket], nil, nil, 5)
240
- if readable
241
- Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
242
- yield
243
- true
244
- else
245
- false
246
- end
247
- rescue SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ENETUNREACH, IOError
248
- sleep 2
249
- false
250
- rescue Errno::EPERM, Errno::ETIMEDOUT
251
- false
252
- rescue Errno::Disconnect
253
- sleep @initial_sleep_delay
254
- retry
255
- ensure
256
- tcp_socket && tcp_socket.close
257
- end
258
-
259
179
  def check_port_available(public_port, ipaddressid)
260
180
  Chef::Log.debug("Checking if port #{public_port} is available.")
261
181
  pubport = public_port.to_i
@@ -310,8 +230,8 @@ class Chef
310
230
  "zoneid" => locate_config_value(:cloudstack_zoneid)
311
231
  }
312
232
 
313
- if locate_config_value(:server_name) != nil
314
- server_def["displayname"] = locate_config_value(:server_name)
233
+ if locate_config_value(:server_display_name) != nil
234
+ server_def["displayname"] = locate_config_value(:server_display_name)
315
235
  end
316
236
 
317
237
  if locate_config_value(:host_name) != nil
@@ -357,6 +277,76 @@ class Chef
357
277
  server_def
358
278
  end
359
279
 
280
+ def knife_ssh
281
+ ssh = Chef::Knife::Ssh.new
282
+ ssh.ui = ui
283
+ ssh.name_args = [ @primary_ip, ssh_command ]
284
+ ssh.config[:ssh_user] = Chef::Config[:knife][:ssh_user] || config[:ssh_user]
285
+ ssh.config[:ssh_password] = config[:ssh_password]
286
+ ssh.config[:ssh_port] = Chef::Config[:knife][:ssh_port] || config[:ssh_port]
287
+ ssh.config[:ssh_gateway] = Chef::Config[:knife][:ssh_gateway] || config[:ssh_gateway]
288
+ ssh.config[:forward_agent] = Chef::Config[:knife][:forward_agent] || config[:forward_agent]
289
+ ssh.config[:identity_file] = Chef::Config[:knife][:identity_file] || config[:identity_file]
290
+ ssh.config[:manual] = true
291
+ ssh.config[:host_key_verify] = Chef::Config[:knife][:host_key_verify] || config[:host_key_verify]
292
+ ssh.config[:on_error] = :raise
293
+ ssh
294
+ end
295
+
296
+ def find_template(template=nil)
297
+ # Are we bootstrapping using an already shipped template?
298
+ if config[:template_file]
299
+ bootstrap_files = config[:template_file]
300
+ else
301
+ bootstrap_files = []
302
+ bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb")
303
+ bootstrap_files << File.join(Knife.chef_config_dir, "bootstrap", "#{config[:distro]}.erb") if Knife.chef_config_dir
304
+ bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb") if ENV['HOME']
305
+ bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb"))
306
+ bootstrap_files.flatten!
307
+ end
308
+
309
+ template = Array(bootstrap_files).find do |bootstrap_template|
310
+ Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}")
311
+ File.exists?(bootstrap_template)
312
+ end
313
+
314
+ unless template
315
+ ui.info("Can not find bootstrap definition for #{config[:distro]}")
316
+ raise Errno::ENOENT
317
+ end
318
+
319
+ Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}")
320
+
321
+ template
322
+ end
323
+
324
+ def render_template(template=nil)
325
+ context = Knife::Core::BootstrapContext.new(config, config[:run_list], Chef::Config)
326
+ Erubis::Eruby.new(template).evaluate(context)
327
+ end
328
+
329
+ def read_template
330
+ IO.read(@template_file).chomp
331
+ end
332
+
333
+ def knife_ssh_with_password_auth
334
+ ssh = knife_ssh
335
+ ssh.config[:identity_file] = nil
336
+ ssh.config[:ssh_password] = ssh.get_password
337
+ ssh
338
+ end
339
+
340
+ def ssh_command
341
+ command = render_template(read_template)
342
+
343
+ if config[:use_sudo]
344
+ command = config[:use_sudo_password] ? "echo #{config[:ssh_password]} | sudo -S #{command}" : "sudo #{command}"
345
+ end
346
+
347
+ command
348
+ end
349
+
360
350
  def run
361
351
  $stdout.sync = true
362
352
  options = create_server_def
@@ -364,6 +354,9 @@ class Chef
364
354
 
365
355
  @initial_sleep_delay = 10
366
356
  @sshport = 22
357
+
358
+ config[:host_key_verify] = false
359
+
367
360
  if locate_config_value(:ssh_port) != nil
368
361
  @sshport = locate_config_value(:ssh_port).to_i
369
362
  end
@@ -398,14 +391,14 @@ class Chef
398
391
 
399
392
  @server = server_start['queryasyncjobresultresponse']['jobresult']['virtualmachine']
400
393
 
401
- server_name = @server['displayname']
394
+ server_display_name = @server['displayname']
402
395
  server_id = @server['name']
403
396
  server_serviceoffering = @server['serviceofferingname']
404
397
  server_template = @server['templatename']
405
398
  if @server['password'] != nil
406
- ssh_password = @server['password']
399
+ config[:ssh_password] = @server['password']
407
400
  else
408
- ssh_password = locate_config_value(:ssh_password)
401
+ config[:ssh_password] = locate_config_value(:ssh_password)
409
402
  end
410
403
 
411
404
  ssh_user = locate_config_value(:ssh_user)
@@ -428,45 +421,81 @@ class Chef
428
421
  @sshport = pubport
429
422
  end
430
423
 
431
-
432
-
433
424
  Chef::Log.debug("Connecting over port #{@sshport}")
434
-
425
+ config[:ssh_port] = @sshport
426
+ config[:server_name] = @primary_ip
427
+ @template_file = find_template(config[:bootstrap_template])
428
+
435
429
  puts "\n\n"
436
- puts "#{ui.color("Name", :cyan)}: #{server_name}"
430
+ puts "#{ui.color("Name", :cyan)}: #{server_display_name}"
437
431
  puts "#{ui.color("Primary IP", :cyan)}: #{@primary_ip}"
438
432
  puts "#{ui.color("Username", :cyan)}: #{ssh_user}"
439
- puts "#{ui.color("Password", :cyan)}: #{ssh_password}"
440
-
441
- print "\n#{ui.color("Waiting for sshd", :magenta)}"
442
- wait_for_sshd(ssh_connect_host)
443
-
444
- puts("#{ui.color("Waiting for password/keys to sync.", :magenta)}")
445
- sleep @initial_sleep_delay
446
- sleep @initial_sleep_delay
447
- sleep @initial_sleep_delay
448
-
449
- Chef::Log.debug("Connnecting to #{@server} via #{ssh_connect_host} and bootstrapping Chef.")
450
-
451
- bootstrap_for_node(@server,ssh_connect_host).run
433
+ puts "#{ui.color("Password", :cyan)}: #{config[:ssh_password]}"
434
+
435
+ print "#{ui.color("Waiting for SSH.", :magenta)}"
436
+ if config[:ssh_gateway]
437
+ Chef::Log.debug("Using SSH Gateway: #{config[:ssh_gateway]}")
438
+ sleep @initial_sleep_delay
439
+ print "#{ui.color(".", :magenta)}"
440
+ sleep @initial_sleep_delay
441
+ print "#{ui.color(".", :magenta)}"
442
+ end
443
+ begin
444
+ knife_ssh.run
445
+ rescue Net::SSH::AuthenticationFailed
446
+ unless config[:ssh_password]
447
+ ui.info("Failed to authenticate #{config[:ssh_user]} - trying password auth")
448
+ knife_ssh_with_password_auth.run
449
+ end
450
+ rescue Errno::ECONNREFUSED
451
+ sleep @initial_sleep_delay
452
+ print "#{ui.color(".", :magenta)}"
453
+ retry
454
+ rescue SocketError
455
+ sleep @initial_sleep_delay
456
+ print "#{ui.color(".", :magenta)}"
457
+ retry
458
+ rescue Errno::ETIMEDOUT
459
+ sleep @initial_sleep_delay
460
+ print "#{ui.color(".", :magenta)}"
461
+ retry
462
+ rescue Errno::EPERM
463
+ sleep @initial_sleep_delay
464
+ print "#{ui.color(".", :magenta)}"
465
+ retry
466
+ rescue Errno::EHOSTUNREACH
467
+ sleep @initial_sleep_delay
468
+ print "#{ui.color(".", :magenta)}"
469
+ retry
470
+ rescue Errno::ENETUNREACH
471
+ sleep @initial_sleep_delay
472
+ print "#{ui.color(".", :magenta)}"
473
+ retry
474
+ rescue Net::SSH::Disconnect
475
+ sleep @initial_sleep_delay
476
+ print "#{ui.color(".", :magenta)}"
477
+ retry
478
+ rescue
479
+ puts caller
480
+ puts $!.inspect
481
+ end
452
482
 
453
483
  Chef::Log.debug("#{@server}")
454
484
 
455
485
  puts "\n"
456
- puts "#{ui.color("Instance Name", :green)}: #{server_name}"
486
+ puts "#{ui.color("Instance Name", :green)}: #{server_display_name}"
457
487
  puts "#{ui.color("Instance ID", :green)}: #{server_id}"
458
488
  puts "#{ui.color("Service Offering", :green)}: #{server_serviceoffering}"
459
489
  puts "#{ui.color("Template", :green)}: #{server_template}"
460
490
  puts "#{ui.color("Public IP Address", :green)}: #{@primary_ip}"
461
491
  puts "#{ui.color("Port", :green)}: #{@sshport}"
462
492
  puts "#{ui.color("User", :green)}: #{ssh_user}"
463
- puts "#{ui.color("Password", :green)}: #{ssh_password}"
493
+ puts "#{ui.color("Password", :green)}: #{config[:ssh_password]}"
464
494
  puts "#{ui.color("Environment", :green)}: #{config[:environment] || '_default'}"
465
495
  puts "#{ui.color("Run List", :green)}: #{config[:run_list].join(', ')}"
466
496
  end
467
497
 
468
498
  end
469
-
470
499
  end
471
500
  end
472
501
  end
@@ -31,6 +31,8 @@ class Chef
31
31
  puts "#{ui.color("Please provide an Instance ID.", :red)}"
32
32
  end
33
33
 
34
+ jobs = {}
35
+
34
36
  @name_args.each do |instance_id|
35
37
  response = connection.list_virtual_machines('name' => instance_id)
36
38
  instance_name = response['listvirtualmachinesresponse']['virtualmachine'].first['name']
@@ -48,22 +50,26 @@ class Chef
48
50
  server = connection.start_virtual_machine('id' => real_instance_id)
49
51
  end
50
52
  jobid = server['startvirtualmachineresponse'].fetch('jobid')
51
- server_start = connection.query_async_job_result('jobid'=>jobid)
52
- print "#{ui.color("Waiting for server", :magenta)}"
53
- while server_start['queryasyncjobresultresponse'].fetch('jobstatus') != 1
54
- print "#{ui.color(".", :magenta)}"
55
- sleep(1)
56
- server_start = connection.query_async_job_result('jobid'=>jobid)
57
- end
58
- puts "\n\n"
59
53
 
60
- ui.warn("Started server #{instance_name}")
54
+ jobs[instance_id] = jobid
61
55
  end
62
- end
63
-
64
-
65
56
 
57
+ print "#{ui.color("Waiting for servers", :magenta)}"
58
+ until jobs.empty?
59
+ jobs.each do |instance_id, jobid|
60
+ server_start = connection.query_async_job_result('jobid'=>jobid)
61
+ if server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 1
62
+ jobs.delete(instance_id)
66
63
 
64
+ puts "\n\n"
65
+ ui.warn("Started server #{instance_id}")
66
+ else
67
+ print "#{ui.color(".", :magenta)}"
68
+ sleep(1)
69
+ end
70
+ end
71
+ end
72
+ end
67
73
  end
68
74
  end
69
75
  end
@@ -25,8 +25,8 @@ class Chef
25
25
  include Knife::CloudstackBase
26
26
  banner "knife cloudstack server stop INSTANCE_ID (options)"
27
27
  option :forced,
28
- :short => "-f",
29
- :description => "Issue this as a forced stop command."
28
+ :short => "-f",
29
+ :description => "Issue this as a forced stop command."
30
30
 
31
31
  def run
32
32
 
@@ -34,6 +34,8 @@ class Chef
34
34
  puts "#{ui.color("Please provide an Instance ID.", :red)}"
35
35
  end
36
36
 
37
+ jobs = {}
38
+
37
39
  @name_args.each do |instance_id|
38
40
  response = connection.list_virtual_machines('name' => instance_id)
39
41
  instance_name = response['listvirtualmachinesresponse']['virtualmachine'].first['name']
@@ -51,22 +53,26 @@ class Chef
51
53
  server = connection.stop_virtual_machine('id' => real_instance_id)
52
54
  end
53
55
  jobid = server['stopvirtualmachineresponse'].fetch('jobid')
54
- server_stop = connection.query_async_job_result('jobid'=>jobid)
55
- print "#{ui.color("Waiting for server", :magenta)}"
56
- while server_stop['queryasyncjobresultresponse'].fetch('jobstatus') != 1
57
- print "#{ui.color(".", :magenta)}"
58
- sleep(1)
59
- server_stop = connection.query_async_job_result('jobid'=>jobid)
60
- end
61
- puts "\n\n"
62
56
 
63
- ui.warn("Stopped server #{instance_name}")
57
+ jobs[instance_id] = jobid
64
58
  end
65
- end
66
-
67
-
68
59
 
60
+ print "#{ui.color("Waiting for servers", :magenta)}"
61
+ until jobs.empty?
62
+ jobs.each do |instance_id, jobid|
63
+ server_stop = connection.query_async_job_result('jobid'=>jobid)
64
+ if server_stop['queryasyncjobresultresponse'].fetch('jobstatus') == 1
65
+ jobs.delete(instance_id)
69
66
 
67
+ puts "\n\n"
68
+ ui.warn("Stopped server #{instance_id}")
69
+ else
70
+ print "#{ui.color(".", :magenta)}"
71
+ sleep(1)
72
+ end
73
+ end
74
+ end
75
+ end
70
76
  end
71
77
  end
72
78
  end
@@ -1,6 +1,6 @@
1
- module Knife
2
- module Cloudstack
3
- VERSION = "0.4.0"
4
- MAJOR, MINOR, TINY = VERSION.split('.')
5
- end
1
+ module Knife
2
+ module Cloudstack
3
+ VERSION = "0.5.0"
4
+ MAJOR, MINOR, TINY = VERSION.split('.')
5
+ end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-cloudstack-fog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2013-10-14 00:00:00.000000000 Z
16
+ date: 2013-10-29 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: fog