Skip to content

Commit

Permalink
Add startup_services dock configuration functionality
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
0xOI committed Mar 29, 2017
1 parent 6e6aefd commit 251286d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
21 changes: 20 additions & 1 deletion bin/dock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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..."
Expand Down
2 changes: 1 addition & 1 deletion script/entrypoint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions test/options/extend.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 ]
}

0 comments on commit 251286d

Please sign in to comment.