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

Add private networking boolean option (Ex. tugboat create -p true mydrop... #75

Merged
merged 2 commits into from
Nov 22, 2013
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
6 changes: 6 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ 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 => "Enable private networking on 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
30 changes: 25 additions & 5 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 @@ -67,6 +68,10 @@ def default_ssh_key
@data['defaults'].nil? ? DEFAULT_SSH_KEY : @data['defaults']['ssh_key']
end

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

# Re-runs initialize
def reset!
self.send(:initialize)
Expand All @@ -78,7 +83,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,13 +113,28 @@ 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 }
}
"authentication" => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, good shout on this change, makes it much easier to read 👍

"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,
"private_networking" => private_networking
}
}
file.write data.to_yaml
end
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 for private networking (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
15 changes: 10 additions & 5 deletions lib/tugboat/middleware/create_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ 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

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
6 changes: 5 additions & 1 deletion 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 for private networking (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,6 +64,8 @@
$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 for private networking (optional, defaults to false): ")
$stdin.should_receive(:gets).and_return('')

@cli.authorize

Expand Down
8 changes: 4 additions & 4 deletions spec/cli/create_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

describe "create a droplet" do
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}").
stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=#{image}&name=#{droplet_name}&private_networking=#{private_networking}&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=#{image}&name=#{droplet_name}&region_id=#{region}&size_id=#{size}&ssh_key_ids=#{ssh_key_id}")).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}&private_networking=#{private_networking}&region_id=#{region}&size_id=#{size}&ssh_key_ids=#{ssh_key_id}")).to have_been_made
end

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").
stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id=555&name=foo&private_networking=#{private_networking}&region_id=3&size_id=666&ssh_key_ids=4321").
to_return(:status => 200, :body => '{"status":"OK"}')

@cli.options = @cli.options.merge(:image => '555', :size => '666', :region => '3', :keys => '4321')
Expand All @@ -27,7 +27,7 @@
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=555&name=foo&region_id=3&size_id=666&ssh_key_ids=4321")).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&private_networking=#{private_networking}&region_id=3&size_id=666&ssh_key_ids=4321")).to have_been_made
end
end

Expand Down
42 changes: 27 additions & 15 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) { 'true' }

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 set" do
private_networking = data["defaults"]
expect(private_networking).to have_key("private_networking")
end
end
describe "backwards compatible" do
let(:client_key) { "foo" }
Expand All @@ -92,11 +98,12 @@
let(:ssh_key_path) { "~/.ssh/id_rsa2" }
let(:ssh_port) { "22" }

let(:config) { config = Tugboat::Configuration.instance }
let(:config_default_region) { Tugboat::Configuration::DEFAULT_REGION }
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) { config = Tugboat::Configuration.instance }
let(:config_default_region) { Tugboat::Configuration::DEFAULT_REGION }
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_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,10 @@
expect(ssh_key).to eql config_default_ssh_key
end

it "should use default private networking option if not in configuration" do
private_networking = config.default_private_networking
expect(private_networking).to eql config_default_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(: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(:private_networking) { 'false'}
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