Skip to content

Instantly share code, notes, and snippets.

@jpospychala
Last active December 21, 2021 16:44
Show Gist options
  • Save jpospychala/69d783f2eeeea1a9450e to your computer and use it in GitHub Desktop.
Save jpospychala/69d783f2eeeea1a9450e to your computer and use it in GitHub Desktop.
create DigitalOcean droplet from shell with curl and rcli
# this script expects $DOTOKEN variable with your digital ocean token
# this script gets or creates smallest DO droplet and remotely executes script on it
# afterwards, droplet is destroyed
DROPLETNAME=example.com
DOHOME="https://api.digitalocean.com/v2"
cat <<EOF > .curlargs
-s
-H "Authorization: Bearer $DOTOKEN"
-H "Content-Type: application/json"
EOF
CURL="curl -K .curlargs"
SSHKEYS=`$CURL "$DOHOME/account/keys" | R path ssh_keys | R map path fingerprint`
CREATECONTAINER=`cat <<EOF
{
"name":"$DROPLETNAME",
"region":"nyc3",
"size":"512mb",
"image":"ubuntu-14-04-x64",
"ssh_keys":$SSHKEYS}
EOF`
echo $SSHKEYS
function GET_DROPLET {
DROPLET=`$CURL "$DOHOME/droplets/" | \
R path droplets | \
R find where "{\"name\": \"$DROPLETNAME\"}"`
export IP=`R path networks.v4.0.ip_address <<< $DROPLET`
export ID=`R path id <<< $DROPLET`
echo "dropet id: $ID ip: $IP"
}
GET_DROPLET
if [ -z "$ID" ]; then
echo create droplet!
$CURL -X POST -d "$CREATECONTAINER" "$DOHOME/droplets/"
while [ -z "$IP" ]; do
GET_DROPLET
done
fi
# execute
ssh "root@$IP" 'bash -s' < run.sh
# delete droplet
$CURL -X DELETE "$DOHOME/droplets/$ID"
rm .curlargs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment