Skip to content

Commit

Permalink
add spot instance support
Browse files Browse the repository at this point in the history
  • Loading branch information
emiddleton committed Jul 2, 2011
1 parent 4764e56 commit 2514dd1
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/chef/knife/ec2_server_create.rb
Expand Up @@ -36,6 +36,11 @@ class Ec2ServerCreate < Knife

attr_accessor :initial_sleep_delay

option :price,
:short => "-p PRICE",
:long => "--price PRICE",
:description => "The maximium hourly USD price for instance"

option :flavor,
:short => "-f FLAVOR",
:long => "--flavor FLAVOR",
Expand Down Expand Up @@ -244,7 +249,22 @@ def run
'Ebs.DeleteOnTermination' => delete_term
}]
end
server = connection.servers.create(server_def)
server = nil

# create a spot instance
if config[:price]
spot_request_def = { :price => config[:price] }
spot_request_def.merge(server_def)
spot_request = connection.spot_requests.create(spot_request_def_def)

spot_request.wait_for { state == 'active' }

server = connection.servers.get('instance-id' => spot_request.instance_id)

# create on demand instance
else
server = connection.servers.create(server_def)
end

# name node consistently with chef node name
connection.tags.create(:key => 'Name', :value => config[:chef_node_name], :resource_id => server.identity)
Expand Down

0 comments on commit 2514dd1

Please sign in to comment.