diff --git a/prepare-vms/lib/commands.sh b/prepare-vms/lib/commands.sh index 91e5077e..822a9539 100644 --- a/prepare-vms/lib/commands.sh +++ b/prepare-vms/lib/commands.sh @@ -69,11 +69,14 @@ _cmd_deploy() { echo deploying > tags/$TAG/status sep "Deploying tag $TAG" - # Wait for cloudinit to be done + # If this VM image is using cloud-init, + # wait for cloud-init to be done pssh " - while [ ! -f /var/lib/cloud/instance/boot-finished ]; do - sleep 1 - done" + if [ -d /var/lib/cloud ]; then + while [ ! -f /var/lib/cloud/instance/boot-finished ]; do + sleep 1 + done + fi" # Special case for scaleway since it doesn't come with sudo if [ "$INFRACLASS" = "scaleway" ]; then @@ -102,6 +105,12 @@ _cmd_deploy() { sudo apt-get update && sudo apt-get install -y python-yaml" + # If there is no "python" binary, symlink to python3 + #pssh " + #if ! which python; then + # ln -s $(which python3) /usr/local/bin/python + #fi" + # Copy postprep.py to the remote machines, and execute it, feeding it the list of IP addresses pssh -I tee /tmp/postprep.py >/tmp/pp.out 2>>/tmp/pp.err" /tmp/token && diff --git a/prepare-vms/lib/infra/linode.sh b/prepare-vms/lib/infra/linode.sh new file mode 100644 index 00000000..8a7630d3 --- /dev/null +++ b/prepare-vms/lib/infra/linode.sh @@ -0,0 +1,58 @@ +if ! command -v linode-cli >/dev/null; then + warn "Linode CLI (linode-cli) not found." +fi +if ! [ -f ~/.config/linode-cli ]; then + warn "~/.config/linode-cli not found." +fi + +# To view available regions: "linode-cli regions list" +LINODE_REGION=${LINODE_REGION-us-west} + +# To view available types: "linode-cli linodes types" +LINODE_TYPE=${LINODE_TYPE-g6-standard-2} + +infra_list() { + linode-cli linodes list --json | + jq -r '.[] | [.id, .label, .status, .type] | @tsv' +} + +infra_start() { + COUNT=$1 + + for I in $(seq 1 $COUNT); do + NAME=$(printf "%s-%03d" $TAG $I) + sep "Starting instance $I/$COUNT" + info " Zone: $LINODE_REGION" + info " Name: $NAME" + info " Instance type: $LINODE_TYPE" + ROOT_PASS="$(base64 /dev/urandom | cut -c1-20 | head -n 1)" + linode-cli linodes create \ + --type=${LINODE_TYPE} --region=${LINODE_REGION} \ + --image=linode/ubuntu18.04 \ + --authorized_keys="${LINODE_SSHKEY}" \ + --root_pass="${ROOT_PASS}" \ + --tags=${TAG} --label=${NAME} + done + sep + + linode_get_ips_by_tag $TAG > tags/$TAG/ips.txt +} + +infra_stop() { + info "Counting instances..." + linode_get_ids_by_tag $TAG | wc -l + info "Deleting instances..." + linode_get_ids_by_tag $TAG | + xargs -n1 -P10 \ + linode-cli linodes delete +} + +linode_get_ids_by_tag() { + TAG=$1 + linode-cli linodes list --tags $TAG --json | jq -r ".[].id" +} + +linode_get_ips_by_tag() { + TAG=$1 + linode-cli linodes list --tags $TAG --json | jq -r ".[].ipv4[0]" +} diff --git a/prepare-vms/lib/pssh.sh b/prepare-vms/lib/pssh.sh index ca3bc639..fb855696 100644 --- a/prepare-vms/lib/pssh.sh +++ b/prepare-vms/lib/pssh.sh @@ -18,11 +18,11 @@ pssh() { echo "[parallel-ssh] $@" export PSSH=$(which pssh || which parallel-ssh) - if [ "$INFRACLASS" = hetzner ]; then - LOGIN=root - else - LOGIN=ubuntu - fi + case "$INFRACLASS" in + hetzner) LOGIN=root ;; + linode) LOGIN=root ;; + *) LOGIN=ubuntu ;; + esac $PSSH -h $HOSTFILE -l $LOGIN \ --par 100 \