#!/usr/bin/env ruby # frozen_string_literal: true require 'kubernetes-deploy' require 'optparse' skip_wait = false template_dir = nil allow_protected_ns = false prune = true bindings = {} verbose_log_prefix = false max_watch_seconds = nil ARGV.options do |opts| opts.on("--bindings=BINDINGS", "Expose additional variables to ERB templates " \ "(format: k1=v1,k2=v2, JSON string or file (JSON or YAML) path prefixed by '@')") do |binds| bindings.merge!(KubernetesDeploy::BindingsParser.parse(binds)) end opts.on("--skip-wait", "Skip verification of non-priority-resource success (not recommended)") { skip_wait = true } prot_ns = KubernetesDeploy::DeployTask::PROTECTED_NAMESPACES.join(', ') opts.on("--allow-protected-ns", "Enable deploys to #{prot_ns}; requires --no-prune") { allow_protected_ns = true } opts.on("--no-prune", "Disable deletion of resources that do not appear in the template dir") { prune = false } opts.on("--template-dir=DIR", "Set the template dir (default: config/deploy/$ENVIRONMENT)") { |v| template_dir = v } opts.on("--verbose-log-prefix", "Add [context][namespace] to the log prefix") { verbose_log_prefix = true } opts.on("--max-watch-seconds=seconds", "Timeout error is raised if it takes longer than the specified number of seconds") do |t| max_watch_seconds = t.to_i end opts.on_tail("-h", "--help", "Print this help") do puts opts exit end opts.on_tail("-v", "--version", "Show version") do puts "v#{KubernetesDeploy::VERSION}" exit end opts.parse! end if !template_dir && ENV.key?("ENVIRONMENT") template_dir = "config/deploy/#{ENV['ENVIRONMENT']}" end if !template_dir || template_dir.empty? puts "Template directory is unknown. " \ "Either specify --template-dir argument or set $ENVIRONMENT to use config/deploy/$ENVIRONMENT as a default path." exit 1 end revision = ENV.fetch('REVISION') do puts "ENV['REVISION'] is missing. Please specify the commit SHA" exit 1 end namespace = ARGV[0] context = ARGV[1] logger = KubernetesDeploy::FormattedLogger.build(namespace, context, verbose_prefix: verbose_log_prefix) runner = KubernetesDeploy::DeployTask.new( namespace: namespace, context: context, current_sha: revision, template_dir: template_dir, bindings: bindings, logger: logger, max_watch_seconds: max_watch_seconds ) begin runner.run!( verify_result: !skip_wait, allow_protected_ns: allow_protected_ns, prune: prune ) rescue KubernetesDeploy::DeploymentTimeoutError exit 70 rescue KubernetesDeploy::FatalDeploymentError exit 1 end