2016-07-07 17:50:46 -04:00
|
|
|
package googlecomputeexport
|
|
|
|
|
2020-08-06 16:18:23 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/builder/googlecompute"
|
|
|
|
)
|
|
|
|
|
|
|
|
var StartupScript string = fmt.Sprintf(`#!/bin/bash
|
2016-07-07 17:50:46 -04:00
|
|
|
|
|
|
|
GetMetadata () {
|
|
|
|
echo "$(curl -f -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/attributes/$1 2> /dev/null)"
|
|
|
|
}
|
2020-08-06 16:18:23 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-07-07 17:50:46 -04:00
|
|
|
IMAGENAME=$(GetMetadata image_name)
|
|
|
|
NAME=$(GetMetadata name)
|
|
|
|
DISKNAME=${NAME}-toexport
|
2019-01-22 12:34:01 -05:00
|
|
|
PATHS=($(GetMetadata paths))
|
2016-07-07 17:50:46 -04:00
|
|
|
|
2016-08-15 19:25:57 -04:00
|
|
|
Exit () {
|
2019-01-22 12:34:01 -05:00
|
|
|
for i in ${PATHS[@]}; do
|
2016-08-15 19:25:57 -04:00
|
|
|
LOGDEST="${i}.exporter.log"
|
|
|
|
echo "Uploading exporter log to ${LOGDEST}..."
|
|
|
|
gsutil -h "Content-Type:text/plain" cp /var/log/daemon.log ${LOGDEST}
|
|
|
|
done
|
|
|
|
exit $1
|
|
|
|
}
|
|
|
|
|
2016-07-07 17:50:46 -04:00
|
|
|
echo "####### Export configuration #######"
|
|
|
|
echo "Image name - ${IMAGENAME}"
|
|
|
|
echo "Instance name - ${NAME}"
|
|
|
|
echo "Instance zone - ${ZONE}"
|
|
|
|
echo "Disk name - ${DISKNAME}"
|
|
|
|
echo "Export paths - ${PATHS}"
|
|
|
|
echo "####################################"
|
|
|
|
|
|
|
|
echo "Creating disk from image to be exported..."
|
2016-08-15 19:25:57 -04:00
|
|
|
if ! gcloud compute disks create ${DISKNAME} --image ${IMAGENAME} --zone ${ZONE}; then
|
|
|
|
echo "Failed to create disk."
|
|
|
|
Exit 1
|
|
|
|
fi
|
|
|
|
|
2016-07-07 17:50:46 -04:00
|
|
|
echo "Attaching disk..."
|
2016-08-15 19:25:57 -04:00
|
|
|
if ! gcloud compute instances attach-disk ${NAME} --disk ${DISKNAME} --device-name toexport --zone ${ZONE}; then
|
|
|
|
echo "Failed to attach disk."
|
|
|
|
Exit 1
|
|
|
|
fi
|
2016-07-07 17:50:46 -04:00
|
|
|
|
2019-01-22 12:34:01 -05:00
|
|
|
echo "GCEExport: Running export tool."
|
|
|
|
gce_export -gcs_path "${PATHS[0]}" -disk /dev/disk/by-id/google-toexport -y
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo "ExportFailed: Failed to export disk source to ${PATHS[0]}."
|
2016-08-15 19:25:57 -04:00
|
|
|
Exit 1
|
|
|
|
fi
|
|
|
|
|
2019-01-22 12:34:01 -05:00
|
|
|
echo "ExportSuccess"
|
|
|
|
sync
|
2016-07-07 17:50:46 -04:00
|
|
|
|
|
|
|
echo "Detaching disk..."
|
2016-08-15 19:25:57 -04:00
|
|
|
if ! gcloud compute instances detach-disk ${NAME} --disk ${DISKNAME} --zone ${ZONE}; then
|
|
|
|
echo "Failed to detach disk."
|
|
|
|
fi
|
|
|
|
|
|
|
|
FAIL=0
|
2016-07-07 17:50:46 -04:00
|
|
|
echo "Deleting disk..."
|
2016-08-15 19:25:57 -04:00
|
|
|
if ! gcloud compute disks delete ${DISKNAME} --zone ${ZONE}; then
|
|
|
|
echo "Failed to delete disk."
|
|
|
|
FAIL=1
|
|
|
|
fi
|
2016-07-07 17:50:46 -04:00
|
|
|
|
2019-01-22 12:34:01 -05:00
|
|
|
for i in ${PATHS[@]:1}; do
|
|
|
|
echo "Copying archive image to ${i}..."
|
|
|
|
if ! gsutil -o GSUtil:parallel_composite_upload_threshold=100M cp ${PATHS[0]} ${i}; then
|
|
|
|
echo "Failed to copy image to ${i}."
|
2016-08-15 19:25:57 -04:00
|
|
|
FAIL=1
|
|
|
|
fi
|
2016-07-07 17:50:46 -04:00
|
|
|
done
|
2016-08-15 19:25:57 -04:00
|
|
|
|
2020-08-06 16:18:23 -04:00
|
|
|
SetMetadata %s %s
|
|
|
|
|
2016-08-15 19:25:57 -04:00
|
|
|
Exit ${FAIL}
|
2020-08-06 16:18:23 -04:00
|
|
|
`, googlecompute.StartupWrappedScriptKey, googlecompute.StartupScriptStatusKey, googlecompute.StartupScriptStatusDone)
|