Automating ServerPilot/DO sync on droplet creation

October 13, 2014 3k views

I'd like to automatically run these steps when a new droplet is created:

  1. Since there is no way to send an email containing droplet password if created with an SSH key ( which should be allowed guys ), have the new droplet's password reset ( current workaround for password on droplets w/ ssh keys ), and send that password to another email. Thus allowing myself to have access via ssh, and the client via password until they add their ssh key in later into authorized_hosts.
  2. Create a new server on ServerPilot.
  3. Run the install script given by ServerPilot on the new DO droplet.

Is this possible and how would i be able to accomplish this, say, with your api?

I would also need to work with serverpilots api as well correct?

Perhaps use the two together to create a dashboard of some sorts?

Thanks ahead of time!

2 Answers

We just released a new feature that sounds like it might help with this use case. Our metadata service allows you to provide "user-data" when you create a new droplet. This data can then be used to allow you to configure the server on first boot. It will run bash scripts or cloud-config files.

Using a cloud-config file, you could run the ServerPilot script automatically on first boot and set a root password. It would look something like:

  - echo "You can run arbitrary commands"
  list: |
  expire: True

This would set a root password that had to be changed on first login.

Of course there is a lot more you can do with this, create new users, add ssh keys, etc... For more information, check out these tutorials:

Update CloudInit link and conclusion Metadata is a service provided to DigitalOcean droplets that allows a droplet to access data about itself, i.e. its metadata. Examples of available droplet metadata include user-provided user data, droplet ID, data center region,...
  • I'm considering using DigitalOcean & Serverpilot on a new project, but I don't have any experience with either of these products yet. I'm basically interested in exactly the same process as ckiriaze is.

    Since, like I mentioned, I don't have any experience with either DO or SP yet, can this really all be done via the API without doing anything manually? Would the end result be a Droplet with SP set up and everything specified installed via SP (php, mysql, etc...) without the need to manually run any commands?

    Thanks a lot!

  • @delaiah: I personally haven't worked with ServerPilot's API, but the DigitalOcean end is entirely possible. To create a new DigitalOcean droplet and run the ServerPilot script to connect to their service, you'd use something like:

    curl -X POST "" \
          -d'{"name":"",  "region":"nyc3", "size":"512mb",  "private_networking":true," "image":"ubuntu-14-04-x64", "user_data":
    (test -e /usr/bin/wget || (sudo apt-get update && sudo apt-get -y install wget)) && \
    sudo wget -nv -O && \
    sudo python \
        --server-id=XXXXXXXXX \
        --server-apikey=XXXXXXXXXXXXXXXXXXX", \
          "ssh_keys":[ SSH_KEY_IDs> ]}' \
          -H "Authorization: Bearer $TOKEN" \
          -H "Content-Type: application/json"

    You just need to provide the script in the user-data field.

  • @asb: Awesome, thanks a lot, this is very helpful!

  • Hey Asb, still having issues trying to create a new DigitalOcean droplet through curl, using the code below - only thing im not sure about is what exactly the <SSH KEY IDs> are, and are they placed within the brackets [ ] ?

    curl -X POST "" \
          -d'{"name":"test1",  "region":"nyc3", "size":"512mb",  "private_networking":true," "image":"ubuntu-14-04-x64", "user_data":
    (test -e /usr/bin/wget || (sudo apt-get update && sudo apt-get -y install wget)) && \
    sudo wget -nv -O && \
    sudo python \
        --server-id=$SERVERID \
        --server-apikey=$SERVERAPIKEY", \
          "ssh_keys":[ <SSH KEY IDs> ]}' \
          -H "Authorization: Bearer $TOKEN" \
          -H "Content-Type: application/json"

    but to no avail, no success or error messages returned or anything. Attempted to setup oauth based on DO's api v2, with both methods outlined at

    curl -X $HTTP_METHOD -H "Authorization: Bearer $TOKEN" "$OBJECT"

    But im getting errors like

    {"id":"translation missing:","message":"Unable to authenticate you."}


    curl: (6) Could not resolve host: Authorization

    Any ideas? I believe it has something to do with the ssh key id delegation, since removing that from the params allows for creation of droplet with a response.

  • @kiriaze The ssh_keys parameter takes an array containing the key IDs like so: "ssh_keys":[109121]

    Here is a real working example that I just used to create a server that connected with ServerPilot:

    curl -X POST -d '{"name":"serverpilot-test","region":"nyc3","size":"512mb","image":"ubuntu-14-04-x64","user_data":"'"$SERVERPILOT_SCRIPT"'","private_networking":true,"ssh_keys":[109121]}' \
    "" \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer '$TOKEN''

    To make it clearer to read, I first put the ServerPilot script into an environmental variable:

    (test -e /usr/bin/wget || (sudo apt-get update && sudo apt-get -y install wget)) && \
    sudo wget -nv -O && \
    sudo python \
        --server-id=XXXXXXXXXX \

    Let me know how it goes!

Thanks a lot guys!

  • Also curious, the ability to edit a comment is no longer active - is there a time restriction on that?

Have another answer? Share your knowledge.