module Kontena::Plugin::Aws::Prompts::Common::Defaults

Constants

CREATE_KEYPAIR_TEXT
DEFAULT_INSTANCE_TYPE
DEFAULT_REGION
DEFAULT_SSH_KEY_PATH
DEFAULT_STORAGE
DummyPair

Public Instance Methods

default_access_key() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 30
def default_access_key
  prompt.mask('AWS access key:')
end
default_key_pair() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 41
def default_key_pair
  key_pairs = aws_client.describe_key_pairs.key_pairs
  if key_pairs.empty?
    import_key_pair
  else
    key_pairs << DummyPair.new(CREATE_KEYPAIR_TEXT)
    answer = prompt.select("Choose EC2 key pair:") do |menu|
      key_pairs.each do |key_pair|
        menu.choice key_pair.key_name, key_pair.key_name
      end
    end
    answer == CREATE_KEYPAIR_TEXT ? import_key_pair : answer
  end
end
default_region() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 79
def default_region
  prompt.select("Choose EC2 region:") do |menu|
    i = 1
    default_region_aws_client.describe_regions.regions.sort_by(&:region_name).each do |region|
      menu.choice region.region_name, region.region_name
      menu.default(i) if region.region_name == DEFAULT_REGION
      i += 1
    end
  end
end
default_region_aws_client() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 75
def default_region_aws_client
  @default_region_aws_client ||= Aws::EC2::Client.new(access_key_id: access_key, secret_access_key: secret_key, region: DEFAULT_REGION)
end
default_secret_key() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 34
def default_secret_key
  prompt.mask('AWS secret key:')
end
default_storage() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 125
def default_storage
  prompt.ask('Storage size (GiB):', default: DEFAULT_STORAGE)
end
default_subnet_id() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 129
def default_subnet_id
  filters = [
    { name: "vpc-id", values: [vpc_id] },
    { name: "availability-zone", values: [region + zone] }
  ]
  subnets_result = aws_client.describe_subnets(filters: filters)
  subnets = subnets_result.subnets.sort_by(&:cidr_block)
  exit_with_error "Failed to find any subnets" if subnets.empty?
  if subnets.size == 1 && subnets.first.state == "available"
    puts "Using Subnet ID #{pastel.cyan(subnets.first.subnet_id)}"
    subnets.first.subnet_id
  else
    prompt.select("Specify subnet to launch instance into:") do |menu|
      subnets.each do |subnet|
        if subnet.state == 'available'
          menu.choice "#{subnet.subnet_id} (#{subnet.cidr_block})", subnet.subnet_id
        end
      end
    end
  end
end
default_type() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 100
def default_type
  prompt.ask('Instance type:', default: DEFAULT_INSTANCE_TYPE)
end
default_vpc_id() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 104
def default_vpc_id
  vpcs = aws_client.describe_vpcs.vpcs
  exit_with_error("Could not find any Virtual Private Cloud (VPC). Please create one in the AWS console first.") if vpcs.size.zero?
  if vpcs.size == 1 && vpcs.first.state == "available"
    puts "Using VPC ID #{pastel.cyan(vpcs.first.vpc_id)}"
    vpcs.first.vpc_id
  else
    prompt.select("Choose Virtual Private Cloud (VPC) ID:") do |menu|
      vpcs.each do |vpc|
        if vpc.state == 'available'
          name = vpc.vpc_id
          name += ' (default)' if vpc.is_default
          menu.choice name, vpc.vpc_id
        end
      end
    end
  end
end
default_zone() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 90
def default_zone
  prompt.select("Choose EC2 Availability Zone:") do |menu|
    aws_client.describe_availability_zones.availability_zones.sort_by(&:zone_name).select { |z| z.state == 'available' }.each do |zone|
      menu.choice zone.zone_name, zone.zone_name.sub(zone.region_name, '')
    end
  end
end
import_key_pair() click to toggle source
# File lib/kontena/plugin/aws/prompts.rb, line 58
def import_key_pair
  if ssh_public_key
    public_key = File.read(ssh_public_key)
  else
    public_key = prompt.ask('SSH public key: (enter a ssh key in OpenSSH format "ssh-xxx xxxxx key_name")', default: File.exist?(DEFAULT_SSH_KEY_PATH) ? File.read(DEFAULT_SSH_KEY_PATH).strip : '') do |q|
      q.validate /^ssh-rsa \S+ \S+$/
    end
    key_name = public_key[/\A\S+\s+\S+\s+(\S+)\z/, 1]
  end

  prompt.yes?("Import public key '#{key_name}' to AWS?") || exit_with_error('Aborted')
  pair = Kontena::Machine::Aws::KeypairProvisioner.new(access_key, secret_key, region).run!(public_key: public_key, keypair_name: key_name)
  pair.name
end