tugboat 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.md +27 -2
  3. data/README.md +61 -0
  4. data/lib/tugboat/cli.rb +91 -4
  5. data/lib/tugboat/middleware.rb +54 -0
  6. data/lib/tugboat/middleware/check_droplet_active.rb +17 -0
  7. data/lib/tugboat/middleware/check_droplet_inactive.rb +17 -0
  8. data/lib/tugboat/middleware/find_droplet.rb +4 -0
  9. data/lib/tugboat/middleware/password_reset.rb +23 -0
  10. data/lib/tugboat/middleware/resize_droplet.rb +24 -0
  11. data/lib/tugboat/middleware/ssh_droplet.rb +4 -0
  12. data/lib/tugboat/middleware/start_droplet.rb +21 -0
  13. data/lib/tugboat/middleware/wait_for_state.rb +35 -0
  14. data/lib/tugboat/version.rb +1 -1
  15. data/spec/cli/authorize_cli_spec.rb +0 -4
  16. data/spec/cli/create_cli_spec.rb +0 -4
  17. data/spec/cli/destroy_cli_spec.rb +0 -4
  18. data/spec/cli/droplets_cli_spec.rb +1 -5
  19. data/spec/cli/halt_cli_spec.rb +16 -4
  20. data/spec/cli/help_cli_spec.rb +17 -0
  21. data/spec/cli/images_cli_spec.rb +0 -4
  22. data/spec/cli/info_cli_spec.rb +0 -4
  23. data/spec/cli/keys_cli_spec.rb +0 -4
  24. data/spec/cli/password_reset_cli_spec.rb +83 -0
  25. data/spec/cli/regions_cli_spec.rb +0 -4
  26. data/spec/cli/resize_cli_spec.rb +82 -0
  27. data/spec/cli/restart_cli_spec.rb +0 -4
  28. data/spec/cli/sizes_cli_spec.rb +0 -4
  29. data/spec/cli/snapshot_cli_spec.rb +18 -7
  30. data/spec/cli/ssh_cli_spec.rb +19 -4
  31. data/spec/cli/start_cli_spec.rb +76 -0
  32. data/spec/cli/version_cli_spec.rb +0 -4
  33. data/spec/cli/wait_cli_spec.rb +66 -0
  34. data/spec/config_spec.rb +2 -0
  35. data/spec/fixtures/show_droplet_inactive.json +13 -0
  36. data/spec/fixtures/show_droplets.json +1 -1
  37. data/spec/fixtures/show_droplets_inactive.json +35 -0
  38. data/spec/middleware/check_configuration_spec.rb +0 -3
  39. data/spec/middleware/check_credentials_spec.rb +0 -3
  40. data/spec/middleware/check_droplet_active_spec.rb +15 -0
  41. data/spec/middleware/check_droplet_inactive_spec.rb +15 -0
  42. data/spec/middleware/find_droplet_spec.rb +0 -3
  43. data/spec/middleware/inject_configuration_spec.rb +0 -3
  44. data/spec/middleware/ssh_droplet_spec.rb +19 -3
  45. data/spec/shared/environment.rb +3 -0
  46. data/spec/spec_helper.rb +5 -0
  47. data/tugboat.gemspec +2 -0
  48. metadata +42 -2
@@ -0,0 +1,24 @@
1
+ module Tugboat
2
+ module Middleware
3
+ class ResizeDroplet < Base
4
+ def call(env)
5
+ ocean = env["ocean"]
6
+
7
+ say "Queuing resize for #{env["droplet_id"]} #{env["droplet_name"]}...", nil, false
8
+
9
+ res = ocean.droplets.resize env["droplet_id"],
10
+ :size_id => env["user_droplet_size"]
11
+
12
+ if res.status == "ERROR"
13
+ say "#{res.status}: #{res.error_message}", :red
14
+ exit 1
15
+ end
16
+
17
+ say "done", :green
18
+
19
+ @app.call(env)
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -19,6 +19,10 @@ module Tugboat
19
19
  options.push("-p", "22")
20
20
  end
21
21
 
22
+ if env["user_droplet_ssh_opts"]
23
+ options.concat env["user_droplet_ssh_opts"].split
24
+ end
25
+
22
26
  ssh_user = env["user_droplet_ssh_user"] || env["config"].ssh_user
23
27
  host_string = "#{ssh_user}@#{env["droplet_ip"]}"
24
28
 
@@ -0,0 +1,21 @@
1
+ module Tugboat
2
+ module Middleware
3
+ class StartDroplet < Base
4
+ def call(env)
5
+ ocean = env["ocean"]
6
+
7
+ say "Queuing start for #{env["droplet_id"]} #{env["droplet_name"]}...", nil, false
8
+ req = ocean.droplets.power_on env["droplet_id"]
9
+
10
+ if req.status == "ERROR"
11
+ say "#{req.status}: #{req.error_message}", :red
12
+ exit 1
13
+ end
14
+
15
+ say "done", :green
16
+
17
+ @app.call(env)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ module Tugboat
2
+ module Middleware
3
+ class WaitForState < Base
4
+ def call(env)
5
+ ocean = env["ocean"]
6
+
7
+ say "Waiting for droplet to become #{env["user_droplet_desired_state"]}.", nil, false
8
+
9
+ start_time = Time.now
10
+
11
+ req = ocean.droplets.show env["droplet_id"]
12
+
13
+ say ".", nil, false
14
+
15
+ if req.status == "ERROR"
16
+ say req.error_message, :red
17
+ exit 1
18
+ end
19
+
20
+ while req.droplet.status != env["user_droplet_desired_state"] do
21
+ sleep 2
22
+ req = ocean.droplets.show env["droplet_id"]
23
+ say ".", nil, false
24
+ end
25
+
26
+ total_time = (Time.now - start_time).to_i
27
+
28
+ say "done#{CLEAR} (#{total_time}s)", :green
29
+
30
+ @app.call(env)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
@@ -1,3 +1,3 @@
1
1
  module Tugboat
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "authorize" do
11
7
  before do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "create a droplet" do
11
7
  it "with a name" do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets/new?api_key=#{api_key}&client_id=#{client_key}&image_id&name=#{droplet_name}&region_id&size_id&ssh_key_ids").
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "destroy" do
11
7
  it "destroys a droplet with a fuzzy name" do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "droplets" do
11
7
  it "shows a list" do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
@@ -17,7 +13,7 @@ describe Tugboat::CLI do
17
13
  expect($stdout.string).to eq <<-eos
18
14
  test222 (ip: 33.33.33.10, status: \e[32mactive\e[0m, region: 1, id: 100823)
19
15
  test223 (ip: 33.33.33.10, status: \e[32mactive\e[0m, region: 1, id: 100823)
20
- foo (ip: 33.33.33.10, status: \e[31moff\e[0m, region: 1, id: 100823)
16
+ foo (ip: 33.33.33.10, status: \e[32mactive\e[0m, region: 1, id: 100823)
21
17
  eos
22
18
 
23
19
  expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "halt" do
11
7
  it "halts a droplet with a fuzzy name" do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
@@ -83,6 +79,22 @@ Queuing shutdown for 100823 (foo)...done
83
79
  expect(a_request(:put, "https://api.digitalocean.com/droplets/100823/shutdown?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
84
80
  end
85
81
 
82
+
83
+ it "does not halt a droplet that is off" do
84
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
85
+ to_return(:status => 200, :body => fixture("show_droplets_inactive"))
86
+
87
+ @cli.options = @cli.options.merge(:name => droplet_name)
88
+ expect {@cli.halt}.to raise_error(SystemExit)
89
+
90
+ expect($stdout.string).to eq <<-eos
91
+ Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
92
+ Droplet must be on for this operation to be successful.
93
+ eos
94
+
95
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).to have_been_made
96
+ end
97
+
86
98
  end
87
99
 
88
100
  end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tugboat::CLI do
4
+ include_context "spec"
5
+
6
+ describe "help" do
7
+ it "shows a help message" do
8
+ @cli.help
9
+ expect($stdout.string).to match("Commands:")
10
+ end
11
+
12
+ it "shows a help message for specific commands" do
13
+ @cli.help "sizes"
14
+ expect($stdout.string).to match("Usage:")
15
+ end
16
+ end
17
+ end
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "images" do
11
7
  it "shows a list" do
12
8
  stub_request(:get, "https://api.digitalocean.com/images?api_key=#{api_key}&client_id=#{client_key}&filter=my_images").
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "show" do
11
7
  it "shows a droplet with a fuzzy name" do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "keys" do
11
7
  it "shows a list" do
12
8
  stub_request(:get, "https://api.digitalocean.com/ssh_keys?api_key=#{api_key}&client_id=#{client_key}").
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tugboat::CLI do
4
+ include_context "spec"
5
+
6
+ describe "passwordreset" do
7
+ it "resets the root password given a fuzzy name" do
8
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
9
+ to_return(:status => 200, :body => fixture("show_droplets"))
10
+ stub_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}").
11
+ to_return(:status => 200, :body => '{"status":"OK","event_id":456}')
12
+
13
+ @cli.password_reset("foo")
14
+
15
+ expect($stdout.string).to eq <<-eos
16
+ Droplet fuzzy name provided. Finding droplet ID...done\e[0m, 100823 (foo)
17
+ Queuing password reset for 100823 (foo)...done
18
+ Your new root password will be emailed to you
19
+ eos
20
+
21
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
22
+ to have_been_made
23
+ expect(a_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}")).
24
+ to have_been_made
25
+ end
26
+
27
+ it "resets the root password given an id" do
28
+ stub_request(:get, "https://api.digitalocean.com/droplets/100823?api_key=#{api_key}&client_id=#{client_key}").
29
+ to_return(:status => 200, :body => fixture("show_droplet"))
30
+ stub_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}").
31
+ to_return(:status => 200, :body => '{"status":"OK","event_id":456}')
32
+
33
+ @cli.options = @cli.options.merge(:id => 100823)
34
+ @cli.password_reset
35
+
36
+ expect($stdout.string).to eq <<-eos
37
+ Droplet id provided. Finding Droplet...done\e[0m, 100823 (foo)
38
+ Queuing password reset for 100823 (foo)...done
39
+ Your new root password will be emailed to you
40
+ eos
41
+
42
+ expect(a_request(:get, "https://api.digitalocean.com/droplets/100823?api_key=#{api_key}&client_id=#{client_key}")).
43
+ to have_been_made
44
+ expect(a_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}")).
45
+ to have_been_made
46
+ end
47
+
48
+ it "resets the root password given a name" do
49
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
50
+ to_return(:status => 200, :body => fixture("show_droplets"))
51
+ stub_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}").
52
+ to_return(:status => 200, :body => '{"status":"OK","event_id":456}')
53
+
54
+ @cli.options = @cli.options.merge(:name => "foo")
55
+ @cli.password_reset
56
+
57
+ expect($stdout.string).to eq <<-eos
58
+ Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
59
+ Queuing password reset for 100823 (foo)...done
60
+ Your new root password will be emailed to you
61
+ eos
62
+
63
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
64
+ to have_been_made
65
+ expect(a_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}")).
66
+ to have_been_made
67
+ end
68
+
69
+ it "raises SystemExit when a request fails" do
70
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
71
+ to_return(:status => 200, :body => fixture("show_droplets"))
72
+ stub_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}").
73
+ to_return(:status => 500, :body => '{"status":"ERROR","message":"Some error"}')
74
+
75
+ expect { @cli.password_reset("foo") }.to raise_error(SystemExit)
76
+
77
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
78
+ to have_been_made
79
+ expect(a_request(:post, "https://api.digitalocean.com/droplets/100823/password_reset?api_key=#{api_key}&client_id=#{client_key}")).
80
+ to have_been_made
81
+ end
82
+ end
83
+ end
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "regions" do
11
7
  it "shows a list" do
12
8
  stub_request(:get, "https://api.digitalocean.com/regions?api_key=#{api_key}&client_id=#{client_key}").
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tugboat::CLI do
4
+ include_context "spec"
5
+
6
+ describe "resize" do
7
+ it "resizes a droplet with a fuzzy name" do
8
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
9
+ to_return(:status => 200, :body => fixture("show_droplets"))
10
+ stub_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123").
11
+ to_return(:status => 200, :body => '{"status":"OK","event_id":456}')
12
+
13
+ @cli.options = @cli.options.merge(:size => 123)
14
+ @cli.resize("foo")
15
+
16
+ expect($stdout.string).to eq <<-eos
17
+ Droplet fuzzy name provided. Finding droplet ID...done\e[0m, 100823 (foo)
18
+ Queuing resize for 100823 (foo)...done
19
+ eos
20
+
21
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
22
+ to have_been_made
23
+ expect(a_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123")).
24
+ to have_been_made
25
+ end
26
+
27
+ it "resizes a droplet with an id" do
28
+ stub_request(:get, "https://api.digitalocean.com/droplets/100823?api_key=#{api_key}&client_id=#{client_key}").
29
+ to_return(:status => 200, :body => fixture("show_droplet"))
30
+ stub_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123").
31
+ to_return(:status => 200, :body => '{"status":"OK","event_id":456}')
32
+
33
+ @cli.options = @cli.options.merge(:size => 123, :id => 100823)
34
+ @cli.resize
35
+
36
+ expect($stdout.string).to eq <<-eos
37
+ Droplet id provided. Finding Droplet...done\e[0m, 100823 (foo)
38
+ Queuing resize for 100823 (foo)...done
39
+ eos
40
+
41
+ expect(a_request(:get, "https://api.digitalocean.com/droplets/100823?api_key=#{api_key}&client_id=#{client_key}")).
42
+ to have_been_made
43
+ expect(a_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123")).
44
+ to have_been_made
45
+ end
46
+
47
+ it "resizes a droplet with a name" do
48
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
49
+ to_return(:status => 200, :body => fixture("show_droplets"))
50
+ stub_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123").
51
+ to_return(:status => 200, :body => '{"status":"OK","event_id":456}')
52
+
53
+ @cli.options = @cli.options.merge(:size => 123, :name => "foo")
54
+ @cli.resize
55
+
56
+ expect($stdout.string).to eq <<-eos
57
+ Droplet name provided. Finding droplet ID...done\e[0m, 100823 (foo)
58
+ Queuing resize for 100823 (foo)...done
59
+ eos
60
+
61
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
62
+ to have_been_made
63
+ expect(a_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123")).
64
+ to have_been_made
65
+ end
66
+
67
+ it "raises SystemExit when a request fails" do
68
+ stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
69
+ to_return(:status => 200, :body => fixture("show_droplets"))
70
+ stub_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123").
71
+ to_return(:status => 500, :body => '{"status":"ERROR","message":"Some error"}')
72
+
73
+ @cli.options = @cli.options.merge(:size => 123)
74
+ expect { @cli.resize("foo") }.to raise_error(SystemExit)
75
+
76
+ expect(a_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}")).
77
+ to have_been_made
78
+ expect(a_request(:get, "https://api.digitalocean.com/droplets/100823/resize?api_key=#{api_key}&client_id=#{client_key}&size_id=123")).
79
+ to have_been_made
80
+ end
81
+ end
82
+ end
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "restarts a droplet" do
11
7
  it "with a fuzzy name" do
12
8
  stub_request(:get, "https://api.digitalocean.com/droplets?api_key=#{api_key}&client_id=#{client_key}").
@@ -3,10 +3,6 @@ require 'spec_helper'
3
3
  describe Tugboat::CLI do
4
4
  include_context "spec"
5
5
 
6
- before :each do
7
- @cli = Tugboat::CLI.new
8
- end
9
-
10
6
  describe "sizes" do
11
7
  it "shows a list" do
12
8
  stub_request(:get, "https://api.digitalocean.com/sizes?api_key=#{api_key}&client_id=#{client_key}").