Skip to content

Commit 719e514

Browse files
committed
Allow to start an instance that was stopped
1 parent eeb319c commit 719e514

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/vagrant-ec2/actions.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'actions/check_state'
44
require_relative 'actions/wait_for_state'
55
require_relative 'actions/run_instance'
6+
require_relative 'actions/start_instance'
67

78
module VagrantPlugins
89
module Ec2
@@ -17,13 +18,34 @@ def self.read_state
1718
end
1819
end
1920

21+
def self.resume
22+
return Vagrant::Action::Builder.new.tap do |builder|
23+
builder.use ConfigValidate
24+
builder.use ConnectAWS
25+
builder.use Call, CheckState do |env, b|
26+
if env[:machine_state] == :stopped || env[:machine_state] == :stopping
27+
b.use WaitForState, :stopped if env[:machine_state] == :stopping
28+
b.use StartInstance
29+
b.use WaitForState, :running
30+
elsif env[:machine_state] == :running
31+
raise env[:machine_state]
32+
else
33+
raise "The instance #{env[:machine].id} is #{env[:machine_state]} this is unexpected"
34+
end
35+
end
36+
end
37+
end
38+
2039
def self.up
2140
return Vagrant::Action::Builder.new.tap do |builder|
2241
builder.use ConfigValidate
2342
builder.use ConnectAWS
2443
builder.use Call, CheckState do |env, b|
2544
if env[:machine_state] == :not_created
2645
b.use RunInstance
46+
elsif env[:machine_state] == :stopped || env[:machine_state] == :stopping
47+
b.use WaitForState, :stopped if env[:machine_state] == :stopping
48+
b.use StartInstance
2749
else
2850
if env[:machine_state] == :running
2951
raise env[:machine_state]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'aws-sdk'
2+
3+
module VagrantPlugins
4+
module Ec2
5+
module Actions
6+
class StartInstance
7+
def initialize(app, env)
8+
@app = app
9+
end
10+
11+
def call(env)
12+
ec2 = Aws::EC2::Resource.new(env[:connection_options])
13+
14+
instance = ec2.instance(env[:machine].id)
15+
instance.start
16+
17+
@app.call(env)
18+
end
19+
end
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)