Permalink
Browse files

assorted enhancements:

* more options should be configurable via knife.rb
* removed remaining fog deprecation notices
* fixed trailing whitespace
* complete README
  • Loading branch information...
1 parent 621fd42 commit ccdb3f8e5849faac9ffe0e04dfe18636cb092ba6 @schisamo schisamo committed Mar 19, 2011
View
@@ -1,30 +1,83 @@
= Knife EC2
-This is the official Opscode Knife plugin for Amazon EC2. This plugin gives the
-knife command line program the ability to create, bootstrap, and manage EC2 inst
-ances.
-== Installation
+= DESCRIPTION:
+
+This is the official Opscode Knife plugin for EC2. This plugin gives knife the ability to create, bootstrap, and manage EC2 instances.
+
+= INSTALLATION:
+
This plugin is distributed as a Ruby Gem. To install it, run:
- gem install knife-ec2
-Depending on your system, you may need to run this command as root.
-== Configuration
-Knife EC2 can be configured with your AWS credentials, favored availability zone
-and more. Your configuration is specified in your knife.rb file, inside the
-`knife` Hash. The following settings are available:
+ gem install knife-ec2
+
+Depending on your system's configuration, you may need to run this command with root privileges.
+
+= CONFIGURATION:
+
+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:
+
+ knife[:aws_access_key_id] = "Your AWS Access Key ID"
+ knife[:aws_secret_access_key] = "Your AWS Secret Access Key"
+
+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:
+
+ knife[:aws_access_key_id] = "#{ENV['AWS_ACCESS_KEY_ID']}"
+ knife[:aws_secret_access_key] = "#{ENV['AWS_SECRET_ACCESS_KEY']}"
+
+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
+
+ # provision a new m1.small Ubuntu 10.04 webserver
+ knife ec2 server create 'role[webserver]' -i ami-7000f019 -f m1.small -A 'Your AWS Access Key ID' -K "Your AWS Secret Access Key"
+
+Additionally the following options may be set in your `knife.rb`:
+
+* flavor
+* image
* availability_zone
* aws_ssh_key_id
-* aws_access_key_id
-* aws_secret_access_key
* region
+* distro
+* template_file
+
+= SUBCOMMANDS:
+
+This plugin provides the following Knife subcommands. Specific command options can be found by invoking the subcommand with a <tt>--help</tt> flag
+
+== knife ec2 server create
+
+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.
+
+== knife ec2 server delete
+
+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.
+
+== knife ec2 server list
+
+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.
+
+== knife ec2 instance data
+
+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.
+
+<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.
+
+In-depth usage instructions can be found on the {Chef Wiki}[http://wiki.opscode.com/display/chef/Amazon+EC2+AMIs+with+Chef].
+
+= LICENSE:
+
+Author:: Adam Jacob (<adam@opscode.com>)
+Copyright:: Copyright (c) 2009-2011 Opscode, Inc.
+License:: Apache License, Version 2.0
-=== Configuration Example:
-In ~/.chef/knife.rb:
- knife :aws_access_key_id => "AAAAAAAAAAAAAAAAAAAAAA",
- :aws_secret_access_key => "Mjc0MzIyMDE5Mjk1MjcxNjczNDYxNjczMzg5Nzg5",
- :aws_ssh_key_id => "mycorp-ssh-key"
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
-== Usage
-TODO
+ http://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
@@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,12 +30,12 @@ class Ec2InstanceData < Knife
:long => "--edit",
:description => "Edit the instance data"
- def run
+ def run
data = {
"chef_server" => Chef::Config[:chef_server_url],
"validation_client_name" => Chef::Config[:validation_client_name],
"validation_key" => IO.read(Chef::Config[:validation_key]),
- "attributes" => { "run_list" => @name_args }
+ "attributes" => { "run_list" => @name_args }
}
data = edit_data(data) if config[:edit]
output(data)
@@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -34,12 +34,14 @@ class Ec2ServerCreate < Knife
:short => "-f FLAVOR",
:long => "--flavor FLAVOR",
:description => "The flavor of server (m1.small, m1.medium, etc)",
+ :proc => Proc.new { |f| Chef::Config[:knife][:flavor] = f },
:default => "m1.small"
option :image,
:short => "-i IMAGE",
:long => "--image IMAGE",
- :description => "The AMI for the server"
+ :description => "The AMI for the server",
+ :proc => Proc.new { |i| Chef::Config[:knife][:image] = i }
option :security_groups,
:short => "-G X,Y,Z",
@@ -70,8 +72,8 @@ class Ec2ServerCreate < Knife
:short => "-x USERNAME",
:long => "--ssh-user USERNAME",
:description => "The ssh username",
- :default => "root"
-
+ :default => "root"
+
option :ssh_password,
:short => "-P PASSWORD",
:long => "--ssh-password PASSWORD",
@@ -81,18 +83,18 @@ class Ec2ServerCreate < Knife
:short => "-I IDENTITY_FILE",
:long => "--identity-file IDENTITY_FILE",
:description => "The SSH identity file used for authentication"
-
+
option :aws_access_key_id,
:short => "-A ID",
:long => "--aws-access-key-id KEY",
:description => "Your AWS Access Key ID",
- :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
option :aws_secret_access_key,
:short => "-K SECRET",
:long => "--aws-secret-access-key SECRET",
:description => "Your AWS API Secret Access Key",
- :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
option :prerelease,
:long => "--prerelease",
@@ -108,11 +110,13 @@ class Ec2ServerCreate < Knife
:short => "-d DISTRO",
:long => "--distro DISTRO",
:description => "Bootstrap a distro using a template",
+ :proc => Proc.new { |d| Chef::Config[:knife][:distro] = d },
:default => "ubuntu10.04-gems"
option :template_file,
:long => "--template-file TEMPLATE",
:description => "Full path to location of template to use",
+ :proc => Proc.new { |t| Chef::Config[:knife][:template_file] = t },
:default => false
option :ebs_size,
@@ -146,7 +150,7 @@ def tcp_test_ssh(hostname)
tcp_socket && tcp_socket.close
end
- def run
+ def run
require 'fog'
require 'highline'
require 'net/ssh/multi'
@@ -158,19 +162,19 @@ def run
:provider => 'AWS',
:aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
:aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
- :region => Chef::Config[:knife][:region]
- )
+ :region => locate_config_value(:region)
+ )
- ami = connection.images.get(config[:image])
+ ami = connection.images.get(locate_config_value(:image))
server_def = {
- :image_id => config[:image],
+ :image_id => locate_config_value(:image),
:groups => config[:security_groups],
- :flavor_id => config[:flavor],
+ :flavor_id => locate_config_value(:flavor),
:key_name => Chef::Config[:knife][:aws_ssh_key_id],
:availability_zone => Chef::Config[:knife][:availability_zone]
}
-
+
if ami.root_device_type == "ebs"
ami_map = ami.block_device_mapping.first
ebs_size = begin
@@ -204,7 +208,7 @@ def run
puts "#{h.color("Availability Zone", :cyan)}: #{server.availability_zone}"
puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
-
+
print "\n#{h.color("Waiting for server", :magenta)}"
# wait for it to be ready to do stuff
@@ -230,11 +234,6 @@ def run
puts "#{h.color("Availability Zone", :cyan)}: #{server.availability_zone}"
puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
- puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
- puts "#{h.color("Public IP Address", :cyan)}: #{server.public_ip_address}"
- puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
- puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
- puts "#{h.color("Run List", :cyan)}: #{@name_args.join(', ')}"
puts "#{h.color("Root Device Type", :cyan)}: #{server.root_device_type}"
if server.root_device_type == "ebs"
device_map = server.block_device_mapping.first
@@ -250,6 +249,11 @@ def run
end
end
end
+ puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
+ puts "#{h.color("Public IP Address", :cyan)}: #{server.public_ip_address}"
+ puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
+ puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
+ puts "#{h.color("Run List", :cyan)}: #{@name_args.join(', ')}"
end
def bootstrap_for_node(server)
@@ -260,13 +264,17 @@ def bootstrap_for_node(server)
bootstrap.config[:identity_file] = config[:identity_file]
bootstrap.config[:chef_node_name] = config[:chef_node_name] || server.id
bootstrap.config[:prerelease] = config[:prerelease]
- bootstrap.config[:distro] = config[:distro]
+ bootstrap.config[:distro] = locate_config_value(:distro)
bootstrap.config[:use_sudo] = true
- bootstrap.config[:template_file] = config[:template_file]
+ bootstrap.config[:template_file] = locate_config_value(:template_file)
bootstrap.config[:environment] = config[:environment]
bootstrap
end
+ def locate_config_value(key)
+ key = key.to_sym
+ Chef::Config[:knife][key] || config[key]
+ end
end
end
end
@@ -6,9 +6,9 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -24,18 +24,18 @@ class Knife
class Ec2ServerDelete < Knife
banner "knife ec2 server delete SERVER [SERVER] (options)"
-
+
option :aws_access_key_id,
:short => "-A ID",
:long => "--aws-access-key-id KEY",
:description => "Your AWS Access Key ID",
- :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_access_key_id] = key }
option :aws_secret_access_key,
:short => "-K SECRET",
:long => "--aws-secret-access-key SECRET",
:description => "Your AWS API Secret Access Key",
- :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
+ :proc => Proc.new { |key| Chef::Config[:knife][:aws_secret_access_key] = key }
option :region,
:long => "--region REGION",
@@ -47,16 +47,17 @@ def h
@highline ||= HighLine.new
end
- def run
+ def run
require 'fog'
require 'highline'
require 'net/ssh/multi'
require 'readline'
- connection = Fog::AWS::Compute.new(
+ connection = Fog::Compute.new(
+ :provider => 'AWS',
:aws_access_key_id => Chef::Config[:knife][:aws_access_key_id],
:aws_secret_access_key => Chef::Config[:knife][:aws_secret_access_key],
- :region => Chef::Config[:knife][:region]
+ :region => Chef::Config[:knife][:region] || config[:region]
)
@name_args.each do |instance_id|
@@ -69,7 +70,7 @@ def run
puts "#{h.color("Security Groups", :cyan)}: #{server.groups.join(", ")}"
puts "#{h.color("SSH Key", :cyan)}: #{server.key_name}"
puts "#{h.color("Public DNS Name", :cyan)}: #{server.dns_name}"
- puts "#{h.color("Public IP Address", :cyan)}: #{server.ip_address}"
+ puts "#{h.color("Public IP Address", :cyan)}: #{server.public_ip_address}"
puts "#{h.color("Private DNS Name", :cyan)}: #{server.private_dns_name}"
puts "#{h.color("Private IP Address", :cyan)}: #{server.private_ip_address}"
Oops, something went wrong.

0 comments on commit ccdb3f8

Please sign in to comment.