class Vidar::CLI

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/vidar/cli.rb, line 5
def self.exit_on_failure?
  true
end

Public Instance Methods

build() click to toggle source
# File lib/vidar/cli.rb, line 29
def build
  Log.info "Building #{Config.get!(:base_stage_name)} image"
  Run.docker_compose "build #{Config.get!(:base_stage_name)}"

  Log.info "Building #{Config.get!(:release_stage_name)} image"
  Run.docker_compose "build #{Config.get!(:release_stage_name)}"
end
cache() click to toggle source
# File lib/vidar/cli.rb, line 38
def cache
  Log.info "Publishing #{Config.get!(:base_stage_name)} image"
  Run.docker "push #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:current_branch)}"
end
console() click to toggle source
# File lib/vidar/cli.rb, line 171
def console
  invoke :kube_exec, [], name: options[:name], command: options[:command] || Config.get!(:console_command)
end
deploy() click to toggle source
# File lib/vidar/cli.rb, line 69
def deploy
  revision = options[:revision] || Config.get!(:revision)
  kubectl_context = options[:kubectl_context] || Config.get!(:kubectl_context)
  Log.info "Current kubectl context: #{kubectl_context}"

  Log.info "Looking for deploy hook..."
  template_name, error, status = Open3.capture3 "kubectl get cronjob deploy-hook-template -n #{Config.get!(:namespace)} -o name --ignore-not-found=true"

  if status.success?
    if template_name.to_s.empty?
      Log.info "No deploy hook found"
    else
      Log.info "Executing deploy hook #{template_name.strip!}..."
      Run.kubectl "delete job deploy-hook --ignore-not-found=true"
      Run.kubectl "set image cronjobs/deploy-hook-template deploy-hook-template=#{Config.get!(:image)}:#{revision} --all"
      Run.kubectl "create job deploy-hook --from=#{template_name}"

      deploy_status = Vidar::DeployStatus.new(namespace: Config.get!(:namespace), filter: "deploy-hook")
      deploy_status.wait_until_completed

      unless deploy_status.success?
        Run.kubectl "describe job deploy-hook"
        Log.error "Error running deploy hook template"
        exit(1)
      end
    end
  else
    Log.info "Error getting deploy hook template: #{error}"
    exit(1)
  end

  Log.info "Set kubectl image..."
  Run.kubectl "set image deployments,cronjobs *=#{Config.get!(:image)}:#{revision} --all"
end
exec() click to toggle source
# File lib/vidar/cli.rb, line 12
def exec
  target = options[:target] || Config.get!(:base_stage_name)
  Run.docker_compose("run #{target} #{options[:command]}") || exit(1)
end
kube_exec() click to toggle source
# File lib/vidar/cli.rb, line 144
def kube_exec
  Log.info "Current kubectl context: #{Config.get!(:kubectl_context)}"

  deploy_config = Config.deploy_config

  Log.error "ERROR: could not find deployment config for #{Config.get!(:kubectl_context)} context" unless deploy_config

  pod_set = K8s::PodSet.new(namespace: Config.get!(:namespace), filter: options[:name])
  containers = pod_set.containers.select(&:ready_and_running?).reject(&:istio?)

  if containers.empty?
    name = options[:name] || 'any'
    Log.error "No running containers found with *#{name}* name"
    exit(1)
  else
    Log.info "Available containers:"
    containers.each(&:print)
    container = containers.first

    Log.info "Running #{options[:command]} in #{container.pod_name}"
    Run.kubectl("exec -it #{container.pod_name} -- #{options[:command]}")
  end
end
monitor_deploy_status() click to toggle source
# File lib/vidar/cli.rb, line 114
def monitor_deploy_status
  Log.info "Current kubectl context: #{Config.get!(:kubectl_context)}"
  Log.info "Checking if all containers in #{Config.get!(:namespace)} namespace(s) are ready..."

  slack_notification = SlackNotification.new(
    github:        Config.get!(:github),
    revision:      Config.get!(:revision),
    revision_name: Config.get!(:revision_name),
    build_url:     Config.build_url,
    deploy_config: Config.deploy_config
  )

  deploy_status = Vidar::DeployStatus.new(namespace: Config.get!(:namespace))

  deploy_status.wait_until_completed

  if deploy_status.success?
    Log.info "OK: All containers are ready"
    slack_notification.success if slack_notification.configured?
    invoke :notify_sentry
  else
    Log.error "ERROR: Some of containers are errored or not ready"
    slack_notification.failure if slack_notification.configured?
    exit(1)
  end
end
notify_sentry() click to toggle source
# File lib/vidar/cli.rb, line 184
def notify_sentry
  sentry_notification = SentryNotification.new(
    revision:      Config.get!(:revision),
    deploy_config: Config.deploy_config
  )

  sentry_notification.call if sentry_notification.configured?
end
notify_slack() click to toggle source
# File lib/vidar/cli.rb, line 195
def notify_slack
  slack_notification = SlackNotification.new(
    github:        Config.get!(:github),
    revision:      Config.get!(:revision),
    revision_name: Config.get!(:revision_name),
    build_url:     Config.build_url,
    deploy_config: Config.deploy_config
  )

  slack_notification.deliver(message: options[:message]) if slack_notification.configured?
end
publish() click to toggle source
# File lib/vidar/cli.rb, line 44
def publish
  base_image_tag = "#{Config.get!(:image)}:#{Config.get!(:base_stage_name)}"
  revision_image_tag = "#{Config.get!(:image)}:#{Config.get!(:revision)}"
  release_image_tag = "#{Config.get!(:image)}:#{Config.get!(:release_stage_name)}"
  latest_image_tag = "#{Config.get!(:image)}:latest"

  Log.info "Publishing #{revision_image_tag}"
  Run.docker "tag #{release_image_tag} #{revision_image_tag}"
  Run.docker "push #{revision_image_tag}"

  return unless Config.default_branch?

  Log.info "Publishing #{base_image_tag}"
  Run.docker "tag #{base_image_tag}-#{Config.get!(:current_branch)} #{base_image_tag}"
  Run.docker "push #{base_image_tag}"

  Log.info "Publishing #{release_image_tag}"
  Run.docker "tag #{release_image_tag} #{latest_image_tag}"
  Run.docker "push #{release_image_tag}"
  Run.docker "push #{latest_image_tag}"
end
pull() click to toggle source
# File lib/vidar/cli.rb, line 18
def pull
  Log.info "Pulling #{Config.get!(:image)} tags"
  Run.docker "pull #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:current_branch)} 2> /dev/null || true"
  Run.docker "pull #{Config.get!(:image)}:#{Config.get!(:base_stage_name)}-#{Config.get!(:default_branch)} 2> /dev/null || true"
  Run.docker "pull #{Config.get!(:image)}:#{Config.get!(:base_stage_name)} 2> /dev/null || true"
  Run.docker "pull #{Config.get!(:image)}:#{Config.get!(:release_stage_name)} 2> /dev/null || true"
  Log.info "Docker images:"
  Run.docker("images")
end
release() click to toggle source
# File lib/vidar/cli.rb, line 105
def release
  Log.info "Build and release #{Config.get!(:image)}:#{Config.get!(:revision)}"
  pull
  build
  cache
  publish
end
ssh() click to toggle source
# File lib/vidar/cli.rb, line 178
def ssh
  invoke :kube_exec, [], name: options[:name], command: options[:command] || Config.get!(:shell_command)
end