rapoport
By:
rapoport

How to launch a droplet via API?

October 11, 2017 548 views
API

I am interested in a reliable way of launching droplets over API. I want to know how to check for all possible errors and how to detect if a new droplet is finally running.

Just doing POST to /v2/droplets sometimes might not be enough. I had following problems:

  • An error response but a simple repeat works fine. I had following errors "There was an error while trying to create your tags. Please try again", "Server was unable to give you a response." Repeating the exact same request helped in those cases.
  • No response at all (or network error) but droplet was launched. A search for the droplet is needed in such a case because droplet ID wasn't returned. Listing all droplets is an option but don't forget about pagination (200 per request is the limit). Tags cannot be used to list droplets because sometimes they are set much later. Name of the droplet can be used as identification but you cannot use it for direct request. Perhaps going through actions could be used to search for the droplet create action. But the list can be long too.
  • A proper response (with ip, id etc.) but later droplet fails to start properly and ends with status "archive" (I had this twice today). This might be checked using "status" field - but what should the value be? In such case tags might be not be set. A better option would be to list actions on the droplet and search for the type "create". But the documentation doesn't even list all possible types and pagination can kick in here too.

It looks quite complex to me.

What would be a reliable yet simple approach to launch droplets that works around all possible cases and keeps working even if you have hundreds of droplets?

2 Answers

Personally on my own projects I use a system where one script sends the create command to the API using the Ruby library (there are libraries for most common languages that make this a lot easier) and store it's data in a database with a status of "creating". I then have a second script that checks for droplets that are listed in the db in that status and every 30seconds or so it queries the API for that droplet to get it's status. Once the API says the droplet is active it updates the db so it stops querying.

That may be a bit more complex that what you need but the overall concept:

  1. Send create request
  2. query periodically to see if the droplet is finished being created.
  3. consider the droplet created

https://github.com/Radau/DigitalOcean-Starbound-Backup-and-Restore

Make-snapshot-and-destroy or restore in one launch (unless DO API starts bugging again).

Have another answer? Share your knowledge.