instant_ec2 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42cfc79bfd329aa789ce68b9d7b955699c9eebfb
4
- data.tar.gz: d9af3f8b46f88b3b95f149fa48ac3bb337b6c2da
3
+ metadata.gz: df4c6b926f369e4b1cb6cdbad52818d61781ce71
4
+ data.tar.gz: 41a8663e649b6fe159814f5b0891cd1f8e2fd1c0
5
5
  SHA512:
6
- metadata.gz: 1b2b7d7edeaf36e7378a0215bd999fa7ee97227765f4f2c822a927decb164a6e6dc6fd1783d05d46b71d3420548ed417f6ee4d0765bd96304ed52ec8b7ca54e0
7
- data.tar.gz: d9526ba7df9a67fe8873c9c520c557c041fd3b70bcd56f6f3d7db39b7d492c591ed3dc046e37be737d9c6ace8ae873e66e88d50242b47be5c45ed6db4fd2f379
6
+ metadata.gz: f2a029b6746f3e63e7b48cde244ce7b122ce96a9accf89904278da34b53fc7f808842125173342d189872008d7d6d689254a12770eb1cc983426db2f907391e4
7
+ data.tar.gz: dbbb8484a8ccc37817376d48fae13ea261b7f0828bd93254c8f7e227505c810dffbd22b54648e1a152fe483ded1931c4aefa4850a19cf158bcbe211448b03259
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/instant_ec2.rb CHANGED
@@ -16,8 +16,19 @@ class EC2Instance < Hash
16
16
 
17
17
  def start(duration: nil)
18
18
 
19
- @c.start_instance self[:instance_id]
20
- Thread.new{ sleep duration.to_i * 60; self.stop} if duration
19
+ @c.start_instance self[:instance_id]
20
+
21
+ if duration then
22
+
23
+ seconds = duration.to_i * 60
24
+
25
+ if @c.async then
26
+ Thread.new{ sleep seconds; self.stop}
27
+ else
28
+ sleep seconds
29
+ self.stop
30
+ end
31
+ end
21
32
 
22
33
  end
23
34
 
@@ -30,9 +41,11 @@ end
30
41
 
31
42
  class InstantEC2
32
43
 
33
- attr_reader :images
44
+ attr_reader :images, :async
34
45
 
35
- def initialize(credentials: [], region: 'us-east-1')
46
+ def initialize(credentials: [], region: 'us-east-1', async: true)
47
+
48
+ @async = async
36
49
 
37
50
  @ec2 = Aws::EC2::Client.new(region: region,
38
51
  credentials: Aws::Credentials.new(*credentials))
@@ -57,6 +70,9 @@ class InstantEC2
57
70
  end
58
71
 
59
72
  @hooks = {
73
+ pending: ->(){
74
+ puts "%s: the instance is now pending" % [Time.now]
75
+ },
60
76
  running: ->(ip){
61
77
  puts "%s: the instance is now accessible from %s" % [Time.now, ip]
62
78
  },
@@ -68,11 +84,19 @@ class InstantEC2
68
84
  def find_image(s)
69
85
  @images.find {|x| x[:image_name][/#{s}/i]}
70
86
  end
87
+
88
+ def find_pending()
89
+
90
+ r = @ec2.describe_instances.reservations.detect do |x|
91
+ x.instances[0].state.name == 'pending'
92
+ end
93
+
94
+ end
71
95
 
72
96
  def find_running()
73
97
 
74
98
  r = @ec2.describe_instances.reservations.detect do |x|
75
- x.instances[0].state.name != 'stopped'
99
+ x.instances[0].state.name == 'running'
76
100
  end
77
101
 
78
102
  end
@@ -81,10 +105,18 @@ class InstantEC2
81
105
  r = self.find_running
82
106
  r.instances[0].public_ip_address if r
83
107
  end
108
+
109
+ def on_pending(&blk)
110
+ @hooks[:pending]= blk
111
+ end
84
112
 
85
113
  def on_running(&blk)
86
114
  @hooks[:running] = blk
87
115
  end
116
+
117
+ def on_stopped(&blk)
118
+ @hooks[:stopped] = blk
119
+ end
88
120
 
89
121
  def running?
90
122
  self.ip ? true : false
@@ -100,7 +132,7 @@ class InstantEC2
100
132
 
101
133
  def start_instance(id)
102
134
  @ec2.start_instances instance_ids: [id]
103
- Thread.new { trigger_on_start() }
135
+ @async ? Thread.new { trigger_on_start() } : trigger_on_start()
104
136
  end
105
137
 
106
138
  def stop()
@@ -120,16 +152,13 @@ class InstantEC2
120
152
 
121
153
  def stop_instance(id)
122
154
  @ec2.stop_instances instance_ids: [id]
123
- trigger_on_stopping()
155
+ @async ? Thread.new { trigger_on_stopping() } : trigger_on_stopping()
124
156
  end
125
157
 
126
158
  def stopped?()
127
159
  self.ip.nil?
128
160
  end
129
161
 
130
- def on_stopped(&blk)
131
- @hooks[:stopped] = blk
132
- end
133
162
 
134
163
  private
135
164
 
@@ -137,7 +166,23 @@ class InstantEC2
137
166
 
138
167
  # timeout after 60 seconds
139
168
  t1 = Time.now
140
- sleep 1; ip = self.ip until ip or Time.now > t1 + 60
169
+ sleep 10
170
+ sleep 2; pending = self.find_pending() until pending or Time.now > t1 + 60
171
+
172
+ if pending then
173
+ @hooks[:pending].call()
174
+ trigger_on_running()
175
+ else
176
+ puts 'on_running timedout'
177
+ end
178
+
179
+ end
180
+
181
+ def trigger_on_running()
182
+
183
+ # timeout after 30 seconds
184
+ t1 = Time.now
185
+ sleep 1; ip = self.ip until ip or Time.now > t1 + 30
141
186
 
142
187
  if ip then
143
188
  @hooks[:running].call(ip)
@@ -145,13 +190,14 @@ class InstantEC2
145
190
  puts 'on_running timedout'
146
191
  end
147
192
 
148
- end
193
+ end
149
194
 
150
195
  def trigger_on_stopping()
151
196
 
152
197
  # timeout after 30 seconds
153
198
  t1 = Time.now
154
- sleep 1; ip = self.ip until ip.nil? or Time.now > t1 + 30
199
+ sleep 7
200
+ sleep 2; ip = self.ip until ip.nil? or Time.now > t1 + 30
155
201
 
156
202
  if ip.nil? then
157
203
  @hooks[:stopping].call()
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: instant_ec2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  I2p/gjcBfDCdq3/4DlrprfJdiPD30+XqKCWbt0NOod/6Ky8nvmilmfrciMvsgp+g
32
32
  bO5j5Um4EdA15w==
33
33
  -----END CERTIFICATE-----
34
- date: 2015-12-20 00:00:00.000000000 Z
34
+ date: 2015-12-21 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: aws-sdk
metadata.gz.sig CHANGED
Binary file