github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/scripts/image-pusher/export-image (about) 1 #! /bin/bash --posix 2 3 set -e 4 set -o nounset 5 6 # Configuration. Quick and dirty for now. 7 readonly scratch_dir='/scratch' 8 9 # Pull in from the command line. 10 readonly type="$1" 11 readonly destination="$2" 12 13 if [ "$type" != "s3" ]; then 14 echo "Unknown type" 15 exit 1 16 fi 17 18 # Introspect. 19 readonly document_url='http://169.254.169.254/latest/dynamic/instance-identity/document' 20 readonly document="$(wget -q -O - "$document_url")" 21 readonly account_id="$(echo "$document" | fgrep 'accountId' | cut -d'"' -f 4)" 22 readonly region="$(echo "$document" | fgrep 'region' | cut -d'"' -f 4)" 23 24 # Load secrets (API keys). The following variables should be set: 25 # AWS_ACCESS_KEY_ID 26 # cert_file 27 # key_file 28 # AWS_SECRET_ACCESS_KEY 29 . "$scratch_dir/secrets" 30 31 readonly tmpdir="$(mktemp -d "$scratch_dir/$(basename "$0").XXXXXX")" || exit 32 trap "rm -rf $tmpdir" EXIT 33 34 cat > "$tmpdir/image" 35 ec2-bundle-image -c "$cert_file" -k "$key_file" -u "$account_id" \ 36 --image "$tmpdir/image" -d "$tmpdir" -r x86_64 --prefix image 37 38 # Version 1.5.7 of ec2-upload-bundle doesn't support colons in the name, and 39 # earlier versions don't support eu-central-1, so only use ec2-upload-bundle if 40 # there are no colons, otherwise do it the hard (but slower) way. 41 if ! echo "$destination" | fgrep -q :; then 42 ec2-upload-bundle -b "$destination" -m "$tmpdir/image.manifest.xml" \ 43 -a "$AWS_ACCESS_KEY_ID" -s "$AWS_SECRET_ACCESS_KEY" \ 44 --batch --retry --region "$region" 45 exit 0 46 fi 47 # Now do this the hard way :-( 48 readonly bucket="${destination%%/*}" 49 readonly destdir="${destination#*/}" 50 for file in "$tmpdir/image.manifest.xml" "$tmpdir"/image.part.*; do 51 aws s3api put-object --acl aws-exec-read --bucket "$bucket" \ 52 --key "$destdir/${file##*/}" --body "$file" --region "$region" || exit 53 done