How do you script a custom droplet?

February 12, 2015 2.3k views

I'd like to be able to write some scripts to completely automate the creation of WordPress droplets on Ubuntu Server. But, I'd like customized WordPress droplets that are not currently offered in the Digital Ocean droplet creation UI.

For example...I'd like the WordPress droplets to run on Nginx on Ubuntu instead of Apache. The Digital Ocean WordPress application runs on Apache while the LEMP application install does not include WordPress.

Neither instance has a firewall enabled - which Digital Ocean does recommend doing once the droplet is created.

Neither instance includes Varnish, which may help with page load times.

While I can look up how to manually enable the firewall, how to install WordPress on LEMP and even how to install Varnish...I would like to be able to script all of this (starting with using the Digital Ocean v2 API) so that I can create my own web UI to start an instance of WordPress on LEMP with Varnish and the firewall configured and ready to go.

Doing this all via scripting seems like it would save a lot more time than manually setting everything up for each server.

How would you go about doing this? Are there any tutorials on scripting this sort of thing?

2 Answers

There are of course many different approaches that you could take. You could use one of the various configuration management tools like Chef, Ansible, or Fabric to script this.

Another option is to pass a script in the "user-data" when creating a new droplet. This allows you to provide a script that will run on first boot of the droplet. For more advance usages, check out these tutorials on cloud-config scripting:

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,...

Internally we use scripts written to use Fabric to install and configure the software needed to create the master images for our one clicks. It's really quite easy to start using, even if you are not very familiar with Python and will allow you to script custom deployments so you would simply launch a droplet with the OS you want to start with (either from the control panel or API) and then execute your fabric script against the new IP address.

Have another answer? Share your knowledge.