Difference between revisions of "Category:DigitalOcean"

From Christoph's Personal Wiki
Jump to: navigation, search
(Droplets)
(Regions)
(5 intermediate revisions by the same user not shown)
Line 43: Line 43:
 
* sfo1 ("San Francisco 1")
 
* sfo1 ("San Francisco 1")
 
* sgp1 ("Singapore 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:
 
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:
Line 48: Line 56:
 
<pre>
 
<pre>
 
#!/usr/bin/env python
 
#!/usr/bin/env python
import json
+
import re, sys, requests
 
from prettytable import PrettyTable
 
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)
  
# Create the 'do_regions.json' file by saving the output of:
+
errmsg = "Could not connect to DigitalOcean. Exiting..."
# $ curl "${API_URL}/regions" \
+
regions = r.json()['regions'] if r.status_code == 200 else sys.exit(errmsg)
#        -H "Authorization: Bearer ${TOKEN}" \
+
#        -H "Content-Type: application/json" > do_regions.json
+
 
+
with open('do_regions.json') as json_regions:
+
    data = json.load(json_regions)
+
  
 
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+)(.*)$")
     slug = region["slug"]
+
mult = dict(KB=2**10, MB=2**20, GB=2**30)
     name = region["name"]
+
 
     ', '.join(region["sizes"]) if region["available"] else "<unavailable>"
+
def getsize(size):
     features = ', '.join(region["features"])
+
     value = size_re.match(size).group(1)
    table.add_row([slug, name, sizes, features])
+
     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
+
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>
  
Line 177: Line 193:
 
<pre>
 
<pre>
 
#!/usr/bin/env python
 
#!/usr/bin/env python
import json
+
import sys, requests
 
from prettytable import PrettyTable
 
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)
  
# Create the 'do_images.json' file by saving the output of:
+
table = PrettyTable(["ID", "Slug", "Distribution", "Name", "Type"])
# $ 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"
 
table.align = "l"
 +
[table.add_row([image["id"], image["slug"], image["distribution"],
 +
                image["name"], image["type"]]) for image in images]
  
for image in data["images"]:
+
print table.get_string(sortby="Slug")
    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
+
 
</pre>
 
</pre>
  
 
Running the above script produces:
 
Running the above script produces:
 
<pre>
 
<pre>
+----------+------------------+--------------+----------------------+-----------+
+
+----------+------------------+--------------+------------------+----------+
| ID      | Slug            | Distribution | Name                 | Type     |
+
| ID      | Slug            | Distribution | Name             | Type     |
+----------+------------------+--------------+----------------------+-----------+
+
+----------+------------------+--------------+------------------+----------+
| 11732785 | None            | Debian       | Maintenance Mode    | snapshot |
+
| 6372425  | centos-5-8-x32  | CentOS       | 5.10 x32        | snapshot |
| 11420434 | coreos-stable    | CoreOS       | 633.1.0 (stable)    | snapshot |
+
| 6372321  | centos-5-8-x64  | CentOS       | 5.10 x64        | snapshot |
| 11434448 | coreos-beta      | CoreOS       | 647.0.0 (beta)      | snapshot |
+
| 11523060 | centos-6-5-x32  | CentOS       | 6.5 x32          | snapshot |
| 11657005 | coreos-alpha    | CoreOS       | 668.2.0 (alpha)      | snapshot |
+
| 11523085 | centos-6-5-x64  | CentOS       | 6.5 x64          | snapshot |
| 11385199 | None            | Debian       | vum-easter-move      | snapshot |
+
| 10322623 | centos-7-0-x64  | CentOS       | 7 x64            | snapshot |
| 11594346 | None            | Ubuntu       | blushibiza.com final | temporary |
+
| 12276929 | coreos-alpha    | CoreOS       | 709.0.0 (alpha) | snapshot |
| 6370882 | fedora-20-x64    | Fedora      | 20 x64              | snapshot |
+
| 12334225 | coreos-beta      | CoreOS       | 695.2.0 (beta)  | snapshot |
| 6370885  | fedora-20-x32    | Fedora       | 20 x32              | snapshot |
+
| 12247463 | coreos-stable    | CoreOS       | 681.0.0 (stable) | snapshot |
| 6372321  | centos-5-8-x64  | CentOS       | 5.10 x64            | snapshot |
+
| 6372662 | debian-6-0-x32  | Debian       | 6.0 x32         | snapshot |
| 6372425 | centos-5-8-x32  | CentOS       | 5.10 x32             | snapshot |
+
| 6372581  | debian-6-0-x64  | Debian      | 6.0 x64         | snapshot |
| 6372581  | debian-6-0-x64  | Debian      | 6.0 x64             | snapshot |
+
| 10322378 | debian-7-0-x32  | Debian      | 7.0 x32         | snapshot |
| 6372662  | debian-6-0-x32  | Debian      | 6.0 x32             | snapshot |
+
| 10322059 | debian-7-0-x64   | Debian       | 7.0 x64         | snapshot |
| 9640922  | fedora-21-x64   | Fedora       | 21 x64               | snapshot |
+
| 6370885 | fedora-20-x32   | Fedora       | 20 x32           | snapshot |
| 9801948 | ubuntu-14-04-x32 | Ubuntu       | 14.04 x32           | snapshot |
+
| 6370882 | fedora-20-x64   | Fedora       | 20 x64           | snapshot |
| 9801950 | ubuntu-14-04-x64 | Ubuntu       | 14.04 x64           | snapshot |
+
| 9640922 | fedora-21-x64   | Fedora       | 21 x64           | snapshot |
| 9801951 | ubuntu-14-10-x32 | Ubuntu      | 14.10 x32            | snapshot  |
+
| 10144573 | freebsd-10-1-x64 | FreeBSD      | 10.1             | snapshot |
| 9801954  | ubuntu-14-10-x64 | Ubuntu       | 14.10 x64           | snapshot |
+
| 10321777 | ubuntu-12-04-x32 | Ubuntu      | 12.04.5 x32      | snapshot |
| 10144573 | freebsd-10-1-x64 | FreeBSD      | 10.1                 | snapshot |
+
| 10321756 | ubuntu-12-04-x64 | Ubuntu      | 12.04.5 x64      | snapshot |
| 10321756 | ubuntu-12-04-x64 | Ubuntu      | 12.04.5 x64          | snapshot |
+
| 9801951  | ubuntu-14-10-x32 | Ubuntu      | 14.10 x32        | snapshot |
| 10321777 | ubuntu-12-04-x32 | Ubuntu      | 12.04.5 x32         | snapshot  |
+
| 9801954 | ubuntu-14-10-x64 | Ubuntu      | 14.10 x64        | snapshot |
+----------+------------------+--------------+----------------------+-----------+
+
+----------+------------------+--------------+------------------+----------+
 
</pre>
 
</pre>
  
Line 234: Line 243:
  
 
[[Category:Technical and Specialized Skills]]
 
[[Category:Technical and Specialized Skills]]
 +
[[Category:APIs]]

Revision as of 20:52, 19 March 2016

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.