Skip to content

Commit

Permalink
Merge pull request #61 from pearkes/pm_default_region_option
Browse files Browse the repository at this point in the history
Default droplet options
  • Loading branch information
pearkes committed Sep 4, 2013
2 parents d94ebf8 + e59b1cc commit d9c52d1
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ rvm:
- 1.8.7
- 1.9.3
- 2.0.0
matrix:
allow_failures:
- rvm: 1.8.7
3 changes: 0 additions & 3 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,14 @@ def ssh(name=nil)
method_option "size",
:type => :numeric,
:aliases => "-s",
:default => 66,
:desc => "The size_id of the droplet"
method_option "image",
:type => :numeric,
:aliases => "-i",
:default => 284203,
:desc => "The image_id of the droplet"
method_option "region",
:type => :numeric,
:aliases => "-r",
:default => 1,
:desc => "The region_id of the droplet"
method_option "keys",
:type => :string,
Expand Down
47 changes: 43 additions & 4 deletions lib/tugboat/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Configuration
FILE_NAME = '.tugboat'
DEFAULT_SSH_KEY_PATH = '.ssh/id_rsa'
DEFAULT_SSH_PORT = '22'
DEFAULT_REGION = '1'
DEFAULT_IMAGE = '284203'
DEFAULT_SIZE = '66'
DEFAULT_SSH_KEY = ''

def initialize
@path = ENV["TUGBOAT_CONFIG_PATH"] || File.join(File.expand_path("~"), FILE_NAME)
Expand Down Expand Up @@ -42,11 +46,27 @@ def ssh_key_path
def ssh_user
@data['ssh']['ssh_user']
end

def ssh_port
@data['ssh']['ssh_port']
end

def default_region
@data['defaults'].nil? ? DEFAULT_REGION : @data['defaults']['region']
end

def default_image
@data['defaults'].nil? ? DEFAULT_IMAGE : @data['defaults']['image']
end

def default_size
@data['defaults'].nil? ? DEFAULT_SIZE : @data['defaults']['size']
end

def default_ssh_key
@data['defaults'].nil? ? DEFAULT_SSH_KEY : @data['defaults']['ssh_key']
end

# Re-runs initialize
def reset!
self.send(:initialize)
Expand All @@ -58,7 +78,7 @@ def reload!
end

# Writes a config file
def create_config_file(client, api, ssh_key_path, ssh_user, ssh_port)
def create_config_file(client, api, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key)
# Default SSH Key path
if ssh_key_path.empty?
ssh_key_path = File.join(File.expand_path("~"), DEFAULT_SSH_KEY_PATH)
Expand All @@ -72,10 +92,29 @@ def create_config_file(client, api, ssh_key_path, ssh_user, ssh_port)
ssh_port = DEFAULT_SSH_PORT
end

if region.empty?
region = DEFAULT_REGION
end

if image.empty?
image = DEFAULT_IMAGE
end

if size.empty?
size = DEFAULT_SIZE
end

if ssh_key.empty?
default_ssh_key = DEFAULT_SSH_KEY
end

require 'yaml'
File.open(@path, File::RDWR|File::TRUNC|File::CREAT, 0600) do |file|
data = {"authentication" => { "client_key" => client, "api_key" => api },
"ssh" => { "ssh_user" => ssh_user, "ssh_key_path" => ssh_key_path , "ssh_port" => ssh_port}}
data = {
"authentication" => { "client_key" => client, "api_key" => api },
"ssh" => { "ssh_user" => ssh_user, "ssh_key_path" => ssh_key_path , "ssh_port" => ssh_port},
"defaults" => { "region" => region, "image" => image, "size" => size, "ssh_key" => ssh_key }
}
file.write data.to_yaml
end
end
Expand Down
10 changes: 9 additions & 1 deletion lib/tugboat/middleware/ask_for_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ def call(env)
ssh_key_path = ask "Enter your SSH key path (optional, defaults to ~/.ssh/id_rsa):"
ssh_user = ask "Enter your SSH user (optional, defaults to #{ENV['USER']}):"
ssh_port = ask "Enter your SSH port number (optional, defaults to 22):"
say
say "To retrieve region, image, size and key ID's, you can use the corresponding tugboat command, such as `tugboat images`."
say "Defaults can be changed at any time in your ~/.tugboat configuration file."
say
region = ask "Enter your default region ID (optional, defaults to 1 (New York)):"
image = ask "Enter your default image ID (optional, defaults to 284203 (Ubuntu 12.04 x64)):"
size = ask "Enter your default size ID (optional, defaults to 66 (512MB)):"
ssh_key = ask "Enter your default ssh key ID (optional, defaults to none):"

# Write the config file.
env['config'].create_config_file(client_key, api_key, ssh_key_path, ssh_user, ssh_port)
env['config'].create_config_file(client_key, api_key, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key)
env['config'].reload!

@app.call(env)
Expand Down
26 changes: 21 additions & 5 deletions lib/tugboat/middleware/create_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,27 @@ def call(env)

say "Queueing creation of droplet '#{env["create_droplet_name"]}'...", nil, false

req = ocean.droplets.create :name => env["create_droplet_name"],
:size_id => env["create_droplet_size_id"],
:image_id => env["create_droplet_image_id"],
:region_id => env["create_droplet_region_id"],
:ssh_key_ids => env["create_droplet_ssh_key_ids"]
env["create_droplet_region_id"] ?
droplet_region_id = env["create_droplet_region_id"] :
droplet_region_id = env["config"].default_region

env["create_droplet_image_id"] ?
droplet_image_id = env["create_droplet_image_id"] :
droplet_image_id = env["config"].default_image

env["create_droplet_size_id"] ?
droplet_size_id = env["create_droplet_size_id"] :
droplet_size_id = env["config"].default_size

env["create_droplet_ssh_key_ids"] ?
droplet_ssh_key_id = env["create_droplet_ssh_key_ids"] :
droplet_ssh_key_id = env["config"].default_ssh_key

req = ocean.droplets.create :name => env["create_droplet_name"],
:size_id => droplet_size_id,
:image_id => droplet_image_id,
:region_id => droplet_region_id,
:ssh_key_ids => droplet_ssh_key_id

if req.status == "ERROR"
say req.error_message, :red
Expand Down
45 changes: 44 additions & 1 deletion spec/cli/authorize_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
describe Tugboat::CLI do
include_context "spec"

let(:tmp_path) { project_path + "/tmp/tugboat" }

describe "authorize" do
before do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200)
to_return(:status => 200)
end

it "asks the right questions and checks credentials" do
Expand All @@ -22,10 +24,51 @@
$stdin.should_receive(:gets).and_return(ssh_user)
$stdout.should_receive(:print).with("Enter your SSH port number (optional, defaults to 22): ")
$stdin.should_receive(:gets).and_return(ssh_port)
$stdout.should_receive(:print).with("Enter your default region ID (optional, defaults to 1 (New York)): ")
$stdin.should_receive(:gets).and_return(region)
$stdout.should_receive(:print).with("Enter your default image ID (optional, defaults to 284203 (Ubuntu 12.04 x64)): ")
$stdin.should_receive(:gets).and_return(image)
$stdout.should_receive(:print).with("Enter your default size ID (optional, defaults to 66 (512MB)): ")
$stdin.should_receive(:gets).and_return(size)
$stdout.should_receive(:print).with("Enter your default ssh key ID (optional, defaults to none): ")
$stdin.should_receive(:gets).and_return(ssh_key_id)

@cli.authorize

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made

File.read(tmp_path).should include "image: '#{image}'", "region: '#{region}'", "size: '#{size}'", "ssh_user: #{ssh_user}", "ssh_key_path: #{ssh_key_path}", "ssh_port: '#{ssh_port}'", "ssh_key: '#{ssh_key_id}'"

end

it "sets defaults if no input given" do

$stdout.should_receive(:print).exactly(6).times
$stdout.should_receive(:print).with("Enter your client key: ")
$stdin.should_receive(:gets).and_return(client_key)
$stdout.should_receive(:print).with("Enter your API key: ")
$stdin.should_receive(:gets).and_return(api_key)
$stdout.should_receive(:print).with("Enter your SSH key path (optional, defaults to ~/.ssh/id_rsa): ")
$stdin.should_receive(:gets).and_return(ssh_key_path)
$stdout.should_receive(:print).with("Enter your SSH user (optional, defaults to #{ENV['USER']}): ")
$stdin.should_receive(:gets).and_return('')
$stdout.should_receive(:print).with("Enter your SSH port number (optional, defaults to 22): ")
$stdin.should_receive(:gets).and_return('')
$stdout.should_receive(:print).with("Enter your default region ID (optional, defaults to 1 (New York)): ")
$stdin.should_receive(:gets).and_return('')
$stdout.should_receive(:print).with("Enter your default image ID (optional, defaults to 284203 (Ubuntu 12.04 x64)): ")
$stdin.should_receive(:gets).and_return('')
$stdout.should_receive(:print).with("Enter your default size ID (optional, defaults to 66 (512MB)): ")
$stdin.should_receive(:gets).and_return('')
$stdout.should_receive(:print).with("Enter your default ssh key ID (optional, defaults to none): ")
$stdin.should_receive(:gets).and_return('')

@cli.authorize

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made

File.read(tmp_path).should include "image: '284203'", "region: '1'", "size: '66'", "ssh_user: #{ENV['USER']}", "ssh_key_path: ~/.ssh/id_rsa", "ssh_port: '22'", "ssh_key: ''"

end
end

Expand Down
14 changes: 7 additions & 7 deletions spec/cli/create_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
include_context "spec"

describe "create a droplet" do
it "with a name" do
stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id&name=#{droplet_name}&region_id&size_id&ssh_key_ids").
it "with a name, uses defaults from configuration" do
stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=#{image}&name=#{droplet_name}&region_id=#{region}&size_id=#{size}&ssh_key_ids=#{ssh_key_id}").
to_return(:status => 200, :body => '{"status":"OK"}')

@cli.create(droplet_name)

expect($stdout.string).to eq <<-eos
Queueing creation of droplet '#{droplet_name}'...done
eos
expect(a_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id&name=#{droplet_name}&region_id&size_id&ssh_key_ids")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=#{image}&name=#{droplet_name}&region_id=#{region}&size_id=#{size}&ssh_key_ids=#{ssh_key_id}")).to have_been_made
end

it "with args" do
stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=2672&name=#{droplet_name}&region_id=2&size_id=64&ssh_key_ids=1234").
it "with args does not use defaults from configuration" do
stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=555&name=foo&region_id=3&size_id=666&ssh_key_ids=4321").
to_return(:status => 200, :body => '{"status":"OK"}')

@cli.options = @cli.options.merge(:image => 2672, :size => 64, :region => 2, :keys => "1234")
@cli.options = @cli.options.merge(:image => '555', :size => '666', :region => '3', :keys => '4321')
@cli.create(droplet_name)

expect($stdout.string).to eq <<-eos
Queueing creation of droplet '#{droplet_name}'...done
eos

expect(a_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=2672&name=foo&region_id=2&size_id=64&ssh_key_ids=1234")).to have_been_made
expect(a_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=555&name=foo&region_id=3&size_id=666&ssh_key_ids=4321")).to have_been_made
end
end

Expand Down
Loading

0 comments on commit d9c52d1

Please sign in to comment.