-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from petems/add_scp_feature
Adds new SCP feature
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,21 @@ match. | |
Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-17-generic x86_64) | ||
pearkes@pearkes-admin-001:~# | ||
|
||
### SCP files to droplet | ||
|
||
*You can configure an SSH username and key path in `tugboat authorize`, | ||
or by changing your `~/.tugboat`.* | ||
|
||
This lets you scp a file into a droplet by providing it's name, or a partial | ||
match. | ||
|
||
$ tugboat scp test-scp /tmp/foo /tmp/bar | ||
Droplet fuzzy name provided. Finding droplet ID...done, 72025053 (test-scp) | ||
Executing SCP on Droplet (test-scp)... | ||
Attempting SCP with `scp -i /Users/petems/.ssh/digital_ocean /tmp/foo [email protected]:/tmp/bar` | ||
foo | ||
100% 0 0.0KB/s 00:00 | ||
|
||
### Create a droplet | ||
|
||
$ tugboat create pearkes-www-002 -s 512mb -i ubuntu-12-04-x64 -r nyc2 -k 11251 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Tugboat | ||
module Middleware | ||
class SCPDroplet < Base | ||
def call(env) | ||
say "Executing SCP on Droplet #{env['droplet_name']}..." | ||
|
||
identity = File.expand_path(env['config'].ssh_key_path.to_s).strip | ||
|
||
ssh_user = env['user_droplet_ssh_user'] || env['config'].ssh_user | ||
|
||
scp_command = env['user_scp_command'] || 'scp' | ||
|
||
host_ip = env['droplet_ip'] | ||
|
||
host_string = "#{ssh_user}@#{host_ip}" | ||
|
||
if env['user_droplet_ssh_wait'] | ||
say 'Wait flag given, waiting for droplet to become active' | ||
wait_for_state(env['droplet_id'], 'active', env['barge']) | ||
end | ||
|
||
identity_string = "-i #{identity}" | ||
|
||
scp_command_string = [scp_command, identity_string, env['user_from_file'], "#{host_string}:#{env['user_to_file']}"].join(' ') | ||
|
||
say "Attempting SCP with `#{scp_command_string}`" | ||
|
||
Kernel.exec(scp_command_string) | ||
|
||
@app.call(env) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'spec_helper' | ||
|
||
describe Tugboat::CLI do | ||
include_context 'spec' | ||
|
||
describe 'scp' do | ||
it "tries to fetch the droplet's IP from the API" do | ||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1'). | ||
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }). | ||
to_return(status: 200, body: fixture('show_droplets'), headers: {}) | ||
|
||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=200'). | ||
to_return(headers: { 'Content-Type' => 'application/json' }, status: 200, body: fixture('show_droplets')) | ||
allow(Kernel).to receive(:exec).with("scp -i #{Dir.home}/.ssh/id_rsa2 /tmp/foo [email protected]:/tmp/bar") | ||
|
||
expected_string = <<-eos | ||
Droplet fuzzy name provided. Finding droplet ID...done\e[0m, 6918990 (example.com) | ||
Executing SCP on Droplet (example.com)... | ||
Attempting SCP with `scp -i #{Dir.home}/.ssh/id_rsa2 /tmp/foo [email protected]:/tmp/bar` | ||
eos | ||
|
||
expect { cli.scp('example.com', '/tmp/foo', '/tmp/bar') }.to output(expected_string).to_stdout | ||
end | ||
|
||
it "runs with rsync if given at the command line" do | ||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1'). | ||
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }). | ||
to_return(status: 200, body: fixture('show_droplets'), headers: {}) | ||
|
||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=200'). | ||
to_return(headers: { 'Content-Type' => 'application/json' }, status: 200, body: fixture('show_droplets')) | ||
allow(Kernel).to receive(:exec).with("rsync -i #{Dir.home}/.ssh/id_rsa2 /tmp/foo [email protected]:/tmp/bar") | ||
|
||
expected_string = <<-eos | ||
Droplet fuzzy name provided. Finding droplet ID...done\e[0m, 6918990 (example.com) | ||
Executing SCP on Droplet (example.com)... | ||
Attempting SCP with `rsync -i #{Dir.home}/.ssh/id_rsa2 /tmp/foo [email protected]:/tmp/bar` | ||
eos | ||
|
||
cli.options = cli.options.merge(scp_command: 'rsync') | ||
|
||
expect { cli.scp('example.com', '/tmp/foo', '/tmp/bar') }.to output(expected_string).to_stdout | ||
end | ||
|
||
it "wait's until droplet active if -w command is given and droplet eventually active" do | ||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=1'). | ||
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }). | ||
to_return(status: 200, body: '', headers: {}) | ||
|
||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets/6918990?per_page=200'). | ||
with(headers: { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => 'Bearer foo', 'Content-Type' => 'application/json', 'User-Agent' => 'Faraday v0.9.2' }). | ||
to_return( | ||
{ status: 200, body: fixture('show_droplet_inactive'), headers: {} }, | ||
status: 200, body: fixture('show_droplet'), headers: {} | ||
) | ||
|
||
stub_request(:get, 'https://api.digitalocean.com/v2/droplets?page=1&per_page=200'). | ||
to_return(headers: { 'Content-Type' => 'application/json' }, status: 200, body: fixture('show_droplets')) | ||
allow(Kernel).to receive(:exec).with("scp -i #{Dir.home}/.ssh/id_rsa2 /tmp/foo [email protected]:/tmp/bar") | ||
|
||
cli.options = cli.options.merge(wait: true) | ||
|
||
expected_string = <<-eos | ||
Droplet fuzzy name provided. Finding droplet ID...done\e[0m, 6918990 (example.com) | ||
Executing SCP on Droplet (example.com)... | ||
Wait flag given, waiting for droplet to become active | ||
..done\e[0m (2s) | ||
Attempting SCP with `scp -i #{Dir.home}/.ssh/id_rsa2 /tmp/foo [email protected]:/tmp/bar` | ||
eos | ||
|
||
expect { cli.scp('example.com', '/tmp/foo', '/tmp/bar') }.to output(expected_string).to_stdout | ||
end | ||
|
||
end | ||
end |