|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# Default values |
| 5 | +BUCKET_NAME="" |
| 6 | +TARGET_ZONES="" |
| 7 | +TARGET_API="block" |
| 8 | +IMAGE_NAME="" |
| 9 | +SNAPSHOT_ID="" |
| 10 | +SNAPSHOT_ZONE="fr-par-1" |
| 11 | +SNAPSHOT_API="block" |
| 12 | +PROJECT_ID="" |
| 13 | +ARCH="x86_64" |
| 14 | + |
| 15 | +print_help() { |
| 16 | + echo "Usage: $0 [options]" |
| 17 | + echo "Options:" |
| 18 | + echo " Transient resources parameters:" |
| 19 | + echo " -b, --bucket-name BUCKET_NAME Specify a bucket name (the bucket must be in the same region as the snapshot)" |
| 20 | + echo " Source snapshot parameters:" |
| 21 | + echo " -s, --snapshot-id SNAPSHOT_ID Specify the ID of the snapshot that will be used to create images" |
| 22 | + echo " -z, --snapshot-zone SNAPSHOT_ZONE Specify the zone of the snapshot that will be used to create images" |
| 23 | + echo " -a, --snapshot-api SNAPSHOT_API Specify the API where the snapshot is present (block or instance)" |
| 24 | + echo " Output images parameters:" |
| 25 | + echo " -c, --arch ARCH Specify the CPU architecture" |
| 26 | + echo " -p, --project-id PROJECT_ID Specify the project ID where the images will be created" |
| 27 | + echo " -i, --image-name IMAGE_NAME Specify the name of the images to create" |
| 28 | + echo " -o, --target-api TARGET_API Specify the target API, where snapshots will be created (block or instance)" |
| 29 | + echo " -t, --target-zones TARGET_ZONES Specify the target zones, separated by comma" |
| 30 | + echo " Misc:" |
| 31 | + echo " -h, --help Show this help message" |
| 32 | +} |
| 33 | + |
| 34 | +# Parse flags |
| 35 | +while [[ "$#" -gt 0 ]]; do |
| 36 | + case "$1" in |
| 37 | + -b|--bucket-name) |
| 38 | + BUCKET_NAME="$2" |
| 39 | + shift 2 |
| 40 | + ;; |
| 41 | + -t|--target-zones) |
| 42 | + TARGET_ZONES="$2" |
| 43 | + shift 2 |
| 44 | + ;; |
| 45 | + -o|--target-api) |
| 46 | + TARGET_API="$2" |
| 47 | + shift 2 |
| 48 | + ;; |
| 49 | + -i|--image-name) |
| 50 | + IMAGE_NAME="$2" |
| 51 | + shift 2 |
| 52 | + ;; |
| 53 | + -s|--snapshot-id) |
| 54 | + SNAPSHOT_ID="$2" |
| 55 | + shift 2 |
| 56 | + ;; |
| 57 | + -z|--snapshot-zone) |
| 58 | + SNAPSHOT_ZONE="$2" |
| 59 | + shift 2 |
| 60 | + ;; |
| 61 | + -a|--snapshot-api) |
| 62 | + SNAPSHOT_API="$2" |
| 63 | + shift 2 |
| 64 | + ;; |
| 65 | + -p|--project-id) |
| 66 | + PROJECT_ID="$2" |
| 67 | + shift 2 |
| 68 | + ;; |
| 69 | + -c|--arch) |
| 70 | + ARCH="$2" |
| 71 | + shift 2 |
| 72 | + ;; |
| 73 | + -h|--help) |
| 74 | + print_help |
| 75 | + exit 0 |
| 76 | + ;; |
| 77 | + *) |
| 78 | + echo "Unknown option: $1" |
| 79 | + print_help |
| 80 | + exit 1 |
| 81 | + ;; |
| 82 | + esac |
| 83 | +done |
| 84 | + |
| 85 | +if [[ -z "$IMAGE_NAME" ]]; then |
| 86 | + echo "No image name specified" |
| 87 | + exit 1 |
| 88 | +fi |
| 89 | + |
| 90 | +# Export snapshot to S3. |
| 91 | + |
| 92 | +if [[ -z "$BUCKET_NAME" ]]; then |
| 93 | + echo "No bucket name specified" |
| 94 | + exit 1 |
| 95 | +fi |
| 96 | + |
| 97 | +if [[ -z "$SNAPSHOT_ID" ]]; then |
| 98 | + echo "No snapshot ID specified" |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +if [[ -z "$SNAPSHOT_ZONE" ]]; then |
| 103 | + echo "No snapshot zone specified" |
| 104 | + exit 1 |
| 105 | +fi |
| 106 | + |
| 107 | +echo "Uploading snapshot to S3" |
| 108 | + |
| 109 | +case "$SNAPSHOT_API" in |
| 110 | + |
| 111 | + block) |
| 112 | + echo " ... Exporting block snapshot to bucket" |
| 113 | + scw block snapshot export-to-object-storage snapshot-id="${SNAPSHOT_ID}" bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" zone="${SNAPSHOT_ZONE}" > /dev/null |
| 114 | + echo " ... Waiting for export to finish" |
| 115 | + scw block snapshot wait "${SNAPSHOT_ID}" zone="${SNAPSHOT_ZONE}" > /dev/null |
| 116 | + ;; |
| 117 | + |
| 118 | + instance) |
| 119 | + echo " ... Exporting instance snapshot to bucket" |
| 120 | + scw instance snapshot export snapshot-id="${SNAPSHOT_ID}" bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" zone="${SNAPSHOT_ZONE}" > /dev/null |
| 121 | + echo " ... Waiting for export to finish" |
| 122 | + scw instance snapshot wait "${SNAPSHOT_ID}" zone="${SNAPSHOT_ZONE}" > /dev/null |
| 123 | + ;; |
| 124 | + |
| 125 | + *) |
| 126 | + echo "unknown snapshot API" |
| 127 | + exit 1 |
| 128 | + ;; |
| 129 | +esac |
| 130 | + |
| 131 | +# Import snapshot from S3 |
| 132 | + |
| 133 | +export IFS="," |
| 134 | +for target_zone in ${TARGET_ZONES}; do |
| 135 | + |
| 136 | + echo "Importing snapshot to ${target_zone} from bucket" |
| 137 | + |
| 138 | + case "$TARGET_API" in |
| 139 | + |
| 140 | + block) |
| 141 | + echo " ... Importing block snapshot to ${target_zone} from bucket" |
| 142 | + imported_snapshot_id=$(scw block snapshot import-from-object-storage bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" name="${IMAGE_NAME}" project-id="${PROJECT_ID}" zone="${target_zone}" -o template="{{ .ID }}") |
| 143 | + echo " ... Waiting for import to finish" |
| 144 | + scw block snapshot wait "${imported_snapshot_id}" zone="${target_zone}" > /dev/null |
| 145 | + ;; |
| 146 | + |
| 147 | + instance) |
| 148 | + echo " ... Importing instance snapshot to ${target_zone} from bucket" |
| 149 | + imported_snapshot_id=$(scw instance snapshot create bucket="${BUCKET_NAME}" key="${IMAGE_NAME}" name="${IMAGE_NAME}" project-id="${PROJECT_ID}" zone="${target_zone}" volume-type=l_ssd -o template="{{ .Snapshot.ID }}") |
| 150 | + echo " ... Waiting for import to finish" |
| 151 | + scw instance snapshot wait "${imported_snapshot_id}" zone="${target_zone}" > /dev/null |
| 152 | + ;; |
| 153 | + |
| 154 | + *) |
| 155 | + echo "unknown snapshot API" |
| 156 | + exit 1 |
| 157 | + ;; |
| 158 | + esac |
| 159 | + |
| 160 | + echo " ... Creating instance image in ${target_zone}" |
| 161 | + scw instance image create name="${IMAGE_NAME}" snapshot-id="${imported_snapshot_id}" project-id="${PROJECT_ID}" arch="${ARCH}" zone="${target_zone}" > /dev/null |
| 162 | +done |
| 163 | + |
| 164 | +echo "Images were successfully created. The S3 bucket needs to be cleaned manually." |
0 commit comments