Skip to content

Commit

Permalink
Merge pull request #1193 from basecamp/filter-by-no-destination
Browse files Browse the repository at this point in the history
Filter correctly for empty destinations
  • Loading branch information
djmb authored Nov 4, 2024
2 parents 685312c + e9ba923 commit 9cf8da6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 62 deletions.
22 changes: 15 additions & 7 deletions lib/kamal/commands/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def stop(version: nil)
end

def info
docker :ps, *filter_args
docker :ps, *container_filter_args
end


Expand All @@ -67,7 +67,7 @@ def current_running_version

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

Expand All @@ -91,25 +91,33 @@ def latest_image_container(format:)
end

def latest_container(format:, filters: nil)
docker :ps, "--latest", *format, *filter_args(statuses: ACTIVE_DOCKER_STATUSES), argumentize("--filter", filters)
docker :ps, "--latest", *format, *container_filter_args(statuses: ACTIVE_DOCKER_STATUSES), argumentize("--filter", filters)
end

def filter_args(statuses: nil)
argumentize "--filter", filters(statuses: statuses)
def container_filter_args(statuses: nil)
argumentize "--filter", container_filters(statuses: statuses)
end

def image_filter_args
argumentize "--filter", image_filters
end

def extract_version_from_name
# Extract SHA from "service-role-dest-SHA"
%(while read line; do echo ${line##{role.container_prefix}-}; done)
end

def filters(statuses: nil)
def container_filters(statuses: nil)
[ "label=service=#{config.service}" ].tap do |filters|
filters << "label=destination=#{config.destination}" if config.destination
filters << "label=destination=#{config.destination}"
filters << "label=role=#{role}" if role
statuses&.each do |status|
filters << "status=#{status}"
end
end
end

def image_filters
[ "label=service=#{config.service}" ]
end
end
4 changes: 2 additions & 2 deletions lib/kamal/commands/app/containers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Kamal::Commands::App::Containers
DOCKER_HEALTH_LOG_FORMAT = "'{{json .State.Health}}'"

def list_containers
docker :container, :ls, "--all", *filter_args
docker :container, :ls, "--all", *container_filter_args
end

def list_container_names
Expand All @@ -20,7 +20,7 @@ def rename_container(version:, new_version:)
end

def remove_containers
docker :container, :prune, "--force", *filter_args
docker :container, :prune, "--force", *container_filter_args
end

def container_health_log(version:)
Expand Down
2 changes: 1 addition & 1 deletion lib/kamal/commands/app/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def list_images
end

def remove_images
docker :image, :prune, "--all", "--force", *filter_args
docker :image, :prune, "--all", "--force", *image_filter_args
end

def tag_latest_image
Expand Down
Loading

0 comments on commit 9cf8da6

Please sign in to comment.