instant_ec2 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/instant_ec2.rb +70 -26
- data.tar.gz.sig +0 -0
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 416943632a687083890012e6991a51547c99a6be
|
4
|
+
data.tar.gz: 81ccf2521db6f8737edf9a6d037cf3e30a5e08cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d267345a6f3824e319ca91b894e8235aa5cbf0e41c9ee0df1eaff6c8c4dbfb5af22cf6996ea4816420fad7389bb9a340bd8293591642f101316390ef24ed304
|
7
|
+
data.tar.gz: 2cef48bad768c185593aa7b366c709b23270f0c1ba8de0a350bfe974cf116c82f5e0a288e8081eda39631d79fd3d6609d3f4e5af6e624057b006b93fb50633f3
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/instant_ec2.rb
CHANGED
@@ -7,18 +7,25 @@ require 'aws-sdk'
|
|
7
7
|
|
8
8
|
class EC2Instance < Hash
|
9
9
|
|
10
|
-
def initialize(h,
|
10
|
+
def initialize(h, caller=nil)
|
11
11
|
|
12
|
-
@
|
12
|
+
@c = caller
|
13
13
|
super().merge!(h)
|
14
14
|
|
15
15
|
end
|
16
16
|
|
17
|
-
def start()
|
17
|
+
def start(duration: nil)
|
18
18
|
|
19
|
-
@
|
19
|
+
@c.start_instance self[:instance_id]
|
20
|
+
Thread.new{ sleep duration * 60; self.stop} if duration
|
20
21
|
|
21
22
|
end
|
23
|
+
|
24
|
+
def stop()
|
25
|
+
|
26
|
+
@c.stop_instance self[:instance_id]
|
27
|
+
|
28
|
+
end
|
22
29
|
end
|
23
30
|
|
24
31
|
class InstantEC2
|
@@ -39,9 +46,16 @@ class InstantEC2
|
|
39
46
|
@images = image_names.zip(instance_ids).inject([]) do |r, x|
|
40
47
|
|
41
48
|
name, id = x
|
42
|
-
r << EC2Instance.new({image_name: name, instance_id: id},
|
49
|
+
r << EC2Instance.new({image_name: name, instance_id: id}, self)
|
43
50
|
|
44
51
|
end
|
52
|
+
|
53
|
+
@hooks = {
|
54
|
+
running: ->(ip){
|
55
|
+
puts "%s: the instance is now accessible from %s" % [Time.now, ip]
|
56
|
+
},
|
57
|
+
stopping: ->(){ puts "%s: the instance is now stopping" % Time.now}
|
58
|
+
}
|
45
59
|
|
46
60
|
end
|
47
61
|
|
@@ -62,52 +76,82 @@ class InstantEC2
|
|
62
76
|
r.instances[0].public_ip_address if r
|
63
77
|
end
|
64
78
|
|
65
|
-
def on_running()
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
79
|
+
def on_running(&blk)
|
80
|
+
@hooks[:running] = blk
|
81
|
+
end
|
82
|
+
|
83
|
+
def running?
|
84
|
+
self.ip ? true : false
|
85
|
+
end
|
86
|
+
|
87
|
+
# Launch an EC2 instance. Duration (optional) specified in minutes
|
88
|
+
#
|
89
|
+
def start(s, duration: nil)
|
70
90
|
|
71
|
-
|
72
|
-
yield(ip)
|
73
|
-
else
|
74
|
-
puts 'on_running timedout'
|
75
|
-
end
|
91
|
+
self.find_image(s).start duration: duration
|
76
92
|
|
77
93
|
end
|
78
|
-
|
79
|
-
alias running find_running
|
80
94
|
|
81
|
-
def
|
82
|
-
|
95
|
+
def start_instance(id)
|
96
|
+
@ec2.start_instances instance_ids: [id]
|
97
|
+
Thread.new { trigger_on_start() }
|
83
98
|
end
|
84
|
-
|
85
|
-
def
|
99
|
+
|
100
|
+
def stopping
|
86
101
|
|
87
102
|
r = self.find_running()
|
88
103
|
|
89
104
|
if r then
|
90
105
|
|
91
106
|
puts 'stopping ...'
|
92
|
-
|
93
|
-
|
107
|
+
|
108
|
+
self.stop_instance r.instances[0].instance_id
|
94
109
|
|
95
110
|
else
|
96
111
|
puts 'no instances to stop'
|
97
112
|
end
|
98
113
|
end
|
99
114
|
|
100
|
-
def
|
115
|
+
def stop_instance(id)
|
116
|
+
@ec2.stop_instances instance_ids: [id]
|
117
|
+
trigger_on_stopping()
|
118
|
+
end
|
119
|
+
|
120
|
+
def stopped?()
|
121
|
+
self.ip.nil?
|
122
|
+
end
|
123
|
+
|
124
|
+
def on_stopped(&blk)
|
125
|
+
@hooks[:stopped] = blk
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def trigger_on_start()
|
131
|
+
|
132
|
+
# timeout after 60 seconds
|
133
|
+
t1 = Time.now
|
134
|
+
sleep 1; ip = self.ip until ip or Time.now > t1 + 60
|
135
|
+
|
136
|
+
if ip then
|
137
|
+
@hooks[:running].call(ip)
|
138
|
+
else
|
139
|
+
puts 'on_running timedout'
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
def trigger_on_stopping()
|
101
145
|
|
102
146
|
# timeout after 30 seconds
|
103
147
|
t1 = Time.now
|
104
148
|
sleep 1; ip = self.ip until ip.nil? or Time.now > t1 + 30
|
105
149
|
|
106
150
|
if ip.nil? then
|
107
|
-
|
151
|
+
@hooks[:stopping].call()
|
108
152
|
else
|
109
153
|
puts 'on_stopping timedout'
|
110
154
|
end
|
111
155
|
|
112
156
|
end
|
113
|
-
end
|
157
|
+
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|