Module: Kontena::Plugin::Aws::Prompts::Common::Defaults
- Included in:
- Master::TerminateCommand, Nodes::RestartCommand, Nodes::TerminateCommand
- Defined in:
- lib/kontena/plugin/aws/prompts.rb
Defined Under Namespace
Classes: DummyPair
Constant Summary collapse
- CREATE_KEYPAIR_TEXT =
'Create new key pair'
- DEFAULT_SSH_KEY_PATH =
File.join(Dir.home, '.ssh', 'id_rsa.pub')
- DEFAULT_REGION =
'eu-west-1'
- DEFAULT_INSTANCE_TYPE =
't2.small'
- DEFAULT_STORAGE =
30
Instance Method Summary collapse
- #default_access_key ⇒ Object
- #default_key_pair ⇒ Object
- #default_region ⇒ Object
- #default_region_aws_client ⇒ Object
- #default_secret_key ⇒ Object
- #default_storage ⇒ Object
- #default_subnet_id ⇒ Object
- #default_type ⇒ Object
- #default_vpc_id ⇒ Object
- #default_zone ⇒ Object
- #import_key_pair ⇒ Object
Instance Method Details
#default_access_key ⇒ Object
30 31 32 |
# File 'lib/kontena/plugin/aws/prompts.rb', line 30 def default_access_key prompt.mask('AWS access key:') end |
#default_key_pair ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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 || key_pairs.each do |key_pair| .choice key_pair.key_name, key_pair.key_name end end answer == CREATE_KEYPAIR_TEXT ? import_key_pair : answer end end |
#default_region ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/kontena/plugin/aws/prompts.rb', line 79 def default_region prompt.select("Choose EC2 region:") do || i = 1 default_region_aws_client.describe_regions.regions.sort_by(&:region_name).each do |region| .choice region.region_name, region.region_name .default(i) if region.region_name == DEFAULT_REGION i += 1 end end end |
#default_region_aws_client ⇒ Object
75 76 77 |
# 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 ⇒ Object
34 35 36 |
# File 'lib/kontena/plugin/aws/prompts.rb', line 34 def default_secret_key prompt.mask('AWS secret key:') end |
#default_storage ⇒ Object
125 126 127 |
# File 'lib/kontena/plugin/aws/prompts.rb', line 125 def default_storage prompt.ask('Storage size (GiB):', default: DEFAULT_STORAGE) end |
#default_subnet_id ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# 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 || subnets.each do |subnet| if subnet.state == 'available' .choice "#{subnet.subnet_id} (#{subnet.cidr_block})", subnet.subnet_id end end end end end |
#default_type ⇒ Object
100 101 102 |
# File 'lib/kontena/plugin/aws/prompts.rb', line 100 def default_type prompt.ask('Instance type:', default: DEFAULT_INSTANCE_TYPE) end |
#default_vpc_id ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# 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 || vpcs.each do |vpc| if vpc.state == 'available' name = vpc.vpc_id name += ' (default)' if vpc.is_default .choice name, vpc.vpc_id end end end end end |
#default_zone ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/kontena/plugin/aws/prompts.rb', line 90 def default_zone prompt.select("Choose EC2 Availability Zone:") do || aws_client.describe_availability_zones.availability_zones.sort_by(&:zone_name).select { |z| z.state == 'available' }.each do |zone| .choice zone.zone_name, zone.zone_name.sub(zone.region_name, '') end end end |
#import_key_pair ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 |