From b66f1bcadde18c5702929dff8f53b78265596fb2 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 8 Feb 2022 20:55:33 -0500 Subject: [PATCH] Day zero scripts to create and delete a VM with a single execution. --- day0_create.sh | 36 ++++++++++++++++++++++++++++++++++++ day0_delete.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 day0_create.sh create mode 100755 day0_delete.sh diff --git a/day0_create.sh b/day0_create.sh new file mode 100755 index 0000000..f5083ea --- /dev/null +++ b/day0_create.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +# +# Day 0 - Create script +# Requires linode-cli to be installed (`pip3 install linode-cli`) +# + +# require ROOT_PASS +if [ -z "$ROOT_PASS" ] +then + echo "Define a root password for this linode, example: ROOT_PASS=password1234 $0" + exit 1 +fi + +# default to current user's SSH key as authorized key +if [ -z "$AUTHORIZED_KEYS" ] +then + AUTHORIZED_KEYS=$(cat ~/.ssh/id_rsa.pub) +fi + +JSON=$(linode-cli linodes create --root_pass "$ROOT_PASS" \ + --authorized_keys "$AUTHORIZED_KEYS" \ + --label "UpskillChallengeNode" \ + --json) + +# Example output: +#[{"id": 34627775, "label": "linode34627775", "group": "", "status": "provisioning", "created": "2022-02-09T01:02:39", "updated": "2022-02-09T01:02:39", "type": "g6-nanode-1", "ipv4": ["170.187.158.244"], "ipv6": "2600:3c02::f03c:93ff:fe98:b296/128", "image": "linode/debian11", "region": "us-southeast", "specs": {"disk": 25600, "memory": 1024, "vcpus": 1, "transfer": 1000}, "alerts": {"cpu": 90, "network_in": 10, "network_out": 10, "transfer_quota": 80, "io": 10000}, "backups": {"enabled": false, "schedule": {"day": null, "window": null}, "last_successful": null}, "hypervisor": "kvm", "watchdog_enabled": true, "tags": []}] + +# Ugly one-line of Python to parse the JSON from linode-cli. Folly? +python -c "import json; j=json.loads('$JSON')[0]; print(f'''IP Address: {j['ipv4']}\nStatus: {j['status']}\nCreated: {j['created']}\n''')" + +# Sleep to wait for provisioning - this isn't long enough and it isn't a good practice. +# sleep 5.0 + +# show pretty table of info to prove changes +linode-cli linodes list --format 'label,status,ipv4' diff --git a/day0_delete.sh b/day0_delete.sh new file mode 100755 index 0000000..6e77eaf --- /dev/null +++ b/day0_delete.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# +# Day 0 - Delete script +# Requires linode-cli to be installed (`pip3 install linode-cli`) +# + + +# The command used below looks like this from command line: +#linode-cli linodes list --format 'id' --label UpskillChallengeNode --text +#id +#34628226 + + +# change the target of this delete script by giving a LABEL value, like this: +# LABEL=DeleteThisNode ./day0_delete.sh +# otherwise, the default "UpskillChallengeNode" will be deleted +if [ -z "$LABEL"] +then + LABEL="UpskillChallengeNode" +fi + +ID=$(linode-cli linodes list --format 'id' --label "$LABEL" --text | tail -n 1) +echo $ID + +linode-cli linodes delete "$ID" +