Skip to content

Instantly share code, notes, and snippets.

@pstadler
Last active April 2, 2017 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pstadler/13898067000c316b446a9a1e7d401f0e to your computer and use it in GitHub Desktop.
Save pstadler/13898067000c316b446a9a1e7d401f0e to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
URL="${1}"
if [ -z "${1}" ]; then
echo "usage: $(basename ${0}) <url>"
echo ""
echo "examples:"
echo " - $(basename ${0}) http://test-images.fr-1.storage.online.net/scw-distrib-ubuntu-trusty.tar"
echo " - VOLUME_SIZE=50GB $(basename ${0}) http://test-images.fr-1.storage.online.net/scw-distrib-ubuntu-trusty.tar"
exit 1
fi
NAME=$(basename "${URL}")
SNAPSHOT_NAME=${NAME%.*}-$(date +%Y-%m-%d_%H:%M)
IMAGE_NAME=${IMAGE_NAME:-$SNAPSHOT_NAME}
IMAGE_BOOTSCRIPT=${IMAGE_BOOTSCRIPT:stable}
SCW_COMMERCIAL_TYPE=${SCW_COMMERCIAL_TYPE:-VC1S}
VOLUME_SIZE=${VOLUME_SIZE:-50GB}
SCW_TARGET_ARCH=x86_64
if [ "$SCW_COMMERCIAL_TYPE" = "C1" ]; then
SCW_TARGET_ARCH=arm
fi
KEY=$(cat ~/.ssh/id_rsa.pub | awk '{ print $1" "$2 }' | tr ' ' '_')
echo "[+] URL of the tarball: ${URL}"
echo "[+] Target name: ${NAME}"
echo "[+] Creating new server in rescue mode with a secondary volume..."
SERVER=$(SCW_TARGET_ARCH="$SCW_TARGET_ARCH" scw --region=ams1 run --commercial-type="${SCW_COMMERCIAL_TYPE}" -d --env="AUTHORIZED_KEY=${KEY} boot=none INITRD_DROPBEAR=1" --name="image-writer-${NAME}" "${VOLUME_SIZE}")
echo "[+] Server created: ${SERVER}"
echo "[+] Booting..."
scw --region=ams1 exec -w "${SERVER}" 'uname -a'
echo "[+] Server is booted"
if [ -n "${SCW_GATEWAY_HTTP_PROXY}" ]; then
echo "[+] Configuring HTTP proxy"
# scw --region=ams1 exec "${SERVER}" "echo proxy=${SCW_GATEWAY_HTTP_PROY} >> .curlrc"
(
set +x
scw --region=ams1 exec "${SERVER}" "echo export http_proxy=${SCW_GATEWAY_HTTP_PROXY} > /proxy-env" >/dev/null 2>/dev/null || (echo "Failed to configure HTTP proxy"; exit 1) || exit 1
)
fi
echo "[+] Formating and mounting disk..."
# FIXME: make disk dynamic between /dev/vda and /dev/nbd1
scw --region=ams1 exec "${SERVER}" '/sbin/mkfs.ext4 /dev/vda && mkdir -p /mnt && mount /dev/vda /mnt'
echo "[+] /dev/nbd1 formatted in ext4 and mounted on /mnt"
echo "[+] Download tarball and write it to /mnt"
scw --region=ams1 exec "${SERVER}" "touch /proxy-env; . /proxy-env; wget -qO - ${URL} | tar -C /mnt/ -xf - && sync"
echo "[+] Tarball extracted on disk"
echo "[+] Stopping the server"
scw --region=ams1 stop "${SERVER}" >/dev/null
scw --region=ams1 wait "${SERVER}"
echo "[+] Server stopped"
echo "[+] Creating a snapshot of disk 1"
SNAPSHOT=$(scw --region=ams1 commit --volume=0 "${SERVER}" "${SNAPSHOT_NAME}")
echo "[+] Snapshot ${SNAPSHOT} created"
echo "[+] Creating an image based of the snapshot"
if [ -n "${IMAGE_ARCH}" ]; then
IMAGE=$(scw --region=ams1 tag --arch="${IMAGE_ARCH}" --bootscript="${IMAGE_BOOTSCRIPT}" "${SNAPSHOT}" "${IMAGE_NAME}")
else
IMAGE=$(scw --region=ams1 tag --bootscript="${IMAGE_BOOTSCRIPT}" "${SNAPSHOT}" "${IMAGE_NAME}")
fi
echo "[+] Image created: ${IMAGE}"
echo "[+] Deleting temporary server"
scw --region=ams1 rm "${SERVER}" >/dev/null
echo "[+] Server deleted"
#!/bin/bash
wget https://github.com/scaleway/scaleway-cli/releases/download/v1.11.1/scw_1.11.1_amd64.deb
dpkg -i scw_1.11.1_amd64.deb
wget -qO /tmp/create-image-from-http.sh https://gist.githubusercontent.com/pstadler/13898067000c316b446a9a1e7d401f0e/raw/create-image-from-http.sh
chmod +x /tmp/create-image-from-http.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment