knife-ec2 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,83 @@
1
+ = Knife EC2
2
+
3
+ = DESCRIPTION:
4
+
5
+ This is the official Opscode Knife plugin for EC2. This plugin gives knife the ability to create, bootstrap, and manage EC2 instances.
6
+
7
+ = INSTALLATION:
8
+
9
+ This plugin is distributed as a Ruby Gem. To install it, run:
10
+
11
+ gem install knife-ec2
12
+
13
+ Depending on your system's configuration, you may need to run this command with root privileges.
14
+
15
+ = CONFIGURATION:
16
+
17
+ In order to communicate with the Amazon's EC2 API you will have to tell Knife about your AWS Access Key and Secret Access Key. The easiest way to accomplish this is to create some entries in your <tt>knife.rb</tt> file:
18
+
19
+ knife[:aws_access_key_id] = "Your AWS Access Key ID"
20
+ knife[:aws_secret_access_key] = "Your AWS Secret Access Key"
21
+
22
+ 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:
23
+
24
+ knife[:aws_access_key_id] = "#{ENV['AWS_ACCESS_KEY_ID']}"
25
+ knife[:aws_secret_access_key] = "#{ENV['AWS_SECRET_ACCESS_KEY']}"
26
+
27
+ You also have the option of passing your AWS API Key/Secret into the individual knife subcommands using the <tt>-A</tt> (or <tt>--aws-access-key-id</tt>) <tt>-K</tt> (or <tt>--aws-secret-access-key</tt>) command options
28
+
29
+ # provision a new m1.small Ubuntu 10.04 webserver
30
+ knife ec2 server create 'role[webserver]' -i ami-7000f019 -f m1.small -A 'Your AWS Access Key ID' -K "Your AWS Secret Access Key"
31
+
32
+ Additionally the following options may be set in your `knife.rb`:
33
+
34
+ * flavor
35
+ * image
36
+ * availability_zone
37
+ * aws_ssh_key_id
38
+ * region
39
+ * distro
40
+ * template_file
41
+
42
+ = SUBCOMMANDS:
43
+
44
+ This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
45
+
46
+ == knife ec2 server create
47
+
48
+ Provisions a new server in the Amazon EC2 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 {ubuntu10.04-gems}[https://github.com/opscode/chef/blob/master/chef/lib/chef/knife/bootstrap/ubuntu10.04-gems.erb] template. This can be overridden using the <tt>-d</tt> or <tt>--template-file</tt> command options.
49
+
50
+ == knife ec2 server delete
51
+
52
+ Deletes an existing server in the currently configured AWS account. <b>PLEASE NOTE</b> - this does not delete the associated node and client objects from the Chef server.
53
+
54
+ == knife ec2 server list
55
+
56
+ Outputs a list of all servers in the currently configured AWS 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.
57
+
58
+ == knife ec2 instance data
59
+
60
+ Generates instance metadata in meant to be used with Opscode's custom AMIs. This will read your knife configuration <tt>~/.chef/knife.rb</tt> for the validation certificate and Chef server URL to use and output in JSON format. The subcommand also accepts a list of roles/recipes that will be in the node's initial run list.
61
+
62
+ <b>PLEASE NOTE</b> - Using Opscode's custom AMIs reflect an older way of launching instances in EC2 for Chef and should be considered DEPRECATED. Leveraging this plugins's <tt>knife ec2 server create</tt> subcommands with a base AMIs directly from your Linux distribution (ie Ubuntu AMIs from Canonical) is much preferred and more flexible. Although this subcommand will remain, the Opscode custom AMIs are currently out of date.
63
+
64
+ In-depth usage instructions can be found on the {Chef Wiki}[http://wiki.opscode.com/display/chef/Amazon+EC2+AMIs+with+Chef].
65
+
66
+ = LICENSE:
67
+
68
+ Author:: Adam Jacob (<adam@opscode.com>)
69
+ Copyright:: Copyright (c) 2009-2011 Opscode, Inc.
70
+ License:: Apache License, Version 2.0
71
+
72
+ Licensed under the Apache License, Version 2.0 (the "License");
73
+ you may not use this file except in compliance with the License.
74
+ You may obtain a copy of the License at
75
+
76
+ http://www.apache.org/licenses/LICENSE-2.0
77
+
78
+ Unless required by applicable law or agreed to in writing, software
79
+ distributed under the License is distributed on an "AS IS" BASIS,
80
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81
+ See the License for the specific language governing permissions and
82
+ limitations under the License.
83
+
@@ -0,0 +1,46 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2009 Opscode, 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
+ require 'chef/json_compat'
21
+
22
+ class Chef
23
+ class Knife
24
+ class Ec2InstanceData < Knife
25
+
26
+ banner "knife ec2 instance data [RUN LIST...] (options)"
27
+
28
+ option :edit,
29
+ :short => "-e",
30
+ :long => "--edit",
31
+ :description => "Edit the instance data"
32
+
33
+ def run
34
+ data = {
35
+ "chef_server" => Chef::Config[:chef_server_url],
36
+ "validation_client_name" => Chef::Config[:validation_client_name],
37
+ "validation_key" => IO.read(Chef::Config[:validation_key]),
38
+ "attributes" => { "run_list" => @name_args }
39
+ }
40
+ data = edit_data(data) if config[:edit]
41
+ output(data)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,325 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2010 Opscode, 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 'fog'
20
+ require 'socket'
21
+ require 'chef/knife'
22
+ require 'chef/knife/bootstrap'
23
+ require 'chef/json_compat'
24
+
25
+ class Chef
26
+ class Knife
27
+ class Ec2ServerCreate < Knife
28
+
29
+ banner "knife ec2 server create (options)"
30
+
31
+ attr_accessor :initial_sleep_delay
32
+
33
+ option :flavor,
34
+ :short => "-f FLAVOR",
35
+ :long => "--flavor FLAVOR",
36
+ :description => "The flavor of server (m1.small, m1.medium, etc)",
37
+ :proc => Proc.new { |f| Chef::Config[:knife][:flavor] = f },
38
+ :default => "m1.small"
39
+
40
+ option :image,
41
+ :short => "-I IMAGE",
42
+ :long => "--image IMAGE",
43
+ :description => "The AMI for the server",
44
+ :proc => Proc.new { |i| Chef::Config[:knife][:image] = i }
45
+
46
+ option :security_groups,
47
+ :short => "-G X,Y,Z",
48
+ :long => "--groups X,Y,Z",
49
+ :description => "The security groups for this server",
50
+ :default => ["default"],
51
+ :proc => Proc.new { |groups| groups.split(',') }
52
+
53
+ option :availability_zone,
54
+ :short => "-Z ZONE",
55
+ :long => "--availability-zone ZONE",
56
+ :description => "The Availability Zone",
57
+ :default => "us-east-1b",
58
+ :proc => Proc.new { |key| Chef::Config[:knife][:availability_zone] = key }
59
+
60
+ option :chef_node_name,
61
+ :short => "-N NAME",
62
+ :long => "--node-name NAME",
63
+ :description => "The Chef node name for your new node"
64
+
65
+ option :ssh_key_name,
66
+ :short => "-S KEY",
67
+ :long => "--ssh-key KEY",
68
+ :description => "The AWS SSH key id",
69
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_ssh_key_id] = key }
70
+
71
+ option :ssh_user,
72
+ :short => "-x USERNAME",
73
+ :long => "--ssh-user USERNAME",
74
+ :description => "The ssh username",
75
+ :default => "root"
76
+
77
+ option :ssh_password,
78
+ :short => "-P PASSWORD",
79
+ :long => "--ssh-password PASSWORD",
80
+ :description => "The ssh password"
81
+
82
+ option :identity_file,
83
+ :short => "-i IDENTITY_FILE",
84
+ :long => "--identity-file IDENTITY_FILE",
85
+ :description => "The SSH identity file used for authentication"
86
+
87
+ option :aws_access_key_id,
88
+ :short => "-A ID",
89
+ :long => "--aws-access-key-id KEY",
90
+ :description => "Your AWS Access Key ID",
91
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
92
+
93
+ option :aws_secret_access_key,
94
+ :short => "-K SECRET",
95
+ :long => "--aws-secret-access-key SECRET",
96
+ :description => "Your AWS API Secret Access Key",
97
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
98
+
99
+ option :prerelease,
100
+ :long => "--prerelease",
101
+ :description => "Install the pre-release chef gems"
102
+
103
+ option :region,
104
+ :long => "--region REGION",
105
+ :description => "Your AWS region",
106
+ :default => "us-east-1",
107
+ :proc => Proc.new { |key| Chef::Config[:knife][:region] = key }
108
+
109
+ option :distro,
110
+ :short => "-d DISTRO",
111
+ :long => "--distro DISTRO",
112
+ :description => "Bootstrap a distro using a template",
113
+ :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
114
+ :default => "ubuntu10.04-gems"
115
+
116
+ option :template_file,
117
+ :long => "--template-file TEMPLATE",
118
+ :description => "Full path to location of template to use",
119
+ :proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
120
+ :default => false
121
+
122
+ option :ebs_size,
123
+ :long => "--ebs-size SIZE",
124
+ :description => "The size of the EBS volume in GB, for EBS-backed instances"
125
+
126
+ option :ebs_no_delete_on_term,
127
+ :long => "--ebs-no-delete-on-term",
128
+ :description => "Do not delete EBS volumn on instance termination"
129
+
130
+ option :run_list,
131
+ :short => "-r RUN_LIST",
132
+ :long => "--run-list RUN_LIST",
133
+ :description => "Comma separated list of roles/recipes to apply",
134
+ :proc => lambda { |o| o.split(/[\s,]+/) },
135
+ :default => []
136
+
137
+ option :subnet_id,
138
+ :short => "-s SUBNET-ID",
139
+ :long => "--subnet SUBNET-ID",
140
+ :description => "create node in this Virtual Private Cloud Subnet ID (implies VPC mode)",
141
+ :default => false
142
+
143
+ option :no_host_key_verify,
144
+ :long => "--no-host-key-verify",
145
+ :description => "Disable host key verification",
146
+ :boolean => true,
147
+ :default => false
148
+
149
+ def h
150
+ @highline ||= HighLine.new
151
+ end
152
+
153
+ def tcp_test_ssh(hostname)
154
+ tcp_socket = TCPSocket.new(hostname, 22)
155
+ readable = IO.select([tcp_socket], nil, nil, 5)
156
+ if readable
157
+ Chef::Log.debug("sshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}")
158
+ yield
159
+ true
160
+ else
161
+ false
162
+ end
163
+ rescue Errno::ETIMEDOUT
164
+ false
165
+ rescue Errno::ECONNREFUSED
166
+ sleep 2
167
+ false
168
+ ensure
169
+ tcp_socket && tcp_socket.close
170
+ end
171
+
172
+ def run
173
+ require 'fog'
174
+ require 'highline'
175
+ require 'net/ssh/multi'
176
+ require 'readline'
177
+
178
+ $stdout.sync = true
179
+
180
+ connection = Fog::Compute.new(
181
+ :provider => 'AWS',
182
+ :aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
183
+ :aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
184
+ :region => locate_config_value(:region)
185
+ )
186
+
187
+ ami = connection.images.get(locate_config_value(:image))
188
+
189
+ server_def = {
190
+ :image_id => locate_config_value(:image),
191
+ :groups => config[:security_groups],
192
+ :flavor_id => locate_config_value(:flavor),
193
+ :key_name => Chef::Config[:knife][:aws_ssh_key_id],
194
+ :availability_zone => Chef::Config[:knife][:availability_zone]
195
+ }
196
+ server_def[:subnet_id] = config[:subnet_id] if config[:subnet_id]
197
+
198
+ if ami.root_device_type == "ebs"
199
+ ami_map = ami.block_device_mapping.first
200
+ ebs_size = begin
201
+ if config[:ebs_size]
202
+ Integer(config[:ebs_size]).to_s
203
+ else
204
+ ami_map["volumeSize"].to_s
205
+ end
206
+ rescue ArgumentError
207
+ puts "--ebs-size must be an integer"
208
+ msg opt_parser
209
+ exit 1
210
+ end
211
+ delete_term = if config[:ebs_no_delete_on_term]
212
+ "false"
213
+ else
214
+ ami_map["deleteOnTermination"]
215
+ end
216
+ server_def[:block_device_mapping] =
217
+ [{
218
+ 'DeviceName' => ami_map["deviceName"],
219
+ 'Ebs.VolumeSize' => ebs_size,
220
+ 'Ebs.DeleteOnTermination' => delete_term
221
+ }]
222
+ end
223
+ server = connection.servers.create(server_def)
224
+
225
+ puts "#{h.color("Instance ID", :cyan)}: #{server.id}"
226
+ puts "#{h.color("Flavor", :cyan)}: #{server.flavor_id}"
227
+ puts "#{h.color("Image", :cyan)}: #{server.image_id}"
228
+ puts "#{h.color("Availability Zone", :cyan)}: #{server.availability_zone}"
229
+ puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
230
+ puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
231
+ puts "#{h.color("Subnet ID", :cyan)}: #{server.subnet_id}" if vpc_mode?
232
+
233
+ print "\n#{h.color("Waiting for server", :magenta)}"
234
+
235
+ display_name = if vpc_mode?
236
+ server.private_ip_address
237
+ else
238
+ server.dns_name
239
+ end
240
+
241
+ # wait for it to be ready to do stuff
242
+ server.wait_for { print "."; ready? }
243
+
244
+ puts("\n")
245
+
246
+ if !vpc_mode?
247
+ puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
248
+ puts "#{h.color("Public IP Address", :cyan)}: #{server.ip_address}"
249
+ puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
250
+ end
251
+ puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
252
+
253
+ print "\n#{h.color("Waiting for sshd", :magenta)}"
254
+
255
+ print(".") until tcp_test_ssh(display_name) {
256
+ sleep @initial_sleep_delay ||= (vpc_mode? ? 40 : 10)
257
+ puts("done")
258
+ }
259
+
260
+ bootstrap_for_node(server).run
261
+
262
+ puts "\n"
263
+ puts "#{h.color("Instance ID", :cyan)}: #{server.id}"
264
+ puts "#{h.color("Flavor", :cyan)}: #{server.flavor_id}"
265
+ puts "#{h.color("Image", :cyan)}: #{server.image_id}"
266
+ puts "#{h.color("Availability Zone", :cyan)}: #{server.availability_zone}"
267
+ puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
268
+ if vpc_mode?
269
+ puts "#{h.color("Subnet ID", :cyan)}: #{server.subnet_id}"
270
+ else
271
+ puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
272
+ puts "#{h.color("Public IP Address", :cyan)}: #{server.ip_address}"
273
+ puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
274
+ end
275
+ puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
276
+ puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
277
+ puts "#{h.color("Root Device Type", :cyan)}: #{server.root_device_type}"
278
+ if server.root_device_type == "ebs"
279
+ device_map = server.block_device_mapping.first
280
+ puts "#{h.color("Root Volume ID", :cyan)}: #{device_map['volumeId']}"
281
+ puts "#{h.color("Root Device Name", :cyan)}: #{device_map['deviceName']}"
282
+ puts "#{h.color("Root Device Delete on Terminate", :cyan)}: #{device_map['deleteOnTermination']}"
283
+ if config[:ebs_size]
284
+ if ami.block_device_mapping.first['volumeSize'].to_i < config[:ebs_size].to_i
285
+ puts ("#{h.color("Warning", :yellow)}: #{config[:ebs_size]}GB " +
286
+ "EBS volume size is larger than size set in AMI of " +
287
+ "#{ami.block_device_mapping.first['volumeSize']}GB.\n" +
288
+ "Use file system tools to make use of the increased volume size.")
289
+ end
290
+ end
291
+ end
292
+ puts "#{h.color("Run List", :cyan)}: #{@name_args.join(', ')}"
293
+ end
294
+
295
+ def bootstrap_for_node(server)
296
+ bootstrap = Chef::Knife::Bootstrap.new
297
+ bootstrap.name_args = [vpc_mode? ? server.private_ip_address : server.dns_name ]
298
+ bootstrap.config[:run_list] = config[:run_list]
299
+ bootstrap.config[:ssh_user] = config[:ssh_user]
300
+ bootstrap.config[:identity_file] = config[:identity_file]
301
+ bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id
302
+ bootstrap.config[:prerelease] = config[:prerelease]
303
+ bootstrap.config[:distro] = locate_config_value(:distro)
304
+ bootstrap.config[:use_sudo] = true
305
+ bootstrap.config[:template_file] = locate_config_value(:template_file)
306
+ bootstrap.config[:environment] = config[:environment]
307
+ # may be needed for vpc_mode
308
+ bootstrap.config[:no_host_key_verify] = config[:no_host_key_verify]
309
+ bootstrap
310
+ end
311
+
312
+ def locate_config_value(key)
313
+ key = key.to_sym
314
+ Chef::Config[:knife][key] || config[key]
315
+ end
316
+
317
+ def vpc_mode?
318
+ # Amazon Virtual Private Cloud requires a subnet_id. If
319
+ # present, do a few things differently
320
+ !!config[:subnet_id]
321
+ end
322
+
323
+ end
324
+ end
325
+ end
@@ -0,0 +1,95 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2009 Opscode, 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
+ require 'chef/json_compat'
21
+
22
+ class Chef
23
+ class Knife
24
+ class Ec2ServerDelete < Knife
25
+
26
+ banner "knife ec2 server delete SERVER [SERVER] (options)"
27
+
28
+ option :aws_access_key_id,
29
+ :short => "-A ID",
30
+ :long => "--aws-access-key-id KEY",
31
+ :description => "Your AWS Access Key ID",
32
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
33
+
34
+ option :aws_secret_access_key,
35
+ :short => "-K SECRET",
36
+ :long => "--aws-secret-access-key SECRET",
37
+ :description => "Your AWS API Secret Access Key",
38
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
39
+
40
+ option :region,
41
+ :long => "--region REGION",
42
+ :description => "Your AWS region",
43
+ :default => "us-east-1",
44
+ :proc => Proc.new { |key| Chef::Config[:knife][:region] = key }
45
+
46
+ def h
47
+ @highline ||= HighLine.new
48
+ end
49
+
50
+ def run
51
+ require 'fog'
52
+ require 'highline'
53
+ require 'net/ssh/multi'
54
+ require 'readline'
55
+
56
+ connection = Fog::Compute.new(
57
+ :provider => 'AWS',
58
+ :aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
59
+ :aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
60
+ :region => Chef::Config[:knife][:region] || config[:region]
61
+ )
62
+
63
+ @name_args.each do |instance_id|
64
+ server = connection.servers.get(instance_id)
65
+
66
+ msg("Instance ID", server.id)
67
+ msg("Flavor", server.flavor_id)
68
+ msg("Image", server.image_id)
69
+ msg("Availability Zone", server.availability_zone)
70
+ msg("Security Groups", server.groups.join(", "))
71
+ msg("SSH Key", server.key_name)
72
+ msg("Public DNS Name", server.dns_name)
73
+ msg("Public IP Address", server.ip_address)
74
+ msg("Private DNS Name", server.private_dns_name)
75
+ msg("Private IP Address", server.private_ip_address)
76
+
77
+ puts "\n"
78
+ confirm("Do you really want to delete this server")
79
+
80
+ server.destroy
81
+
82
+ Chef::Log.warn("Deleted server #{server.id}")
83
+ end
84
+ end
85
+
86
+ def msg(label, value)
87
+ if value && !value.empty?
88
+ puts "#{h.color(label, :cyan)}: #{value}"
89
+ end
90
+ end
91
+
92
+ end
93
+ end
94
+ end
95
+
@@ -0,0 +1,90 @@
1
+ #
2
+ # Author:: Adam Jacob (<adam@opscode.com>)
3
+ # Copyright:: Copyright (c) 2010 Opscode, 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
+ require 'chef/json_compat'
21
+
22
+ class Chef
23
+ class Knife
24
+ class Ec2ServerList < Knife
25
+
26
+ banner "knife ec2 server list [RUN LIST...] (options)"
27
+
28
+ option :aws_access_key_id,
29
+ :short => "-A ID",
30
+ :long => "--aws-access-key-id KEY",
31
+ :description => "Your AWS Access Key ID",
32
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
33
+
34
+ option :aws_secret_access_key,
35
+ :short => "-K SECRET",
36
+ :long => "--aws-secret-access-key SECRET",
37
+ :description => "Your AWS API Secret Access Key",
38
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
39
+
40
+ option :region,
41
+ :long => "--region REGION",
42
+ :description => "Your AWS region",
43
+ :default => "us-east-1",
44
+ :proc => Proc.new { |key| Chef::Config[:knife][:region] = key }
45
+
46
+ def h
47
+ @highline ||= HighLine.new
48
+ end
49
+
50
+ def run
51
+ require 'fog'
52
+ require 'highline'
53
+ require 'net/ssh/multi'
54
+ require 'readline'
55
+
56
+ $stdout.sync = true
57
+
58
+ connection = Fog::Compute.new(
59
+ :provider => 'AWS',
60
+ :aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
61
+ :aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
62
+ :region => Chef::Config[:knife][:region] || config[:region]
63
+ )
64
+
65
+ server_list = [
66
+ h.color('Instance ID', :bold),
67
+ h.color('Public IP', :bold),
68
+ h.color('Private IP', :bold),
69
+ h.color('Flavor', :bold),
70
+ h.color('Image', :bold),
71
+ h.color('Security Groups', :bold),
72
+ h.color('State', :bold)
73
+ ]
74
+ connection.servers.all.each do |server|
75
+ server_list << server.id.to_s
76
+ server_list << (server.ip_address == nil ? "" : server.public_ip_address)
77
+ server_list << (server.private_ip_address == nil ? "" : server.private_ip_address)
78
+ server_list << (server.flavor_id == nil ? "" : server.flavor_id)
79
+ server_list << (server.image_id == nil ? "" : server.image_id)
80
+ server_list << server.groups.join(", ")
81
+ server_list << server.state
82
+ end
83
+ puts h.list(server_list, :columns_across, 7)
84
+
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+
@@ -0,0 +1,4 @@
1
+ module KnifeEC2
2
+ VERSION = "0.5.1"
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: knife-ec2
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.5.1
6
+ platform: ruby
7
+ authors:
8
+ - Adam Jacob
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-03-29 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: chef
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.9.14
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: fog
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 0.6.0
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: net-ssh
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.1.3
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: net-ssh-multi
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 1.0.1
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: highline
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.1
69
+ type: :runtime
70
+ version_requirements: *id005
71
+ description: EC2 Support for Chef's Knife Command
72
+ email: adam@opscode.com
73
+ executables: []
74
+
75
+ extensions: []
76
+
77
+ extra_rdoc_files:
78
+ - README.rdoc
79
+ - LICENSE
80
+ files:
81
+ - LICENSE
82
+ - README.rdoc
83
+ - lib/chef/knife/ec2_instance_data.rb
84
+ - lib/chef/knife/ec2_server_create.rb
85
+ - lib/chef/knife/ec2_server_delete.rb
86
+ - lib/chef/knife/ec2_server_list.rb
87
+ - lib/knife-ec2/version.rb
88
+ has_rdoc: true
89
+ homepage: http://wiki.opscode.com/display/chef
90
+ licenses: []
91
+
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ requirements: []
110
+
111
+ rubyforge_project:
112
+ rubygems_version: 1.6.2
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: EC2 Support for Chef's Knife Command
116
+ test_files: []
117
+