pontoon¶
A Python CLI (and library) for Digital Ocean.
Introduction¶
Pontoon makes interacting with Digital Ocean on the command line smooth sailing.
It is designed for human consumption, and aims to have good defaults.
Caveats¶
Pontoon has one rule it imposes on top of Digital Ocean:
Names are unique.
Unique names make for a much easier command line experience. For Droplets, names are hostnames, and hostnames should be unique anyway; it’s just a good idea.
They don’t have to be unique forever though; once a Droplet is destroyed, it’s fine to use the name again.
Anything “recommended” (like secure-erasing the drive on termination) is optional, but enabled by default.
User Guide¶
Installation¶
Via pip:
$ pip install pontoon
Pontoon works with Pythons 2.6 through 3.5, and should work on any Linux/BSD with Python support.
It’s also possible to use pontoon on Windows, though portions of the configuration command (which make use of Linux CLI tools like OpenSSH) do not work.
CLI Usage¶
Configure¶
Set up your credentials and preferences:
$ pontoon configure
You’ll be prompted for your Digital Ocean API credentials (available here), and whether you want to use existing SSH credentials or for them to be generated (using OpenSSH).
The rest are preferences, and can be set at any time by running
configure again, editing the ~/.pontoon
config file (YAML format),
or by specifying them with options on the command line.
Configuration File¶
Here’s an example of the options set in the configuration file:
api_token: foo-bar-baz
auth_key_name: Macbook.local
image: ubuntu-15-10-x32
region: lon1
size: 512mb
ssh_private_key: ~/.ssh/id_rsa
ssh_public_key: ~/.ssh/id_rsa.pub
username: root
Managing Droplets is probably what you’ll spend most of your time doing with pontoon.
You can get a full list of subcommands and options by running:
$ pontoon droplet --help
Basic Usage¶
Creating and destroying Droplets is very straight forward:
$ pontoon droplet create my-droplet
Creating Droplet my-droplet (512mb using ubuntu-15-10-x32 in lon1)...
...............active
$ pontoon droplet destroy my-droplet
Destroying ud1 and scrubbing data...
To SSH into your Droplet:
$ pontoon droplet ssh my-droplet
Welcome to Ubuntu 15.10 (GNU/Linux 4.2.0-27-generic i686)
* Documentation: https://help.ubuntu.com/
Last login: Sun Mar 6 10:41:17 2016 from 192.168.1.200
To get a list of your Droplets:
$ pontoon droplet list
my-droplet: (512mb, ubuntu-15-10-x32, lon1, 104.236.32.182, active)
See the Command Reference for more.
Command Reference¶
A complete list of commands and their options.
Available on the command line by appending --help
to the command.
Configure¶
The configure
command interactively configures pontoon.
You’re prompted for details and a configuration file is written to ~/.pontoon
.
Alternatively, you can manually place a config file at this location with this syntax:
api_token: foo-bar-baz
auth_key_name: Macbook.local
image: ubuntu-15-10-x32
region: lon1
size: 512mb
ssh_private_key: ~/.ssh/id_rsa
ssh_public_key: ~/.ssh/id_rsa.pub
username: root
-
pontoon
configure
¶ Launch interactive configuration of pontoon.
Droplets¶
-
pontoon
droplet list [options]
¶
-
--detail
¶
Show full Droplet info.
$ pontoon droplet list --detail
example.com
id: 3164444
size: 512mb
image: ubuntu-14-04-x64
region: nyc3
ip_address: 104.236.32.182
status: active
kernel:
id: 2233
name: Ubuntu 14.04 x64 vmlinuz-3.13.0-37-generic
version: 3.13.0-37-generic
...
-
pontoon
droplet create <name> [options]
¶
-
--size
size
¶ Droplet RAM allocation. e.g., 512mb
-
--image
image
¶ Droplet image.
-
--region
region
¶ Droplet region.
-
--keys
keys...
¶ List of registered keys to add to Droplet(s)
-
--user-data
userdata
¶ String of user data to pass to Droplet. Include a file like:
--user-data="$(cat file.yml)"
-
--private-networking
¶
Assign private address to Droplet (where available)
-
--disable-virtio
¶
Disable VirtIO. (not recommended)
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet ssh <name> [command] [options]
¶
-
--user
user
¶ Override configured username for SSH login.
-
--key
path
¶ Override configured private key for SSH login.
-
pontoon
droplet rename <from> <to> [options]
¶ Rename a Droplet. Takes the current name as the first parameter, and the new name as the second.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet resize <name> <size> [options]
¶ Resize a Droplet. Takes Droplet name as first paramter, size as second.
-
--yes
¶
Don’t prompt for confirmation.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet snapshot <droplet> <snapshot> [options]
¶ Snapshot a Droplet. Takes Droplet name as first paramter, snapshot name as second.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet show <name> [options]
¶ Show detailed information about a particular Droplet.
-
--field
field
¶ Extract and return a single field. Access nested items with dot syntax, e.g.:
networks.v4.0.gateway
-
pontoon
droplet status <name>
¶ Return Droplet status.
-
pontoon
droplet destroy <name>
¶ Destroy a Droplet.
-
pontoon
droplet start <name>
¶ Start a Droplet.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet shutdown <name>
¶ Shut down a Droplet.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet reboot <name>
¶ Reboot a Droplet (sending signal to OS).
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet restore <name> <snapshot>
¶ Restore a Droplet from a snapshot.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet rebuild <name> <image>
¶ Rebuild a Droplet from a given image.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet powercycle <name>
¶ Powercycle (hard restart) a Droplet.
-
--yes
¶
Don’t prompt for confirmation.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet poweroff <name>
¶ Power off (without signalling the OS) a Droplet.
-
--yes
¶
Don’t prompt for confirmation.
-
--no-wait
¶
Don’t wait for action to complete, return immediately.
-
pontoon
droplet passwordreset <name>
¶ Reset the root password on a Droplet.
-
--yes
¶
Don’t prompt for confirmation.
-
pontoon
droplet backups <name>
¶ Manage backups on a Droplet.
-
--enable
¶
Enable backups.
-
--disable
¶
Depracated by Digital Ocean for their v2 API release, later added back but still deprecated here for the moment.
Events¶
These is an interface to Digital Ocean events.
Events are usually only an implementation detail, and an interface is provided here only for completeness.
-
pontoon
event show <id>
¶ Retrieve details for a particular event id.
Images¶
Public base images made available by Digital Ocean.
-
pontoon
image list [options]
¶ Retrieve a list of public images.
-
--with-ids
¶
Include image IDs in tabular output.
-
pontoon
image oses
¶ Retrieve a list of Operating Systems for which there are base images.
-
pontoon
image show <name>
¶ Show details for a particular image, including regions where it is available.
Regions¶
Regions available to launch Droplets.
-
pontoon
region list
¶ List regions in which Droplets can be launched.
Snapshots¶
Commands for interacting with snapshots.
-
pontoon
snapshot list [options]
¶ List available snapshots.
-
--with-ids
¶
Include image IDs in tabular output.
-
pontoon
snapshot show <name>
¶ Show snapshot details.
-
pontoon
snapshot destroy <name>
¶ Destroy a snapshot.
-
pontoon
snapshot transfer <name> <region>
¶ Move a snapshot from one region to another. A list of regions can be retrieved with
pontoon region list
SSH Keys¶
Manage SSH keys in your account.
-
pontoon
sshkey list
¶ List of SSH keys in account.
-
pontoon
sshkey add <name> <public-key-path>
¶ Register a public SSH key from the specified path to your account.
-
pontoon
sshkey show <name>
¶ Retrieve a public key by name.
-
pontoon
sshkey replace <name> <public-key-path>
¶ Replace an existing key name with a new public key.
-
pontoon
sshkey destroy <name>
¶ Remove a given key from Digital Ocean. Note: this doesn’t remove the key from any existing Droplets, just removes it from the keys available to boot Droplets with.
Developer / Contributor Guide¶
Developer Interface¶
This part of the documentation covers all the interfaces of Pontoon.
Library Module¶
pontoon.lib is the main entry point for using the library as a developer.
digitalocean API to manage droplets
Manager Class¶
-
class
pontoon.lib.
Manager
(*args, **kwargs)[source]¶ -
_Manager__deal_with_pagination
(url, data, params)¶ Perform multiple calls in order to have a full list of elements when the API are “paginated”. (content list is divided in more than one page)
-
get_all_images
()[source]¶ This function returns a list of Image objects containing all available DigitalOcean images, both public and private.
-
get_app_images
()[source]¶ This function returns a list of Image objectobjects representing public DigitalOcean ‘One-Click’ application images.
-
get_data
(*args, **kwargs)[source]¶ Customized version of get_data to perform __check_actions_in_data.
The default amount of elements per page defined is 200 as explained here: https://github.com/koalalorenzo/python-digitalocean/pull/78
-
get_distro_images
()[source]¶ This function returns a list of Image objects representing public base distribution images.
-
get_global_images
()[source]¶ This function returns a list of Image objects representing public DigitalOcean images (e.g. base distribution images and ‘One-Click’ applications).
-
Droplet Class¶
-
class
pontoon.lib.
Droplet
(*args, **kwargs)[source]¶ “Droplet management
- Attributes accepted at creation time:
- name: str - name size_slug: str - droplet size image: str - image name to use to create droplet region: str - region ssh_keys: [str] - list of ssh keys backups: bool - True if backups enabled ipv6: bool - True if ipv6 enabled private_networking: bool - True if private networking enabled user_data: str - arbitrary data to pass to droplet
- Attributes returned by API:
id: int - droplet id memory: str - memory size vcpus: int - number of vcpus disk: int - disk size in GB status: str - status locked: bool - True if locked created_at: str - creation date in format u‘2014-11-06T10:42:09Z’ status: str - status, e.g. ‘new’, ‘active’, etc networks: dict - details of connected networks kernel: dict - details of kernel backup_ids: [int] - list of ids of backups of this droplet snapshot_ids: [int] - list of ids of snapshots of this droplet action_ids: [int] - list of ids of actions features: [str] - list of enabled features. e.g.
[u’private_networking’, u’virtio’]- min_size: str - minumum size of droplet that can bew created from a
- snapshot of this droplet
image: dict - details of image used to create this droplet ip_address: str - public ip addresses private_ip_address: str - private ip address ip_v6_address: [str] - list of ipv6 addresses assigned end_point: str - url of api endpoint used
-
_Droplet__get_ssh_keys_id_or_fingerprint
()¶ Check and return a list of SSH key IDs or fingerprints according to DigitalOcean’s API. This method is used to check and create a droplet with the correct SSH keys.
-
_perform_action
(params, return_dict=True)[source]¶ Perform a droplet action.
- Args:
- params - dict : parameters of the action
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
change_kernel
(kernel, return_dict=True)[source]¶ Change the kernel to a new one
- Args:
- kernel : instance of digitalocean.Kernel.Kernel
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
create
(*args, **kwargs)[source]¶ Create the droplet with object properties.
Note: Every argument and parameter given to this method will be assigned to the object.
-
disable_backups
(return_dict=True)[source]¶ Disable automatic backups
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
enable_ipv6
(return_dict=True)[source]¶ Enable IPv6 on an existing Droplet where available.
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
enable_private_networking
(return_dict=True)[source]¶ Enable private networking on an existing Droplet where available.
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
get_action
(action_id)[source]¶ Returns a specific Action by its ID.
- Args:
- action_id: int - id of action
-
get_actions
()[source]¶ Returns a list of Action objects This actions can be used to check the droplet’s status
-
get_data
(*args, **kwargs)[source]¶ Customized version of get_data to perform __check_actions_in_data
-
classmethod
get_object
(api_token, droplet_id, mocked)[source]¶ Class method that will return a Droplet object by ID.
- Args:
- api_token: str - token droplet_id: int - droplet id mocked: bool - mocked
-
get_snapshots
()[source]¶ This method will return the snapshots/images connected to that specific droplet.
-
power_cycle
(return_dict=True)[source]¶ restart the droplet
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
power_off
(return_dict=True)[source]¶ restart the droplet
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
power_on
(return_dict=True)[source]¶ Boot up the droplet
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
reboot
(return_dict=True)[source]¶ restart the droplet
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
rebuild
(image_id=None, return_dict=True)[source]¶ Restore the droplet to an image ( snapshot or backup )
- Args:
- image_id : int - id of image
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
rename
(name, return_dict=True)[source]¶ Rename the droplet
- Args:
- name : str - new name
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
reset_root_password
(return_dict=True)[source]¶ reset the root password
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
resize
(new_size_slug, return_dict=True, disk=True)[source]¶ Resize the droplet to a new size slug. https://developers.digitalocean.com/documentation/v2/#resize-a-droplet
- Args:
- new_size_slug: str - name of new size
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
disk - bool : If a permanent resize, with disk changes included.
Returns dict or Action
-
restore
(image_id, return_dict=True)[source]¶ Restore the droplet to an image ( snapshot or backup )
- Args:
- image_id : int - id of image
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
shutdown
(return_dict=True)[source]¶ shutdown the droplet
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
Returns dict or Action
-
take_snapshot
(snapshot_name, return_dict=True, power_off=False)[source]¶ Take a snapshot!
- Args:
- snapshot_name: str - name of snapshot
- Optional Args:
- return_dict - bool : Return a dict when True (default),
- otherwise return an Action.
- power_off - bool : Before taking the snapshot the droplet will be
- turned off with another API call. It will wait until the droplet will be powered off.
Returns dict or Action
Account Class¶
Action Class¶
Domain Class¶
-
class
pontoon.lib.
Domain
(*args, **kwargs)[source]¶ -
-
create_new_domain_record
(*args, **kwargs)[source]¶ Create new domain record. https://developers.digitalocean.com/#create-a-new-domain-record
Args:
@type The record type (A, MX, CNAME, etc). @name The host name, alias, or service being defined by the record @data Variable data depending on record type.
Optional Args:
@priority The priority of the host @port The port that the service is accessible on @weight The weight of records with the same priority
-
FloatingIP Class¶
-
class
pontoon.lib.
FloatingIP
(*args, **kwargs)[source]¶ -
-
create
(*args, **kwargs)[source]¶ Creates a FloatingIP and assigns it to a Droplet.
Note: Every argument and parameter given to this method will be assigned to the object.
- Args:
- droplet_id: int - droplet id
-
classmethod
get_object
(api_token, ip, mocked)[source]¶ Class method that will return a FloatingIP object by its IP.
- Args:
- api_token: str - token ip: str - floating ip address
-
Image Class¶
Record Class¶
SSHKey Class¶
UI module¶
This module handles all output for the CLI, as well as some filesystem interactions. This is the only location outside of tests where the print statement is invoked directly.
-
pontoon.ui.
ask_yesno
(question)[source]¶ Present a string as a yes/no question on an interactive prompt
-
pontoon.ui.
box
(text, decor='*', decor_x=None, decor_y=None, boxwidth=60, borderwidth=2)[source]¶ Create a formatted textbox for highlighting important information
-
pontoon.ui.
format_droplet_info
(machine)[source]¶ Present Droplet information in a more human parseable format
-
pontoon.ui.
mask
(text, masker='*')[source]¶ Hide part of a string,
If the input is small, hide the entire string. Otherwise, show only the last few characters.
-
pontoon.ui.
ticker
()[source]¶ A loading/waiting indicator.
Sends a ‘.’ to the screen, resets, and sleeps.
Mocking Module¶
-
pontoon.mocking.
_raise
(ex=None)[source]¶ Wrapper for exceptions so they can be thrown from inside lambdas
Exceptions¶
Configure Module¶
The configure module is a collection of methods for handling interaction with some external tools, config files, and for interactive configuration.
-
pontoon.configure.
combined
(*args, **kwargs)[source]¶ Merge configuration defaults with values from config file.
-
pontoon.configure.
create_config
(*args, **kwargs)[source]¶ Create a YAML config file from a dictionary
-
pontoon.configure.
read_config
(*args, **kwargs)[source]¶ Read a YAML formatted config into a dictionary
Command Class¶
The Command class encapsulates plumbing common to all CLI commands.
Contributing¶
Pull requests for bugs are always welcome! New functionality should generally be preceded by a discussion, though if you’ve written something that you needed and want to contribute back, a pull request is a fine way to start that discussion :)
All of the code in pontoon is PEP-8 audited (using pytest-pep8), and there’s a full suite of tests written for py.test (library code) and Bats (interface). Contributions should, therefore, include tests and pass a PEP-8 audit.
Running the tests¶
Running the tests locally requires the contents of requirements.txt
as well as bats.
$ pip install -r requirements.txt
On OSX, bats can be installed with homebrew:
$ brew install bats
On Debian/Ubuntu, I’ve set up a PPA for easy installation of bats:
$ add-apt-repository ppa:duggan/bats
$ apt-get update
$ apt-get install bats
Tests can then be run from the root directory:
$ py.test --pep8 --cov pontoon
$ bats test/bats
Debugging¶
Set the DEBUG
environment variable (to anything) to enable debug
output for pontoon.
This will give a step through of most methods being executed during a command, like so:
$ DEBUG=1 pontoon droplet destroy foobar
2013-11-09 18:37:06,187 [pontoon.configure:DEBUG] combined: (){}
2013-11-09 18:37:06,187 [pontoon.configure:DEBUG] read_config: (){}
Destroying foobar and scrubbing data...
2013-11-09 18:37:06,204 [pontoon.droplet:DEBUG] destroy: (<pontoon.droplet.Droplet instance at 0x10ce1fd40>, 'foobar', False){}
2013-11-09 18:37:06,204 [pontoon.droplet:DEBUG] id_from_name: (<pontoon.droplet.Droplet instance at 0x10ce1fd40>, 'foobar'){}
2013-11-09 18:37:06,204 [pontoon.droplet:DEBUG] list: (<pontoon.droplet.Droplet instance at 0x10ce1fd40>,){}
2013-11-09 18:37:06,205 [pontoon.pontoon:DEBUG] render: (<pontoon.pontoon.Pontoon instance at 0x10ce1fcf8>, 'droplets', '/droplets'){}
2013-11-09 18:37:06,205 [pontoon.pontoon:DEBUG] request: (<pontoon.pontoon.Pontoon instance at 0x10ce1fcf8>, '/droplets'){'params': {}, 'method': 'GET'}
2013-11-09 18:37:07,498 [pontoon.pontoon:DEBUG] render: (<pontoon.pontoon.Pontoon instance at 0x10ce1fcf8>, 'event_id', '/droplets/998/destroy'){'params': {'scrub_data': 1}}
2013-11-09 18:37:07,498 [pontoon.pontoon:DEBUG] request: (<pontoon.pontoon.Pontoon instance at 0x10ce1fcf8>, '/droplets/998/destroy'){'params': {'scrub_data': 1}, 'method': 'GET'}
A timestamp, followed by the module, debug level, the method called and the arguments to that method (positional as brackets, keywords as curlies).
This functionality is implemented by the @debug
decorator, the code
for which can be seen at pontoon/log.py
.
Mocking¶
Set the MOCK
environment variable (to anything) to return mock
request data instead of querying Digital Ocean.
This is implemented solely for end-to-end testing of the CLI, but you may find it useful in some other scenarios.