Class: InstantEC2

Inherits:
Object
  • Object
show all
Defined in:
lib/instant_ec2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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
61
62
63
64
65
66
# 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
  
  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 = {
    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

#imagesObject (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



68
69
70
# File 'lib/instant_ec2.rb', line 68

def find_image(s)
  @images.find {|x| x[:image_name][/#{s}/i]}
end

#find_runningObject



72
73
74
75
76
77
78
# File 'lib/instant_ec2.rb', line 72

def find_running()

  r = @ec2.describe_instances.reservations.detect do |x|
    x.instances[0].state.name != 'stopped'
  end

end

#ipObject



80
81
82
83
# File 'lib/instant_ec2.rb', line 80

def ip()
  r = self.find_running
  r.instances[0].public_ip_address if r
end

#on_running(&blk) ⇒ Object



85
86
87
# File 'lib/instant_ec2.rb', line 85

def on_running(&blk)
  @hooks[:running] = blk
end

#on_stopped(&blk) ⇒ Object



130
131
132
# File 'lib/instant_ec2.rb', line 130

def on_stopped(&blk)
  @hooks[:stopped] = blk
end

#running?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/instant_ec2.rb', line 89

def running?
  self.ip ? true : false
end

#start(s, duration: nil) ⇒ Object

Launch an EC2 instance. Duration (optional) specified in minutes



95
96
97
98
99
# File 'lib/instant_ec2.rb', line 95

def start(s, duration: nil)
  
  self.find_image(s).start duration: duration
  
end

#start_instance(id) ⇒ Object



101
102
103
104
# File 'lib/instant_ec2.rb', line 101

def start_instance(id)
  @ec2.start_instances instance_ids: [id]
  Thread.new { trigger_on_start() }
end

#stopObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/instant_ec2.rb', line 106

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



121
122
123
124
# File 'lib/instant_ec2.rb', line 121

def stop_instance(id)
  @ec2.stop_instances instance_ids: [id]
  trigger_on_stopping()    
end

#stopped?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/instant_ec2.rb', line 126

def stopped?()
  self.ip.nil?
end