• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

digitalocean/H19-Nov-2020-3,6892,969

python_digitalocean.egg-info/H03-May-2022-201143

PKG-INFOH A D19-Nov-20206.9 KiB201143

README.mdH A D19-Nov-202014.1 KiB454338

setup.cfgH A D19-Nov-202038 53

setup.pyH A D19-Nov-2020903 2924

README.md

1<h1 align="center">python-digitalocean</h1>
2<p align="center">Easy access to Digital Ocean APIs to deploy droplets, images and more.</p>
3
4<p align="center">
5<a href="https://travis-ci.org/koalalorenzo/python-digitalocean"><img src="https://travis-ci.org/koalalorenzo/python-digitalocean.svg" alt="Build Status"></a>
6<a href="https://github.com/koalalorenzo/python-digitalocean"><img src="https://img.shields.io/github/forks/koalalorenzo/python-digitalocean.svg?style=social&label=Fork"></a>
7<a href="https://github.com/koalalorenzo/python-digitalocean"><img src="https://img.shields.io/github/stars/koalalorenzo/python-digitalocean.svg?style=social&label=Star"></a>
8<a href="https://github.com/koalalorenzo/python-digitalocean"><img src="https://img.shields.io/github/watchers/koalalorenzo/python-digitalocean.svg?style=social&label=Watch"></a>
9</p>
10
11## Table of Contents
12
13- [How to install](#how-to-install)
14- [Configurations](#configurations)
15- [Features](#features)
16- [Examples](#examples)
17   - [Listing the droplets](#listing-the-droplets)
18   - [Listing the droplets by tags](#listing-the-droplets-by-tags)
19   - [Add a tag to a droplet](#add-a-tag-to-a-droplet)
20   - [Shutdown all droplets](#shutdown-all-droplets)
21   - [Creating a Droplet and checking its status](#creating-a-droplet-and-checking-its-status)
22   - [Checking the status of the droplet](#checking-the-status-of-the-droplet)
23   - [Listing the Projects](#listing-the-projects)
24   - [Assign a resource for specific project](#assigne-a-resource-for-specific-project)
25   - [List all the resources of a project](#list-all-the-resources-of-a-project)
26   - [Add SSHKey into DigitalOcean Account](#add-sshkey-into-digitalocean-account)
27   - [Creating a new droplet with all your SSH keys](#creating-a-new-droplet-with-all-your-ssh-keys)
28   - [Creating a Firewall](#creating-a-firewall)
29   - [Listing the domains](#listing-the-domains)
30   - [Listing records of a domain](#listing-records-of-a-domain)
31   - [Creating a domain record](#creating-a-domain-record)
32   - [Update a domain record](#update-a-domain-record)
33- [Getting account requests/hour limits status](#getting-account-requestshour-limits-status)
34- [Session customization](#session-customization)
35- [Testing](#testing)
36   - [Test using Docker](#test-using-docker)
37   - [Testing using pytest manually](#testing-using-pytest-manually)
38- [Links](#links)
39
40## How to install
41
42You can install python-digitalocean using **pip**
43
44    pip install -U python-digitalocean
45
46or via sources:
47
48    python setup.py install
49
50**[⬆ back to top](#table-of-contents)**
51
52## Configurations
53
54Specify a custom provider using environment variable
55
56    export DIGITALOCEAN_END_POINT=http://example.com/
57
58Specify the DIGITALOCEAN_ACCESS_TOKEN using environment variable
59
60    export DIGITALOCEAN_ACCESS_TOKEN='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
61
62Note: Probably want to add the export line above to your `.bashrc` file.
63
64**[⬆ back to top](#table-of-contents)**
65
66## Features
67python-digitalocean support all the features provided via digitalocean.com APIs, such as:
68
69* Get user's Projects
70* Assign a resource to a user project
71* List the resources of user's project
72* Get user's Droplets
73* Get user's Images (Snapshot and Backups)
74* Get public Images
75* Get Droplet's event status
76* Create and Remove a Droplet
77* Create, Add and Remove Tags from Droplets
78* Resize a Droplet
79* Shutdown, restart and boot a Droplet
80* Power off, power on and "power cycle" a Droplet
81* Perform Snapshot
82* Enable/Disable automatic Backups
83* Restore root password of a Droplet
84
85**[⬆ back to top](#table-of-contents)**
86
87## Examples
88### Listing the droplets
89
90This example shows how to list all the active droplets:
91
92```python
93import digitalocean
94manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
95my_droplets = manager.get_all_droplets()
96print(my_droplets)
97```
98
99This example shows how to specify custom provider's end point URL:
100
101```python
102import digitalocean
103manager = digitalocean.Manager(token="secretspecialuniquesnowflake", end_point="http://example.com/")
104```
105
106**[⬆ back to top](#table-of-contents)**
107
108### Listing the droplets by tags
109
110This example shows how to list all the active droplets:
111
112```python
113import digitalocean
114manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
115my_droplets = manager.get_all_droplets(tag_name="awesome")
116print(my_droplets)
117```
118
119**[⬆ back to top](#table-of-contents)**
120
121### Add a tag to a droplet
122
123This example shows how to add a tag to a droplet:
124
125```python
126import digitalocean
127tag = digitalocean.Tag(token="secretspecialuniquesnowflake", name="tag_name")
128tag.create() # create tag if not already created
129tag.add_droplets(["DROPLET_ID"])
130```
131
132**[⬆ back to top](#table-of-contents)**
133
134### Shutdown all droplets
135
136This example shows how to shutdown all the active droplets:
137
138```python
139import digitalocean
140manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
141my_droplets = manager.get_all_droplets()
142for droplet in my_droplets:
143    droplet.shutdown()
144```
145
146**[⬆ back to top](#table-of-contents)**
147
148### Creating a Droplet and checking its status
149
150This example shows how to create a droplet and how to check its status
151
152```python
153import digitalocean
154droplet = digitalocean.Droplet(token="secretspecialuniquesnowflake",
155                               name='Example',
156                               region='nyc2', # New York 2
157                               image='ubuntu-20-04-x64', # Ubuntu 20.04 x64
158                               size_slug='s-1vcpu-1gb',  # 1GB RAM, 1 vCPU
159                               backups=True)
160droplet.create()
161```
162
163**[⬆ back to top](#table-of-contents)**
164
165### Checking the status of the droplet
166```python
167actions = droplet.get_actions()
168for action in actions:
169    action.load()
170    # Once it shows "completed", droplet is up and running
171    print(action.status)
172```
173
174**[⬆ back to top](#table-of-contents)**
175
176### Listing the Projects
177
178This example shows how to list all the projects:
179
180```python
181import digitalocean
182manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
183my_projects = manager.get_all_projects()
184print(my_projects)
185```
186
187**[⬆ back to top](#table-of-contents)**
188
189### Assign a resource for specific project
190
191```python
192import digitalocean
193manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
194my_projects = manager.get_all_projects()
195my_projects[0].assign_resource(["do:droplet:<Droplet Number>"])
196```
197
198**[⬆ back to top](#table-of-contents)**
199
200### List all the resources of a project
201```python
202import digitalocean
203manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
204my_projects = manager.get_all_projects()
205resources = my_projects[0].get_all_resources()
206print(resources)
207```
208
209**[⬆ back to top](#table-of-contents)**
210
211### Add SSHKey into DigitalOcean Account
212```python
213from digitalocean import SSHKey
214
215user_ssh_key = open('/home/<$USER>/.ssh/id_rsa.pub').read()
216key = SSHKey(token='secretspecialuniquesnowflake',
217             name='uniquehostname',
218             public_key=user_ssh_key)
219key.create()
220```
221
222**[⬆ back to top](#table-of-contents)**
223
224### Creating a new droplet with all your SSH keys
225```python
226manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
227keys = manager.get_all_sshkeys()
228
229droplet = digitalocean.Droplet(token=manager.token,
230                               name='DropletWithSSHKeys',
231                               region='ams3', # Amster
232                               image='ubuntu-20-04-x64', # Ubuntu 20.04 x64
233                               size_slug='s-1vcpu-1gb',  # 1GB RAM, 1 vCPU
234                               ssh_keys=keys, #Automatic conversion
235                               backups=False)
236droplet.create()
237```
238
239**[⬆ back to top](#table-of-contents)**
240
241### Creating a Firewall
242
243This 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.
244
245```python
246from digitalocean import Firewall, InboundRule, OutboundRule, Destinations, Sources
247
248inbound_rule = InboundRule(protocol="tcp", ports="80",
249                           sources=Sources(
250                               load_balancer_uids=[
251                                   "4de7ac8b-495b-4884-9a69-1050c6793cd6"]
252                               )
253                           )
254
255outbound_rule = OutboundRule(protocol="tcp", ports="all",
256                             destinations=Destinations(
257                               addresses=[
258                                   "0.0.0.0/0",
259                                   "::/0"]
260                                 )
261                             )
262
263firewall = Firewall(token="secretspecialuniquesnowflake",
264                    name="new-firewall",
265                    inbound_rules=[inbound_rule],
266                    outbound_rules=[outbound_rule],
267                    droplet_ids=[8043964, 8043972])
268firewall.create()
269```
270
271**[⬆ back to top](#table-of-contents)**
272
273### Listing the domains
274
275This example shows how to list all the active domains:
276
277```python
278import digitalocean
279TOKEN="secretspecialuniquesnowflake"
280manager = digitalocean.Manager(token=TOKEN)
281my_domains = manager.get_all_domains()
282print(my_domains)
283```
284
285**[⬆ back to top](#table-of-contents)**
286
287### Listing records of a domain
288
289This example shows how to list all records of a domain:
290
291```python
292import digitalocean
293TOKEN="secretspecialuniquesnowflake"
294domain = digitalocean.Domain(token=TOKEN, name="example.com")
295records = domain.get_records()
296for r in records:
297    print(r.name, r.domain, r.type, r.data)
298```
299
300**[⬆ back to top](#table-of-contents)**
301
302### Creating a domain record
303
304This example shows how to create new domain record (sub.example.com):
305
306```python
307import digitalocean
308TOKEN="secretspecialuniquesnowflake"
309domain = digitalocean.Domain(token=TOKEN, name="example.com")
310new_record =  domain.create_new_domain_record(
311                type='A',
312                name='sub',
313                data='93.184.216.34'
314                )
315print(new_record)
316```
317
318**[⬆ back to top](#table-of-contents)**
319
320### Update a domain record
321
322This example shows how to create new domain record (sub.example.com):
323
324```python
325import digitalocean
326TOKEN="secretspecialuniquesnowflake"
327domain = digitalocean.Domain(token=TOKEN, name="example.com")
328records = domain.get_records()
329id = None
330for r in records:
331    if r.name == 'usb':
332        r.data = '1.1.1.1'
333        r.save()
334```
335
336**[⬆ back to top](#table-of-contents)**
337
338## Getting account requests/hour limits status
339
340Each request will also include the rate limit information:
341
342```python
343import digitalocean
344account = digitalocean.Account(token="secretspecialuniquesnowflake").load()
345# or
346manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
347account = manager.get_account()
348```
349Output:
350```text
351droplet_limit: 25
352email: 'name@domain.me'
353email_verified: True
354end_point: 'https://api.digitalocean.com/v2/'
355floating_ip_limit: 3
356ratelimit_limit: '5000'
357ratelimit_remaining: '4995'
358ratelimit_reset: '1505378973'
359status: 'active'
360status_message: ''
361token:'my_secret_token'
362uuid: 'my_id'
363```
364
365When using the Manager().get_all.. functions, the rate limit will be stored on the manager object:
366 ```python
367import digitalocean
368manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
369domains = manager.get_all_domains()
370
371print(manager.ratelimit_limit)
372```
373
374**[⬆ back to top](#table-of-contents)**
375
376## Session customization
377
378You can take advandtage of the [requests](http://docs.python-requests.org/en/master/) library and configure the HTTP client under python-digitalocean.
379
380### Configure retries in case of connection error
381
382This example shows how to configure your client to retry 3 times in case of `ConnectionError`:
383```python
384import digitalocean
385import requests
386from requests.adapters import HTTPAdapter
387from requests.packages.urllib3.util.retry import Retry
388
389manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
390retry = Retry(connect=3)
391adapter = HTTPAdapter(max_retries=retry)
392manager._session.mount('https://', adapter)
393```
394
395See [`Retry`](https://urllib3.readthedocs.io/en/latest/reference/urllib3.util.html#urllib3.util.retry.Retry) object reference to get more details about all retries options.
396
397### Configure a hook on specified answer
398
399This example shows how to launch custom actions if a HTTP 500 occurs:
400
401```python
402import digitalocean
403
404def handle_response(response, *args, **kwargs):
405    if response.status_code == 500:
406        # Make a lot things from the raw response
407        pass
408    return response
409
410manager = digitalocean.Manager(token="secretspecialuniquesnowflake")
411manager._session.hooks['response'].append(handle_response)
412```
413
414See [event hooks documentation](http://docs.python-requests.org/en/master/user/advanced/?highlight=HTTPAdapter#event-hooks) to get more details about this feature.
415
416**[⬆ back to top](#table-of-contents)**
417
418## Testing
419
420### Test using Docker
421To test this python-digitalocean you can use [docker](https://www.docker.com) to have a **clean environment automatically**. First you have to build the container by running in your shell on the repository directory:
422
423    docker build -t "pdo-tests" .
424
425Then you can run all the tests (for both python 2 and python 3)
426
427    docker run pdo-tests
428
429**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.
430
431**[⬆ back to top](#table-of-contents)**
432
433### Testing using pytest manually
434Use [pytest](http://pytest.org/) to perform testing. It is recommended to use a dedicated virtualenv to perform tests, using these commands:
435
436    $ virtualenv /tmp/digitalocean_env
437    $ source /tmp/digitalocean_env/bin/activate
438    $ pip install -r requirements.txt
439
440To run all the tests manually use py.test command:
441
442    $ python -m pytest
443
444**[⬆ back to top](#table-of-contents)**
445
446## Links
447
448  * GitHub: [https://github.com/koalalorenzo/python-digitalocean](https://github.com/koalalorenzo/python-digitalocean)
449  * PyPI page: [https://pypi.python.org/pypi/python-digitalocean/](https://pypi.python.org/pypi/python-digitalocean/)
450  * Author Website: [http://who.is.lorenzo.setale.me/?](http://setale.me/)
451  * Author Blog: [http://blog.setale.me/](http://blog.setale.me/)
452
453**[⬆ back to top](#table-of-contents)**
454