Class: Kamal::Commands::App

Inherits:
Base
  • Object
show all
Includes:
Assets, Containers, Cord, Execution, Images, Logging
Defined in:
lib/kamal/commands/app.rb

Defined Under Namespace

Modules: Assets, Containers, Cord, Execution, Images, Logging

Constant Summary collapse

ACTIVE_DOCKER_STATUSES =
[ :running, :restarting ]

Constants included from Containers

Containers::DOCKER_HEALTH_LOG_FORMAT

Constants inherited from Base

Base::DOCKER_HEALTH_STATUS_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods included from Assets

#clean_up_assets, #extract_assets, #sync_asset_volumes

Methods included from Containers

#container_health_log, #list_container_names, #list_containers, #remove_container, #remove_containers, #rename_container

Methods included from Cord

#cord, #cut_cord, #tie_cord

Methods included from Execution

#execute_in_existing_container, #execute_in_existing_container_over_ssh, #execute_in_new_container, #execute_in_new_container_over_ssh

Methods included from Images

#list_images, #remove_images, #tag_latest_image

Methods included from Logging

#follow_logs, #logs

Methods inherited from Base

#container_id_for, #make_directory, #make_directory_for, #remove_directory, #run_over_ssh

Constructor Details

#initialize(config, role: nil, host: nil) ⇒ App

Returns a new instance of App.



8
9
10
11
12
# File 'lib/kamal/commands/app.rb', line 8

def initialize(config, role: nil, host: nil)
  super(config)
  @role = role
  @host = host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/kamal/commands/app.rb', line 6

def host
  @host
end

#roleObject (readonly)

Returns the value of attribute role.



6
7
8
# File 'lib/kamal/commands/app.rb', line 6

def role
  @role
end

Instance Method Details

#container_id_for_version(version, only_running: false) ⇒ Object



56
57
58
# File 'lib/kamal/commands/app.rb', line 56

def container_id_for_version(version, only_running: false)
  container_id_for(container_name: container_name(version), only_running: only_running)
end

#current_running_container_idObject



52
53
54
# File 'lib/kamal/commands/app.rb', line 52

def current_running_container_id
  current_running_container(format: "--quiet")
end

#current_running_versionObject



60
61
62
63
64
# File 'lib/kamal/commands/app.rb', line 60

def current_running_version
  pipe \
    current_running_container(format: "--format '{{.Names}}'"),
    extract_version_from_name
end

#infoObject



47
48
49
# File 'lib/kamal/commands/app.rb', line 47

def info
  docker :ps, *filter_args
end

#list_versions(*docker_args, statuses: nil) ⇒ Object



66
67
68
69
70
# File 'lib/kamal/commands/app.rb', line 66

def list_versions(*docker_args, statuses: nil)
  pipe \
    docker(:ps, *filter_args(statuses: statuses), *docker_args, "--format", '"{{.Names}}"'),
    extract_version_from_name
end

#make_env_directoryObject



73
74
75
# File 'lib/kamal/commands/app.rb', line 73

def make_env_directory
  make_directory role.env(host).secrets_directory
end

#remove_env_fileObject



77
78
79
# File 'lib/kamal/commands/app.rb', line 77

def remove_env_file
  [ :rm, "-f", role.env(host).secrets_file ]
end

#run(hostname: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kamal/commands/app.rb', line 14

def run(hostname: nil)
  docker :run,
    "--detach",
    "--restart unless-stopped",
    "--name", container_name,
    *([ "--hostname", hostname ] if hostname),
    "-e", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
    "-e", "KAMAL_VERSION=\"#{config.version}\"",
    *role.env_args(host),
    *role.health_check_args,
    *role.logging_args,
    *config.volume_args,
    *role.asset_volume_args,
    *role.label_args,
    *role.option_args,
    config.absolute_image,
    role.cmd
end

#startObject



33
34
35
# File 'lib/kamal/commands/app.rb', line 33

def start
  docker :start, container_name
end

#status(version:) ⇒ Object



37
38
39
# File 'lib/kamal/commands/app.rb', line 37

def status(version:)
  pipe container_id_for_version(version), xargs(docker(:inspect, "--format", DOCKER_HEALTH_STATUS_FORMAT))
end

#stop(version: nil) ⇒ Object



41
42
43
44
45
# File 'lib/kamal/commands/app.rb', line 41

def stop(version: nil)
  pipe \
    version ? container_id_for_version(version) : current_running_container_id,
    xargs(config.stop_wait_time ? docker(:stop, "-t", config.stop_wait_time) : docker(:stop))
end