Class: Kamal::Cli::App

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

Instance Method Summary collapse

Methods inherited from Base

exit_on_failure?, #initialize

Constructor Details

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

Instance Method Details

#bootObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kamal/cli/app.rb', line 3

def boot
  mutating do
    hold_lock_on_error do
      say "Get most recent version available as an image...", :magenta unless options[:version]
      using_version(version_or_latest) do |version|
        say "Start container with version #{version} using a #{KAMAL.config.readiness_delay}s readiness delay (or reboot if already running)...", :magenta

        on(KAMAL.hosts) do
          execute *KAMAL.auditor.record("Tagging #{KAMAL.config.absolute_image} as the latest image"), verbosity: :debug
          execute *KAMAL.app.tag_current_image_as_latest

          KAMAL.roles_on(host).each do |role|
            app = KAMAL.app(role: role)
            role_config = KAMAL.config.role(role)

            if role_config.assets?
              execute *app.extract_assets
              old_version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip
              execute *app.sync_asset_volumes(old_version: old_version)
            end
          end
        end

        on(KAMAL.hosts, **KAMAL.boot_strategy) do |host|
          KAMAL.roles_on(host).each do |role|
            app = KAMAL.app(role: role)
            auditor = KAMAL.auditor(role: role)
            role_config = KAMAL.config.role(role)

            if capture_with_info(*app.container_id_for_version(version), raise_on_non_zero_exit: false).present?
              tmp_version = "#{version}_replaced_#{SecureRandom.hex(8)}"
              info "Renaming container #{version} to #{tmp_version} as already deployed on #{host}"
              execute *auditor.record("Renaming container #{version} to #{tmp_version}"), verbosity: :debug
              execute *app.rename_container(version: version, new_version: tmp_version)
            end

            old_version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip

            execute *app.tie_cord(role_config.cord_host_file) if role_config.uses_cord?

            execute *auditor.record("Booted app version #{version}"), verbosity: :debug

            execute *app.run(hostname: "#{host}-#{SecureRandom.hex(6)}")

            Kamal::Cli::Healthcheck::Poller.wait_for_healthy(pause_after_ready: true) { capture_with_info(*app.status(version: version)) }

            if old_version.present?
              if role_config.uses_cord?
                cord = capture_with_info(*app.cord(version: old_version), raise_on_non_zero_exit: false).strip
                if cord.present?
                  execute *app.cut_cord(cord)
                  Kamal::Cli::Healthcheck::Poller.wait_for_unhealthy(pause_after_ready: true) { capture_with_info(*app.status(version: old_version)) }
                end
              end

              execute *app.stop(version: old_version), raise_on_non_zero_exit: false

              execute *app.clean_up_assets if role_config.assets?
            end
          end
        end
      end
    end
  end
end

#containersObject



162
163
164
# File 'lib/kamal/cli/app.rb', line 162

def containers
  on(KAMAL.hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_containers) }
end

#detailsObject



99
100
101
102
103
104
105
106
107
# File 'lib/kamal/cli/app.rb', line 99

def details
  on(KAMAL.hosts) do |host|
    roles = KAMAL.roles_on(host)

    roles.each do |role|
      puts_by_host host, capture_with_info(*KAMAL.app(role: role).info)
    end
  end
end

#exec(cmd) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kamal/cli/app.rb', line 112

def exec(cmd)
  case
  when options[:interactive] && options[:reuse]
    say "Get current version of running container...", :magenta unless options[:version]
    using_version(options[:version] || current_running_version) do |version|
      say "Launching interactive command with version #{version} via SSH from existing container on #{KAMAL.primary_host}...", :magenta
      run_locally { exec KAMAL.app(role: KAMAL.primary_role).execute_in_existing_container_over_ssh(cmd, host: KAMAL.primary_host) }
    end

  when options[:interactive]
    say "Get most recent version available as an image...", :magenta unless options[:version]
    using_version(version_or_latest) do |version|
      say "Launching interactive command with version #{version} via SSH from new container on #{KAMAL.primary_host}...", :magenta
      run_locally do
        exec KAMAL.app(role: KAMAL.primary_role).execute_in_new_container_over_ssh(cmd, host: KAMAL.primary_host)
      end
    end

  when options[:reuse]
    say "Get current version of running container...", :magenta unless options[:version]
    using_version(options[:version] || current_running_version) do |version|
      say "Launching command with version #{version} from existing container...", :magenta

      on(KAMAL.hosts) do |host|
        roles = KAMAL.roles_on(host)

        roles.each do |role|
          execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}", role: role), verbosity: :debug
          puts_by_host host, capture_with_info(*KAMAL.app(role: role).execute_in_existing_container(cmd))
        end
      end
    end

  else
    say "Get most recent version available as an image...", :magenta unless options[:version]
    using_version(version_or_latest) do |version|
      say "Launching command with version #{version} from new container...", :magenta
      on(KAMAL.hosts) do |host|
        roles = KAMAL.roles_on(host)

        roles.each do |role|
          execute *KAMAL.auditor.record("Executed cmd '#{cmd}' on app version #{version}"), verbosity: :debug
          puts_by_host host, capture_with_info(*KAMAL.app(role: role).execute_in_new_container(cmd))
        end
      end
    end
  end
end

#imagesObject



192
193
194
# File 'lib/kamal/cli/app.rb', line 192

def images
  on(KAMAL.hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_images) }
end

#logsObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kamal/cli/app.rb', line 201

def logs
  # FIXME: Catch when app containers aren't running

  grep = options[:grep]

  if options[:follow]
    run_locally do
      info "Following logs on #{KAMAL.primary_host}..."

      KAMAL.specific_roles ||= ["web"]
      role = KAMAL.roles_on(KAMAL.primary_host).first

      info KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, grep: grep)
      exec KAMAL.app(role: role).follow_logs(host: KAMAL.primary_host, grep: grep)
    end
  else
    since = options[:since]
    lines = options[:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set

    on(KAMAL.hosts) do |host|
      roles = KAMAL.roles_on(host)

      roles.each do |role|
        begin
          puts_by_host host, capture_with_info(*KAMAL.app(role: role).logs(since: since, lines: lines, grep: grep))
        rescue SSHKit::Command::Failed
          puts_by_host host, "Nothing found"
        end
      end
    end
  end
end

#removeObject



235
236
237
238
239
240
241
# File 'lib/kamal/cli/app.rb', line 235

def remove
  mutating do
    stop
    remove_containers
    remove_images
  end
end

#remove_container(version) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/kamal/cli/app.rb', line 244

def remove_container(version)
  mutating do
    on(KAMAL.hosts) do |host|
      roles = KAMAL.roles_on(host)

      roles.each do |role|
        execute *KAMAL.auditor.record("Removed app container with version #{version}", role: role), verbosity: :debug
        execute *KAMAL.app(role: role).remove_container(version: version)
      end
    end
  end
end

#remove_containersObject



258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/kamal/cli/app.rb', line 258

def remove_containers
  mutating do
    on(KAMAL.hosts) do |host|
      roles = KAMAL.roles_on(host)

      roles.each do |role|
        execute *KAMAL.auditor.record("Removed all app containers", role: role), verbosity: :debug
        execute *KAMAL.app(role: role).remove_containers
      end
    end
  end
end

#remove_imagesObject



272
273
274
275
276
277
278
279
# File 'lib/kamal/cli/app.rb', line 272

def remove_images
  mutating do
    on(KAMAL.hosts) do
      execute *KAMAL.auditor.record("Removed all app images"), verbosity: :debug
      execute *KAMAL.app.remove_images
    end
  end
end

#stale_containersObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/kamal/cli/app.rb', line 168

def stale_containers
  mutating do
    stop = options[:stop]

    cli = self

    on(KAMAL.hosts) do |host|
      roles = KAMAL.roles_on(host)

      roles.each do |role|
        cli.send(:stale_versions, host: host, role: role).each do |version|
          if stop
            puts_by_host host, "Stopping stale container for role #{role} with version #{version}"
            execute *KAMAL.app(role: role).stop(version: version), raise_on_non_zero_exit: false
          else
            puts_by_host host,  "Detected stale container for role #{role} with version #{version} (use `kamal app stale_containers --stop` to stop)"
          end
        end
      end
    end
  end
end

#startObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kamal/cli/app.rb', line 70

def start
  mutating do
    on(KAMAL.hosts) do |host|
      roles = KAMAL.roles_on(host)

      roles.each do |role|
        execute *KAMAL.auditor.record("Started app version #{KAMAL.config.version}"), verbosity: :debug
        execute *KAMAL.app(role: role).start, raise_on_non_zero_exit: false
      end
    end
  end
end

#stopObject



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kamal/cli/app.rb', line 84

def stop
  mutating do
    on(KAMAL.hosts) do |host|
      roles = KAMAL.roles_on(host)

      roles.each do |role|
        execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug
        execute *KAMAL.app(role: role).stop, raise_on_non_zero_exit: false
      end
    end
  end
end

#versionObject



282
283
284
285
286
287
# File 'lib/kamal/cli/app.rb', line 282

def version
  on(KAMAL.hosts) do |host|
    role = KAMAL.roles_on(host).first
    puts_by_host host, capture_with_info(*KAMAL.app(role: role).current_running_version).strip
  end
end