Category:DigitalOcean

From Christoph's Personal Wiki
Jump to: navigation, search

This category will be all about using DigitalOcean's various Cloud products and services.

DigitalOcean API

Note: This category and associated articles will only cover version 2 (v2) of the DigitalOcean API.

Environment variables

$ API_URL="https://api.digitalocean.com/v2"
$ TOKEN=<YOUR_API_TOKEN>

I will be using the above environment variables for the remainder of this article.

Account

  • Get your DigitalOcean (basic) account information:
$ curl -sXGET "${API_URL}/account" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H "Content-Type: application/json" | python -mjson.tool
{
    "account": {
        "droplet_limit": 25,
        "email": "bob@example.com",
        "email_verified": true,
        "uuid": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    }
}

Regions

$ curl "${API_URL}/regions" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H "Content-Type: application/json" | python -mjson.tool
  • ams1 ("Amsterdam 1")
  • ams2 ("Amsterdam 2")
  • ams3 ("Amsterdam 3")
  • fra1 ("Frankfurt 1")
  • lon1 ("London 1")
  • nyc1 ("New York 1")
  • nyc2 ("New York 2")
  • nyc3 ("New York 3")
  • sfo1 ("San Francisco 1")
  • sgp1 ("Singapore 1")
  • tor1 ("Toronto 1")

Another way (useful for scripting) is to pipe the output through jq:

$ curl "${API_URL}/regions" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H "Content-Type: application/json" | jq -crM '[.regions[] | .slug]'
["nyc1","sfo1","nyc2","ams2","sgp1","lon1","nyc3","ams3","fra1","tor1"]

A quick-and-dirty way to get a pretty table output of the DigitalOcean regions available for Droplets (and the associated sizes and features) is with a Python script something like the following:

#!/usr/bin/env python
import re, sys, requests
from prettytable import PrettyTable
TOKEN = "<REDACTED>"
ENDPOINT = "https://api.digitalocean.com/v2"
headers = {'Content-Type': 'application/json',
           "Authorization": "Bearer %s" % TOKEN}
r = requests.get(ENDPOINT + "/regions", headers=headers)

errmsg = "Could not connect to DigitalOcean. Exiting..."
regions = r.json()['regions'] if r.status_code == 200 else sys.exit(errmsg)

table = PrettyTable(["Slug", "Name", "Sizes", "Features"])
table.align = "l"

size_re = re.compile(r"^(\d+)(.*)$")
mult = dict(KB=2**10, MB=2**20, GB=2**30)

def getsize(size):
    value = size_re.match(size).group(1)
    unit = size_re.match(size).group(2)
    multiplier = mult[unit.upper()]
    return float(value) * multiplier

for region in regions:
    sizes = '; '.join(sorted(region["sizes"], key=getsize)) \
            if region["available"] else "<unavailable>"
    table.add_row([region["slug"], region["name"], sizes,
                   ', '.join(region["features"])])

print table.get_string(sortby="Slug")

Running the above script produces:

+------+-----------------+---------------------------------------------------+---------------------------------------------+
| Slug | Name            | Sizes                                             | Features                                    |
+------+-----------------+---------------------------------------------------+---------------------------------------------+
| ams1 | Amsterdam 1     | <unavailable>                                     | backups                                     |
| ams2 | Amsterdam 2     | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
| ams3 | Amsterdam 3     | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
| fra1 | Frankfurt 1     | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
| lon1 | London 1        | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
| nyc1 | New York 1      | <unavailable>                                     | backups, metadata                           |
| nyc2 | New York 2      | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups                 |
| nyc3 | New York 3      | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
| sfo1 | San Francisco 1 | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
| sgp1 | Singapore 1     | 512mb; 1gb; 2gb; 4gb; 8gb; 16gb; 32gb; 48gb; 64gb | private_networking, backups, ipv6, metadata |
+------+-----------------+---------------------------------------------------+---------------------------------------------+

Droplets

  • Create a Droplet:
$ curl -vXPOST "${API_URL}/droplets" \
   -d'{"name":"my-test-droplet","region":"nyc3","size":"512mb","image":"ubuntu-14-04-x64"}' \
   -H "Authorization: Bearer ${TOKEN}" \
   -H "Content-Type: application/json"
# RESPONSE BODY:
{
    "droplet": {
        "backup_ids": [],
        "created_at": "2015-05-05T20:26:24Z",
        "disk": 20,
        "features": [
            "virtio"
        ],
        "id": 1234567,
        "image": {},
        "kernel": {
            "id": 2924,
            "name": "Ubuntu 14.04 x64 vmlinuz-3.13.0-43-generic",
            "version": "3.13.0-43-generic"
        },
        "locked": true,
        "memory": 512,
        "name": "my-test-droplet",
        "networks": {},
        "next_backup_window": null,
        "region": {},
        "size": {},
        "size_slug": "512mb",
        "snapshot_ids": [],
        "status": "new",
        "vcpus": 1
    },
    "links": {
        "actions": [
            {
                "href": "https://api.digitalocean.com/v2/actions/87654321",
                "id": 87654321,
                "rel": "create"
            }
        ]
    }
}
  • List Droplets on your account:
$ curl -XGET "${API_URL}/droplets" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H "Content-Type: application/json" | python -mjson.tool

Or, pretty print the output:

$ curl -sXGET "${API_URL}/droplets" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H "Content-Type: application/json" |\
       python -c 'import sys,json;data=json.loads(sys.stdin.read());\
                  print "ID\tName\tRegion\n";\
                  print "\n".join(["%s\t%s\t%s"%(d["id"],d["name"],d["region"]["slug"])\
                  for d in data["droplets"]])'|column -t
ID       Name               Region
1234567  my-test-droplet-1  nyc3
2345678  my-test-droplet-2  ams1
  • List the details of a given Droplet:
$ DROPLET_ID=1234567
$ curl -sXGET "${API_URL}/droplets/${DROPLET_ID}" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H "Content-Type: application/json" | python -mjson.tool
  • Delete a given droplet:
$ DROPLET_ID=1234567
$ curl -X DELETE "${API_URL}/droplets/${DROPLET_ID}" \
       -H "Authorization: Bearer ${TOKEN}" \
       -H 'Content-Type: application/json'

Images

A quick-and-dirty way to get a pretty table output of the DigitalOcean images available for Droplets is with a Python script something like the following:

#!/usr/bin/env python
import sys, requests
from prettytable import PrettyTable
TOKEN = "<REDACTED>"
ENDPOINT = "https://api.digitalocean.com/v2"
headers = {'Content-Type': 'application/json',
           "Authorization": "Bearer %s" % TOKEN}
r = requests.get(ENDPOINT + "/images", headers=headers)
errmsg = "Could not connect to DigitalOcean. Exiting..."
images = r.json()['images'] if r.status_code == 200 else sys.exit(errmsg)

table = PrettyTable(["ID", "Slug", "Distribution", "Name", "Type"])
table.align = "l"
[table.add_row([image["id"], image["slug"], image["distribution"],
                image["name"], image["type"]]) for image in images]

print table.get_string(sortby="Slug")

Running the above script produces:

+----------+------------------+--------------+------------------+----------+
| ID       | Slug             | Distribution | Name             | Type     |
+----------+------------------+--------------+------------------+----------+
| 6372425  | centos-5-8-x32   | CentOS       | 5.10 x32         | snapshot |
| 6372321  | centos-5-8-x64   | CentOS       | 5.10 x64         | snapshot |
| 11523060 | centos-6-5-x32   | CentOS       | 6.5 x32          | snapshot |
| 11523085 | centos-6-5-x64   | CentOS       | 6.5 x64          | snapshot |
| 10322623 | centos-7-0-x64   | CentOS       | 7 x64            | snapshot |
| 12276929 | coreos-alpha     | CoreOS       | 709.0.0 (alpha)  | snapshot |
| 12334225 | coreos-beta      | CoreOS       | 695.2.0 (beta)   | snapshot |
| 12247463 | coreos-stable    | CoreOS       | 681.0.0 (stable) | snapshot |
| 6372662  | debian-6-0-x32   | Debian       | 6.0 x32          | snapshot |
| 6372581  | debian-6-0-x64   | Debian       | 6.0 x64          | snapshot |
| 10322378 | debian-7-0-x32   | Debian       | 7.0 x32          | snapshot |
| 10322059 | debian-7-0-x64   | Debian       | 7.0 x64          | snapshot |
| 6370885  | fedora-20-x32    | Fedora       | 20 x32           | snapshot |
| 6370882  | fedora-20-x64    | Fedora       | 20 x64           | snapshot |
| 9640922  | fedora-21-x64    | Fedora       | 21 x64           | snapshot |
| 10144573 | freebsd-10-1-x64 | FreeBSD      | 10.1             | snapshot |
| 10321777 | ubuntu-12-04-x32 | Ubuntu       | 12.04.5 x32      | snapshot |
| 10321756 | ubuntu-12-04-x64 | Ubuntu       | 12.04.5 x64      | snapshot |
| 9801951  | ubuntu-14-10-x32 | Ubuntu       | 14.10 x32        | snapshot |
| 9801954  | ubuntu-14-10-x64 | Ubuntu       | 14.10 x64        | snapshot |
+----------+------------------+--------------+------------------+----------+

External links

This category currently contains no pages or media.