github.com/raghuse92/packer@v1.3.2/post-processor/googlecompute-export/startup.go (about) 1 package googlecomputeexport 2 3 var StartupScript string = `#!/bin/sh 4 5 GetMetadata () { 6 echo "$(curl -f -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/attributes/$1 2> /dev/null)" 7 } 8 IMAGENAME=$(GetMetadata image_name) 9 NAME=$(GetMetadata name) 10 DISKNAME=${NAME}-toexport 11 PATHS=$(GetMetadata paths) 12 ZONE=$(GetMetadata zone) 13 14 Exit () { 15 for i in ${PATHS}; do 16 LOGDEST="${i}.exporter.log" 17 echo "Uploading exporter log to ${LOGDEST}..." 18 gsutil -h "Content-Type:text/plain" cp /var/log/daemon.log ${LOGDEST} 19 done 20 exit $1 21 } 22 23 echo "####### Export configuration #######" 24 echo "Image name - ${IMAGENAME}" 25 echo "Instance name - ${NAME}" 26 echo "Instance zone - ${ZONE}" 27 echo "Disk name - ${DISKNAME}" 28 echo "Export paths - ${PATHS}" 29 echo "####################################" 30 31 echo "Creating disk from image to be exported..." 32 if ! gcloud compute disks create ${DISKNAME} --image ${IMAGENAME} --zone ${ZONE}; then 33 echo "Failed to create disk." 34 Exit 1 35 fi 36 37 echo "Attaching disk..." 38 if ! gcloud compute instances attach-disk ${NAME} --disk ${DISKNAME} --device-name toexport --zone ${ZONE}; then 39 echo "Failed to attach disk." 40 Exit 1 41 fi 42 43 echo "Dumping disk..." 44 if ! dd if=/dev/disk/by-id/google-toexport of=disk.raw bs=4096 conv=sparse; then 45 echo "Failed to dump disk to image." 46 Exit 1 47 fi 48 49 echo "Compressing and tar'ing disk image..." 50 if ! tar -czf root.tar.gz disk.raw; then 51 echo "Failed to tar disk image." 52 Exit 1 53 fi 54 55 echo "Detaching disk..." 56 if ! gcloud compute instances detach-disk ${NAME} --disk ${DISKNAME} --zone ${ZONE}; then 57 echo "Failed to detach disk." 58 fi 59 60 FAIL=0 61 echo "Deleting disk..." 62 if ! gcloud compute disks delete ${DISKNAME} --zone ${ZONE}; then 63 echo "Failed to delete disk." 64 FAIL=1 65 fi 66 67 for i in ${PATHS}; do 68 echo "Uploading tar'ed disk image to ${i}..." 69 if ! gsutil -o GSUtil:parallel_composite_upload_threshold=100M cp root.tar.gz ${i}; then 70 echo "Failed to upload image to ${i}." 71 FAIL=1 72 fi 73 done 74 75 Exit ${FAIL} 76 `