Python library for managing your Digital Ocean account via API v2
The DigitalOcean API allows you to manage Droplets and resources within the DigitalOcean cloud in a simple, programmatic way using conventional HTTP requests. The endpoints are intuitive and powerful, allowing you to easily make calls to retrieve information or to execute actions.
All of the functionality that you are familiar with in the DigitalOcean control panel is also available through the API, allowing you to script the complex actions that your situation requires.
This library starts with a python wrapper for the API and aims to build tools to make it easier to manage, provision, and deploy to Digital Ocean.
Full featured : API wrapper covering the published DigitalOcean API v2
Tested : integration test coverage against most of the API
SSH integration: integrates paramiko
library so you can SSH in and issue commands
Deployment conveniences: methods like apt
, pip
, and git
for easier deployment
pip install -U poseidon
To run the unit tests make sure you have the pytest
module.
If not, run pip install -U pytest
Setup authentication by generating an API key and exporting it as the value of the
DIGITALOCEAN_API_KEY
environment variable.
import poseidon.api as po
client = po.connect() # or po.connect(api_key=<KEY>) for custom api key
image_id = 135123 # replace with your own
key_id = 175235 # e.g., client.keys.list()[0]['id']
droplet = client.droplets.create(name='test', region='sfo1', size='512mb',
image=image_id, ssh_keys=[key_id])
droplet.power_off() # snapshots are only allowed while powered off
droplet.take_snapshot('test-snapshot')
snapshots = droplet.snapshots() # one of these should be named 'test-snapshot'
poseidon connects to your droplet via SSH using the paramiko
library
ssh = droplet.connect()
ssh.apt('git python-pip')
# requires GITHUB_TOKEN envvar
ssh.git(username='changhiskhan', repo='hello_world')
ssh.chdir('hello_world')
ssh.pip_r('requirements.txt')
ssh.nohup('python app.py')
print ssh.ps()
droplet.reboot()
droplet.shutdown()
droplet.power_on()
droplet.power_cycle()
droplet.password_reset()
droplet.enable_ipv6()
droplet.disable_backups()
droplet.enable_private_networking()
droplet.resize('1024mb')
droplet.restore(image_id) # integer
droplet.rebuild(image_id)
droplet.rename('new-name')
droplet.change_kernel(12534)
droplet.delete()
# list keys
client.keys.list() # it works
# create a new key
public_key = ("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDWF7SdoK0JvdjGR/8MHjj"
"b7qtKVSdqoVZ2bCX0SXdn2pxZitnFjUx+lQ4osMGjOOTE/Hi86qQnFGE8Ym"
"Sur/LT example")
key = client.keys.create('test-key', public_key)
print rs['public_key']
print rs['name']
# rename the key
client.keys.update(key['id'], 'test-key2')
# delete the key
client.keys.delete(new_id)
# list domains
client.domains.list() # it works
# create new domain
ip_address = '127.0.0.1'
test = 'b7qtKVSdqoVZ2bCX0SXdn2pxZitnFjUx.com' # must be unique
domain = client.domains.create(test, ip_address)
print domain['name']
# retrieve a domain by name
new_domain = client.domains.get(domain['name'])
# delete a domain by name
client.domains.delete(new_domain['name'])
client.regions.list()
client.sizes.list()
pip install -U pytest
py.test
Because the test for droplets goes through the exercise of creating a new droplet,
modifying it, then finally destroying it, the test takes a long time to run.
To only run the other tests, use the not slow
marker from pytest
:
py.test -v -m "not slow"