Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename price -> spot_price, fix rubocop #229

Merged
merged 1 commit into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ The EC2 IAM profile name to use.

The default is `nil`.

### `price`
### `spot_price`

The price you bid in order to submit a spot request. An additional step will be required during the spot request process submission. If no price is set, it will use an on-demand instance.

Expand Down
24 changes: 16 additions & 8 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Ec2 < Kitchen::Driver::Base # rubocop:disable Metrics/ClassLength
end
default_config :private_ip_address, nil
default_config :iam_profile_name, nil
default_config :price, nil
default_config :spot_price, nil
default_config :block_duration_minutes, nil
default_config :retryable_tries, 60
default_config :retryable_sleep, 5
Expand Down Expand Up @@ -162,7 +162,7 @@ def create(state) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
are responsible for your incurred costs.
END

if config[:price]
if config[:spot_price]
# Spot instance when a price is set
server = submit_spot(state)
else
Expand Down Expand Up @@ -321,13 +321,8 @@ def submit_server

def submit_spot(state) # rubocop:disable Metrics/AbcSize
debug("Creating EC2 Spot Instance..")
request_data = {}
request_data[:spot_price] = config[:price].to_s
request_data[:launch_specification] = instance_generator.ec2_instance_data
request_data[:block_duration_minutes] = config[:block_duration_minutes] if config[:block_duration_minutes]

response = ec2.client.request_spot_instances(request_data)
spot_request_id = response[:spot_instance_requests][0][:spot_instance_request_id]
spot_request_id = create_spot_request
# deleting the instance cancels the request, but deleting the request
# does not affect the instance
state[:spot_request_id] = spot_request_id
Expand All @@ -346,6 +341,19 @@ def submit_spot(state) # rubocop:disable Metrics/AbcSize
ec2.get_instance_from_spot_request(spot_request_id)
end

def create_spot_request
request_data = {
:spot_price => config[:spot_price].to_s,
:launch_specification => instance_generator.ec2_instance_data
}
if config[:block_duration_minutes]
request_data[:block_duration_minutes] = config[:block_duration_minutes]
end

response = ec2.client.request_spot_instances(request_data)
response[:spot_instance_requests][0][:spot_instance_request_id]
end

def tag_server(server)
tags = []
config[:tags].each do |k, v|
Expand Down
10 changes: 8 additions & 2 deletions spec/kitchen/driver/ec2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@

let(:logged_output) { StringIO.new }
let(:logger) { Logger.new(logged_output) }
let(:config) { { :aws_ssh_key_id => "key", :image_id => "ami-1234567", :block_duration_minutes => 60 } }
let(:config) do
{
:aws_ssh_key_id => "key",
:image_id => "ami-1234567",
:block_duration_minutes => 60
}
end
let(:platform) { Kitchen::Platform.new(:name => "fooos-99") }
let(:transport) { Kitchen::Transport::Dummy.new }
let(:generator) { instance_double(Kitchen::Driver::Aws::InstanceGenerator) }
Expand Down Expand Up @@ -361,7 +367,7 @@

context "config is for a spot instance" do
before do
config[:price] = 1
config[:spot_price] = 1
expect(driver).to receive(:submit_spot).with(state).and_return(server)
end

Expand Down