Class: CfnManage::StartStopHandler::Ec2

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_manage/handlers/ec2.rb

Instance Method Summary collapse

Constructor Details

#initialize(instance_id, options = {}) ⇒ Ec2

Returns a new instance of Ec2.



8
9
10
11
12
13
# File 'lib/cfn_manage/handlers/ec2.rb', line 8

def initialize(instance_id, options = {})
  credentials = CfnManage::AWSCredentials.get_session_credentials("stoprun_#{instance_id}")
  ec2_client = Aws::EC2::Client.new(credentials: credentials, retry_limit: 20)
  @instance = Aws::EC2::Resource.new(client: ec2_client, retry_limit: 20).instance(instance_id)
  @instance_id = instance_id
end

Instance Method Details

#start(configuration) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/cfn_manage/handlers/ec2.rb', line 15

def start(configuration)
  if %w(running).include?(@instance.state.name)
    $log.info("Instance #{@instance_id} already running")
    return
  end
  $log.info("Starting instance #{@instance_id}")
  @instance.start()
end

#stopObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cfn_manage/handlers/ec2.rb', line 24

def stop
  if %w(stopped stopping).include?(@instance.state.name)
    $log.info("Instance #{@instance_id} already stopping or stopped")
    return
  end
  $log.info("Stopping instance #{@instance_id}")
  @instance.stop()

  # empty configuration for ec2 instances
  return {}
end

#wait(wait_states = []) ⇒ Object



36
37
38
# File 'lib/cfn_manage/handlers/ec2.rb', line 36

def wait(wait_states=[])
  $log.debug("Not waiting for EC2 instance #{@instance_id}")
end