-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #470 from basecamp/extract-app-concerns
Extract app concerns
- Loading branch information
Showing
10 changed files
with
161 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Kamal::Commands::App::Assets | ||
def extract_assets | ||
asset_container = "#{role_config.container_prefix}-assets" | ||
|
||
combine \ | ||
make_directory(role_config.asset_extracted_path), | ||
[*docker(:stop, "-t 1", asset_container, "2> /dev/null"), "|| true"], | ||
docker(:run, "--name", asset_container, "--detach", "--rm", config.latest_image, "sleep 1000000"), | ||
docker(:cp, "-L", "#{asset_container}:#{role_config.asset_path}/.", role_config.asset_extracted_path), | ||
docker(:stop, "-t 1", asset_container), | ||
by: "&&" | ||
end | ||
|
||
def sync_asset_volumes(old_version: nil) | ||
new_extracted_path, new_volume_path = role_config.asset_extracted_path(config.version), role_config.asset_volume.host_path | ||
if old_version.present? | ||
old_extracted_path, old_volume_path = role_config.asset_extracted_path(old_version), role_config.asset_volume(old_version).host_path | ||
end | ||
|
||
commands = [make_directory(new_volume_path), copy_contents(new_extracted_path, new_volume_path)] | ||
|
||
if old_version.present? | ||
commands << copy_contents(new_extracted_path, old_volume_path, continue_on_error: true) | ||
commands << copy_contents(old_extracted_path, new_volume_path, continue_on_error: true) | ||
end | ||
|
||
chain *commands | ||
end | ||
|
||
def clean_up_assets | ||
chain \ | ||
find_and_remove_older_siblings(role_config.asset_extracted_path), | ||
find_and_remove_older_siblings(role_config.asset_volume_path) | ||
end | ||
|
||
private | ||
def find_and_remove_older_siblings(path) | ||
[ | ||
:find, | ||
Pathname.new(path).dirname.to_s, | ||
"-maxdepth 1", | ||
"-name", "'#{role_config.container_prefix}-*'", | ||
"!", "-name", Pathname.new(path).basename.to_s, | ||
"-exec rm -rf \"{}\" +" | ||
] | ||
end | ||
|
||
def copy_contents(source, destination, continue_on_error: false) | ||
[ :cp, "-rnT", "#{source}", destination, *("|| true" if continue_on_error)] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Kamal::Commands::App::Containers | ||
def list_containers | ||
docker :container, :ls, "--all", *filter_args | ||
end | ||
|
||
def list_container_names | ||
[ *list_containers, "--format", "'{{ .Names }}'" ] | ||
end | ||
|
||
def remove_container(version:) | ||
pipe \ | ||
container_id_for(container_name: container_name(version)), | ||
xargs(docker(:container, :rm)) | ||
end | ||
|
||
def rename_container(version:, new_version:) | ||
docker :rename, container_name(version), container_name(new_version) | ||
end | ||
|
||
def remove_containers | ||
docker :container, :prune, "--force", *filter_args | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Kamal::Commands::App::Cord | ||
def cord(version:) | ||
pipe \ | ||
docker(:inspect, "-f '{{ range .Mounts }}{{printf \"%s %s\\n\" .Source .Destination}}{{ end }}'", container_name(version)), | ||
[:awk, "'$2 == \"#{role_config.cord_volume.container_path}\" {print $1}'"] | ||
end | ||
|
||
def tie_cord(cord) | ||
create_empty_file(cord) | ||
end | ||
|
||
def cut_cord(cord) | ||
remove_directory(cord) | ||
end | ||
|
||
private | ||
def create_empty_file(file) | ||
chain \ | ||
make_directory_for(file), | ||
[:touch, file] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Kamal::Commands::App::Execution | ||
def execute_in_existing_container(*command, interactive: false) | ||
docker :exec, | ||
("-it" if interactive), | ||
container_name, | ||
*command | ||
end | ||
|
||
def execute_in_new_container(*command, interactive: false) | ||
docker :run, | ||
("-it" if interactive), | ||
"--rm", | ||
*role_config&.env_args, | ||
*config.volume_args, | ||
*role_config&.option_args, | ||
config.absolute_image, | ||
*command | ||
end | ||
|
||
def execute_in_existing_container_over_ssh(*command, host:) | ||
run_over_ssh execute_in_existing_container(*command, interactive: true), host: host | ||
end | ||
|
||
def execute_in_new_container_over_ssh(*command, host:) | ||
run_over_ssh execute_in_new_container(*command, interactive: true), host: host | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Kamal::Commands::App::Images | ||
def list_images | ||
docker :image, :ls, config.repository | ||
end | ||
|
||
def remove_images | ||
docker :image, :prune, "--all", "--force", *filter_args | ||
end | ||
|
||
def tag_current_image_as_latest | ||
docker :tag, config.absolute_image, config.latest_image | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Kamal::Commands::App::Logging | ||
def logs(since: nil, lines: nil, grep: nil) | ||
pipe \ | ||
current_running_container_id, | ||
"xargs docker logs#{" --since #{since}" if since}#{" --tail #{lines}" if lines} 2>&1", | ||
("grep '#{grep}'" if grep) | ||
end | ||
|
||
def follow_logs(host:, grep: nil) | ||
run_over_ssh \ | ||
pipe( | ||
current_running_container_id, | ||
"xargs docker logs --timestamps --tail 10 --follow 2>&1", | ||
(%(grep "#{grep}") if grep) | ||
), | ||
host: host | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters