Files
Packer-Cn/post-processor/yandex-export/cloud-init-script.go
T

82 lines
1.9 KiB
Go
Raw Normal View History

2020-12-07 14:20:06 +03:00
// CODE GENERATED. DO NOT EDIT
package yandexexport
var (
CloudInitScript = `#!/usr/bin/env bash
GetMetadata() {
curl -f -H "Metadata-Flavor: Google" http://169.254.169.254/computeMetadata/v1/instance/attributes/$1 2>/dev/null
}
2020-12-10 14:52:22 +03:00
[[ "$(GetMetadata debug)" == "1" || "$(GetMetadata debug)" == "true" ]] && set -x
2020-12-07 14:20:06 +03:00
InstallPackages() {
2020-12-09 18:51:34 +03:00
sudo apt-get update -qq && sudo apt-get install -y qemu-utils awscli
2020-12-07 14:20:06 +03:00
}
2020-12-09 18:51:34 +03:00
WaitFile() {
local RETRIES=60
while [[ ${RETRIES} -gt 0 ]]; do
echo "Wait ${1}"
2020-12-10 14:52:22 +03:00
if [ -e "${1}" ]; then
2020-12-09 18:51:34 +03:00
echo "[${1}] has been found"
return 0
fi
RETRIES=$((RETRIES-1))
sleep 5
done
echo "[${1}] not found"
return 1
2020-12-07 14:20:06 +03:00
}
PATHS=$(GetMetadata paths)
S3_ENDPOINT="https://storage.yandexcloud.net"
2020-12-10 14:52:22 +03:00
DISK_EXPORT_PATH="/dev/disk/by-id/virtio-doexport"
2020-12-09 18:51:34 +03:00
export AWS_SHARED_CREDENTIALS_FILE="/tmp/aws-credentials"
export AWS_REGION=ru-central1
2020-12-07 14:20:06 +03:00
Exit() {
for i in ${PATHS}; do
LOGDEST="${i}.exporter.log"
echo "Uploading exporter log to ${LOGDEST}..."
2020-12-09 18:51:34 +03:00
aws s3 --endpoint-url="${S3_ENDPOINT}" cp /var/log/syslog "${LOGDEST}"
2020-12-07 14:20:06 +03:00
done
exit $1
}
2020-12-09 18:51:34 +03:00
InstallPackages
2020-12-07 14:20:06 +03:00
echo "####### Export configuration #######"
echo "Export paths - ${PATHS}"
echo "####################################"
2020-12-09 18:51:34 +03:00
if ! WaitFile "${AWS_SHARED_CREDENTIALS_FILE}"; then
echo "Failed wait credentials"
2020-12-07 14:20:06 +03:00
Exit 1
fi
2020-12-10 14:52:22 +03:00
udevadm trigger || true
if ! WaitFile "${DISK_EXPORT_PATH}"; then
echo "Failed wait attach disk"
Exit 1
fi
2020-12-07 14:20:06 +03:00
echo "Dumping disk..."
2020-12-10 14:52:22 +03:00
if ! qemu-img convert -p -O qcow2 -o cluster_size=2M "${DISK_EXPORT_PATH}" disk.qcow2; then
2020-12-07 14:20:06 +03:00
echo "Failed to dump disk to qcow2 image."
Exit 1
fi
for i in ${PATHS}; do
echo "Uploading qcow2 disk image to ${i}..."
2020-12-09 18:51:34 +03:00
if ! aws s3 --endpoint-url="${S3_ENDPOINT}" cp disk.qcow2 "${i}"; then
2020-12-07 14:20:06 +03:00
echo "Failed to upload image to ${i}."
FAIL=1
fi
done
Exit ${FAIL}
`
)