-
-
Save ChristianBagley/8de6f516c06ebc722648b27dda52719f to your computer and use it in GitHub Desktop.
Easily start/stop Paperspace and Parsec instances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# CloudyGamerLauncher by Larry Gadea | |
# Easily start/stop Paperspace and Parsec instances | |
# | |
# Make sure to fill out the variables below. For the machine name, use the | |
# 8-letter identifier for the machine on Paperspace (ex. PS8RGDUY) | |
PAPERSPACE_EMAIL='' | |
PAPERSPACE_PASSWORD='' | |
PARSEC_EMAIL='' | |
PARSEC_PASSWORD='' | |
MACHINE_NAME='' | |
##### | |
echo "Logging into Paperspace..." | |
DATA=$(curl --silent --data-urlencode "email=$PAPERSPACE_EMAIL" --data-urlencode "password=$PAPERSPACE_PASSWORD" 'https://api.paperspace.io/users/login?include=user') | |
TOKEN=$(echo $DATA | jq -r '.id') | |
USER_ID=$(echo $DATA | jq -r '.userId') | |
echo "Getting machine status..." | |
MACHINE_NAME=$(echo $MACHINE_NAME | tr '[:upper:]' '[:lower:]') | |
DATA=$(curl --silent "https://api.paperspace.io/users/$USER_ID/getMachines?access_token=$TOKEN") | |
STATE=$(echo $DATA | jq -r '.[] | select(.xenName == "'"$MACHINE_NAME"'") | .state') | |
MACHINE_ID=$(echo $DATA | jq -r '.[] | select(.xenName == "'"$MACHINE_NAME"'") | .id') | |
if [ $STATE = "Off" ]; then | |
echo "Starting machine..." | |
DATA=$(curl --silent --header 'Content-Type: application/json' --data "{\"id\":$MACHINE_ID}" "https://api.paperspace.io/machines/start?access_token=$TOKEN") | |
fi | |
echo "Waiting for machine to start..." | |
while true; do | |
DATA=$(curl --silent "https://api.paperspace.io/machines/getSnapshots/$MACHINE_ID?access_token=$TOKEN") | |
echo $DATA | jq -e '.state == "AgentReady"' > /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_NAME"'")' > /dev/null && break | |
sleep 5 | |
done | |
echo "Starting Parsec..." | |
INSTANCE_ID=$(echo $DATA | jq -r '.[] | select(.name == "'"$MACHINE_NAME"'") | .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 --header 'Content-Type: application/json' --data "{\"id\":$MACHINE_ID}" "https://api.paperspace.io/machines/stop?access_token=$TOKEN") | |
echo "Waiting for machine to stop..." | |
while $(curl --silent "https://api.paperspace.io/machines/getSnapshots/$MACHINE_ID?access_token=$TOKEN" | jq -e '.state != "Off"'); do | |
sleep 5 | |
done | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment