Skip to content

Commit

Permalink
fix(component_image): Don't leave residual images in build project. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jtk54 committed May 11, 2017
1 parent 931232e commit f86d2d6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
37 changes: 31 additions & 6 deletions dev/build_google_component_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

set -e
set -o pipefail

# Import some functions from other scripts.
source $(dirname $0)/build_google_image_functions.sh
Expand Down Expand Up @@ -206,12 +207,23 @@ function create_component_image() {

delete_prototype_disk

# Set $PROJECT to the publish project so we can clear the target
# image and disk if it exists.
PROJECT=$PUBLISH_PROJECT
delete_disk_if_exists $TARGET_IMAGE
delete_image_if_exists $TARGET_IMAGE
bash spinnaker/google/dev/publish_gce_release.sh \
--zone $ZONE \
--service_account $ACCOUNT \
--original_image $TARGET_IMAGE \
--original_project $BUILD_PROJECT \
--publish_image $TARGET_IMAGE \
--publish_project $PUBLISH_PROJECT

# Clear the image and disk from the build project after the copy.
PROJECT=$BUILD_PROJECT
delete_disk_if_exists $TARGET_IMAGE
delete_image_if_exists $TARGET_IMAGE
}


Expand Down Expand Up @@ -258,18 +270,23 @@ SSH_KEY_FILE=$HOME/.ssh/google_empty
fix_defaults
create_empty_ssh_key

PIDS=

for artifact in "${!COMPONENTS[@]}"; do
service=${COMPONENTS[$artifact]}
LOG="create-${service}-image.log"
echo "Creating component image for $service with artifact $artifact; output will be logged to $LOG..."
create_component_image $artifact $service &> $LOG &
PID=$!
PIDS+=($PID)
done
echo "Waiting on PIDs: ${PIDS[@]}..."
wait ${PIDS[@]}

# We track the subprocesses by job instead of pid since
# waiting for the pids requires the subprocesses still be running.
# If we are waiting for process i, and process i+1 finishes, `wait`
# will fail to find process i+1 and incorrectly fail and exit.
# Job IDs exist and can be waited on even after the actual subprocess exits.
FAILED=0
for job in $(jobs -p); do
echo "Waiting for job $job"
wait $job || let "FAILED+=1"
done

for logfile in *-image.log; do
echo "---- start $logfile ----"
Expand All @@ -279,4 +296,12 @@ for logfile in *-image.log; do
echo "---- end $logfile ----"
done

if [ "$FAILED" == "0" ]; then
echo "All jobs succeeded."
else
echo "Some jobs failed. Exiting..."
exit "$FAILED"
fi


echo "`date`: DONE"
19 changes: 18 additions & 1 deletion dev/build_google_image_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,30 @@ function image_from_prototype_disk() {
}


function delete_disk_if_exists() {
local target_disk="$1"

echo "gcloud compute disks describe $target_disk --project $PROJECT --account $ACCOUNT --zone $ZONE"
if gcloud compute disks describe $target_disk \
--project $PROJECT \
--account $ACCOUNT \
--zone $ZONE &> /dev/null; then
echo "`date`: Deleting preexisting disk '$target_disk' in '$PROJECT'"
gcloud compute disks delete $target_disk \
--project $PROJECT \
--account $ACCOUNT \
--zone $ZONE
fi
}


function delete_image_if_exists() {
local target_image="$1"

if gcloud compute images describe $target_image \
--project $PROJECT \
--account $ACCOUNT &> /dev/null; then
echo "`date`: Deleting preexisting '$target_image' in '$PROJECT'"
echo "`date`: Deleting preexisting image '$target_image' in '$PROJECT'"
gcloud compute images delete $target_image \
--project $PROJECT \
--account $ACCOUNT \
Expand Down
5 changes: 3 additions & 2 deletions google/dev/publish_gce_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

set -e
set -u
set -o pipefail

SERVICE_ACCOUNT=${SERVICE_ACCOUNT:-""}
ORIGINAL_ARTIFACT_REPO_PATH=${ORIGINAL_ARTIFACT_REPO_PATH:-""}
Expand Down Expand Up @@ -57,7 +58,7 @@ function validate_args() {
die_if_empty "$ORIGINAL_PROJECT_ID" "--original_project_id"
die_if_empty "$ZONE" "--zone"


if [[ "$PUBLISH_ARTIFACT_REPO_PATH" && ! "$ORIGINAL_ARTIFACT_REPO_PATH" ]] \
|| [[ ! "$PUBLISH_ARTIFACT_REPO_PATH" \
&& "$ORIGINAL_ARTIFACT_REPO_PATH" ]]; then
Expand Down Expand Up @@ -178,7 +179,7 @@ gcloud compute disks delete "$PUBLISH_IMAGE" $GCLOUD_ACCOUNT_ARG\
if [[ "$PUBLISH_ARTIFACT_REPO_PATH" ]]; then
echo "Copying artifact repository"
copy_artifact_repository \
"$ORIGINAL_ARTIFACT_REPO_PATH" "$PUBLISH_ARTIFACT_REPO_PATH"
"$ORIGINAL_ARTIFACT_REPO_PATH" "$PUBLISH_ARTIFACT_REPO_PATH"
fi

echo "Published $PUBLISH_IMAGE to $PUBLISH_PROJECT_ID."

0 comments on commit f86d2d6

Please sign in to comment.