Difference between revisions of "Category:DigitalOcean"

From Christoph's Personal Wiki
Jump to: navigation, search
(Regions)
Line 48: Line 48:
 
<pre>
 
<pre>
 
#!/usr/bin/env python
 
#!/usr/bin/env python
import json
+
import re, sys, json, requests
 
from prettytable import PrettyTable
 
from prettytable import PrettyTable
 
+
TOKEN = "<REDACTED>"
# Create the 'do_regions.json' file by saving the output of:
+
ENDPOINT = "https://api.digitalocean.com/v2"
# $ curl "${API_URL}/regions" \
+
headers = {'Content-Type': 'application/json',
#        -H "Authorization: Bearer ${TOKEN}" \
+
          "Authorization": "Bearer %s" % TOKEN}
#        -H "Content-Type: application/json" > do_regions.json
+
r = requests.get(ENDPOINT + "/regions", headers=headers)
 
+
if r.status_code == 200:
with open('do_regions.json') as json_regions:
+
    regions = r.json()['regions']
     data = json.load(json_regions)
+
else:
 +
     print "Could not connect to DigitalOcean\n"
 +
    sys.exit(0)
  
 
table = PrettyTable(["Slug", "Name", "Sizes", "Features"])
 
table = PrettyTable(["Slug", "Name", "Sizes", "Features"])
 
table.align = "l"
 
table.align = "l"
  
for region in data["regions"]:
+
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:
 
     slug = region["slug"]
 
     slug = region["slug"]
 
     name = region["name"]
 
     name = region["name"]
     ', '.join(region["sizes"]) if region["available"] else "<unavailable>"
+
     region["sizes"].sort(key=getsize)
 +
    sizes = ', '.join(region["sizes"]) \
 +
            if region["available"] else "<unavailable>"
 
     features = ', '.join(region["features"])
 
     features = ', '.join(region["features"])
 
     table.add_row([slug, name, sizes, features])
 
     table.add_row([slug, name, sizes, features])
  
print table
+
print table.get_string(sortby="Slug")
 
</pre>
 
</pre>
  
 
Running the above script produces:
 
Running the above script produces:
 
<pre>
 
<pre>
+------+-----------------+---------------------------------------------------+-----------------------------------------------------+
+
+------+-----------------+---------------------------------------------------+---------------------------------------------+
| Slug | Name            | Sizes                                            | Features                                           |
+
| Slug | Name            | Sizes                                            | Features                                   |
+------+-----------------+---------------------------------------------------+-----------------------------------------------------+
+
+------+-----------------+---------------------------------------------------+---------------------------------------------+
| nyc1 | New York 1      | <unavailable>                                    | virtio, backups, metadata                          |
+
| ams1 | Amsterdam 1    | <unavailable>                                    | backups                                    |
| ams1 | Amsterdam 1    | <unavailable>                                    | virtio, backups                                    |
+
| ams2 | Amsterdam 2    | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
| sfo1 | San Francisco 1 | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| ams3 | Amsterdam 3    | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
| nyc2 | New York 2      | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups                 |
+
| fra1 | Frankfurt 1     | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
| ams2 | Amsterdam 2     | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| lon1 | London 1       | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
| sgp1 | Singapore 1     | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| nyc1 | New York 1     | <unavailable>                                    | backups, metadata                          |
| lon1 | London 1       | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| nyc2 | New York 2      | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups                 |
| nyc3 | New York 3      | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| nyc3 | New York 3      | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
| ams3 | Amsterdam 3    | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| sfo1 | San Francisco 1 | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
| fra1 | Frankfurt 1    | 32gb, 16gb, 2gb, 1gb, 4gb, 8gb, 512mb, 64gb, 48gb | virtio, private_networking, backups, ipv6, metadata |
+
| sgp1 | Singapore 1    | 512mb, 1gb, 2gb, 4gb, 8gb, 16gb, 32gb, 48gb, 64gb | private_networking, backups, ipv6, metadata |
+------+-----------------+---------------------------------------------------+-----------------------------------------------------+
+
+------+-----------------+---------------------------------------------------+---------------------------------------------+
 
</pre>
 
</pre>
  

Revision as of 00:15, 17 June 2015

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")

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, json, 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)
if r.status_code == 200:
    regions = r.json()['regions']
else:
    print "Could not connect to DigitalOcean\n"
    sys.exit(0)

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:
    slug = region["slug"]
    name = region["name"]
    region["sizes"].sort(key=getsize)
    sizes = ', '.join(region["sizes"]) \
            if region["available"] else "<unavailable>"
    features = ', '.join(region["features"])
    table.add_row([slug, name, sizes, 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 json
from prettytable import PrettyTable

# Create the 'do_images.json' file by saving the output of:
# $ curl "${API_URL}/images" \
#        -H "Authorization: Bearer ${TOKEN}" \
#        -H "Content-Type: application/json" > do_images.json

with open('do_images.json') as json_data:
    data = json.load(json_data)

table = PrettyTable(["ID","Slug","Distribution","Name","Type"])
table.align = "l"

for image in data["images"]:
    image_id = image["id"]
    slug = image["slug"]
    distribution = image["distribution"]
    name = image["name"]
    image_type = image["type"]
    table.add_row([image_id,slug,distribution,name,image_type])

print table

Running the above script produces:

+----------+------------------+--------------+----------------------+-----------+
| ID       | Slug             | Distribution | Name                 | Type      |
+----------+------------------+--------------+----------------------+-----------+
| 11732785 | None             | Debian       | Maintenance Mode     | snapshot  |
| 11420434 | coreos-stable    | CoreOS       | 633.1.0 (stable)     | snapshot  |
| 11434448 | coreos-beta      | CoreOS       | 647.0.0 (beta)       | snapshot  |
| 11657005 | coreos-alpha     | CoreOS       | 668.2.0 (alpha)      | snapshot  |
| 11385199 | None             | Debian       | vum-easter-move      | snapshot  |
| 11594346 | None             | Ubuntu       | blushibiza.com final | temporary |
| 6370882  | fedora-20-x64    | Fedora       | 20 x64               | snapshot  |
| 6370885  | fedora-20-x32    | Fedora       | 20 x32               | snapshot  |
| 6372321  | centos-5-8-x64   | CentOS       | 5.10 x64             | snapshot  |
| 6372425  | centos-5-8-x32   | CentOS       | 5.10 x32             | snapshot  |
| 6372581  | debian-6-0-x64   | Debian       | 6.0 x64              | snapshot  |
| 6372662  | debian-6-0-x32   | Debian       | 6.0 x32              | snapshot  |
| 9640922  | fedora-21-x64    | Fedora       | 21 x64               | snapshot  |
| 9801948  | ubuntu-14-04-x32 | Ubuntu       | 14.04 x32            | snapshot  |
| 9801950  | ubuntu-14-04-x64 | Ubuntu       | 14.04 x64            | snapshot  |
| 9801951  | ubuntu-14-10-x32 | Ubuntu       | 14.10 x32            | snapshot  |
| 9801954  | ubuntu-14-10-x64 | Ubuntu       | 14.10 x64            | snapshot  |
| 10144573 | freebsd-10-1-x64 | FreeBSD      | 10.1                 | snapshot  |
| 10321756 | ubuntu-12-04-x64 | Ubuntu       | 12.04.5 x64          | snapshot  |
| 10321777 | ubuntu-12-04-x32 | Ubuntu       | 12.04.5 x32          | snapshot  |
+----------+------------------+--------------+----------------------+-----------+

External links

This category currently contains no pages or media.