Skip to content

Instantly share code, notes, and snippets.

@cfchou
Forked from lg/cloudy-gamer-launcher.sh
Last active August 7, 2019 16:54
Show Gist options
  • Save cfchou/6da15a4e0ce4313ece88fe25ce3dcbf0 to your computer and use it in GitHub Desktop.
Save cfchou/6da15a4e0ce4313ece88fe25ce3dcbf0 to your computer and use it in GitHub Desktop.
Easily start/stop Paperspace and Parsec instances
#!/bin/bash
#
# CloudyGamerLauncher by Larry Gadea
# Easily start/stop Paperspace and Parsec instances
#
# Make sure to fill out the variables below. For the machine id, use the
# 8-letter identifier for the machine on Paperspace (ex. PS8RGDUY)
#
# Note: Requires Paperspace API key (generate one in account settings)
PAPERSPACE_API_KEY=''
MACHINE_ID=''
#####
function status() {
MACHINE_ID=$(echo $MACHINE_ID | tr '[:upper:]' '[:lower:]')
DATA=$(curl --silent --header "x-api-key: $PAPERSPACE_API_KEY" "https://api.paperspace.io/machines/getMachines")
#STATE=$(echo $DATA | jq -r '.[] | select(.id == "'"$MACHINE_ID"'") | .state')
echo $DATA | jq -r '.[] | select(.id == "'"$MACHINE_ID"'") | .state'
}
function start() {
if [ $STATE = "off" ]; then
echo "Starting machine..."
DATA=$(curl --silent -X POST --header "x-api-key: $PAPERSPACE_API_KEY" "https://api.paperspace.io/machines/$MACHINE_ID/start")
else
echo "Starting machine..."
fi
echo "Waiting for machine to start..."
while true; do
DATA=$(curl --silent --header "x-api-key: $PAPERSPACE_API_KEY" "https://api.paperspace.io/machines/getMachines")
echo $DATA | jq -e '.[] | select(.id == "'"$MACHINE_ID"'") | .state == "ready"' > /dev/null && break
sleep 5
done
}
function stop() {
echo "Stopping machine..."
DATA=$(curl --silent -X POST --header "x-api-key: $PAPERSPACE_API_KEY" "https://api.paperspace.io/machines/$MACHINE_ID/stop")
echo "Waiting for machine to stop..."
while true; do
DATA=$(curl --silent --header "x-api-key: $PAPERSPACE_API_KEY" "https://api.paperspace.io/machines/getMachines")
echo $DATA | jq -e '.[] | select(.id == "'"$MACHINE_ID"'") | .state == "off"' > /dev/null && break
sleep 5
done
}
STATE=$(status)
echo "Getting Paperspace machine status..."
echo "machine $MACHINE_ID is $STATE"
if [[ "start" == "$1" ]]; then
start
elif [[ "stop" == "$1" ]]; then
stop
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment