Class: Kontena::Machine::Aws::NodeRestarter

Inherits:
Object
  • Object
show all
Includes:
Cli::ShellSpinner, Common
Defined in:
lib/kontena/machine/aws/node_restarter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#default_subnet, #default_vpc, #resolve_ami, #resolve_security_groups_to_ids

Constructor Details

#initialize(access_key_id, secret_key, region) ⇒ NodeRestarter

Returns a new instance of NodeRestarter.

Parameters:

  • access_key_id (String)

    aws_access_key_id

  • secret_key (String)

    aws_secret_access_key

  • region (String)


13
14
15
16
17
# File 'lib/kontena/machine/aws/node_restarter.rb', line 13

def initialize(access_key_id, secret_key, region)
  @ec2 = ::Aws::EC2::Resource.new(
    region: region, credentials: ::Aws::Credentials.new(access_key_id, secret_key)
  )
end

Instance Attribute Details

#api_clientObject (readonly)

Returns the value of attribute api_client.



8
9
10
# File 'lib/kontena/machine/aws/node_restarter.rb', line 8

def api_client
  @api_client
end

#ec2Object (readonly)

Returns the value of attribute ec2.



8
9
10
# File 'lib/kontena/machine/aws/node_restarter.rb', line 8

def ec2
  @ec2
end

Instance Method Details

#run!(name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kontena/machine/aws/node_restarter.rb', line 19

def run!(name)
  instances = ec2.instances({
    filters: [
      {name: 'tag:Name', values: [name]}
    ]
  })
  abort("Cannot find AWS instance #{name}") if instances.to_a.size == 0
  abort("There are multiple instances with name #{name}") if instances.to_a.size > 1
  instance = instances.first
  if instance
    spinner "Restarting AWS instance #{name.colorize(:cyan)} " do
      instance.reboot(dry_run: false)
    end
  else
    abort "Cannot find instance #{name.colorize(:cyan)} in AWS"
  end
end