Files
cloudron-box/baseimage/createImage

193 lines
6.1 KiB
Plaintext
Raw Normal View History

2015-08-04 16:29:49 -07:00
#!/bin/bash
2015-11-23 10:27:27 -08:00
set -eu -o pipefail
2015-08-04 16:29:49 -07:00
assertNotEmpty() {
: "${!1:? "$1 is not set."}"
}
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
export JSON="${SOURCE_DIR}/node_modules/.bin/json"
2015-11-01 08:46:28 -08:00
2015-11-23 08:38:56 -08:00
provider="digitalocean"
2015-08-04 16:29:49 -07:00
installer_revision=$(git rev-parse HEAD)
box_name=""
2015-11-23 08:32:54 -08:00
server_id=""
server_ip=""
destroy_server="yes"
2015-08-04 16:29:49 -07:00
deploy_env="dev"
# Only GNU getopt supports long options. OS X comes bundled with the BSD getopt
# brew install gnu-getopt to get the GNU getopt on OS X
[[ $(uname -s) == "Darwin" ]] && GNU_GETOPT="/usr/local/opt/gnu-getopt/bin/getopt" || GNU_GETOPT="getopt"
readonly GNU_GETOPT
2015-11-23 11:20:21 -08:00
args=$(${GNU_GETOPT} -o "" -l "provider:,revision:,regions:,size:,name:,no-destroy,env:" -n "$0" -- "$@")
2015-08-04 16:29:49 -07:00
eval set -- "${args}"
while true; do
case "$1" in
--env) deploy_env="$2"; shift 2;;
--revision) installer_revision="$2"; shift 2;;
2015-11-23 08:38:56 -08:00
--provider) provider="$2"; shift 2;;
2015-11-23 11:20:21 -08:00
--name) box_name="$2"; destroy_server="no"; shift 2;;
2015-11-23 08:32:54 -08:00
--no-destroy) destroy_server="no"; shift 2;;
2015-08-04 16:29:49 -07:00
--) break;;
*) echo "Unknown option $1"; exit 1;;
esac
done
2015-11-23 12:46:08 -08:00
echo "Creating image using ${provider}"
2015-11-23 08:38:56 -08:00
if [[ "${provider}" == "digitalocean" ]]; then
if [[ "${deploy_env}" == "staging" ]]; then
assertNotEmpty DIGITAL_OCEAN_TOKEN_STAGING
2015-11-23 08:55:19 -08:00
export DIGITAL_OCEAN_TOKEN="${DIGITAL_OCEAN_TOKEN_STAGING}"
2015-11-23 08:38:56 -08:00
elif [[ "${deploy_env}" == "dev" ]]; then
assertNotEmpty DIGITAL_OCEAN_TOKEN_DEV
2015-11-23 08:55:19 -08:00
export DIGITAL_OCEAN_TOKEN="${DIGITAL_OCEAN_TOKEN_DEV}"
2015-11-23 08:38:56 -08:00
elif [[ "${deploy_env}" == "prod" ]]; then
assertNotEmpty DIGITAL_OCEAN_TOKEN_PROD
2015-11-23 08:55:19 -08:00
export DIGITAL_OCEAN_TOKEN="${DIGITAL_OCEAN_TOKEN_PROD}"
2015-11-23 08:38:56 -08:00
else
echo "No such env ${deploy_env}."
exit 1
fi
2015-11-23 08:55:19 -08:00
vps="${SCRIPT_DIR}/digitalocean.sh"
2015-08-04 16:29:49 -07:00
else
2015-11-23 08:38:56 -08:00
echo "Unknown provider : ${provider}"
2015-08-04 16:29:49 -07:00
exit 1
fi
2015-11-16 12:15:15 -08:00
readonly ssh_keys="${HOME}/.ssh/id_rsa_caas_${deploy_env}"
2015-11-16 14:48:05 -08:00
readonly scp202="scp -P 202 -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${ssh_keys}"
readonly scp22="scp -o ConnectTimeout=10 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${ssh_keys}"
2015-11-16 12:15:15 -08:00
2015-11-16 13:04:09 -08:00
readonly ssh202="ssh -p 202 -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${ssh_keys}"
readonly ssh22="ssh -o IdentitiesOnly=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${ssh_keys}"
2015-11-16 12:15:15 -08:00
2015-08-04 16:29:49 -07:00
if [[ ! -f "${ssh_keys}" ]]; then
2015-11-16 12:15:44 -08:00
echo "caas ssh key is missing at ${ssh_keys} (pick it up from secrets repo)"
2015-08-04 16:29:49 -07:00
exit 1
fi
function get_pretty_revision() {
local git_rev="$1"
local sha1=$(git rev-parse --short "${git_rev}" 2>/dev/null)
2015-10-30 17:40:17 -07:00
echo "${sha1}"
2015-08-04 16:29:49 -07:00
}
now=$(date "+%Y-%m-%d-%H%M%S")
pretty_revision=$(get_pretty_revision "${installer_revision}")
if [[ -z "${box_name}" ]]; then
# if you change this, change the regexp is appstore/janitor.js
box_name="box-${deploy_env}-${pretty_revision}-${now}" # remove slashes
2015-11-23 08:32:54 -08:00
# create a new server if no name given
2015-11-23 12:31:58 -08:00
if ! caas_ssh_key_id=$($vps get_ssh_key_id "caas"); then
2015-11-16 12:11:57 -08:00
echo "Could not query caas ssh key"
2015-08-04 16:29:49 -07:00
exit 1
fi
2015-11-16 16:43:53 -08:00
echo "Detected caas ssh key id: ${caas_ssh_key_id}"
2015-08-04 16:29:49 -07:00
2015-11-23 10:49:09 -08:00
echo "Creating Server with name [${box_name}]"
2015-11-23 12:31:58 -08:00
if ! server_id=$($vps create ${caas_ssh_key_id} ${box_name}); then
2015-11-23 08:32:54 -08:00
echo "Failed to create server"
2015-08-04 16:29:49 -07:00
exit 1
fi
2015-11-23 08:32:54 -08:00
echo "Created server with id: ${server_id}"
2015-08-04 16:29:49 -07:00
# If we run scripts overenthusiastically without the wait, setup script randomly fails
2015-11-23 08:32:54 -08:00
echo -n "Waiting 120 seconds for server creation"
2015-08-04 16:29:49 -07:00
for i in $(seq 1 24); do
echo -n "."
sleep 5
done
echo ""
else
2015-11-23 12:31:58 -08:00
if ! server_id=$($vps get_id "${box_name}"); then
echo "Could not determine id from name"
exit 1
fi
2015-11-23 08:32:54 -08:00
echo "Reusing server with id: ${server_id}"
2015-08-04 16:29:49 -07:00
2015-11-23 09:13:30 -08:00
$vps power_on "${server_id}"
2015-08-04 16:29:49 -07:00
fi
2015-11-23 11:20:21 -08:00
# Query until we get an IP
2015-08-04 16:29:49 -07:00
while true; do
2015-11-23 08:32:54 -08:00
echo "Trying to get the server IP"
2015-11-23 12:31:58 -08:00
if server_ip=$($vps get_ip "${server_id}"); then
2015-11-23 08:32:54 -08:00
echo "Server IP : [${server_ip}]"
2015-08-04 16:29:49 -07:00
break
fi
echo "Timedout, trying again in 10 seconds"
sleep 10
done
while true; do
2015-11-23 08:32:54 -08:00
echo "Trying to copy init script to server"
if $scp22 "${SCRIPT_DIR}/initializeBaseUbuntuImage.sh" root@${server_ip}:.; then
2015-08-04 16:29:49 -07:00
break
fi
echo "Timedout, trying again in 30 seconds"
sleep 30
done
2015-08-12 19:52:43 -07:00
echo "Copying INFRA_VERSION"
$scp22 "${SCRIPT_DIR}/../setup/INFRA_VERSION" root@${server_ip}:.
2015-08-12 19:52:43 -07:00
echo "Copying box source"
cd "${SOURCE_DIR}"
2016-01-11 15:19:52 +01:00
git archive --format=tar HEAD | $ssh22 "root@${server_ip}" "cat - > /tmp/box.tar.gz"
2015-08-04 16:29:49 -07:00
echo "Executing init script"
2015-11-23 08:32:54 -08:00
if ! $ssh22 "root@${server_ip}" "/bin/bash /root/initializeBaseUbuntuImage.sh ${installer_revision}"; then
2015-08-04 16:29:49 -07:00
echo "Init script failed"
exit 1
fi
echo "Copy over certs"
cd "${SCRIPT_DIR}/../../secrets"
2015-11-23 08:32:54 -08:00
blackbox_cat installer/server.crt.gpg | $ssh202 "root@${server_ip}" "cat - > /home/yellowtent/installer/src/certs/server.crt"
blackbox_cat installer/server.key.gpg | $ssh202 "root@${server_ip}" "cat - > /home/yellowtent/installer/src/certs/server.key"
blackbox_cat installer_ca/ca.crt.gpg | $ssh202 "root@${server_ip}" "cat - > /home/yellowtent/installer/src/certs/ca.crt"
2015-11-23 08:32:54 -08:00
echo "Shutting down server with id : ${server_id}"
$ssh202 "root@${server_ip}" "shutdown -f now" || true # shutdown sometimes terminates ssh connection immediately making this command fail
# wait 10 secs for actual shutdown
2015-11-23 08:32:54 -08:00
echo "Waiting for 10 seconds for server to shutdown"
sleep 30
2015-11-23 08:32:54 -08:00
echo "Powering off server"
2015-11-23 12:31:58 -08:00
if ! $vps power_off "${server_id}"; then
echo "Could not power off server"
exit 1
fi
2015-08-04 16:29:49 -07:00
snapshot_name="box-${deploy_env}-${pretty_revision}-${now}"
echo "Snapshotting as ${snapshot_name}"
2015-11-23 12:45:09 -08:00
if ! image_id=$($vps snapshot "${server_id}" "${snapshot_name}"); then
echo "Could not snapshot and get image id"
2015-11-23 12:31:58 -08:00
exit 1
fi
2015-08-04 16:29:49 -07:00
2015-11-23 08:32:54 -08:00
if [[ "${destroy_server}" == "yes" ]]; then
echo "Destroying server"
2015-11-23 12:31:58 -08:00
if ! $vps destroy "${server_id}"; then
echo "Could not destroy server"
exit 1
fi
2015-08-04 16:29:49 -07:00
else
2015-11-23 08:32:54 -08:00
echo "Skipping server destroy"
2015-08-04 16:29:49 -07:00
fi
2015-11-23 13:35:05 -08:00
echo "Transferring image ${image_id} to other regions"
2015-11-23 13:51:14 -08:00
$vps transfer_image_to_all_regions "${image_id}"
2015-08-04 16:29:49 -07:00
echo "Done."