Class: InstantEC2
- Inherits:
-
Object
- Object
- InstantEC2
- Defined in:
- lib/instant_ec2.rb
Instance Attribute Summary collapse
-
#async ⇒ Object
readonly
Returns the value of attribute async.
-
#images ⇒ Object
readonly
Returns the value of attribute images.
Instance Method Summary collapse
- #find_image(s) ⇒ Object
- #find_pending ⇒ Object
- #find_running ⇒ Object
-
#initialize(credentials: [], region: 'us-east-1', async: true) ⇒ InstantEC2
constructor
A new instance of InstantEC2.
- #ip ⇒ Object
- #on_pending(&blk) ⇒ Object
- #on_running(&blk) ⇒ Object
- #on_stopped(&blk) ⇒ Object
- #running? ⇒ Boolean
-
#start(s, duration: nil) ⇒ Object
Launch an EC2 instance.
- #start_instance(id) ⇒ Object
- #stop ⇒ Object
- #stop_instance(id) ⇒ Object
- #stopped? ⇒ Boolean
Constructor Details
#initialize(credentials: [], region: 'us-east-1', async: true) ⇒ InstantEC2
Returns a new instance of InstantEC2.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/instant_ec2.rb', line 46 def initialize(credentials: [], region: 'us-east-1', async: true) @async = async @ec2 = Aws::EC2::Client.new(region: region, credentials: Aws::Credentials.new(*credentials)) r = @ec2.describe_instances.reservations ids = @ec2.describe_instances[:reservations].inject({}) do |r, item| x = item.instances[0] r.merge(x.image_id => x.instance_id) end rows = @ec2.describe_images(image_ids: ids.keys)[:images].\ map{|x| [x.name, x.image_id] } @images = rows.inject([]) do |r, x| name, image_id = x r << EC2Instance.new({image_name: name, \ instance_id: ids[image_id]}, self) end @hooks = { pending: ->(){ puts "%s: the instance is now pending" % [Time.now] }, running: ->(ip){ puts "%s: the instance is now accessible from %s" % [Time.now, ip] }, stopping: ->(){ puts "%s: the instance is now stopping" % Time.now} } end |
Instance Attribute Details
#async ⇒ Object (readonly)
Returns the value of attribute async.
44 45 46 |
# File 'lib/instant_ec2.rb', line 44 def async @async end |
#images ⇒ Object (readonly)
Returns the value of attribute images.
44 45 46 |
# File 'lib/instant_ec2.rb', line 44 def images @images end |
Instance Method Details
#find_image(s) ⇒ Object
84 85 86 |
# File 'lib/instant_ec2.rb', line 84 def find_image(s) @images.find {|x| x[:image_name][/#{s}/i]} end |
#find_pending ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/instant_ec2.rb', line 88 def find_pending() r = @ec2.describe_instances.reservations.detect do |x| x.instances[0].state.name == 'pending' end end |
#find_running ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/instant_ec2.rb', line 96 def find_running() r = @ec2.describe_instances.reservations.detect do |x| x.instances[0].state.name == 'running' end end |
#ip ⇒ Object
104 105 106 107 |
# File 'lib/instant_ec2.rb', line 104 def ip() r = self.find_running r.instances[0].public_ip_address if r end |
#on_pending(&blk) ⇒ Object
109 110 111 |
# File 'lib/instant_ec2.rb', line 109 def on_pending(&blk) @hooks[:pending]= blk end |
#on_running(&blk) ⇒ Object
113 114 115 |
# File 'lib/instant_ec2.rb', line 113 def on_running(&blk) @hooks[:running] = blk end |
#on_stopped(&blk) ⇒ Object
117 118 119 |
# File 'lib/instant_ec2.rb', line 117 def on_stopped(&blk) @hooks[:stopped] = blk end |
#running? ⇒ Boolean
121 122 123 |
# File 'lib/instant_ec2.rb', line 121 def running? self.ip ? true : false end |
#start(s, duration: nil) ⇒ Object
Launch an EC2 instance. Duration (optional) specified in minutes
127 128 129 130 131 132 133 134 |
# File 'lib/instant_ec2.rb', line 127 def start(s, duration: nil) found = self.find_image(s) return unless found found.start duration: duration end |
#start_instance(id) ⇒ Object
136 137 138 139 |
# File 'lib/instant_ec2.rb', line 136 def start_instance(id) @ec2.start_instances instance_ids: [id] @async ? Thread.new { trigger_on_start() } : trigger_on_start() end |
#stop ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/instant_ec2.rb', line 141 def stop() r = self.find_running() if r then puts 'stopping ...' self.stop_instance r.instances[0].instance_id else puts 'no instances to stop' end end |
#stop_instance(id) ⇒ Object
156 157 158 159 |
# File 'lib/instant_ec2.rb', line 156 def stop_instance(id) @ec2.stop_instances instance_ids: [id] @async ? Thread.new { trigger_on_stopping() } : trigger_on_stopping() end |
#stopped? ⇒ Boolean
161 162 163 |
# File 'lib/instant_ec2.rb', line 161 def stopped?() self.ip.nil? end |