Class: Provider::Ec2::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/providers/ec2/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(ec2, name, image) ⇒ Instance

Returns a new instance of Instance.



5
6
7
8
9
10
11
# File 'lib/providers/ec2/instance.rb', line 5

def initialize(ec2, name, image)
@ec2 = ec2
@name = name
  @instance = instance
    @ami_name = image || name
    @logger = Veronic::Deployer.new().logger
end

Instance Method Details

#create_imageObject



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_nameObject



59
60
61
# File 'lib/providers/ec2/instance.rb', line 59

def dns_name
  @instance.dns_name
end

#exists?Boolean

Returns:



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.tags['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_instanceObject



75
76
77
78
79
# File 'lib/providers/ec2/instance.rb', line 75

def get_instance
  AWS.memoize do
    @ec2.instances.select {|x| x.tags['Name'] == @name && x.status != :shutting_down && x.status != :terminated}.first
  end
end

#idObject



67
68
69
# File 'lib/providers/ec2/instance.rb', line 67

def id
  @instance.id
end

#instanceObject



71
72
73
# File 'lib/providers/ec2/instance.rb', line 71

def instance
  @instance ||= get_instance
end

#public_ip_addressObject



63
64
65
# File 'lib/providers/ec2/instance.rb', line 63

def public_ip_address
  @instance.public_ip_address
end

#startObject



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

#statusObject



55
56
57
# File 'lib/providers/ec2/instance.rb', line 55

def status
  @instance.status if @instance
end

#stopObject



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 tags(hash={})
  @logger.info "Tagging instance ..."
  hash.keys.each do |k|
    @logger.info k + ': ' + hash[k]
    @instance.tags[k] = hash[k]
  end
end