Skip to content

Commit

Permalink
[CHEF-1990] Allow setting EBS volume size
Browse files Browse the repository at this point in the history
This patch introduces two new options relevant for EBS-backed instances:
--ebs-size SIZE
  Set the size of the EBS root device.  The specified size must be
  larger than the size baked into the AMI.
--ebs-no-delete-on-term
  If specified, the EBS volume will not be deleted when the instance
  is terminated.

Also fixes a few Fog deprecation warnings.
  • Loading branch information
Seth Falcon committed Mar 16, 2011
1 parent 146bed9 commit 2f01ef0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
65 changes: 59 additions & 6 deletions lib/chef/knife/ec2_server_create.rb
Expand Up @@ -115,6 +115,14 @@ class Ec2ServerCreate < Knife
:description => "Full path to location of template to use",
:default => false

option :ebs_size,
:long => "--ebs-size SIZE",
:description => "The size of the EBS volume in GB, for EBS-backed instances"

option :ebs_no_delete_on_term,
:long => "--ebs-no-delete-on-term",
:description => "Do not delete EBS volumn on instance termination"

def h
@highline ||= HighLine.new
end
Expand Down Expand Up @@ -146,19 +154,49 @@ def run

$stdout.sync = true

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]
)
)

ami = connection.images.get(config[:image])

server = connection.servers.create(
server_def = {
:image_id => config[:image],
:groups => config[:security_groups],
:flavor_id => config[: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
if config[:ebs_size]
Integer(config[:ebs_size]).to_s
else
ami_map["volumeSize"].to_s
end
rescue ArgumentError
puts "--ebs-size must be an integer"
msg opt_parser
exit 1
end
delete_term = if config[:ebs_no_delete_on_term]
"false"
else
ami_map["deleteOnTermination"]
end
server_def[:block_device_mapping] =
[{
'DeviceName' => ami_map["deviceName"],
'Ebs.VolumeSize' => ebs_size,
'Ebs.DeleteOnTermination' => delete_term
}]
end
server = connection.servers.create(server_def)

puts "#{h.color("Instance ID", :cyan)}: #{server.id}"
puts "#{h.color("Flavor", :cyan)}: #{server.flavor_id}"
Expand All @@ -175,7 +213,7 @@ def run
puts("\n")

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}"

Expand All @@ -193,10 +231,25 @@ 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}"
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
puts "#{h.color("Root Volume ID", :cyan)}: #{device_map['volumeId']}"
puts "#{h.color("Root Device Name", :cyan)}: #{device_map['deviceName']}"
puts "#{h.color("Root Device Delete on Terminate", :cyan)}: #{device_map['deleteOnTermination']}"
if config[:ebs_size]
if ami.block_device_mapping.first['volumeSize'].to_i < config[:ebs_size].to_i
puts ("#{h.color("Warning", :yellow)}: #{config[:ebs_size]}GB " +
"EBS volume size is larger than size set in AMI of " +
"#{ami.block_device_mapping.first['volumeSize']}GB.\n" +
"Use file system tools to make use of the increased volume size.")
end
end
end
end

def bootstrap_for_node(server)
Expand Down
2 changes: 1 addition & 1 deletion lib/knife-ec2/version.rb
@@ -1,4 +1,4 @@
module KnifeEC2
VERSION = "0.5.0"
VERSION = "0.5.1"
end

0 comments on commit 2f01ef0

Please sign in to comment.