Skip to content

Commit

Permalink
Merge pull request #107 from iPublications/ipub-private-ip
Browse files Browse the repository at this point in the history
Display + SSH to private IP
  • Loading branch information
petems committed Oct 29, 2014
2 parents e577adf + 9d73470 commit 0576902
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions lib/tugboat/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def images
:type => :string,
:aliases => "-p",
:desc => "The custom SSH Port to connect to"
method_option "use_public_ip",
:type => :boolean,
:aliases => "-e",
:desc => "Use public IP while private IP is present"
method_option "ssh_user",
:type => :string,
:aliases => "-u",
Expand All @@ -103,6 +107,7 @@ def ssh(name=nil)
"user_droplet_fuzzy_name" => name,
"user_droplet_ssh_port" => options[:ssh_port],
"user_droplet_ssh_user" => options[:ssh_user],
"user_droplet_use_public_ip" => options[:use_public_ip],
"user_droplet_ssh_opts" => options[:ssh_opts],
"user_droplet_ssh_command" => options[:ssh_command],
"user_quiet" => options[:quiet]
Expand Down
4 changes: 4 additions & 0 deletions lib/tugboat/middleware/find_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def call(env)
env["droplet_id"] = req.droplet.id
env["droplet_name"] = "(#{req.droplet.name})"
env["droplet_ip"] = req.droplet.ip_address
env["droplet_ip_private"] = req.droplet.private_ip_address
env["droplet_status"] = req.droplet.status
end

Expand All @@ -47,6 +48,7 @@ def call(env)
env["droplet_id"] = d.id
env["droplet_name"] = "(#{d.name})"
env["droplet_ip"] = d.ip_address
env["droplet_ip_private"] = d.private_ip_address
env["droplet_status"] = d.status
end
end
Expand Down Expand Up @@ -83,6 +85,7 @@ def call(env)
env["droplet_id"] = found_droplets.first.id
env["droplet_name"] = "(#{found_droplets.first.name})"
env["droplet_ip"] = found_droplets.first.ip_address
env["droplet_ip_private"] = found_droplets.first.private_ip_address
env["droplet_status"] = found_droplets.first.status
elsif found_droplets.length > 1
# Did we run the multiple questionairre?
Expand All @@ -99,6 +102,7 @@ def call(env)
env["droplet_id"] = found_droplets[choice.to_i].id
env["droplet_name"] = found_droplets[choice.to_i].name
env["droplet_ip"] = found_droplets[choice.to_i].ip_address
env["droplet_ip_private"] = found_droplets[choice.to_i].private_ip_address
env["droplet_status"] = found_droplets[choice.to_i].status
end

Expand Down
5 changes: 5 additions & 0 deletions lib/tugboat/middleware/info_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def call(env)
say "ID: #{droplet.id}"
say "Status: #{status_color}#{droplet.status}#{CLEAR}"
say "IP: #{droplet.ip_address}"

if droplet.private_ip_address
say "Private IP: #{droplet.private_ip_address}"
end

say "Region ID: #{droplet.region_id}"
say "Image ID: #{droplet.image_id}"
say "Size ID: #{droplet.size_id}"
Expand Down
6 changes: 5 additions & 1 deletion lib/tugboat/middleware/list_droplets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ def call(env)
else
droplet_list.each do |droplet|

if droplet.private_ip_address
private_ip = ", privateip: #{droplet.private_ip_address}"
end

if droplet.status == "active"
status_color = GREEN
else
status_color = RED
end

say "#{droplet.name} (ip: #{droplet.ip_address}, status: #{status_color}#{droplet.status}#{CLEAR}, region: #{droplet.region_id}, id: #{droplet.id})"
say "#{droplet.name} (ip: #{droplet.ip_address}#{private_ip}, status: #{status_color}#{droplet.status}#{CLEAR}, region: #{droplet.region_id}, id: #{droplet.id})"
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/tugboat/middleware/ssh_droplet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def call(env)
ssh_user = env["user_droplet_ssh_user"] || env["config"].ssh_user
host_string = "#{ssh_user}@#{env["droplet_ip"]}"

if env["droplet_ip_private"] and not env["user_droplet_use_public_ip"]
host_string = "#{ssh_user}@#{env["droplet_ip_private"]}"
end

options << host_string

if env["user_droplet_ssh_command"]
Expand Down
4 changes: 2 additions & 2 deletions spec/cli/droplets_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
@cli.droplets

expect($stdout.string).to eq <<-eos
test222 (ip: 33.33.33.10, status: \e[32mactive\e[0m, region: 1, id: 100823)
test222 (ip: 33.33.33.10, privateip: 10.20.30.1, status: \e[32mactive\e[0m, region: 1, id: 100823)
test223 (ip: 33.33.33.10, status: \e[32mactive\e[0m, region: 1, id: 100823)
foo (ip: 33.33.33.10, status: \e[32mactive\e[0m, region: 1, id: 100823)
foo (ip: 33.33.33.10, privateip: 10.20.30.40, status: \e[32mactive\e[0m, region: 1, id: 100823)
eos

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
Expand Down
3 changes: 3 additions & 0 deletions spec/cli/info_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
ID: 100823
Status: \e[32mactive\e[0m
IP: 33.33.33.10
Private IP: 10.20.30.40
Region ID: 1
Image ID: 420
Size ID: 33
Expand Down Expand Up @@ -47,6 +48,7 @@
ID: 100823
Status: \e[32mactive\e[0m
IP: 33.33.33.10
Private IP: 10.20.30.40
Region ID: 1
Image ID: 420
Size ID: 33
Expand Down Expand Up @@ -74,6 +76,7 @@
ID: 100823
Status: \e[32mactive\e[0m
IP: 33.33.33.10
Private IP: 10.20.30.40
Region ID: 1
Image ID: 420
Size ID: 33
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/show_droplet.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"image_id": 420,
"name": "foo",
"ip_address": "33.33.33.10",
"private_ip_address": "10.20.30.40",
"region_id": 1,
"size_id": 33,
"status": "active"
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/show_droplet_inactive.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"image_id": 420,
"name": "foo",
"ip_address": "33.33.33.10",
"private_ip_address": "10.20.30.40",
"region_id": 1,
"size_id": 33,
"status": "off"
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/show_droplets.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"droplets": [
{
"ip_address": "33.33.33.10",
"private_ip_address": "10.20.30.1",
"backups_active": null,
"id": 100823,
"image_id": 420,
Expand All @@ -23,6 +24,7 @@
},
{
"ip_address": "33.33.33.10",
"private_ip_address": "10.20.30.40",
"backups_active": null,
"id": 100823,
"image_id": 420,
Expand Down
2 changes: 2 additions & 0 deletions spec/fixtures/show_droplets_inactive.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"droplets": [
{
"ip_address": "33.33.33.10",
"private_ip_address": "10.20.30.1",
"backups_active": null,
"id": 100823,
"image_id": 420,
Expand All @@ -23,6 +24,7 @@
},
{
"ip_address": "33.33.33.10",
"private_ip_address": "10.20.30.40",
"backups_active": null,
"id": 100823,
"image_id": 420,
Expand Down
8 changes: 6 additions & 2 deletions spec/middleware/ssh_droplet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
"-o", "UserKnownHostsFile=/dev/null",
"-i", ssh_key_path,
"-p", ssh_port,
"#{ssh_user}@#{droplet_ip}")
"#{ssh_user}@#{droplet_ip_private}")

env["droplet_ip"] = droplet_ip
env["droplet_ip_private"] = droplet_ip_private
env["config"] = config

described_class.new(app).call(env)
Expand All @@ -33,15 +34,18 @@
"-o", "UserKnownHostsFile=/dev/null",
"-i", ssh_key_path,
"-p", ssh_port,
"-e",
"-q",
"-X",
"#{ssh_user}@#{droplet_ip}",
"echo hello")

env["droplet_ip"] = droplet_ip
env["droplet_ip_private"] = droplet_ip_private
env["config"] = config
env["user_droplet_ssh_command"] = "echo hello"
env["user_droplet_ssh_opts"] = "-q -X"
env["user_droplet_use_public_ip"] = true
env["user_droplet_ssh_opts"] = "-e -q -X"

described_class.new(app).call(env)
end
Expand Down
1 change: 1 addition & 0 deletions spec/shared/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
let(:ssh_key_path) { "~/.ssh/id_rsa2" }
let(:droplet_name) { "foo" }
let(:droplet_ip) { "33.33.33.10" }
let(:droplet_ip_private) { "10.20.30.40" }
let(:droplet_id) { 1234 }
let(:region) { '3' }
let(:image) { '345791'}
Expand Down

0 comments on commit 0576902

Please sign in to comment.