DEV Community

Cover image for Digital Ocean deployment automation is simple
Chris McCormick
Chris McCormick

Posted on

Digital Ocean deployment automation is simple

I built a git hosting service called Hosted Gitea and discovered that automated deployment of VPS boxes on Digital Ocean is super simple. Here is how I got it working.

When a customer signs up a chain of events is set in motion:

  • Their details are stored.
  • A background task starts.
  • Use the Digital Ocean API to create a fresh Ubuntu VPS box.
  • Use the Gandi DNS API to point a domain name at the box.
  • Run some Ansible scripts to provision Gitea on the new box.

The Digital Ocean API part consists of just a handful of function calls using python-digitalocean:

from digitalocean import Droplet, Manager

# get our DO API token from an environment variable
token = environ.get("DIGITALOCEAN_ACCESS_TOKEN") or exit("DIGITALOCEAN_ACCESS_TOKEN is not set.")

manager = Manager(token=token)
ssh_keys = manager.get_all_sshkeys()
droplet = Droplet(token=token, ssh_keys=ssh_keys ... etc.)
droplet.create()

# then you can wait for the droplet to be created:
actions = droplet.get_actions()
for action in actions:
    if action.type == "create":
        while action.status != "completed":
            sleep(2)
            print("Waiting for box to deploy.")

It's as simple as that to launch a new VPS box and wait for it to come online. After this step there are a few other things that happen like emails being sent, Ansible scripts to set up Gitea etc. but the part of bringing a new box online is done.

Photo by Todd Cravens on Unsplash.

Top comments (0)