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

Droplet State Checks #31

Merged
merged 6 commits into from
May 7, 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/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module Middleware
autoload :ListSSHKeys, "tugboat/middleware/list_ssh_keys"
autoload :ListRegions, "tugboat/middleware/list_regions"
autoload :ListSizes, "tugboat/middleware/list_sizes"
autoload :CheckDropletActive, "tugboat/middleware/check_droplet_active"
autoload :CheckDropletInactive, "tugboat/middleware/check_droplet_inactive"

# Start the authorization flow.
# This writes a ~/.tugboat file, which can be edited manually.
Expand Down Expand Up @@ -75,6 +77,7 @@ def self.sequence_start_droplet
use CheckConfiguration
use InjectClient
use FindDroplet
use CheckDropletInactive
use StartDroplet
end
end
Expand All @@ -86,6 +89,7 @@ def self.sequence_halt_droplet
use CheckConfiguration
use InjectClient
use FindDroplet
use CheckDropletActive
use HaltDroplet
end
end
Expand All @@ -108,6 +112,7 @@ def self.sequence_ssh_droplet
use CheckConfiguration
use InjectClient
use FindDroplet
use CheckDropletActive
use SSHDroplet
end
end
Expand Down Expand Up @@ -141,6 +146,7 @@ def self.sequence_snapshot_droplet
use CheckConfiguration
use InjectClient
use FindDroplet
use CheckDropletInactive
use SnapshotDroplet
end
end
Expand Down
17 changes: 17 additions & 0 deletions lib/tugboat/middleware/check_droplet_active.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Tugboat
module Middleware
# Check if the droplet in the environment is active
class CheckDropletActive < Base
def call(env)

if env["droplet_status"] != "active"
say "Droplet must be on for this operation to be successful.", :red
exit 1
end

@app.call(env)
end
end
end
end

17 changes: 17 additions & 0 deletions lib/tugboat/middleware/check_droplet_inactive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Tugboat
module Middleware
# Check if the droplet in the environment is inactive, or "off"
class CheckDropletInactive < Base
def call(env)

if env["droplet_status"] != "off"
say "Droplet must be off for this operation to be successful.", :red
exit 1
end

@app.call(env)
end
end
end
end

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_status"] = req.droplet.status
end

# If they provide a name, we need to get the ID for it.
Expand All @@ -46,6 +47,7 @@ def call(env)
env["droplet_id"] = d.id
env["droplet_name"] = "(#{d.name})"
env["droplet_ip"] = d.ip_address
env["droplet_status"] = d.status
end
end

Expand Down Expand Up @@ -81,6 +83,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_status"] = found_droplets.first.status
elsif found_droplets.length > 1
# Did we run the multiple questionairre?
did_run_multiple = true
Expand All @@ -96,6 +99,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_status"] = found_droplets[choice.to_i].status
end

# If we coulnd't find it, tell the user and drop out of the
Expand Down
2 changes: 1 addition & 1 deletion spec/cli/droplets_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
expect($stdout.string).to eq <<-eos
test222 (ip: 33.33.33.10, 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[31moff\e[0m, region: 1, id: 100823)
foo (ip: 33.33.33.10, 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
16 changes: 16 additions & 0 deletions spec/cli/halt_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@
expect(a_request(:put, "https://api.digitalocean.com/droplets/100823/shutdown?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end


it "does not halt a droplet that is off" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets_inactive"))

@cli.options = @cli.options.merge(:name => droplet_name)
expect {@cli.halt}.to raise_error(SystemExit)

expect($stdout.string).to eq <<-eos
Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
Droplet must be on for this operation to be successful.
eos

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

end

end
21 changes: 18 additions & 3 deletions spec/cli/snapshot_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
describe "snapshots a droplet" do
it "with a fuzzy name" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets"))
to_return(:status => 200, :body => fixture("show_droplets_inactive"))

stub_request(:get, "https://api.digitalocean.com/droplets/100823/snapshot?api_key=#{api_key}&client_id=#{client_key}&name=#{snapshot_name}").
to_return(:status => 200, :body => fixture("show_droplet"))
Expand All @@ -31,7 +31,7 @@

it "with an id" do
stub_request(:get, "https://api.digitalocean.com/droplets/#{droplet_id}?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplet"))
to_return(:status => 200, :body => fixture("show_droplet_inactive"))

stub_request(:get, "https://api.digitalocean.com/droplets/100823/snapshot?api_key=#{api_key}&client_id=#{client_key}&name=#{snapshot_name}").
to_return(:status => 200, :body => fixture("show_droplet"))
Expand All @@ -52,7 +52,7 @@

it "with a name" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets"))
to_return(:status => 200, :body => fixture("show_droplets_inactive"))

stub_request(:get, "https://api.digitalocean.com/droplets/100823/snapshot?api_key=#{api_key}&client_id=#{client_key}&name=#{snapshot_name}").
to_return(:status => 200, :body => fixture("show_droplet"))
Expand All @@ -70,6 +70,21 @@
expect(a_request(:get, "https://api.digitalocean.com/droplets/100823/snapshot?api_key=#{api_key}&client_id=#{client_key}&name=#{snapshot_name}")).to have_been_made
end

it "does not snaphshot a droplet that is active" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets"))

@cli.options = @cli.options.merge(:name => droplet_name)
expect {@cli.snapshot(snapshot_name)}.to raise_error(SystemExit)

expect($stdout.string).to eq <<-eos
Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
Droplet must be off for this operation to be successful.
eos

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

end

end
19 changes: 19 additions & 0 deletions spec/cli/ssh_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,24 @@
expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
to have_been_made
end

it "does not allow ssh into a droplet that is inactive" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets_inactive"))

Kernel.stub(:exec)

@cli.options = @cli.options.merge(:name => droplet_name)

expect {@cli.ssh("test222")}.to raise_error(SystemExit)

expect($stdout.string).to eq <<-eos
Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
Droplet must be on for this operation to be successful.
eos

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

end
end
21 changes: 18 additions & 3 deletions spec/cli/start_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
describe "start" do
it "starts the droplet with a fuzzy name" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets"))
to_return(:status => 200, :body => fixture("show_droplets_inactive"))
stub_request(:put, "https://api.digitalocean.com/droplets/100823/power_on?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplet"))

Expand All @@ -27,7 +27,7 @@

it "starts the droplet with an id" do
stub_request(:get, "https://api.digitalocean.com/droplets/#{droplet_id}?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplet"))
to_return(:status => 200, :body => fixture("show_droplet_inactive"))
stub_request(:put, "https://api.digitalocean.com/droplets/100823/power_on?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplet"))

Expand All @@ -46,7 +46,7 @@

it "starts the droplet with a name" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets"))
to_return(:status => 200, :body => fixture("show_droplets_inactive"))
stub_request(:put, "https://api.digitalocean.com/droplets/100823/power_on?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplet"))

Expand All @@ -61,5 +61,20 @@
expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
expect(a_request(:put, "https://api.digitalocean.com/droplets/100823/power_on?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end

it "does not start a droplet that is inactive" do
stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
to_return(:status => 200, :body => fixture("show_droplets"))

@cli.options = @cli.options.merge(:name => droplet_name)
expect {@cli.start}.to raise_error(SystemExit)

expect($stdout.string).to eq <<-eos
Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
Droplet must be off for this operation to be successful.
eos

expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
end
end
end
13 changes: 13 additions & 0 deletions spec/fixtures/show_droplet_inactive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status": "OK",
"droplet": {
"backups_active": null,
"id": 100823,
"image_id": 420,
"name": "foo",
"ip_address": "33.33.33.10",
"region_id": 1,
"size_id": 33,
"status": "off"
}
}
2 changes: 1 addition & 1 deletion spec/fixtures/show_droplets.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"name": "foo",
"region_id": 1,
"size_id": 33,
"status": "off"
"status": "active"
}
]
}
35 changes: 35 additions & 0 deletions spec/fixtures/show_droplets_inactive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"status": "OK",
"droplets": [
{
"ip_address": "33.33.33.10",
"backups_active": null,
"id": 100823,
"image_id": 420,
"name": "test222",
"region_id": 1,
"size_id": 33,
"status": "off"
},
{
"ip_address": "33.33.33.10",
"backups_active": null,
"id": 100823,
"image_id": 420,
"name": "test223",
"region_id": 1,
"size_id": 33,
"status": "off"
},
{
"ip_address": "33.33.33.10",
"backups_active": null,
"id": 100823,
"image_id": 420,
"name": "foo",
"region_id": 1,
"size_id": 33,
"status": "off"
}
]
}
18 changes: 18 additions & 0 deletions spec/middleware/check_droplet_active_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Tugboat::Middleware::CheckDropletActive do
include_context "spec"

let(:app) { lambda { |env| } }
let(:env) { {} }

describe ".call" do
it "raises an error when droplet is not active" do

env["droplet_status"] = "off"

expect {described_class.new(app).call(env) }.to raise_error(SystemExit)
end
end

end
18 changes: 18 additions & 0 deletions spec/middleware/check_droplet_inactive_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Tugboat::Middleware::CheckDropletInactive do
include_context "spec"

let(:app) { lambda { |env| } }
let(:env) { {} }

describe ".call" do
it "raises an error when droplet is active" do

env["droplet_status"] = "active"

expect {described_class.new(app).call(env) }.to raise_error(SystemExit)
end
end

end