Class: Ec2::Blackout::Ec2Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2-blackout/ec2_instance.rb

Constant Summary collapse

TIMESTAMP_TAG_NAME =
'ec2:blackout:on'
EIP_TAG_NAME =
'ec2:blackout:eip'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, options) ⇒ Ec2Instance

Returns a new instance of Ec2Instance.



19
20
21
22
# File 'lib/ec2-blackout/ec2_instance.rb', line 19

def initialize(instance, options)
  @instance, @options = instance, options
  @eip_retry_delay_seconds = 5
end

Instance Attribute Details

#eip_retry_delay_secondsObject

Returns the value of attribute eip_retry_delay_seconds.



17
18
19
# File 'lib/ec2-blackout/ec2_instance.rb', line 17

def eip_retry_delay_seconds
  @eip_retry_delay_seconds
end

Class Method Details

.running_instances(region, options) ⇒ Object



8
9
10
# File 'lib/ec2-blackout/ec2_instance.rb', line 8

def self.running_instances(region, options)
  instances(region, options, 'running')
end

.stopped_instances(region, options) ⇒ Object



12
13
14
# File 'lib/ec2-blackout/ec2_instance.rb', line 12

def self.stopped_instances(region, options)
  instances(region, options, 'stopped')
end

Instance Method Details

#startObject



29
30
31
32
33
# File 'lib/ec2-blackout/ec2_instance.rb', line 29

def start
  @instance.start
  associate_eip
  untag
end

#startable?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ec2-blackout/ec2_instance.rb', line 51

def startable?
  if @instance.status != :stopped
    [false, "instance is not in stopped state"]
  elsif @options.force
    true
  elsif !tags[TIMESTAMP_TAG_NAME]
    [false, "instance was not originally stopped by ec2-blackout"]
  elsif @options.matches_exclude_tags?(tags)
    [false, "matches exclude tags"]
  elsif !@options.matches_include_tags?(tags)
    [false, "does not match include tags"]
  else
    true
  end
end

#stopObject



24
25
26
27
# File 'lib/ec2-blackout/ec2_instance.rb', line 24

def stop
  tag
  @instance.stop
end

#stoppable?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ec2-blackout/ec2_instance.rb', line 35

def stoppable?
  if tags['aws:autoscaling:groupName']
    [false, "instance is part of an autoscaling group"]
  elsif @instance.status != :running
    [false, "instance is not in running state"]
  elsif @options.matches_exclude_tags?(tags)
    [false, "matches exclude tags"]
  elsif !@options.matches_include_tags?(tags)
    [false, "does not match include tags"]
  elsif @instance.root_device_type != :ebs
    [false, "is not ebs root device"]
  else
    true
  end
end

#to_sObject



67
68
69
70
71
# File 'lib/ec2-blackout/ec2_instance.rb', line 67

def to_s
  s = "instance #{@instance.id}"
  s += " (#{tags['Name']})" if tags['Name']
  s
end