Module: Lono::Configure::Helpers
Instance Method Summary collapse
- #aws_account ⇒ Object
- #default_key_name ⇒ Object
- #default_subnet ⇒ Object
- #default_vpc_id ⇒ Object
-
#find_vpc(vpc_id = nil) ⇒ Object
If not vpc_id is provided, it tries to find the default vpc.
- #from_seed(key) ⇒ Object
- #get_input(key, default: '') ⇒ Object
-
#get_seed(key, default:, load_default: true) ⇒ Object
Defaults to loading the default.
- #key_name ⇒ Object
- #load_default(default) ⇒ Object
- #seed ⇒ Object
- #select_subnets(subnets, regexp = nil) ⇒ Object
- #subnet_ids(vpc_id: nil, regexp: nil) ⇒ Object
Instance Method Details
#aws_account ⇒ Object
79 80 81 |
# File 'lib/lono/configure/helpers.rb', line 79 def aws_account sts.get_caller_identity.account end |
#default_key_name ⇒ Object
43 44 45 |
# File 'lib/lono/configure/helpers.rb', line 43 def default_key_name key_name end |
#default_subnet ⇒ Object
34 35 36 |
# File 'lib/lono/configure/helpers.rb', line 34 def default_subnet subnet_ids.first end |
#default_vpc_id ⇒ Object
38 39 40 41 |
# File 'lib/lono/configure/helpers.rb', line 38 def default_vpc_id vpc = find_vpc vpc.vpc_id if vpc # default vpc might have been deleted end |
#find_vpc(vpc_id = nil) ⇒ Object
If not vpc_id is provided, it tries to find the default vpc. If the default vpc doesnt exist, nilt is returned.
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/lono/configure/helpers.rb', line 114 def find_vpc(vpc_id=nil) if vpc_id resp = ec2.describe_vpcs(vpc_ids: [vpc_id]) else resp = ec2.describe_vpcs(filters: [{name: "isDefault", values: ["true"]}]) end vpcs = resp.vpcs vpcs.first rescue Aws::EC2::Errors::InvalidVpcIDNotFound nil end |
#from_seed(key) ⇒ Object
47 48 49 |
# File 'lib/lono/configure/helpers.rb', line 47 def from_seed(key) seed[key.to_s] end |
#get_input(key, default: '') ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/lono/configure/helpers.rb', line 5 def get_input(key, default:'') value = get_seed(key, default: default, load_default: @options[:defaults]) return value if value default_text = default.blank? ? '' : "(default: #{default})" print "Please provide value for #{key}#{default_text}: " value = $stdin.gets.strip value.blank? ? load_default(default) : value end |
#get_seed(key, default:, load_default: true) ⇒ Object
Defaults to loading the default
16 17 18 19 20 21 22 23 24 |
# File 'lib/lono/configure/helpers.rb', line 16 def get_seed(key, default:, load_default: true) seed_value = from_seed(key) if seed_value puts "For #{key}, using seed value #{seed_value}" return seed_value end return load_default(default) if load_default end |
#key_name ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lono/configure/helpers.rb', line 68 def key_name resp = ec2.describe_key_pairs key = resp.key_pairs.first if key key.key_name else "Please create a keypair and configure it here" end end |
#load_default(default) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/lono/configure/helpers.rb', line 26 def load_default(default) if default.is_a?(Symbol) send(default) # IE: default_subnet else default # return the default as is end end |
#seed ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lono/configure/helpers.rb', line 51 def seed if @options[:seed] == :convention convention_path = "seeds/#{@blueprint}/#{Lono.env}.yml" seed_file = convention_path else seed_file = @options[:seed] end unless File.exist?(seed_file) puts "WARN: unable to find seed file #{seed_file}".color(:yellow) if @options[:seed] != :convention return {} end puts "Using seed file: #{seed_file}" YAML.load_file(seed_file) end |
#select_subnets(subnets, regexp = nil) ⇒ Object
102 103 104 105 106 107 108 109 110 |
# File 'lib/lono/configure/helpers.rb', line 102 def select_subnets(subnets, regexp=nil) return subnets unless regexp subnets.select do |subnet| = subnet. name_tag = .find { |t| t.key == "Name" } name_tag && name_tag.value =~ regexp end end |
#subnet_ids(vpc_id: nil, regexp: nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lono/configure/helpers.rb', line 83 def subnet_ids(vpc_id: nil, regexp: nil) vpc = find_vpc(vpc_id) unless vpc # sometimes even default VPC has been deleted abort "ERROR: Cannot find subnets because cannot find vpc #{@vpc_id} Please double check that is the right vpc".color(:red) end resp = ec2.describe_subnets(filters: [{name: 'vpc-id', values: [vpc.vpc_id]}]) subnets = resp.subnets # first subnet # Assume that the subnets have a Name tag with PrivateSubnet1, PrivateSubnet2, etc # This is part of the BoltOps blueprint stack. selected_subnets = select_subnets(subnets, regexp) # if cannot find selected subnets, then keep all the subnets subnets = selected_subnets unless selected_subnets.empty? subnets.map(&:subnet_id) end |