Skip to content

Commit

Permalink
Merge pull request #51 from buildkite-plugins/fix-broken-image-prebuild
Browse files Browse the repository at this point in the history
Fix broken prebuild, closes #50
  • Loading branch information
lox authored Apr 27, 2017
2 parents c41f62f + af8627c commit 1ee4e5c
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 87 deletions.
17 changes: 17 additions & 0 deletions .buildkite/pipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,21 @@ steps:
run: helloworldimage
config: tests/composefiles/docker-compose.v2.1.yml
- wait
- label: build with image-repository
command: /hello
plugins:
${BUILDKITE_REPO}#${commit}:
build: helloworld
image-repository: buildkiteci/docker-compose-buildkite-plugin
config: tests/composefiles/docker-compose.v2.1.yml
- wait
- label: run after build with pre-built image
command: /hello
plugins:
${BUILDKITE_REPO}#${commit}:
run: helloworld
config: tests/composefiles/docker-compose.v2.1.yml
YAML
1 change: 1 addition & 0 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -ueo pipefail
DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"

. "$DIR/../lib/shared.bash"
. "$DIR/../lib/metadata.bash"

if [[ -n "$(plugin_read_list BUILD)" ]]; then
. "$DIR/commands/build.sh"
Expand Down
6 changes: 4 additions & 2 deletions hooks/commands/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ if [[ -n "$image_repository" ]]; then

i=0
while [[ ${#build_images[@]} -gt 0 ]] ; do
plugin_set_build_image_metadata "${build_images[@]:0:2}"
plugin_set_build_image_metadata "$i" "${build_images[@]:1:2}"
plugin_set_metadata "built-image-tag-${build_images[0]}" "${build_images[1]}"
plugin_set_metadata "built-image-tag-${i}" "${build_images[0]}"
build_images=("${build_images[@]:2}")
i=$((i+1))
done

plugin_set_metadata "built-image-count" "$i"
fi
6 changes: 4 additions & 2 deletions hooks/commands/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ test -f "$override_file" && rm "$override_file"

built_images=( $(get_prebuilt_images_from_metadata) )

echo "~~~ :docker: Found $((${#built_images[@]}/2)) pre-built services"

if [[ ${#built_images[@]} -gt 0 ]] ; then
echo "~~~ :docker: Creating a modified docker-compose config for pre-built images"
echo "~~~ :docker: Creating a modified docker-compose config for pre-built images" >&2;
build_image_override_file "${built_images[@]}" | tee "$override_file"
built_services=( $(get_services_from_map "${built_images[@]}") )

echo "~~~ :docker: Pulling pre-built services ${built_services[*]}"
run_docker_compose -f "$override_file" pull "${built_services[@]}"
fi

echo "+++ :docker: Running command in Docker Compose service: $service_name"
echo "+++ :docker: Running command in Docker Compose service: $service_name" >&2;
set +e

# $BUILDKITE_COMMAND needs to be unquoted because:
Expand Down
45 changes: 45 additions & 0 deletions lib/metadata.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

readonly META_IMAGE_COUNT=built-image-count
readonly META_IMAGE_TAG_IDX=built-image-tag-
readonly META_IMAGE_TAG=built-image-tag-

# Read agent metadata for the plugin
function plugin_get_metadata() {
local key="docker-compose-plugin-$1"
plugin_prompt buildkite-agent meta-data get "$key"
buildkite-agent meta-data get "$key"
}

# Write agent metadata for the plugin
function plugin_set_metadata() {
local key="docker-compose-plugin-$1"
local value="$2"
plugin_prompt_and_must_run buildkite-agent meta-data set "$key" "$value"
}

# Gets a list of service / image pairs, each pair on a newline, delimited by space
function get_prebuilt_images_from_metadata() {
local service
local image
local count
count=$(plugin_get_metadata "$META_IMAGE_COUNT")

[[ $count -gt 0 ]] || return 0

for i in $(seq 0 $((count-1))) ; do
service="$(plugin_get_metadata "${META_IMAGE_TAG_IDX}${i}")"
image="$(plugin_get_metadata "${META_IMAGE_TAG}${service}")"
echo "$service $image"
i=$((i+1))
done
}

# Helper for use with get_prebuilt_images_from_metadata
function get_services_from_map() {
for ((n=1;n<$#;n++)) ; do
if (( $((n % 2)) == 1 )) ; then
echo ${!n}
fi
done
}
22 changes: 0 additions & 22 deletions lib/run.bash
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ compose_cleanup() {
fi
}

get_prebuilt_images_from_metadata() {
local value
for i in {0..10} ; do
if ! value="$(plugin_get_build_image_metadata "$i")" ; then
return $?
fi
if [[ -z "$value" ]] ; then
break
fi
echo "$value"
i=$((i+1))
done
}

get_services_from_map() {
for ((n=1;n<$#;n++)) ; do
if (( $((n % 2)) == 1 )) ; then
echo ${!n}
fi
done
}

list_linked_containers() {
for container_id in $(HIDE_PROMPT=1 run_docker_compose ps -q); do
docker inspect --format='{{.Name}}' "$container_id"
Expand Down
16 changes: 0 additions & 16 deletions lib/shared.bash
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@ function plugin_read_config() {
echo "${!var:-$default}"
}

# Read agent metadata for pre-built images
function plugin_get_build_image_metadata() {
local service="$1"
local key="docker-compose-plugin-built-image-tag-${service}"
plugin_prompt buildkite-agent meta-data get "$key"
buildkite-agent meta-data get "$key"
}

# Write agent metadata for pre-built images
function plugin_set_build_image_metadata() {
local service="$1"
local value="$2"
plugin_prompt_and_must_run buildkite-agent meta-data set \
"docker-compose-plugin-built-image-tag-${service}" "$value"
}

# Reads either a value or a list from plugin config
function plugin_read_list() {
local prefix="BUILDKITE_PLUGIN_DOCKER_COMPOSE_$1"
Expand Down
28 changes: 16 additions & 12 deletions tests/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ load '../lib/shared'
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml push myservice : echo pushed myservice" \

stub buildkite-agent \
"meta-data set docker-compose-plugin-built-image-tag-myservice my.repository/llamas:test-myservice-build-1 : echo set legacy metadata" \
"meta-data set docker-compose-plugin-built-image-tag-0 my.repository/llamas:test-myservice-build-1 : echo set metadata 0"
"meta-data set docker-compose-plugin-built-image-tag-myservice my.repository/llamas:test-myservice-build-1 : echo set image metadata 0" \
"meta-data set docker-compose-plugin-built-image-tag-0 myservice : echo set key metadata 0 " \
"meta-data set docker-compose-plugin-built-image-count 1 : echo set image count"

run $PWD/hooks/command

assert_success
assert_output --partial "built myservice"
assert_output --partial "pushed myservice"
assert_output --partial "set legacy metadata"
assert_output --partial "set metadata 0"
assert_output --partial "set image metadata 0"
assert_output --partial "set key metadata 0"
assert_output --partial "set image count"
unstub docker-compose
unstub buildkite-agent
}
Expand All @@ -62,20 +64,22 @@ load '../lib/shared'
"-f docker-compose.yml -p buildkite1112 -f docker-compose.buildkite-1-override.yml push myservice1 myservice2 : echo pushed all services" \

stub buildkite-agent \
"meta-data set docker-compose-plugin-built-image-tag-myservice1 my.repository/llamas:test-myservice1-build-1 : echo set legacy metadata 1" \
"meta-data set docker-compose-plugin-built-image-tag-0 my.repository/llamas:test-myservice1-build-1 : echo set metadata 0" \
"meta-data set docker-compose-plugin-built-image-tag-myservice2 my.repository/llamas:test-myservice2-build-1 : echo set legacy metadata 2" \
"meta-data set docker-compose-plugin-built-image-tag-1 my.repository/llamas:test-myservice2-build-1 : echo set metadata 1"
"meta-data set docker-compose-plugin-built-image-tag-myservice1 my.repository/llamas:test-myservice1-build-1 : echo set image metadata 0" \
"meta-data set docker-compose-plugin-built-image-tag-0 myservice1 : echo set key metadata 0" \
"meta-data set docker-compose-plugin-built-image-tag-myservice2 my.repository/llamas:test-myservice2-build-1 : echo set image metadata 1" \
"meta-data set docker-compose-plugin-built-image-tag-1 myservice2 : echo set key metadata 1" \
"meta-data set docker-compose-plugin-built-image-count 2 : echo set image count"

run $PWD/hooks/command

assert_success
assert_output --partial "built all services"
assert_output --partial "pushed all services"
assert_output --partial "set legacy metadata 1"
assert_output --partial "set legacy metadata 2"
assert_output --partial "set metadata 0"
assert_output --partial "set metadata 1"
assert_output --partial "set image metadata 0"
assert_output --partial "set key metadata 0"
assert_output --partial "set image metadata 1"
assert_output --partial "set key metadata 1"
assert_output --partial "set image count"
unstub docker-compose
unstub buildkite-agent
}
46 changes: 46 additions & 0 deletions tests/prebuilt-image-metadata.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

#!/usr/bin/env bats

load '/usr/local/lib/bats/load.bash'
load '../lib/shared'
load '../lib/metadata'

@test "Get prebuilt images from agent metadata (two images)" {
stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-count : echo 2" \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo service1" \
"meta-data get docker-compose-plugin-built-image-tag-service1 : echo image" \
"meta-data get docker-compose-plugin-built-image-tag-1 : echo service2" \
"meta-data get docker-compose-plugin-built-image-tag-service2 : echo image "

run get_prebuilt_images_from_metadata

assert_success
assert_output --partial "service1 image"
assert_output --partial "service2 image"
unstub buildkite-agent
}

@test "Get prebuilt images from agent metadata (no images)" {
stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-count : echo 0"

run get_prebuilt_images_from_metadata

assert_success
unstub buildkite-agent
}

@test "Get services from an image map" {
image_map=(
"myservice1" "myimage1"
"myservice2" "myimage2"
)
run get_services_from_map "${image_map[@]}"

assert_success
assert_equal "${#lines[@]}" "2"
assert_equal "${lines[0]}" "myservice1"
assert_equal "${lines[1]}" "myservice2"
}

42 changes: 9 additions & 33 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,6 @@ load '../lib/run'
# export BUILDKITE_AGENT_STUB_DEBUG=/dev/tty
# export BATS_MOCK_TMPDIR=$PWD

@test "Get services from an image map" {
image_map=(
"myservice1" "myimage1"
"myservice2" "myimage2"
)
run get_services_from_map "${image_map[@]}"

assert_success
assert_equal "${#lines[@]}" "2"
assert_equal "${lines[0]}" "myservice1"
assert_equal "${lines[1]}" "myservice2"
}

@test "Get prebuilt images from agent metadata" {
stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo service1 image" \
"meta-data get docker-compose-plugin-built-image-tag-1 : echo service2 image" \
"meta-data get docker-compose-plugin-built-image-tag-2 : echo "

run get_prebuilt_images_from_metadata

assert_success
assert_output --partial "service1 image"
assert_output --partial "service2 image"
unstub buildkite-agent
}

@test "Run without a prebuilt image" {
export BUILDKITE_JOB_ID=1111
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_RUN=myservice
Expand All @@ -48,7 +21,7 @@ load '../lib/run'
"-f docker-compose.yml -p buildkite1111 run myservice pwd : echo ran myservice"

stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo "
"meta-data get docker-compose-plugin-built-image-count : echo 0"

run $PWD/hooks/command

Expand All @@ -72,8 +45,9 @@ load '../lib/run'
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run myservice pwd : echo ran myservice"

stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo myservice myimage" \
"meta-data get docker-compose-plugin-built-image-tag-1 : echo "
"meta-data get docker-compose-plugin-built-image-count : echo 1" \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo myservice" \
"meta-data get docker-compose-plugin-built-image-tag-myservice : echo myimage"

run $PWD/hooks/command

Expand All @@ -97,9 +71,11 @@ load '../lib/run'
"-f docker-compose.yml -p buildkite1111 -f docker-compose.buildkite-1-override.yml run myservice1 pwd : echo ran myservice"

stub buildkite-agent \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo myservice1 myimage" \
"meta-data get docker-compose-plugin-built-image-tag-1 : echo myservice2 myimage " \
"meta-data get docker-compose-plugin-built-image-tag-2 : echo " \
"meta-data get docker-compose-plugin-built-image-count : echo 2" \
"meta-data get docker-compose-plugin-built-image-tag-0 : echo myservice1" \
"meta-data get docker-compose-plugin-built-image-tag-myservice1 : echo myimage" \
"meta-data get docker-compose-plugin-built-image-tag-1 : echo myservice2" \
"meta-data get docker-compose-plugin-built-image-tag-myservice2 : echo myimage"

run $PWD/hooks/command

Expand Down

0 comments on commit 1ee4e5c

Please sign in to comment.