Class: InstantEC2
- Inherits:
-
Object
- Object
- InstantEC2
- Defined in:
- lib/instant_ec2.rb
Instance Attribute Summary collapse
-
#images ⇒ Object
readonly
Returns the value of attribute images.
Instance Method Summary collapse
- #find_image(s) ⇒ Object
- #find_running ⇒ Object
-
#initialize(credentials: [], region: 'us-east-1') ⇒ InstantEC2
constructor
A new instance of InstantEC2.
- #ip ⇒ 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_instance(id) ⇒ Object
- #stopped? ⇒ Boolean
- #stopping ⇒ Object
Constructor Details
#initialize(credentials: [], region: 'us-east-1') ⇒ InstantEC2
Returns a new instance of InstantEC2.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/instant_ec2.rb', line 35 def initialize(credentials: [], region: 'us-east-1') @ec2 = Aws::EC2::Client.new(region: region, credentials: Aws::Credentials.new(*credentials)) r = @ec2.describe_instances.reservations image_ids = r.map{|x| x[:instances][0][:image_id] } image_names = @ec2.describe_images(image_ids: image_ids).images.\ map{|x| x.name } instance_ids = r.map{|x| x.instances[0].instance_id} @images = image_names.zip(instance_ids).inject([]) do |r, x| name, id = x r << EC2Instance.new({image_name: name, instance_id: id}, self) end @hooks = { 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
#images ⇒ Object (readonly)
Returns the value of attribute images.
33 34 35 |
# File 'lib/instant_ec2.rb', line 33 def images @images end |
Instance Method Details
#find_image(s) ⇒ Object
62 63 64 |
# File 'lib/instant_ec2.rb', line 62 def find_image(s) @images.find {|x| x[:image_name][/#{s}/i]} end |
#find_running ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/instant_ec2.rb', line 66 def find_running() r = @ec2.describe_instances.reservations.detect do |x| x.instances[0].state.name != 'stopped' end end |
#ip ⇒ Object
74 75 76 77 |
# File 'lib/instant_ec2.rb', line 74 def ip() r = self.find_running r.instances[0].public_ip_address if r end |
#on_running(&blk) ⇒ Object
79 80 81 |
# File 'lib/instant_ec2.rb', line 79 def on_running(&blk) @hooks[:running] = blk end |
#on_stopped(&blk) ⇒ Object
124 125 126 |
# File 'lib/instant_ec2.rb', line 124 def on_stopped(&blk) @hooks[:stopped] = blk end |
#running? ⇒ Boolean
83 84 85 |
# File 'lib/instant_ec2.rb', line 83 def running? self.ip ? true : false end |
#start(s, duration: nil) ⇒ Object
Launch an EC2 instance. Duration (optional) specified in minutes
89 90 91 92 93 |
# File 'lib/instant_ec2.rb', line 89 def start(s, duration: nil) self.find_image(s).start duration: duration end |
#start_instance(id) ⇒ Object
95 96 97 98 |
# File 'lib/instant_ec2.rb', line 95 def start_instance(id) @ec2.start_instances instance_ids: [id] Thread.new { trigger_on_start() } end |
#stop_instance(id) ⇒ Object
115 116 117 118 |
# File 'lib/instant_ec2.rb', line 115 def stop_instance(id) @ec2.stop_instances instance_ids: [id] trigger_on_stopping() end |
#stopped? ⇒ Boolean
120 121 122 |
# File 'lib/instant_ec2.rb', line 120 def stopped?() self.ip.nil? end |
#stopping ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/instant_ec2.rb', line 100 def stopping 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 |