Class: Provider::Ec2::Instance
- Inherits:
-
Object
- Object
- Provider::Ec2::Instance
- Defined in:
- lib/providers/ec2/instance.rb
Instance Method Summary collapse
- #create_image ⇒ Object
- #dns_name ⇒ Object
- #exists? ⇒ Boolean
- #get_instance ⇒ Object
- #id ⇒ Object
-
#initialize(ec2, name, image) ⇒ Instance
constructor
A new instance of Instance.
- #instance ⇒ Object
- #public_ip_address ⇒ Object
- #start ⇒ Object
- #status ⇒ Object
- #stop ⇒ Object
- #tags(hash = {}) ⇒ Object
Constructor Details
Instance Method Details
#create_image ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/providers/ec2/instance.rb', line 81 def create_image @logger.info "Create image #{@ami_name}" new_image = @instance.create_image(@ami_name, { :no_reboot => true }) while new_image.exists? == false && new_image.state != :failed @logger.info "." sleep 1 end while new_image.state == :pending && new_image.state != :failed @logger.info "." sleep 1 end @logger.info "" return new_image end |
#dns_name ⇒ Object
59 60 61 |
# File 'lib/providers/ec2/instance.rb', line 59 def dns_name @instance.dns_name end |
#exists? ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/providers/ec2/instance.rb', line 44 def exists? @logger.info "Checking for ec2 server #{@name} ..." if AWS.memoize do @ec2.instances.any? {|x| x.['Name'] == @name && x.status != :shutting_down && x.status != :terminated} end @logger.info "Instance #{@name} found" return true else @logger.info "Instance #{@name} is misssing" return false end end |
#get_instance ⇒ Object
75 76 77 78 79 |
# File 'lib/providers/ec2/instance.rb', line 75 def get_instance AWS.memoize do @ec2.instances.select {|x| x.['Name'] == @name && x.status != :shutting_down && x.status != :terminated}.first end end |
#id ⇒ Object
67 68 69 |
# File 'lib/providers/ec2/instance.rb', line 67 def id @instance.id end |
#instance ⇒ Object
71 72 73 |
# File 'lib/providers/ec2/instance.rb', line 71 def instance @instance ||= get_instance end |
#public_ip_address ⇒ Object
63 64 65 |
# File 'lib/providers/ec2/instance.rb', line 63 def public_ip_address @instance.public_ip_address end |
#start ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/providers/ec2/instance.rb', line 27 def start @logger.info "Starting instance #{@name}..." if self.exists? while self.status == :stopping sleep 2 end @instance.start i = 0 while self.status != :running @logger.info "." ; sleep 3 ; i += 1 return false if i > 120 end @logger.info "\nInstance #{@name} is started" end return true end |
#status ⇒ Object
55 56 57 |
# File 'lib/providers/ec2/instance.rb', line 55 def status @instance.status if @instance end |
#stop ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/providers/ec2/instance.rb', line 13 def stop @logger.info "Stopping instance #{@name}..." if self.exists? @instance.stop i = 0 while self.status != :stopped @logger.info "." ; sleep 3 ; i += 1 return false if i > 120 end @logger.info "\nInstance #{@name} is stopped" end return true end |
#tags(hash = {}) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/providers/ec2/instance.rb', line 96 def (hash={}) @logger.info "Tagging instance ..." hash.keys.each do |k| @logger.info k + ': ' + hash[k] @instance.[k] = hash[k] end end |