Class: Kamal::Cli::Main

Inherits:
Base
  • Object
show all
Defined in:
lib/kamal/cli/main.rb

Instance Method Summary collapse

Methods inherited from Base

dynamic_command_class, exit_on_failure?, #initialize

Constructor Details

This class inherits a constructor from Kamal::Cli::Base

Instance Method Details

#auditObject



114
115
116
117
118
# File 'lib/kamal/cli/main.rb', line 114

def audit
  on(KAMAL.hosts) do |host|
    puts_by_host host, capture_with_info(*KAMAL.auditor.reveal)
  end
end

#configObject



121
122
123
124
125
# File 'lib/kamal/cli/main.rb', line 121

def config
  run_locally do
    puts Kamal::Utils.redacted(KAMAL.config.to_h).to_yaml
  end
end

#deploy(boot_accessories: false) ⇒ Object



19
20
21
22
23
24
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
# File 'lib/kamal/cli/main.rb', line 19

def deploy(boot_accessories: false)
  runtime = print_runtime do
    invoke_options = deploy_options

    say "Log into image registry...", :magenta
    invoke "kamal:cli:registry:login", [], invoke_options.merge(skip_local: options[:skip_push])

    if options[:skip_push]
      say "Pull app image...", :magenta
      invoke "kamal:cli:build:pull", [], invoke_options
    else
      say "Build and push app image...", :magenta
      invoke "kamal:cli:build:deliver", [], invoke_options
    end

    with_lock do
      run_hook "pre-deploy", secrets: true

      say "Ensure kamal-proxy is running...", :magenta
      invoke "kamal:cli:proxy:boot", [], invoke_options

      invoke "kamal:cli:accessory:boot", [ "all" ], invoke_options if boot_accessories

      say "Detect stale containers...", :magenta
      invoke "kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true)

      invoke "kamal:cli:app:boot", [], invoke_options

      say "Prune old containers and images...", :magenta
      invoke "kamal:cli:prune:all", [], invoke_options
    end
  end

  run_hook "post-deploy", secrets: true, runtime: runtime.round.to_s
end

#detailsObject



107
108
109
110
111
# File 'lib/kamal/cli/main.rb', line 107

def details
  invoke "kamal:cli:proxy:details"
  invoke "kamal:cli:app:details"
  invoke "kamal:cli:accessory:details", [ "all" ]
end

#docs(section = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
# File 'lib/kamal/cli/main.rb', line 128

def docs(section = nil)
  case section
  when NilClass
    puts Kamal::Configuration.validation_doc
  else
    puts Kamal::Configuration.const_get(section.titlecase.to_sym).validation_doc
  end
rescue NameError
  puts "No documentation found for #{section}"
end

#initObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kamal/cli/main.rb', line 141

def init
  require "fileutils"

  if (deploy_file = Pathname.new(File.expand_path("config/deploy.yml"))).exist?
    puts "Config file already exists in config/deploy.yml (remove first to create a new one)"
  else
    FileUtils.mkdir_p deploy_file.dirname
    FileUtils.cp_r Pathname.new(File.expand_path("templates/deploy.yml", __dir__)), deploy_file
    puts "Created configuration file in config/deploy.yml"
  end

  unless (secrets_file = Pathname.new(File.expand_path(".kamal/secrets"))).exist?
    FileUtils.mkdir_p secrets_file.dirname
    FileUtils.cp_r Pathname.new(File.expand_path("templates/secrets", __dir__)), secrets_file
    puts "Created .kamal/secrets file"
  end

  unless (hooks_dir = Pathname.new(File.expand_path(".kamal/hooks"))).exist?
    hooks_dir.mkpath
    Pathname.new(File.expand_path("templates/sample_hooks", __dir__)).each_child do |sample_hook|
      FileUtils.cp sample_hook, hooks_dir, preserve: true
    end
    puts "Created sample hooks in .kamal/hooks"
  end

  if options[:bundle]
    if (binstub = Pathname.new(File.expand_path("bin/kamal"))).exist?
      puts "Binstub already exists in bin/kamal (remove first to create a new one)"
    else
      puts "Adding Kamal to Gemfile and bundle..."
      run_locally do
        execute :bundle, :add, :kamal
        execute :bundle, :binstubs, :kamal
      end
      puts "Created binstub file in bin/kamal"
    end
  end
end

#redeployObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kamal/cli/main.rb', line 57

def redeploy
  runtime = print_runtime do
    invoke_options = deploy_options

    if options[:skip_push]
      say "Pull app image...", :magenta
      invoke "kamal:cli:build:pull", [], invoke_options
    else
      say "Build and push app image...", :magenta
      invoke "kamal:cli:build:deliver", [], invoke_options
    end

    with_lock do
      run_hook "pre-deploy", secrets: true

      say "Detect stale containers...", :magenta
      invoke "kamal:cli:app:stale_containers", [], invoke_options.merge(stop: true)

      invoke "kamal:cli:app:boot", [], invoke_options
    end
  end

  run_hook "post-deploy", secrets: true, runtime: runtime.round.to_s
end

#removeObject



182
183
184
185
186
187
188
189
190
191
# File 'lib/kamal/cli/main.rb', line 182

def remove
  confirming "This will remove all containers and images. Are you sure?" do
    with_lock do
      invoke "kamal:cli:app:remove", [], options.without(:confirmed)
      invoke "kamal:cli:proxy:remove", [], options.without(:confirmed)
      invoke "kamal:cli:accessory:remove", [ "all" ], options
      invoke "kamal:cli:registry:logout", [], options.without(:confirmed).merge(skip_local: true)
    end
  end
end

#rollback(version) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/kamal/cli/main.rb', line 83

def rollback(version)
  rolled_back = false
  runtime = print_runtime do
    with_lock do
      invoke_options = deploy_options

      KAMAL.config.version = version
      old_version = nil

      if container_available?(version)
        run_hook "pre-deploy", secrets: true

        invoke "kamal:cli:app:boot", [], invoke_options.merge(version: version)
        rolled_back = true
      else
        say "The app version '#{version}' is not available as a container (use 'kamal app containers' for available versions)", :red
      end
    end
  end

  run_hook "post-deploy", secrets: true, runtime: runtime.round.to_s if rolled_back
end

#setupObject



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/kamal/cli/main.rb', line 4

def setup
  print_runtime do
    with_lock do
      invoke_options = deploy_options

      say "Ensure Docker is installed...", :magenta
      invoke "kamal:cli:server:bootstrap", [], invoke_options

      deploy(boot_accessories: true)
    end
  end
end

#upgradeObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kamal/cli/main.rb', line 196

def upgrade
  confirming "This will replace Traefik with kamal-proxy and restart all accessories" do
    with_lock do
      if options[:rolling]
        (KAMAL.hosts | KAMAL.accessory_hosts).each do |host|
          KAMAL.with_specific_hosts(host) do
            say "Upgrading #{host}...", :magenta
            if KAMAL.hosts.include?(host)
              invoke "kamal:cli:proxy:upgrade", [], options.merge(confirmed: true, rolling: false)
              reset_invocation(Kamal::Cli::Proxy)
            end
            if KAMAL.accessory_hosts.include?(host)
              invoke "kamal:cli:accessory:upgrade", [ "all" ], options.merge(confirmed: true, rolling: false)
              reset_invocation(Kamal::Cli::Accessory)
            end
            say "Upgraded #{host}", :magenta
          end
        end
      else
        say "Upgrading all hosts...", :magenta
        invoke "kamal:cli:proxy:upgrade", [], options.merge(confirmed: true)
        invoke "kamal:cli:accessory:upgrade", [ "all" ], options.merge(confirmed: true)
        say "Upgraded all hosts", :magenta
      end
    end
  end
end

#versionObject



225
226
227
# File 'lib/kamal/cli/main.rb', line 225

def version
  puts Kamal::VERSION
end