|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require 'json' |
| 5 | + |
| 6 | +NOTSET = '<notset>' |
| 7 | + |
| 8 | +def main(argv: ARGV) |
| 9 | + return usage if $stdin.tty? || argv.include?('-h') || argv.include?('--help') |
| 10 | + |
| 11 | + in_args = JSON.parse($stdin.read) |
| 12 | + zones = in_args.fetch('zones', '').split(',').map(&:strip).reject(&:empty?) |
| 13 | + count = Integer(in_args.fetch('count', zones.length)) |
| 14 | + project = in_args.fetch('project') |
| 15 | + region = in_args.fetch('region') |
| 16 | + retries = Integer(in_args.fetch('retries', '4')) |
| 17 | + retry_sleep = Integer(in_args.fetch('retry_sleep', '120')) |
| 18 | + accounting = %w[1 yes on true].include?( |
| 19 | + ENV.fetch( |
| 20 | + 'ACCOUNTING', in_args.fetch('accounting', true) |
| 21 | + ).to_s |
| 22 | + ) |
| 23 | + retried = 0 |
| 24 | + |
| 25 | + instances = {} |
| 26 | + while retried <= retries |
| 27 | + instances = fetch_nats_by_zone(zones, count, project, region) |
| 28 | + break unless accounting |
| 29 | + break if instances.length == count |
| 30 | + |
| 31 | + warn( |
| 32 | + "#{$PROGRAM_NAME}: sleeping=#{retry_sleep}s " \ |
| 33 | + "instances_length=#{instances.length} count=#{count}" |
| 34 | + ) |
| 35 | + |
| 36 | + sleep(retry_sleep) |
| 37 | + retried += 1 |
| 38 | + end |
| 39 | + |
| 40 | + $stdout.puts(JSON.pretty_generate(instances)) |
| 41 | + 0 |
| 42 | +end |
| 43 | + |
| 44 | +def fetch_nats_by_zone(zones, count, project, region) |
| 45 | + instances = {} |
| 46 | + instances_command = %w[ |
| 47 | + gcloud compute instance-groups list-instances |
| 48 | + ] |
| 49 | + |
| 50 | + zones.each do |zone| |
| 51 | + (count / zones.length).times do |count_index| |
| 52 | + full_command = instances_command + %W[ |
| 53 | + nat-#{zone}-#{count_index + 1} |
| 54 | + --project=#{project} |
| 55 | + --zone=#{region}-#{zone} |
| 56 | + --format="value(instance)" |
| 57 | + ] |
| 58 | + instance_name = `#{full_command.join(' ')}`.strip |
| 59 | + next if instance_name.empty? |
| 60 | + |
| 61 | + instances["nat-#{zone}-#{count_index + 1}"] = %W[ |
| 62 | + projects |
| 63 | + #{project} |
| 64 | + zones |
| 65 | + #{region}-#{zone} |
| 66 | + instances |
| 67 | + #{instance_name} |
| 68 | + ].join('/') |
| 69 | + rescue StandardError => e |
| 70 | + warn e |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + instances |
| 75 | +end |
| 76 | + |
| 77 | +def usage |
| 78 | + warn <<~EOF |
| 79 | + Usage: #{$PROGRAM_NAME} [-h|--help] |
| 80 | +
|
| 81 | + Print a mapping of {zone}-{index}=>{instance-id} given a JSON blob |
| 82 | + containing the following arguments provided via stdin: |
| 83 | +
|
| 84 | + accounting - boolean controlling count check and retries, default=false |
| 85 | + count - total number of expected instances, default=zones.length |
| 86 | + project - the gcloud project name *REQUIRED* |
| 87 | + region - the region in which to look for nat instances *REQUIRED* |
| 88 | + retries - number of total retries, default=4 |
| 89 | + retry_sleep - sleep interval between retries, default=120 |
| 90 | + zones - a comma-delimited list of zones within the region |
| 91 | + EOF |
| 92 | + 2 |
| 93 | +end |
| 94 | + |
| 95 | +exit(main) if $PROGRAM_NAME == __FILE__ |
0 commit comments