Class: Kamal::Cli::App
Defined Under Namespace
Classes: Assets, Boot, ErrorPages, RolloutBoot, SslCertificates
Constant Summary
Constants inherited from Base
Base::AUTOMATIC_DEPLOY_LOCK_MESSAGE, Base::HOOK_MUTEX, Base::VERBOSITY
Instance Method Summary collapse
- #boot ⇒ Object
- #containers ⇒ Object
- #details ⇒ Object
- #exec(*cmd) ⇒ Object
- #images ⇒ Object
- #live ⇒ Object
- #logs ⇒ Object
- #maintenance ⇒ Object
- #remove ⇒ Object
- #remove_app_directories ⇒ Object
- #remove_container(version) ⇒ Object
- #remove_containers ⇒ Object
- #remove_images ⇒ Object
- #rollout(action) ⇒ Object
- #stale_containers ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #version ⇒ Object
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
#boot ⇒ Object
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 |
# File 'lib/kamal/cli/app.rb', line 3 def boot modify(lock: true) do say "Get most recent version available as an image...", :magenta unless [:version] using_version(version_or_latest) do |version| say "Start container with version #{version} (or reboot if already running)...", :magenta # Assets are prepared in a separate step to ensure they are on all hosts before booting on(KAMAL.app_hosts) do Kamal::Cli::App::ErrorPages.new(host, self).run KAMAL.roles_on(host).each do |role| Kamal::Cli::App::Assets.new(host, role, self).run Kamal::Cli::App::SslCertificates.new(host, role, self).run end end # Primary hosts and roles are returned first, so they can open the barrier = Kamal::Cli::Healthcheck::Barrier.new cli = self boot_groups = host_boot_groups boot_groups.each_with_index do |hosts, index| host_list = Array(hosts).join(",") run_hook "pre-app-boot", hosts: host_list on_roles(KAMAL.roles, hosts: hosts, parallel: KAMAL.config.parallel_roles?, rolling: true) do |host, role| Kamal::Cli::App::Boot.new(host, role, self, version, , cli).run end run_hook "post-app-boot", hosts: host_list # `wait` paces one group against the next, so the last group has nothing to pace # against. Without a `limit` there is only ever one group, and waiting at all # would be a fixed delay on every deploy buying no staggering. sleep KAMAL.config.boot.wait if KAMAL.config.boot.wait && index < boot_groups.size - 1 end # Tag once the app booted on all hosts on(KAMAL.app_hosts) do |host| execute *KAMAL.auditor.record("Tagging #{KAMAL.config.absolute_image} as the latest image"), verbosity: :debug execute *KAMAL.app.tag_latest_image end end end end |
#containers ⇒ Object
175 176 177 178 |
# File 'lib/kamal/cli/app.rb', line 175 def containers quiet = [:quiet] on(KAMAL.app_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_containers), quiet: quiet } end |
#details ⇒ Object
95 96 97 98 99 100 |
# File 'lib/kamal/cli/app.rb', line 95 def details quiet = [:quiet] on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, role| puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).info), quiet: quiet end end |
#exec(*cmd) ⇒ Object
108 109 110 111 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 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/kamal/cli/app.rb', line 108 def exec(*cmd) raw = [:raw] if ( = [ :interactive, :reuse ].select { |key| [:detach] && [key] }.presence) raise ArgumentError, "Detach is not compatible with #{.join(" or ")}" end if raw && ( = [ :interactive, :detach ].select { |key| [key] }.presence) raise ArgumentError, "Raw is not compatible with #{.join(" or ")}" end if cmd.empty? raise ArgumentError, "No command provided. You must specify a command to execute." end with_raw_output(raw) do pre_connect_if_required cmd = Kamal::Utils.join_commands(cmd) env = [:env] detach = [:detach] quiet = [:quiet] case when [:interactive] && [:reuse] say "Get current version of running container...", :magenta unless [:version] using_version([: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, host: KAMAL.primary_host).execute_in_existing_container_over_ssh(cmd, env: env) } end when [:interactive] say "Get most recent version available as an image...", :magenta unless [: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 on(KAMAL.primary_host) { execute *KAMAL.registry.login } run_locally do exec KAMAL.app(role: KAMAL.primary_role, host: KAMAL.primary_host).execute_in_new_container_over_ssh(cmd, env: env) end end when [:reuse] say "Get current version of running container...", :magenta unless [:version] using_version([:version] || current_running_version) do |version| say "Launching command with version #{version} from existing container...", :magenta on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, 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, host: host).execute_in_existing_container(cmd, env: env), strip: !raw), quiet: quiet, raw: raw end end else say "Get most recent version available as an image...", :magenta unless [:version] using_version(version_or_latest) do |version| say "Launching command with version #{version} from new container...", :magenta on(KAMAL.app_hosts) { execute *KAMAL.registry.login } on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, 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, host: host).execute_in_new_container(cmd, env: env, detach: detach), strip: !raw), quiet: quiet, raw: raw end end end end end |
#images ⇒ Object
205 206 207 208 |
# File 'lib/kamal/cli/app.rb', line 205 def images quiet = [:quiet] on(KAMAL.app_hosts) { |host| puts_by_host host, capture_with_info(*KAMAL.app.list_images), quiet: quiet } end |
#live ⇒ Object
265 266 267 268 269 270 271 |
# File 'lib/kamal/cli/app.rb', line 265 def live modify(lock: true) do on_roles(KAMAL.roles, hosts: KAMAL.proxy_hosts) do |host, role| execute *KAMAL.app(role: role, host: host).live if role.running_proxy? end end end |
#logs ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/kamal/cli/app.rb', line 218 def logs # FIXME: Catch when app containers aren't running grep = [:grep] = [:grep_options] since = [:since] container_id = [:container_id] = ![:skip_timestamps] quiet = [:quiet] if [:follow] lines = [:lines].presence || ((since || grep) ? nil : 10) # Default to 10 lines if since or grep isn't set run_locally do info "Following logs on #{KAMAL.primary_host}..." KAMAL.specific_roles ||= [ KAMAL.primary_role.name ] role = KAMAL.roles_on(KAMAL.primary_host).first app = KAMAL.app(role: role, host: host) info app.follow_logs(host: KAMAL.primary_host, container_id: container_id, timestamps: , lines: lines, grep: grep, grep_options: ) exec app.follow_logs(host: KAMAL.primary_host, container_id: container_id, timestamps: , lines: lines, grep: grep, grep_options: ) end else lines = [:lines].presence || ((since || grep) ? nil : 100) # Default to 100 lines if since or grep isn't set on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, role| begin puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).logs(container_id: container_id, timestamps: , since: since, lines: lines, grep: grep, grep_options: )), quiet: quiet rescue SSHKit::Command::Failed puts_by_host host, "Nothing found", quiet: quiet end end end end |
#maintenance ⇒ Object
276 277 278 279 280 281 282 283 284 |
# File 'lib/kamal/cli/app.rb', line 276 def maintenance = { drain_timeout: [:drain_timeout] || KAMAL.config.drain_timeout, message: [:message] } modify(lock: true) do on_roles(KAMAL.roles, hosts: KAMAL.proxy_hosts) do |host, role| execute *KAMAL.app(role: role, host: host).maintenance(**) if role.running_proxy? end end end |
#remove ⇒ Object
255 256 257 258 259 260 261 262 |
# File 'lib/kamal/cli/app.rb', line 255 def remove modify(lock: true) do stop remove_containers remove_images remove_app_directories end end |
#remove_app_directories ⇒ Object
329 330 331 332 333 334 335 336 337 |
# File 'lib/kamal/cli/app.rb', line 329 def remove_app_directories modify(lock: true) do on(hosts_removing_all_roles) do |host| execute *KAMAL.server.remove_app_directory, raise_on_non_zero_exit: false execute *KAMAL.auditor.record("Removed #{KAMAL.config.app_directory}"), verbosity: :debug execute *KAMAL.app.remove_proxy_app_directory, raise_on_non_zero_exit: false end end end |
#remove_container(version) ⇒ Object
299 300 301 302 303 304 305 306 |
# File 'lib/kamal/cli/app.rb', line 299 def remove_container(version) modify(lock: true) do on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, role| execute *KAMAL.auditor.record("Removed app container with version #{version}", role: role), verbosity: :debug execute *KAMAL.app(role: role, host: host).remove_container(version: version) end end end |
#remove_containers ⇒ Object
309 310 311 312 313 314 315 316 |
# File 'lib/kamal/cli/app.rb', line 309 def remove_containers modify(lock: true) do on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, role| execute *KAMAL.auditor.record("Removed all app containers", role: role), verbosity: :debug execute *KAMAL.app(role: role, host: host).remove_containers end end end |
#remove_images ⇒ Object
319 320 321 322 323 324 325 326 |
# File 'lib/kamal/cli/app.rb', line 319 def remove_images modify(lock: true) do on(hosts_removing_all_roles) do execute *KAMAL.auditor.record("Removed all app images"), verbosity: :debug execute *KAMAL.app.remove_images end end end |
#rollout(action) ⇒ Object
289 290 291 292 293 294 295 296 |
# File 'lib/kamal/cli/app.rb', line 289 def rollout(action) case action when "deploy" then start_rollout when "set" then set_rollout when "stop" then stop_rollout else raise ArgumentError, "Unknown rollout action '#{action}', expected deploy, set or stop" end end |
#stale_containers ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/kamal/cli/app.rb', line 182 def stale_containers quiet = [:quiet] stop = [:stop] with_lock_if_stopping do on_roles(KAMAL.roles, hosts: KAMAL.app_hosts) do |host, role| app = KAMAL.app(role: role, host: host) versions = capture_with_info(*app.list_versions, raise_on_non_zero_exit: false).split("\n") versions -= [ capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip ] versions.each do |version| if stop puts_by_host host, "Stopping stale container for role #{role} with version #{version}", quiet: quiet execute *app.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)", quiet: quiet end end end end end |
#start ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/kamal/cli/app.rb', line 51 def start modify(lock: true) do cli = self on_roles(KAMAL.roles, hosts: KAMAL.app_hosts, parallel: KAMAL.config.parallel_roles?, rolling: true) do |host, role| app = KAMAL.app(role: role, host: host) execute *KAMAL.auditor.record("Started app version #{KAMAL.config.version}"), verbosity: :debug execute *app.start, raise_on_non_zero_exit: false if role.running_proxy? version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip endpoint = capture_with_info(*app.container_id_for_version(version)).strip raise Kamal::Cli::BootError, "Failed to get endpoint for #{role} on #{host}, did the container boot?" if endpoint.empty? cli.run_hook "pre-proxy-deploy", hosts: host.to_s, role: role.name execute *app.deploy(target: endpoint) cli.run_hook "post-proxy-deploy", hosts: host.to_s, role: role.name end end end end |
#stop ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kamal/cli/app.rb', line 74 def stop modify(lock: true) do on_roles(KAMAL.roles, hosts: KAMAL.app_hosts, parallel: KAMAL.config.parallel_roles?) do |host, role| app = KAMAL.app(role: role, host: host) execute *KAMAL.auditor.record("Stopped app", role: role), verbosity: :debug if role.running_proxy? version = capture_with_info(*app.current_running_version, raise_on_non_zero_exit: false).strip endpoint = capture_with_info(*app.container_id_for_version(version)).strip if endpoint.present? execute *app.remove, raise_on_non_zero_exit: false end end execute *app.stop, raise_on_non_zero_exit: false end end end |
#version ⇒ Object
340 341 342 343 344 345 346 |
# File 'lib/kamal/cli/app.rb', line 340 def version quiet = [:quiet] on(KAMAL.app_hosts) do |host| role = KAMAL.roles_on(host).first puts_by_host host, capture_with_info(*KAMAL.app(role: role, host: host).current_running_version).strip, quiet: quiet end end |