#!/usr/bin/env ruby if ENV['HACKING'] require_relative '../lib/cloudstrap' else require 'cloudstrap' end require 'burdened-acrobat' require 'moneta' require 'path53' require 'pastel' gem = Gem::Specification .find_all_by_name('cloudstrap') .sort_by { |spec| spec.version } .last puts <<-EOS # #{gem.name} v#{gem.version} # #{gem.summary} EOS config = Cloudstrap::Config.new route53 = Cloudstrap::Amazon::Route53.new config abort "Cache not found! #{$PROGRAM_NAME} should be run *after* cloudstrap." unless Dir.exist? config.cache_path cache = Moneta.new :File, dir: config.cache_path DOMAIN = ENV.fetch('CLOUDSTRAP_DOMAIN') { config.domain_name } VPC = ENV.fetch('CLOUDSTRAP_VPC_ID') { cache.fetch :vpc_id } abort "VPC ID missing from cache. Have you run cloudstrap yet?" if VPC.nil? ZONE = route53.zone(route53.longest_matching_suffix(DOMAIN) || '') abort "Invalid Zone: #{DOMAIN}" unless ZONE load_balancers = BurdenedAcrobat::LoadBalancers.new elbs = load_balancers.search { tagged('KubernetesCluster') & with(VPC) } abort "No clusters found in #{VPC}. Unable to proceed." if elbs.empty? clusters = load_balancers.tags(elbs).flat_map do |lb| lb.tags.select { |tag| tag.key == 'KubernetesCluster' }.map(&:value) end.uniq begin CLUSTER = (clusters.size == 1) ? clusters.first : ENV.fetch('CLOUDSTRAP_KUBERNETES_CLUSTER') rescue KeyError pastel = Pastel.new STDERR.puts <<-EOS #{$PROGRAM_NAME} is confused and needs your help! #{pastel.red "Multiple clusters found in #{pastel.bold VPC}:"} #{clusters.map { |cluster| " * #{cluster}\n" }.join.chomp} Set the environment variable #{pastel.bold("CLOUDSTRAP_KUBERNETES_CLUSTER")} to the correct cluster, and invoke #{$PROGRAM_NAME} again. EOS abort end services = load_balancers .search { tags(with(VPC) & tagged('KubernetesCluster', CLUSTER)) } .map do |lb| [ lb.tags.find { |tag| tag.key == 'kubernetes.io/service-name' }.value, load_balancers.named([lb.load_balancer_name]) ] end.to_h hcp = services.fetch('hcp/ipmgr').first identity = services.fetch('hcp/ident-api').first hsm = services.fetch('hcp/hsm-server').first puts <<-EOS # Configuring the following DNS records... hcp.#{DOMAIN} IN A #{hcp.canonical_hosted_zone_name} hsm.#{DOMAIN} IN A #{hsm.canonical_hosted_zone_name} identity.#{DOMAIN} IN A #{identity.canonical_hosted_zone_name} *.identity.#{DOMAIN} IN CNAME identity.#{DOMAIN} EOS change = Path53.change(ZONE).batch do add a "hcp.#{DOMAIN}", hcp add a "hsm.#{DOMAIN}", hsm add a "identity.#{DOMAIN}", identity add cname "*.identity.#{DOMAIN}", "identity.#{DOMAIN}" end.apply! loop do info = route53.api.get_change(id: change).change_info STDERR.puts "#{change} is #{info.status}" (info.status == 'PENDING') ? sleep(4) : break end