|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Implement the following commands: |
| 4 | +# |
| 5 | +# ensure: |
| 6 | +# |
| 7 | +# Get data about currently provisioned M1 server on Scaleway. If needed, |
| 8 | +# provision one. |
| 9 | +# |
| 10 | +# The script requires the SCW_SECRET_KEY env var set to a valid secret. |
| 11 | +# |
| 12 | +# If successful, return the response data on stdout. It may look like: |
| 13 | +# |
| 14 | +# { |
| 15 | +# "id": "8b196119-3cea-4a9d-b916-265037a85e60", |
| 16 | +# "type": "M1-M", |
| 17 | +# "name": "mac-m1-psycopg", |
| 18 | +# "project_id": "4cf7a85e-f21e-40d4-b758-21d1f4ad3dfb", |
| 19 | +# "organization_id": "4cf7a85e-f21e-40d4-b758-21d1f4ad3dfb", |
| 20 | +# "ip": "1.2.3.4", |
| 21 | +# "vnc_url": "vnc://m1:[email protected]:5900", |
| 22 | +# "status": "starting", |
| 23 | +# "created_at": "2023-09-22T18:00:18.754646Z", |
| 24 | +# "updated_at": "2023-09-22T18:00:18.754646Z", |
| 25 | +# "deletable_at": "2023-09-23T18:00:18.754646Z", |
| 26 | +# "zone": "fr-par-3" |
| 27 | +# } |
| 28 | +# |
| 29 | +# delete: |
| 30 | +# |
| 31 | +# Delete one provisioned server, if available. |
| 32 | +# |
| 33 | +# See https://www.scaleway.com/en/developers/api/apple-silicon/ for api docs. |
| 34 | + |
| 35 | +set -euo pipefail |
| 36 | +# set -x |
| 37 | + |
| 38 | +project_id="4cf7a85e-f21e-40d4-b758-21d1f4ad3dfb" |
| 39 | +zone=fr-par-3 |
| 40 | +servers_url="https://api.scaleway.com/apple-silicon/v1alpha1/zones/${zone}/servers" |
| 41 | + |
| 42 | +function log { |
| 43 | + echo "$@" >&2 |
| 44 | +} |
| 45 | +function error { |
| 46 | + log "ERROR: $@" |
| 47 | + exit 1 |
| 48 | +} |
| 49 | + |
| 50 | +function req { |
| 51 | + method=$1 |
| 52 | + shift |
| 53 | + curl -sSL --fail-with-body -X $method \ |
| 54 | + -H "Content-Type: application/json" \ |
| 55 | + -H "X-Auth-Token: ${SCW_SECRET_KEY}" \ |
| 56 | + "$@" |
| 57 | +} |
| 58 | +function get { |
| 59 | + req GET "$@" |
| 60 | +} |
| 61 | +function post { |
| 62 | + req POST "$@" |
| 63 | +} |
| 64 | +function delete { |
| 65 | + req DELETE "$@" |
| 66 | +} |
| 67 | + |
| 68 | +function server_id { |
| 69 | + # Return the id of the first server available, else the empty string |
| 70 | + servers=$(get $servers_url || error "failed to request servers list") |
| 71 | + server_ids=$(echo "$servers" | jq -r ".servers[].id") |
| 72 | + for id in $server_ids; do |
| 73 | + echo $id |
| 74 | + break |
| 75 | + done |
| 76 | +} |
| 77 | + |
| 78 | +function maybe_jq { |
| 79 | + # Process the output via jq if displaying on console, otherwise leave |
| 80 | + # it unprocessed. |
| 81 | + if [ -t 1 ]; then |
| 82 | + jq . |
| 83 | + else |
| 84 | + cat |
| 85 | + fi |
| 86 | +} |
| 87 | + |
| 88 | +cmd=${1:-list} |
| 89 | +case $cmd in |
| 90 | + ensure) |
| 91 | + id=$(server_id) |
| 92 | + if [[ "$id" ]]; then |
| 93 | + log "You have servers." |
| 94 | + get "$servers_url/$id" | maybe_jq |
| 95 | + else |
| 96 | + log "Creating new server." |
| 97 | + post $servers_url -d " |
| 98 | + { |
| 99 | + \"name\": \"mac-m1-psycopg\", |
| 100 | + \"project_id\": \"$project_id\", |
| 101 | + \"type\": \"M1-M\" |
| 102 | + }" | maybe_jq |
| 103 | + fi |
| 104 | + ;; |
| 105 | + delete) |
| 106 | + id=$(server_id) |
| 107 | + if [[ "$id" ]]; then |
| 108 | + log "Deleting server $id." |
| 109 | + delete "$servers_url/$id" | maybe_jq |
| 110 | + else |
| 111 | + log "No server found." |
| 112 | + fi |
| 113 | + ;; |
| 114 | + list) |
| 115 | + get $servers_url | maybe_jq |
| 116 | + ;; |
| 117 | + *) |
| 118 | + error "Usage: $(basename $0) [list|ensure|delete]" |
| 119 | +esac |
0 commit comments