From 251286d1f82517bd0e1d8ac71cb506d751317322 Mon Sep 17 00:00:00 2001 From: Ahmad Wilson Date: Tue, 28 Mar 2017 19:08:22 +0000 Subject: [PATCH] Add startup_services dock configuration functionality This change enables owners of projects to specify which services, defined within the project's docker-compose file, to launch when standing up the project within an extended dock environment. Particulary of note here is that this change allows project owners to target variations of services and backend components depending on context. --- bin/dock | 21 +++++++++++++- script/entrypoint.bash | 2 +- test/options/extend.bats | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/bin/dock b/bin/dock index 8f2c05a..47a7d64 100755 --- a/bin/dock +++ b/bin/dock @@ -266,6 +266,15 @@ image() { fi } +startup_services() { + if [ -z "${1+x}" ]; then + error "Must provide list of services (e.g service mysql)!" + return 1 + else + startup_services="$1" + fi +} + pull_latest() { if [ -z "${1+x}" ] || "$1"; then pull=true @@ -506,6 +515,13 @@ extend_container() { info "Unable to locate a $default_compose_file file for ${project}." fi + # Record the list of services to startup for this project and + # append to startup services list for container environment + existing_services="$(get_label_value $container_name startup_services)" + s="${existing_services} ${startup_services:- }" + s="$(echo $s | xargs -n1 | sort -u | xargs)" + label "startup_services" "$s" + # proceed to launch extended dock container... # TODO: refactor below common setup (i.e. build of docker run args) to remove code # duplication @@ -649,7 +665,10 @@ terraform_container() { docker-compose config > ${tmp_workspace}/$output_file" compose_args+=("--file" "$output_file") done - compose_args+=("up" "-d") + # Only start services which have been defined as startup services by composed + # projects + local services=$(get_label_value "$container_name" "startup_services") + compose_args+=("up" "-d" "$services") # Purge all existing containers within Dock environment info "Purging existing Dock environment..." diff --git a/script/entrypoint.bash b/script/entrypoint.bash index 6d56287..79a616a 100755 --- a/script/entrypoint.bash +++ b/script/entrypoint.bash @@ -11,7 +11,7 @@ start_docker() { return fi - sudo dockerd >/dev/null 2>&1 & + sudo dockerd>/dev/null 2>&1 & dockerd_pid=$! local max_tries=5 diff --git a/test/options/extend.bats b/test/options/extend.bats index 921164e..5142c84 100644 --- a/test/options/extend.bats +++ b/test/options/extend.bats @@ -17,6 +17,7 @@ teardown() { @test "configuration labels are added to Dock container during extension" { file Dockerfile <<-EOF FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* EOF file .dock <<-EOF @@ -40,6 +41,7 @@ EOF @test "compose configuration label is not added to Dock container if compose file does not exist" { file Dockerfile <<-EOF FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* EOF file .dock <<-EOF @@ -60,6 +62,7 @@ EOF @test "workspace dir is set as repo root of project during extension" { file Dockerfile <<-EOF FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* EOF file .dock <<-EOF @@ -81,6 +84,7 @@ EOF @test "extending a project overrides previous Dock configuration labels for project" { file Dockerfile <<-EOF FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* EOF file .dock <<-EOF @@ -109,6 +113,7 @@ EOF @test "extending a non-existent Dock container successfully" { file Dockerfile <<-EOF FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* EOF file .dock <<-EOF @@ -132,6 +137,7 @@ EOF @test "extending an existing Dock container successfully" { file Dockerfile <<-EOF FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* EOF echo "mytesting" > /tmp/myfile @@ -164,3 +170,58 @@ EOF # verify project ports are published docker port test | grep 8888 } + +@test "startup_services labels are set when specified by project configuration" { + file Dockerfile <<-EOF +FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* +EOF + + file .dock <<-EOF +dockerfile Dockerfile +startup_services 'service backend' +default_command sh +EOF + + file docker-compose.yml <<-EOF +version: '2' +EOF + + run dock -e test + + [ "$status" -eq 0 ] + # verify startup_services label has been applied + labels="$(get_labels test)" + [ "$(echo "$labels" | grep -c startup_services)" -eq 1 ] + echo "$labels" | grep service + echo "$labels" | grep backend +} + +@test "duplicate startup_services label values are filtered during extension" { + file Dockerfile <<-EOF +FROM alpine:latest +RUN apk update && apk add jq && rm -rf /var/cache/apk/* +EOF + + file .dock <<-EOF +dockerfile Dockerfile +startup_services 'another_service backend another_backend' +default_command sh +EOF + + file docker-compose.yml <<-EOF +version: '2' +EOF + + docker run --name test -d --label startup_services='service backend' alpine:latest sh + run dock -e test + + [ "$status" -eq 0 ] + # verify duplicated startup_services labels are filtered accordingly + labels="$(get_labels test)" + [ "$(echo "$labels" | grep -c startup_services)" -eq 1 ] + [ "$(echo "$labels" | grep -c service)" -eq 1 ] + [ "$(echo "$labels" | grep -c another_service)" -eq 1 ] + [ "$(echo "$labels" | grep -c backend)" -eq 1 ] + [ "$(echo "$labels" | grep -c another_backend)" -eq 1 ] +}