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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73d849bb6b5614cceddcdb42507e205d62db20c2
4
- data.tar.gz: 310876d9dbe8088f169329d3e6dd9bcefa8212f4
3
+ metadata.gz: 416943632a687083890012e6991a51547c99a6be
4
+ data.tar.gz: 81ccf2521db6f8737edf9a6d037cf3e30a5e08cc
5
5
  SHA512:
6
- metadata.gz: a932aee73fb96aee99650408738b6a5506292fab620738e5973fdc21516e443c2c740d6adbe9a9e7162cbb7afdb871b643eadcd89c6f158a8c4e316621956ee7
7
- data.tar.gz: fd2d533ecf671566de3eef0428e244a94fa431d145fa46e68d11c715e95a15f36cc42696bb2cd2ebfa342f0665b0bd5e0cb8b3ef3a10d9bdfe510f9c987467b5
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, ec2=nil)
10
+ def initialize(h, caller=nil)
11
11
 
12
- @ec2 = ec2
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
- @ec2.start_instances instance_ids: [self[:instance_id]]
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}, @ec2)
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
- # timeout after 30 seconds
68
- t1 = Time.now
69
- sleep 1; ip = self.ip until ip or Time.now > t1 + 30
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
- if ip then
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 start(s)
82
- self.find_image(s).start
95
+ def start_instance(id)
96
+ @ec2.start_instances instance_ids: [id]
97
+ Thread.new { trigger_on_start() }
83
98
  end
84
-
85
- def stop
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
- instance_id = r.instances[0].instance_id
93
- @ec2.stop_instances instance_ids: [instance_id]
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 on_stopping()
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
- yield
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
@@ -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.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file