Skip to content

Commit

Permalink
Merge af01aa5 into 2ce2949
Browse files Browse the repository at this point in the history
  • Loading branch information
obradovic committed Nov 21, 2013
2 parents 2ce2949 + af01aa5 commit f157b98
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 39 deletions.
5 changes: 5 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,17 @@ def ssh(name=nil)
:type => :string,
:aliases => "-k",
:desc => "A comma separated list of SSH key ids to add to the droplet"
method_option "private_networking",
:type => :boolean,
:aliases => "-p",
:desc => "Adds private networking to the droplet"
def create(name)
Middleware.sequence_create_droplet.call({
"create_droplet_size_id" => options[:size],
"create_droplet_image_id" => options[:image],
"create_droplet_region_id" => options[:region],
"create_droplet_ssh_key_ids" => options[:keys],
"create_droplet_private_networking" => options[:private_networking],
"create_droplet_name" => name
})
end
Expand Down
20 changes: 18 additions & 2 deletions lib/tugboat/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Configuration
DEFAULT_IMAGE = '284203'
DEFAULT_SIZE = '66'
DEFAULT_SSH_KEY = ''
DEFAULT_PRIVATE_NETWORKING = 'false'

def initialize
@path = ENV["TUGBOAT_CONFIG_PATH"] || File.join(File.expand_path("~"), FILE_NAME)
Expand Down Expand Up @@ -51,6 +52,11 @@ def ssh_port
@data['ssh']['ssh_port']
end

def private_networking
@data['private_networking']
end


def default_region
@data['defaults'].nil? ? DEFAULT_REGION : @data['defaults']['region']
end
Expand All @@ -67,6 +73,11 @@ def default_ssh_key
@data['defaults'].nil? ? DEFAULT_SSH_KEY : @data['defaults']['ssh_key']
end

def default_private_networking
@data['defaults'].nil? || @data['defaults']['private_networking'].nil? ? DEFAULT_PRIVATE_NETWORKING : @data['defaults']['private_networking']
end


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

# Writes a config file
def create_config_file(client, api, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key)
def create_config_file(client, api, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key, private_networking)
# Default SSH Key path
if ssh_key_path.empty?
ssh_key_path = File.join(File.expand_path("~"), DEFAULT_SSH_KEY_PATH)
Expand Down Expand Up @@ -108,12 +119,17 @@ def create_config_file(client, api, ssh_key_path, ssh_user, ssh_port, region, im
default_ssh_key = DEFAULT_SSH_KEY
end

if private_networking.empty?
private_networking = DEFAULT_PRIVATE_NETWORKING
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},
"defaults" => { "region" => region, "image" => image, "size" => size, "ssh_key" => ssh_key }
"defaults" => { "region" => region, "image" => image, "size" => size, "ssh_key" => ssh_key, "private_networking" => private_networking }
}
file.write data.to_yaml
end
Expand Down
3 changes: 2 additions & 1 deletion lib/tugboat/middleware/ask_for_credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ def call(env)
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):"
private_networking = ask "Enter your default private networking option (optional, defaults to false):"

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

@app.call(env)
Expand Down
17 changes: 12 additions & 5 deletions lib/tugboat/middleware/create_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ def call(env)
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
env["create_droplet_private_networking"] ?
droplet_private_networking = env["create_droplet_private_networking"] :
droplet_private_networking = env["config"].default_private_networking

droplet_private_networking = droplet_private_networking.to_s

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,
:private_networking => droplet_private_networking

if req.status == "ERROR"
say req.error_message, :red
Expand Down
8 changes: 6 additions & 2 deletions spec/cli/authorize_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
$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)
$stdout.should_receive(:print).with("Enter your default private networking option (optional, defaults to false): ")
$stdin.should_receive(:gets).and_return(private_networking)

@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}'"
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}'", "private_networking: '#{private_networking}'"

end

Expand All @@ -62,12 +64,14 @@
$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('')
$stdout.should_receive(:print).with("Enter your default private networking option (optional, defaults to false): ")
$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: ''"
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: ''", "private_networking: 'false'"

end
end
Expand Down
33 changes: 23 additions & 10 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@
end

describe "the file" do
let(:client_key) { "foo" }
let(:api_key) { "bar" }
let(:ssh_user) { "baz" }
let(:ssh_key_path) { "~/.ssh/id_rsa2" }
let(:ssh_port) { "22" }
let(:region) { "2" }
let(:image) { "345791" }
let(:size) { "66" }
let(:ssh_key_id) { '1234' }
let(:client_key) { "foo" }
let(:api_key) { "bar" }
let(:ssh_user) { "baz" }
let(:ssh_key_path) { "~/.ssh/id_rsa2" }
let(:ssh_port) { "22" }
let(:region) { "2" }
let(:image) { "345791" }
let(:size) { "66" }
let(:ssh_key_id) { '1234' }
let(:private_networking) { 'false' }

let(:config) { config = Tugboat::Configuration.instance }

before :each do
# Create a temporary file
config.create_config_file(client_key, api_key, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key_id)
config.create_config_file(client_key, api_key, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key_id, private_networking)
end

it "can be created" do
Expand Down Expand Up @@ -84,6 +85,11 @@
ssh = data["ssh"]
expect(ssh).to have_key("ssh_port")
end

it "should have private networking" do
ssh = data["ssh"]
expect(ssh).to have_key("private_networking")
end
end
describe "backwards compatible" do
let(:client_key) { "foo" }
Expand All @@ -97,6 +103,7 @@
let(:config_default_image) { Tugboat::Configuration::DEFAULT_IMAGE }
let(:config_default_size) { Tugboat::Configuration::DEFAULT_SIZE }
let(:config_default_ssh_key) { Tugboat::Configuration::DEFAULT_SSH_KEY }
let(:config_default_private_networking) { Tugboat::Configuration::DEFAULT_PRIVATE_NETWORKING }
let(:backwards_config) {
{
"authentication" => { "client_key" => client_key, "api_key" => api_key },
Expand Down Expand Up @@ -133,5 +140,11 @@
expect(ssh_key).to eql config_default_ssh_key
end

it "should use default private networking if not in configuration" do
private_networking = config.default_private_networking
expect(private_networking).to eql config_default_private_networking
end


end
end
39 changes: 20 additions & 19 deletions spec/shared/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

shared_context "spec" do
# Default configuration and
let(:config) { Tugboat::Configuration.instance }
let(:client_key) { "foo" }
let(:api_key) { "bar" }
let(:ssh_user) { "baz" }
let(:ssh_port) { "33" }
let(:ssh_key_path) { "~/.ssh/id_rsa2" }
let(:droplet_name) { "foo" }
let(:droplet_ip) { "33.33.33.10" }
let(:droplet_id) { 1234 }
let(:region) { '3' }
let(:image) { '345791'}
let(:size) { '67'}
let(:ssh_key_id) { '1234' }
let(:ssh_key_name) { 'macbook_pro' }
let(:ssh_public_key) { 'ssh-dss A123= user@host' }
let(:ocean) { DigitalOcean::API.new :client_id => client_key, :api_key =>api_key }
let(:app) { lambda { |env| } }
let(:env) { {} }
let(:config) { Tugboat::Configuration.instance }
let(:client_key) { "foo" }
let(:api_key) { "bar" }
let(:ssh_user) { "baz" }
let(:ssh_port) { "33" }
let(:ssh_key_path) { "~/.ssh/id_rsa2" }
let(:droplet_name) { "foo" }
let(:droplet_ip) { "33.33.33.10" }
let(:droplet_id) { 1234 }
let(:private_networking) { "false" }
let(:region) { '3' }
let(:image) { '345791'}
let(:size) { '67'}
let(:ssh_key_id) { '1234' }
let(:ssh_key_name) { 'macbook_pro' }
let(:ssh_public_key) { 'ssh-dss A123= user@host' }
let(:ocean) { DigitalOcean::API.new :client_id => client_key, :api_key =>api_key }
let(:app) { lambda { |env| } }
let(:env) { {} }

before(:each) do
$stdout.sync = true
Expand All @@ -28,7 +29,7 @@
@cli = Tugboat::CLI.new

# Set a temprary project path and create fake config.
config.create_config_file(client_key, api_key, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key_id)
config.create_config_file(client_key, api_key, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key_id, private_networking)
config.reload!

# Keep track of the old stderr / out
Expand Down

0 comments on commit f157b98

Please sign in to comment.