Skip to content

Instantly share code, notes, and snippets.

@lg
Last active April 28, 2021 14:25
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save lg/878b9ebe75000c4cc7510e0d30e77989 to your computer and use it in GitHub Desktop.
Save lg/878b9ebe75000c4cc7510e0d30e77989 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=''
PARSEC_EMAIL=''
PARSEC_PASSWORD=''
MACHINE_ID=''
#####
echo "Getting Paperspace machine 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')
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")
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
#####
echo "Logging into Parsec..."
DATA=$(curl --silent --cookie-jar parsec_cookies.txt --header 'Content-Type: application/json' --data "{\"email\":\"$PARSEC_EMAIL\",\"password\":\"$PARSEC_PASSWORD\"}" 'https://parsec.tv/ui/auth/')
SESSION_ID=$(echo $DATA | jq -r '.session_id')
echo "Waiting for Paperspace server to appear on Parsec..."
while true; do
DATA=$(curl --silent --cookie parsec_cookies.txt --header 'Content-Type: application/json' --data '{"include_managed":true,"online_only":false}' 'https://parsec.tv/v1/server-list/')
echo $DATA | jq -e '.[] | select(.name == "'"$MACHINE_ID"'")' > /dev/null && break
sleep 5
done
echo "Starting Parsec..."
INSTANCE_ID=$(echo $DATA | jq -r '.[] | select(.name == "'"$MACHINE_ID"'") | .instance_id')
open parsec:server_instance_id=$INSTANCE_ID:session_id=$SESSION_ID
read -p "Press Enter to stop machine."
#####
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
echo "Done!"
@lg
Copy link
Author

lg commented Jun 19, 2017

Note this requires 'jq' be installed, and same with the Parsec client. Only tested on macOS.

@lg
Copy link
Author

lg commented Jul 5, 2017

Updated the gist to now use the new Paperspace API (https://www.paperspace.com/api)

@F4MES
Copy link

F4MES commented May 7, 2018

Any way to get parsec to launch(Play) the machine with the script as well?

@InvaderJ
Copy link

Dude, been looking for something like this! :)

Running it (zsh) seems to fire up the Paperspace machine and Parsec, but accompanied by an endless stream of parse error: Invalid numeric literal at line 1, column 10

After it's running tempting to get status seems to hang and also spits out endless parse error: Invalid numeric literal at line 1, column 10

Unfortunately I don't know enough about debugging this to help fix, but, it would be so awesome to get this up and running properly. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment