Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andragabr/6451c92ba84d8afe039be7b1b4f9c4c3 to your computer and use it in GitHub Desktop.
Save andragabr/6451c92ba84d8afe039be7b1b4f9c4c3 to your computer and use it in GitHub Desktop.

Revisions

  1. @jpospychala jpospychala renamed this gist Dec 11, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jpospychala jpospychala created this gist Dec 11, 2014.
    51 changes: 51 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    # 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