Class: Dash::Commands::App

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

Defined Under Namespace

Modules: Assets, Containers, ErrorPages, Execution, Images, Logging, Proxy

Constant Summary collapse

ACTIVE_DOCKER_STATUSES =
[ :running, :restarting ]
BOOT_STATE_SEPARATOR =

Separates the two answers #boot_state and #stale_state return. A container id is hex and a version is a name suffix, so neither can produce this line on its own.

"--%--"

Constants included from Containers

Containers::DOCKER_HEALTH_LOG_FORMAT

Constants inherited from Base

Base::DOCKER_HEALTH_STATUS_FORMAT, Base::EXEC_PROBE_FAILED, Base::NO_HEALTHCHECK, Base::READINESS_PROGRESS_PREFIX, Base::READY_STATUSES

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Class Method Summary collapse

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 ErrorPages

#clean_up_error_pages, #create_error_pages_directory

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 included from Proxy

#create_ssl_directory, #deploy, #live, #maintenance, #remove, #remove_proxy_app_directory, #rollout_deploy, #rollout_set, #rollout_stop

Methods inherited from Base

#confirmed_empty?, #container_id_for, #ensure_docker_installed, #ensure_run_directory, #make_directory, #make_directory_for, #read_file, #remove_directory, #remove_file, #run_over_ssh

Constructor Details

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

Returns a new instance of App.



20
21
22
23
24
# File 'lib/dash/commands/app.rb', line 20

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

Instance Attribute Details

#host ⇒ Object (readonly)

Returns the value of attribute host.



16
17
18
# File 'lib/dash/commands/app.rb', line 16

def host
  @host
end

#role ⇒ Object (readonly)

Returns the value of attribute role.



16
17
18
# File 'lib/dash/commands/app.rb', line 16

def role
  @role
end

Class Method Details

.split_state(output) ⇒ Object

The two halves of a #boot_state or #stale_state capture, raw. Callers decide what an empty half means; the separator line itself is dropped.



12
13
14
# File 'lib/dash/commands/app.rb', line 12

def self.split_state(output)
  output.to_s.partition(/^#{Regexp.escape(BOOT_STATE_SEPARATOR)}$/).values_at(0, 2)
end

Instance Method Details

#boot_state(version) ⇒ Object

Everything a boot needs to know about a host before it starts anything: whether a container for the version being deployed already exists (so it can be renamed out of the way) and which version is running now (so it can be stopped once the new one is live). Two questions, one round trip, answers split on BOOT_STATE_SEPARATOR.

Chained with ; rather than &&: an empty answer to either is a normal result, not a failure, and the second question must be asked whatever the first one said.



120
121
122
123
124
125
# File 'lib/dash/commands/app.rb', line 120

def boot_state(version)
  chain \
    container_id_for_version(version),
    [ :echo, BOOT_STATE_SEPARATOR ],
    current_running_version
end

#container_id_for_version(version, only_running: false) ⇒ Object



103
104
105
# File 'lib/dash/commands/app.rb', line 103

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_id ⇒ Object



99
100
101
# File 'lib/dash/commands/app.rb', line 99

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

#current_running_version ⇒ Object



107
108
109
110
111
# File 'lib/dash/commands/app.rb', line 107

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

#ensure_env_directory ⇒ Object



143
144
145
# File 'lib/dash/commands/app.rb', line 143

def ensure_env_directory
  make_directory role.env_directory
end

#health_probe(version:) ⇒ Object

The healthcheck: exec: probe, run from the deploy host against the container that is booting. Single-quoted by #shell, so the command expands inside the container rather than in the deploy host's shell. Non-zero exit means not ready.



59
60
61
# File 'lib/dash/commands/app.rb', line 59

def health_probe(version:)
  docker :exec, container_name(version), *shell([ role.healthcheck.exec ])
end

#info ⇒ Object



94
95
96
# File 'lib/dash/commands/app.rb', line 94

def info
  docker :ps, *container_filter_args
end

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



137
138
139
140
141
# File 'lib/dash/commands/app.rb', line 137

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

#run(hostname: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dash/commands/app.rb', line 26

def run(hostname: nil)
  docker :run,
    "--detach",
    "--restart", role.restart_policy,
    "--name", container_name,
    "--network", "dash",
    *([ "--hostname", hostname ] if hostname),
    "--env", "KAMAL_CONTAINER_NAME=\"#{container_name}\"",
    "--env", "KAMAL_VERSION=\"#{config.version}\"",
    "--env", "KAMAL_HOST=\"#{host}\"",
    *([ "--env", "KAMAL_DESTINATION=\"#{config.destination}\"" ] if config.destination),
    *role.env_args(host),
    *role.logging_args,
    *config.volume_args,
    *role.asset_volume_args,
    *role.label_args,
    *role.option_args,
    *role.healthcheck_args,
    config.absolute_image,
    role.cmd
end

#stale_state ⇒ Object

Everything the stale check needs from a host: every version of the role that has a container, and the version running now - the difference is what is stale. Same shape as #boot_state, same separator, same reason for ; over &&.



130
131
132
133
134
135
# File 'lib/dash/commands/app.rb', line 130

def stale_state
  chain \
    list_versions,
    [ :echo, BOOT_STATE_SEPARATOR ],
    current_running_version
end

#start ⇒ Object



48
49
50
# File 'lib/dash/commands/app.rb', line 48

def start
  docker :start, container_name
end

#status(version:) ⇒ Object



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

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

#stop(version: nil) ⇒ Object



88
89
90
91
92
# File 'lib/dash/commands/app.rb', line 88

def stop(version: nil)
  pipe \
    version ? container_id_for_version(version) : current_running_container_id,
    xargs(docker(:stop, *role.stop_args))
end

#wait_for_ready(version:, timeout:) ⇒ Object

Waits on the host for the container to reach a status the poller accepts, so a boot pays one round trip for the wait however long the container takes to come up - the client-side poll paid one per attempt. Prints the status it stopped on to stdout: the moment it sees one of READY_STATUSES, or the last one it saw when the deadline passes. Progress goes to stderr once a second in between. Waiting through every other status is deliberate: docker reports a container unhealthy after three failed probes, which for an app slower than that is a state it recovers from.

Reaching the deadline exits 0, because it is an answer - the poller phrases it. Only a status that could not be read at all exits non-zero, which is a broken command and SSHKit's to raise, exactly as it was when the read was a round trip of its own.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dash/commands/app.rb', line 74

def wait_for_ready(version:, timeout:)
  shell [
    "started=$(date +%s);",
    "while true; do",
    *readiness_probe(version: version),
    "case \"$status\" in #{READY_STATUSES.join("|")}) echo \"$status\"; exit 0;; esac;",
    "elapsed=$(( $(date +%s) - started ));",
    "if [ \"$elapsed\" -ge #{timeout.to_i} ]; then echo \"$status\"; exit 0; fi;",
    "echo \"#{READINESS_PROGRESS_PREFIX} $elapsed $(( #{timeout.to_i} - elapsed )) $status\" 1>&2;",
    "sleep 1;",
    "done"
  ]
end