[Python] Basics on how to create instances with digitalocean

Andy Ferguson andyf at sonic.net
Thu Nov 12 15:02:04 UTC 2015


Jason, thanks for the information! Very helpful. Andy

 

From: python-bounces at lists.noisebridge.net [mailto:python-bounces at lists.noisebridge.net] On Behalf Of Jason Monroe
Sent: Tuesday, November 10, 2015 12:56 AM
To: python at lists.noisebridge.net
Subject: [Python] Basics on how to create instances with digitalocean

 

Hello All,

 

In the class tonight we didn't go over all of these various aspects but we got through some of them. 

 

I thought I would put together a sort of step by step to help those that didn't go who had an interest get through it. 

 

 

 

Step 1) Go to Digitalocean.com and sign up for an account

 

2) Click on the API button the top of the screen and create yourself an API token 

store the token in a safe place

 

3) Create an ssh-key

you can do this with ssh-keygen from a unix terminal 

command: ssh-keygen -t rsa -b 8192 -f ~/.ssh/do

 

You can add a passphrase if you like

 

 

4) Go to Digital Ocean In the top on the right click the [ GEAR ICON ] -> Select Settings 

then go to Security on the left hand side

 

5) Select Add key 

Paste in THE PUBLIC KEY 

Which from the example above is ~/.ssh/do.pub 

 

6) Select "Create Key" 

 

7) Get the fingerprint for that key 

curl -X GET -H 'Content-Type: application/json' -H 'Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582' "https://api.digitalocean.com/v2/account/keys"

 

The value b7XXX is your TOKEN 

 

CURL mostly available on Linux, Mac, or Cygwin 

 

8) You will get something like this: 

{
  "ssh_keys": [
    {
      "id": 512189,
      "fingerprint": "3b:16:bf:e4:8b:00:8b:b8:59:8c:a9:d3:f0:19:45:fa",
      "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDDHr/jh2Jy4yALcK4JyWbVkPRaWmhck3IgCoeOO3z1e2dBowLh64QAM+Qb72pxekALga2oi4GvT+TlWNhzPH4V example",
      "name": "My SSH Public Key"
    }
  ],
  "links": {
  },
  "meta": {
    "total": 1
  }
}

 

9)  Record the Fingerprint value

 

10) Create a text file called myconfig.cfg 

 

11) In the file put the following values 

[mysection]

stoken=    REPLACE_ME_WITH_YOUR_TOKEN_REMOVE_LEADING_SPACES

 

12) The code to make it work 

 

import requests 

import ConfigParser

 

Config = ConfigParser.ConfigParser()

filename="myconfig.cfg"

Config.read(filename)

stoken=Config.get('mysection','stoken')

 

mypayload = {"name":"example.com","region":"nyc3","size":"512mb","image":"ubuntu-14-04-x64","backups":False,"ipv6":True,'ssh_keys':["e3:5f:6c:79:f4:3d:8f:2e:22:d3:59:c3:e1:59:4b:3c"]}

headers = { 'Content-Type':'application/json','Authorization':'Bearer ' + stoken }

endpoint= "https://api.digitalocean.com/v2/droplets"

r =requests.post(endpoint,json=mypayload,headers=headers)

print r.text

 

 

13) SEE THE ssh_keys VALUE? 

 

14) Change that to be the value from STEP 8 

 

15) You should then see 

A JSON output like: 

{"droplet":{"id":8710128,"name":"example.com","memory":512,"vcpus":1,"disk":20,"locked":true,"status":"new","kernel":{"id":5175,"name":"Ubuntu 14.04 x64 vmlinuz-3.13.0-57-generic","version":"3.13.0-57-generic"},"created_at":"2015-11-10T08:36:50Z","features":["virtio"],"backup_ids":[],"next_backup_window":null,"snapshot_ids":[],"image":{"id":13089493,"name":"14.04 x64","distribution":"Ubuntu","slug":"ubuntu-14-04-x64","public":true,"regions":["nyc1","ams1","sfo1","nyc2","ams2","sgp1","lon1","nyc3","ams3","fra1","tor1"],"created_at":"2015-08-10T21:30:19Z","min_disk_size":20,"type":"snapshot"},"size":{"slug":"512mb","memory":512,"vcpus":1,"disk":20,"transfer":1.0,"price_monthly":5.0,"price_hourly":0.00744,"regions":["nyc1","sgp1","sfo1","nyc2","lon1","nyc3","ams3","ams2","fra1","tor1"],"available":true},"size_slug":"512mb","networks":{"v4":[],"v6":[]},"region":{"name":"New York 3","slug":"nyc3","sizes":["512mb","1gb","2gb","4gb","8gb","16gb","32gb","48gb","64gb"],"features":["private_networking","backups","ipv6","metadata"],"available":true}},"links":{"actions":[{"id":71198937,"rel":"create","href":"https://api.digitalocean.com/v2/actions/71198937"}]}

 

 

 

 

16) After 60 seconds you should see something like this in the console

Inline image 1

You can test the login with 

ssh -i ~/.ssh/do root at 159.203.140.36

 

after a few seconds you should see the root login prompt

 

 

 

DONE

 

 

 

More information on the digitial ocean API 

https://developers.digitalocean.com/documentation/v2/

 

More information on python requests API and how to do more complicated stuff in that library

http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

 

 

More information on how to use ConfigParser: can be found at: 

 

https://wiki.python.org/moin/ConfigParserExamples

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.noisebridge.net/pipermail/python/attachments/20151112/c9ea8508/attachment-0003.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 6385 bytes
Desc: not available
URL: <http://lists.noisebridge.net/pipermail/python/attachments/20151112/c9ea8508/attachment-0003.png>


More information about the Python mailing list