DEV Community

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

Posted on

1 1

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Try REST API Generation for MS SQL Server.

DevOps for Private APIs. With DreamFactory API Generation, you get:

  • Auto-generated live APIs mapped from database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay