Skip to content

Instantly share code, notes, and snippets.

@bm371613
Last active July 24, 2021 16:43
Show Gist options
  • Save bm371613/07d093b3965683a0acdef689b37820ad to your computer and use it in GitHub Desktop.
Save bm371613/07d093b3965683a0acdef689b37820ad to your computer and use it in GitHub Desktop.
genesis script
#!/bin/bash
set -euo pipefail
API=https://api.genesiscloud.com/compute/v1
TOKEN=$(cat $HOME/.genesis.token)
api_get () {
curl -s -H "X-Auth-Token: ${TOKEN}" "${API}/${1}"
}
api_post () {
set -x
curl -s -H "X-Auth-Token: ${TOKEN}" \
"${API}/${1}" \
-H "Content-Type: application/json" \
$@
set +x
}
api_patch () {
set -x
curl -s -H "X-Auth-Token: ${TOKEN}" \
-X PATCH \
"${API}/${1}" \
-H "Content-Type: application/json" \
$@
set +x
}
get_instance_by_name () {
api_get instances | jq '.instances | map(select (.name == "'$1'"))[0]'
}
get_volume_by_name () {
api_get volumes | jq '.volumes | map(select (.name == "'$1'"))[0]'
}
COMMAND=$1
shift
case $COMMAND in
show)
NAME=$1
shift
get_instance_by_name $NAME | jq
;;
ssh)
NAME=$1
shift
PUBLIC_IP=$(get_instance_by_name $NAME | jq -r '.public_ip')
set -x
ssh ubuntu@$PUBLIC_IP $@
;;
hosts)
NAME=$1
shift
PUBLIC_IP=$(get_instance_by_name $NAME | jq -r '.public_ip')
set -x
cat /etc/hosts
sudo sed -i -E '/^([0-9]+\.){3}[0-9]+\s+'$NAME'$/d' /etc/hosts
echo "$PUBLIC_IP $NAME" | sudo tee -a /etc/hosts
cat /etc/hosts
;;
action)
NAME=$1
shift
ACTION=$1
shift
ID=$(get_instance_by_name $NAME | jq -r '.id')
api_post instances/$ID/actions -d "{\"action\":\"$ACTION\"}"
;;
attach)
NAME=$1
shift
VOLUME_NAME=$1
shift
ID=$(get_instance_by_name $NAME | jq -r '.id')
VOLUME_ID=$(get_volume_by_name $VOLUME_NAME | jq -r '.id')
api_patch instances/$ID -d "{\"volumes\":[\"$VOLUME_ID\"]}" | jq
;;
*)
echo "Unknown command"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment