class DeployApp

Public Instance Methods

main() click to toggle source
# File lib/deploy_app.rb, line 5
def main
  options = get_options
  take_approval(options)

  gcr_url = "gcr.io/#{options[:project_id]}/#{options[:image_repo] || options[:app_name]}"

  if(!options[:image_tag].nil?)
    image_url = validate_image_tag_exists?(gcr_url, options[:image_tag])
  else
    image_url = build_image(gcr_url, options)
  end

  deploy_image(gcr_url, image_url, options)
end

Private Instance Methods

build_image(gcr_url, options) click to toggle source
# File lib/deploy_app.rb, line 110
def build_image(gcr_url, options)
  commit_image_tag = ENV['CI_COMMIT_SHORT_SHA'] || `git rev-parse --short HEAD`
  time_image_tag=`TZ=IST-5:30 date +'%Y.%m.%d.%HH.%MM.%SS'`
  commit_image_url = "#{gcr_url}:#{commit_image_tag}".strip
  time_image_url = "#{gcr_url}:#{time_image_tag}".strip

  project_id = options[:project_id]

  shell_cmd("gcloud builds submit --project #{project_id} --timeout=20m --tag #{time_image_url} || true")

  shell_cmd("gcloud container images add-tag --quiet #{time_image_url} #{commit_image_url}")

  commit_image_url
end
deploy_image(gcr_url, image_url, options) click to toggle source
# File lib/deploy_app.rb, line 125
def deploy_image(gcr_url, image_url, options)
  shell_cmd(
    "gcloud container clusters get-credentials #{options[:cluster]} " \
    "--region #{options[:cluster_location]} " \
    "--project #{options[:project_id]} "
  )

  app_name = options[:app_name]
  shell_cmd("kubectl set image deployment #{app_name} #{app_name}=#{image_url} -n #{options[:namespace]}")

  shell_cmd("kubectl rollout status deployment/#{app_name} -n #{options[:namespace]}")

  shell_cmd("gcloud container images add-tag --quiet #{image_url} #{gcr_url}:current")
end
get_options() click to toggle source
# File lib/deploy_app.rb, line 21
def get_options
  options = {}

  require_options_keys = [:project_id, :app_name, :namespace, :cluster, :cluster_location]

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: ather deploy app [options]"

    opts.on("-y", "Auto approve") do |flag|
      options[:approve] = flag
    end

    opts.on("--image-tag IMAGE_TAG", "Image tag to deploy [optional]") do |val|
      options[:image_tag] = val
    end

    opts.on("--image-repo IMAGE_REPO", "Image repo to deploy [optional] default to K8s app name") do |val|
      options[:image_repo] = val
    end

    opts.on("--project-id PROJECT_ID", "GCP project id [required or set env var PROJECT_ID]") do |val|
      options[:project_id] = val
    end

    opts.on("--app-name APP_NAME", "K8s app name [required or set env var APP_NAME]") do |val|
      options[:app_name] = val
    end

    opts.on("--namespace NAMESPACE", "K8s app namespace [required or set env var NAMESPACE]") do |val|
      options[:namespace] = val
    end

    opts.on("--cluster CLUSTER", "App name [required or set env var CLUSTER]") do |val|
      options[:cluster] = val
    end

    opts.on("--cluster-location CLUSTER_LOCATION", "App name [required or set env var CLUSTER_LOCATION]") do |val|
      options[:cluster_location] = val
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  opt_parser.parse!

  require_options_keys.each do |key|
    options[key] ||= ENV[key.to_s.upcase]
    if options[key].nil?
      puts "ERROR: Required option --#{key.to_s.gsub('_', '-')} or set env var #{key.to_s.upcase} \n"
      opt_parser.parse! %w[--help]
      exit 1
    end
  end

  options
end
shell_cmd(cmd) click to toggle source
# File lib/deploy_app.rb, line 81
def shell_cmd(cmd)
  puts "EXECUTING : \e[32m\e[1m#{cmd}\e[22m\e[0m"
  op = system(cmd)
  if !op
    puts "FAILED : \e[31m\e[1m#{cmd}\e[22m\e[0m"
    exit 1
  end
  puts "SUCCESS : \e[32m\e[1m#{cmd}\e[22m\e[0m"
end
take_approval(options) click to toggle source
# File lib/deploy_app.rb, line 91
def take_approval(options)
  puts "\e[32m\e[1mDeployment parameter details \e[22m\e[0m"
  options.each do |key, val|
    puts "\e[1m#{key} = \e[31m#{val}\e[0m \e[22m"
  end

  return if(options[:approve])
  puts 'Are you sure to want to deploy as per the above deployment parameters?'
  confirmation = STDIN.gets.strip
  return if(confirmation == 'y' || confirmation == 'Y')
  exit
end
validate_image_tag_exists?(gcr_url, image_tag) click to toggle source
# File lib/deploy_app.rb, line 104
def validate_image_tag_exists?(gcr_url, image_tag)
  image_url = "#{gcr_url}:#{image_tag}"
  shell_cmd("gcloud container images describe #{image_url}")
  image_url
end