From 6d1a1461435512160f56d858c3d4cecc5ae2aaf5 Mon Sep 17 00:00:00 2001 From: Luke Swithenbank Date: Fri, 7 Aug 2020 06:18:23 +1000 Subject: [PATCH] Fix Google Compute Export Post-Processor (#9708) * Fix Google Compute Export Post-Processor The current Post-Processor hangs on waiting for the Startup Script to finish. The startup script doesn't update the metadata (StartupScriptStatusKey) so this change aims to fix that but adding the `SetMetadata` functionality to this script. * Update startup.go --- .../googlecompute-export/startup.go | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/post-processor/googlecompute-export/startup.go b/post-processor/googlecompute-export/startup.go index 779c97405..1edb38bc6 100644 --- a/post-processor/googlecompute-export/startup.go +++ b/post-processor/googlecompute-export/startup.go @@ -1,15 +1,36 @@ package googlecomputeexport -var StartupScript string = `#!/bin/bash +import ( + "fmt" + + "github.com/hashicorp/packer/builder/googlecompute" +) + +var StartupScript string = fmt.Sprintf(`#!/bin/bash GetMetadata () { echo "$(curl -f -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/attributes/$1 2> /dev/null)" } + +ZONE=$(basename $(GetMetadata zone)) + +SetMetadata () { + gcloud compute instances add-metadata ${HOSTNAME} --metadata ${1}=${2} --zone ${ZONE} +} + +STARTUPSCRIPT=$(GetMetadata attributes/%s) +STARTUPSCRIPTPATH=/packer-wrapped-startup-script +if [ -f "/var/log/startupscript.log" ]; then + STARTUPSCRIPTLOGPATH=/var/log/startupscript.log +else + STARTUPSCRIPTLOGPATH=/var/log/daemon.log +fi +STARTUPSCRIPTLOGDEST=$(GetMetadata attributes/startup-script-log-dest) + IMAGENAME=$(GetMetadata image_name) NAME=$(GetMetadata name) DISKNAME=${NAME}-toexport PATHS=($(GetMetadata paths)) -ZONE=$(GetMetadata zone) Exit () { for i in ${PATHS[@]}; do @@ -70,5 +91,7 @@ for i in ${PATHS[@]:1}; do fi done +SetMetadata %s %s + Exit ${FAIL} -` +`, googlecompute.StartupWrappedScriptKey, googlecompute.StartupScriptStatusKey, googlecompute.StartupScriptStatusDone)