Day zero scripts to create and delete a VM with a single execution.
This commit is contained in:
		
							parent
							
								
									d3f4dff70a
								
							
						
					
					
						commit
						b66f1bcadd
					
				|  | @ -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' | ||||||
|  | @ -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" | ||||||
|  | 
 | ||||||
		Reference in New Issue