Skip to content

Commit 4c4631d

Browse files
committed
Add suspend action
1 parent 1fb30d3 commit 4c4631d

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/vagrant-ec2/actions.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require_relative 'actions/find_host'
55
require_relative 'actions/run_instance'
66
require_relative 'actions/start_instance'
7+
require_relative 'actions/stop_instance'
78
require_relative 'actions/terminate_instance'
89
require_relative 'actions/wait_for_state'
910

@@ -55,6 +56,26 @@ def self.resume
5556
when :running
5657
env[:ui].info I18n.t('vagrant_ec2.info.state.already_running')
5758
next
59+
end
60+
end
61+
end
62+
end
63+
64+
def self.suspend
65+
return Vagrant::Action::Builder.new.tap do |builder|
66+
builder.use ConfigValidate
67+
builder.use ConnectAWS
68+
builder.use Call, CheckState do |env, b|
69+
case env[:result]
70+
when :running
71+
b.use StopInstance
72+
b.use WaitForState, :stopped
73+
when :stopped
74+
env[:ui].info I18n.t('vagrant_ec2.info.state.already_stopped')
75+
next
76+
when :not_created
77+
env[:ui].info I18n.t('vagrant_ec2.info.state.not_created')
78+
next
5879
else
5980
env[:ui].info I18n.t('vagrant_ec2.info.state.unexpected_state', :state => env[:result])
6081
next
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'aws-sdk'
2+
3+
module VagrantPlugins
4+
module Ec2
5+
module Actions
6+
class StopInstance
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.stop
16+
env[:ui].info I18n.t('vagrant_ec2.info.action.stop_instance', :instance_id => env[:machine].id)
17+
18+
@app.call(env)
19+
end
20+
end
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)