Sha256: 7d211534f321f22dcba2850a4328061f63290301f4785412c50018b7e489a225

Contents?: true

Size: 1.71 KB

Versions: 18

Compression:

Stored size: 1.71 KB

Contents

require 'shell-spinner'

module Kontena
  module Machine
    module Aws
      class NodeDestroyer

        attr_reader :ec2, :api_client

        # @param [Kontena::Client] api_client Kontena api client
        # @param [String] access_key_id aws_access_key_id
        # @param [String] secret_key aws_secret_access_key
        # @param [String] region
        def initialize(api_client, access_key_id, secret_key, region = 'eu-west-1')
          @api_client = api_client
          @ec2 = ::Aws::EC2::Resource.new(
            region: region,
            credentials: ::Aws::Credentials.new(access_key_id, secret_key)
          )
        end

        def run!(grid, 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
            ShellSpinner "Terminating AWS instance #{name.colorize(:cyan)} " do
              instance.terminate
              until instance.reload.state.name.to_s == 'terminated'
                sleep 2
              end
            end
          else
            abort "Cannot find instance #{name.colorize(:cyan)} in AWS"
          end
          node = api_client.get("grids/#{grid['id']}/nodes")['nodes'].find{|n| n['name'] == name}
          if node
            ShellSpinner "Removing node #{name.colorize(:cyan)} from grid #{grid['name'].colorize(:cyan)} " do
              api_client.delete("grids/#{grid['id']}/nodes/#{name}")
            end
          end
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
kontena-plugin-aws-0.1.0 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.7 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.6 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.5 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.4 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.3 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.2 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.1 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.14.0 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.13.4 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.13.3 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.13.2 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.13.1 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.13.0.1 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.13.0 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.12.3 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.12.2 lib/kontena/machine/aws/node_destroyer.rb
kontena-cli-0.12.1 lib/kontena/machine/aws/node_destroyer.rb