Code Monkey home page Code Monkey logo

python-digitalocean's Introduction

python-digitalocean

Easy access to Digital Ocean APIs to deploy droplets, images and more.

Build Status

Table of Contents

How to install

You can install python-digitalocean using pip

pip install -U python-digitalocean

or via sources:

python setup.py install

⬆ back to top

Features

python-digitalocean support all the features provided via digitalocean.com APIs, such as:

  • Get user's Droplets
  • Get user's Images (Snapshot and Backups)
  • Get public Images
  • Get Droplet's event status
  • Create and Remove a Droplet
  • Create, Add and Remove Tags from Droplets
  • Resize a Droplet
  • Shutdown, restart and boot a Droplet
  • Power off, power on and "power cycle" a Droplet
  • Perform Snapshot
  • Enable/Disable automatic Backups
  • Restore root password of a Droplet

⬆ back to top

Examples

Listing the droplets

This example shows how to list all the active droplets:

import digitalocean
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
my_droplets = manager.get_all_droplets()
print(my_droplets)

⬆ back to top

Listing the droplets by tags

This example shows how to list all the active droplets:

import digitalocean
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
my_droplets = manager.get_all_droplets(tag_name="awesome")
print(my_droplets)

⬆ back to top

Add a tag to a droplet

This example shows how to add a tag to a droplet:

import digitalocean
tag = digitalocean.Tag(token="secretspecialuniquesnowflake", name="tag_name")
tag.create() # create tag if not already created
tag.add_droplets(["DROPLET_ID"])

⬆ back to top

Shutdown all droplets

This example shows how to shutdown all the active droplets:

import digitalocean
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
my_droplets = manager.get_all_droplets()
for droplet in my_droplets:
    droplet.shutdown()

⬆ back to top

Creating a Droplet and checking its status

This example shows how to create a droplet and how to check its status

import digitalocean
droplet = digitalocean.Droplet(token="secretspecialuniquesnowflake",
                               name='Example',
                               region='nyc2', # New York 2
                               image='ubuntu-14-04-x64', # Ubuntu 14.04 x64
                               size_slug='512mb',  # 512MB
                               backups=True)
droplet.create()

⬆ back to top

Checking the status of the droplet

actions = droplet.get_actions()
for action in actions:
    action.load()
    # Once it shows complete, droplet is up and running
    print action.status

⬆ back to top

Add SSHKey into DigitalOcean Account

from digitalocean import SSHKey

user_ssh_key = open('/home/<$USER>/.ssh/id_rsa.pub').read()
key = SSHKey(token='secretspecialuniquesnowflake',
             name='uniquehostname',
             public_key=user_ssh_key)
key.create()

⬆ back to top

Creating a new droplet with all your SSH keys

manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
keys = manager.get_all_sshkeys()

droplet = digitalocean.Droplet(token="secretspecialuniquesnowflake",
                               name='DropletWithSSHKeys',
                               region='ams3', # Amster
                               image='ubuntu-14-04-x64', # Ubuntu 14.04 x64
                               size_slug='512mb',  # 512MB
                               ssh_keys=keys, #Automatic conversion
                               backups=False)
droplet.create()

⬆ back to top

Creating a Firewall

This example creates a firewall that only accepts inbound tcp traffic on port 80 from a specific load balancer and allows outbout tcp traffic on all ports to all addresses.

from digitalocean import Firewall, InboundRule, OutboundRule, Destinations, Sources

inbound_rule = InboundRule(protocol="tcp", ports="80",
                           sources=Sources(
                               load_balancer_uids=[
                                   "4de7ac8b-495b-4884-9a69-1050c6793cd6"]
                               )
                           )

outbound_rule = OutboundRule(protocol="tcp", ports="all",
                             destinations=Destinations(
                               addresses=[
                                   "0.0.0.0/0",
                                   "::/0"]
                                 )
                             )

firewall = Firewall(token="secretspecialuniquesnowflake",
                    name="new-firewall",
                    inbound_rules=[inbound_rule],
                    outbound_rules=[outbound_rule],
                    droplet_ids=[8043964, 8043972])
firewall.create()

⬆ back to top

Getting account requests/hour limits status

Each request will also include the rate limit information:

import digitalocean
account = digitalocean.Account(token="secretspecialuniquesnowflake").load()
# or
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
account = manager.get_account()

Output:

droplet_limit: 25
email: '[email protected]'
email_verified: True
end_point: 'https://api.digitalocean.com/v2/'
floating_ip_limit: 3
ratelimit_limit: '5000'
ratelimit_remaining: '4995'
ratelimit_reset: '1505378973'
status: 'active'
status_message: ''
token:'my_secret_token'
uuid: 'my_id'

When using the Manager().get_all.. functions, the rate limit will be stored on the manager object:

import digitalocean
manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
domains = manager.get_all_domains()

print(manager.ratelimit_limit)

⬆ back to top

Testing

Test using Docker

To test this python-digitalocean you can use docker to have a clean environment automatically. First you have to build the container by running in your shell on the repository directory:

docker build -t "pdo-tests" .

Then you can run all the tests (for both python 2 and python 3)

docker run pdo-tests

Note: This will use Ubuntu 14.04 as base and use your repository to run tests. So every time you edit some files, please run these commands to perform tests on your changes.

⬆ back to top

Testing using pytest manually

Use pytest to perform testing. It is recommended to use a dedicated virtualenv to perform tests, using these commands:

$ virtualenv /tmp/digitalocean_env
$ source /tmp/digitalocean_env/bin/activate
$ pip install -r requirements.txt

To run all the tests manually use py.test command:

$ python -m pytest

⬆ back to top

Links

⬆ back to top

python-digitalocean's People

Contributors

andrewsomething avatar antuanha avatar atuchak avatar avagin avatar caseyhen avatar clumsical avatar dmaust avatar dmyerscough avatar edwardbetts avatar francescortiz avatar jwilk avatar koalalorenzo avatar mbugeia avatar mfallone avatar micvbang avatar moeseth avatar moyamo avatar peterlandry avatar rdgreis avatar rgbkrk avatar samdobson avatar schwartzie avatar sirioz avatar sylvainde avatar szabolcsdombi avatar thatrubenaguilar avatar the94air avatar tomscytale avatar viktorkrivak avatar wolph avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.