Skip to content

Commit

Permalink
Add configurable timeout to config file
Browse files Browse the repository at this point in the history
* Default to previous 10 sec default
  • Loading branch information
lachesis authored and petems committed Nov 16, 2017
1 parent bc58bed commit 88b14b1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
12 changes: 11 additions & 1 deletion lib/tugboat/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Configuration
DEFAULT_PRIVATE_NETWORKING = 'false'.freeze
DEFAULT_BACKUPS_ENABLED = 'false'.freeze
DEFAULT_USER_DATA = nil
DEFAULT_TIMEOUT = 10

# Load config file from current directory, if not exit load from user's home directory
def initialize
Expand Down Expand Up @@ -95,6 +96,10 @@ def env_access_token
ENV['DO_API_TOKEN'] unless ENV['DO_API_TOKEN'].to_s.empty?
end

def timeout
(@data['connection'].nil? || @data['connection']['timeout'].nil?) ? DEFAULT_TIMEOUT : @data['connection']['timeout']
end

# Re-runs initialize
def reset!
send(:initialize)
Expand All @@ -106,7 +111,7 @@ def reload!
end

# Writes a config file
def create_config_file(access_token, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key, private_networking, backups_enabled, ip6)
def create_config_file(access_token, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key, private_networking, backups_enabled, ip6, timeout)
# Default SSH Key path
ssh_key_path = File.join('~', DEFAULT_SSH_KEY_PATH) if ssh_key_path.empty?

Expand All @@ -130,12 +135,17 @@ def create_config_file(access_token, ssh_key_path, ssh_user, ssh_port, region, i

ip6 = DEFAULT_IP6 if ip6.empty?

timeout = DEFAULT_IP6 if timeout.empty?

require 'yaml'
File.open(@path, File::RDWR | File::TRUNC | File::CREAT, 0o600) do |file|
data = {
'authentication' => {
'access_token' => access_token
},
'connection' => {
'timeout' => timeout
},
'ssh' => {
'ssh_user' => ssh_user,
'ssh_key_path' => ssh_key_path,
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 @@ -6,6 +6,7 @@ def call(env)
say 'Note: You can get your Access Token from https://cloud.digitalocean.com/settings/tokens/new', :yellow
say
access_token = ask 'Enter your access token:'
timeout = ask 'Enter your default timeout for connections in seconds (optional, defaults to 10):'
access_token.strip!
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 root):'
Expand All @@ -23,7 +24,7 @@ def call(env)
ip6 = ask 'Enter your default for IPv6 (optional, defaults to false):'

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

@app.call(env)
Expand Down
11 changes: 10 additions & 1 deletion lib/tugboat/middleware/inject_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ def call(env)
# Sets the digital ocean client into the environment for use
# later.
@access_token = env['config'].access_token
config_timeout = env['config'].timeout

env['barge'] = Barge::Client.new(access_token: @access_token,
timeout: config_timeout,
open_timeout: config_timeout)

env['barge'] = Barge::Client.new(access_token: @access_token)
env['droplet_kit'] = DropletKit::Client.new(access_token: @access_token)

env['droplet_kit'].connection.options.timeout = config_timeout
env['droplet_kit'].connection.options.open_timeout = config_timeout

env['barge'].faraday.use CustomLogger if ENV['DEBUG']
env['droplet_kit'].connection.use CustomLogger if ENV['DEBUG']

env['droplet_kit'].connection

@app.call(env)
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/cli/authorize_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
expect($stdout).to receive(:print).exactly(6).times
expect($stdout).to receive(:print).with('Enter your access token: ')
expect($stdin).to receive(:gets).and_return(access_token)
expect($stdout).to receive(:print).with('Enter your default timeout for connections in seconds (optional, defaults to 10): ')
expect($stdin).to receive(:gets).and_return(timeout)
expect($stdout).to receive(:print).with('Enter your SSH key path (optional, defaults to ~/.ssh/id_rsa): ')
expect($stdin).to receive(:gets).and_return(ssh_key_path)
expect($stdout).to receive(:print).with('Enter your SSH user (optional, defaults to root): ')
Expand Down Expand Up @@ -66,6 +68,8 @@
expect($stdout).to receive(:print).exactly(6).times
expect($stdout).to receive(:print).with('Enter your access token: ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your default timeout for connections in seconds (optional, defaults to 10): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your SSH key path (optional, defaults to ~/.ssh/id_rsa): ')
expect($stdin).to receive(:gets).and_return('')
expect($stdout).to receive(:print).with('Enter your SSH user (optional, defaults to root): ')
Expand Down
4 changes: 4 additions & 0 deletions spec/cli/config_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
---
authentication:
access_token: foo
connection:
timeout: '15'
ssh:
ssh_user: baz
ssh_key_path: ~/.ssh/id_rsa2
Expand All @@ -38,6 +40,8 @@
---
authentication:
access_token:\x20\x20[REDACTED]
connection:
timeout: '15'
ssh:
ssh_user: baz
ssh_key_path: ~/.ssh/id_rsa2
Expand Down
8 changes: 7 additions & 1 deletion spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@
let(:private_networking) { 'false' }
let(:backups_enabled) { 'false' }
let(:ip6) { 'false' }
let(:timeout) { '30' }

let(:config) { described_class.instance }

before do
# Create a temporary file
config.create_config_file(access_token, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key_id, private_networking, backups_enabled, ip6)
config.create_config_file(access_token, ssh_key_path, ssh_user, ssh_port, region, image, size, ssh_key_id, private_networking, backups_enabled, ip6, timeout)
end

it 'can be created' do
Expand Down Expand Up @@ -91,6 +92,11 @@
backups_enabled = data['defaults']
expect(backups_enabled).to have_key('backups_enabled')
end

it 'has timeout set' do
timeout_set = data['connection']
expect(timeout_set).to have_key('timeout')
end
end
describe 'backwards compatible' do
let(:client_key) { 'foo' }
Expand Down
3 changes: 2 additions & 1 deletion spec/shared/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
let(:private_networking) { 'false' }
let(:backups_enabled) { 'false' }
let(:ip6) { 'false' }
let(:timeout) { '15' }
let(:ocean) { Barge::Client.new(access_token: access_token) }
let(:app) { ->(env) {} }
let(:env) { {} }
Expand All @@ -30,7 +31,7 @@
$stderr.sync = true

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

# Keep track of the old stderr / out
Expand Down

0 comments on commit 88b14b1

Please sign in to comment.