Module: KamalGitlabReviewApp::CLI

Defined in:
lib/kamal_gitlab_review_app/cli.rb,
lib/kamal_gitlab_review_app/cli/stop.rb,
lib/kamal_gitlab_review_app/cli/deploy.rb

Defined Under Namespace

Classes: Deploy, Stop

Constant Summary collapse

USAGE =
<<~USAGE
  Usage: kamal-gitlab-review-app <command> [args...]

  Commands:
    deploy              Deploy or setup the MR review app
    stop                Tear down the MR review app (best-effort)
    runtime-env [IID]   Print runtime env for an MR IID
    decide [IID]        Decide setup vs deploy from remote container list (stdin)
    dns-upsert [IID]    Upsert DNS record for an MR IID
    dns-delete [IID]    Delete DNS record for an MR IID
    docker-cleanup [IID]  Remote Docker cleanup for an MR IID (debug)

  IID defaults to CI_MERGE_REQUEST_IID when omitted.
USAGE

Class Method Summary collapse

Class Method Details

.delete_dns(args) ⇒ Object



90
91
92
93
94
95
# File 'lib/kamal_gitlab_review_app/cli.rb', line 90

def delete_dns(args)
  host = KamalGitlabReviewApp::Naming.for_mr(iid: mr_iid(args)).fetch(:host)

  KamalGitlabReviewApp::Dns::Registry.resolve.delete_record!(name: host)
  puts "dns_delete:#{host}"
end

.mr_iid(args) ⇒ Object



66
67
68
# File 'lib/kamal_gitlab_review_app/cli.rb', line 66

def mr_iid(args)
  args.fetch(0) { ENV.fetch('CI_MERGE_REQUEST_IID') }
end


76
77
78
79
# File 'lib/kamal_gitlab_review_app/cli.rb', line 76

def print_decision(args)
  container_names = $stdin.tty? ? [] : $stdin.read.split("\n")
  puts KamalGitlabReviewApp::LifecycleDecider.call(iid: mr_iid(args), container_names:)
end


70
71
72
73
74
# File 'lib/kamal_gitlab_review_app/cli.rb', line 70

def print_runtime_env(args)
  KamalGitlabReviewApp::RuntimeEnv.to_h(iid: mr_iid(args)).sort.each do |key, value|
    puts "#{key}=#{value}"
  end
end

.run(argv) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kamal_gitlab_review_app/cli.rb', line 25

def run(argv)
  command, *args = argv

  case command
  when nil, '', '-h', '--help', 'help'
    puts USAGE
    0
  when 'deploy'
    run_deploy
  when 'stop'
    Stop.call ? 0 : 1
  when 'runtime-env'
    print_runtime_env(args)
    0
  when 'decide'
    print_decision(args)
    0
  when 'dns-upsert'
    upsert_dns(args)
    0
  when 'dns-delete'
    delete_dns(args)
    0
  when 'docker-cleanup'
    KamalGitlabReviewApp::Remote::DockerCleanup.call(iid: mr_iid(args))
    0
  else
    warn "Unknown command: #{command.inspect}"
    warn USAGE
    1
  end
end

.run_deployObject



58
59
60
61
62
63
64
# File 'lib/kamal_gitlab_review_app/cli.rb', line 58

def run_deploy
  Deploy.call
  0
rescue Deploy::Error, KeyError, KamalGitlabReviewApp::Dns::Error => e
  warn "kamal-gitlab-review-app deploy: #{e.message}"
  1
end

.upsert_dns(args) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/kamal_gitlab_review_app/cli.rb', line 81

def upsert_dns(args)
  host = KamalGitlabReviewApp::Naming.for_mr(iid: mr_iid(args)).fetch(:host)
  ip = ENV.fetch('REVIEW_TARGET_IP')
  ttl = ENV.fetch('REVIEW_DNS_TTL', '120').to_i

  KamalGitlabReviewApp::Dns::Registry.resolve.upsert_a_record!(name: host, ip:, ttl:)
  puts "dns_upsert:#{host}"
end