Class: Asg::Rebooter::Wrappers::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/asg/rebooter/wrappers/instance.rb

Constant Summary collapse

LIFECYCLE_STANDBY =
'Standby'
LIFECYCLE_IN_SERVICE =
'InService'
LIFECYCLE_CHANGE_POLL_TIMEOUT =
4
HEALTH_STATUS_HEALTHY =
'HEALTHY'
SLEEP_AFTER_REBOOT =
30
SLEEP_AFTER_STANDBY =
10
POLL_TIMEOUT =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Instance

Returns a new instance of Instance.



37
38
39
40
41
42
43
44
# File 'lib/asg/rebooter/wrappers/instance.rb', line 37

def initialize(instance)
  #
  # @instance is of type Aws::AutoScaling::Instance
  #
  # #ec2_instance is of type Aws::EC2::Instance
  #
  @instance = instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



46
47
48
# File 'lib/asg/rebooter/wrappers/instance.rb', line 46

def method_missing(m, *args, &block)
  instance.send(m, *args, &block)
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



35
36
37
# File 'lib/asg/rebooter/wrappers/instance.rb', line 35

def instance
  @instance
end

Instance Method Details

#ensure_lifecycle_state(state) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/asg/rebooter/wrappers/instance.rb', line 66

def ensure_lifecycle_state(state)
  loop do
    instance.reload

    break if instance.lifecycle_state == state

    sleep LIFECYCLE_CHANGE_POLL_TIMEOUT
  end

  true
end

#enter_standbyObject



50
51
52
53
54
55
56
# File 'lib/asg/rebooter/wrappers/instance.rb', line 50

def enter_standby
  # raise when in invalid state for action

  instance.enter_standby(should_decrement_desired_capacity: true)

  ensure_lifecycle_state(LIFECYCLE_STANDBY)
end

#exit_standbyObject



58
59
60
61
62
63
64
# File 'lib/asg/rebooter/wrappers/instance.rb', line 58

def exit_standby
  # raise if in invalid state for action

  instance.exit_standby

  ensure_lifecycle_state(LIFECYCLE_IN_SERVICE)
end

#health_statusObject



84
85
86
# File 'lib/asg/rebooter/wrappers/instance.rb', line 84

def health_status
  instance.reload.health_status
end

#healthy?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/asg/rebooter/wrappers/instance.rb', line 88

def healthy?
  health_status == HEALTH_STATUS_HEALTHY
end

#rebootObject



105
106
107
# File 'lib/asg/rebooter/wrappers/instance.rb', line 105

def reboot
  ec2_instance.reboot
end

#standby?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/asg/rebooter/wrappers/instance.rb', line 78

def standby?
  instance.reload

  instance.lifecycle_state == LIFECYCLE_STANDBY
end

#wait_for_healthyObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/asg/rebooter/wrappers/instance.rb', line 92

def wait_for_healthy
  #
  # TODO: Should check health state on the TargetGroup to ensure the instance is ready for work
  #
  loop do
    break if healthy?

    sleep(POLL_TIMEOUT)
  end

  true
end