Skip to content
This repository was archived by the owner on Jan 20, 2020. It is now read-only.

Commit 2d9586a

Browse files
committed
dockercloud
1 parent 86f4a23 commit 2d9586a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2701
-2
lines changed

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
__pycache__/
33
*.py[cod]
44

5-
# C extensions
6-
*.so
75

86
# Distribution / packaging
97
.Python
@@ -22,6 +20,7 @@ var/
2220
*.egg-info/
2321
.installed.cfg
2422
*.egg
23+
venv/
2524

2625
# PyInstaller
2726
# Usually these files are written by a python script from a template
@@ -55,3 +54,8 @@ docs/_build/
5554

5655
# PyBuilder
5756
target/
57+
58+
# IDE
59+
.idea/
60+
61+
venv/

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
test:prepare
2+
venv/bin/python setup.py test
3+
4+
clean:
5+
rm -rf venv build dist *.egg-info
6+
find . -name '*.pyc' -delete
7+
8+
prepare:clean
9+
set -ex
10+
virtualenv venv
11+
venv/bin/pip install mock
12+
venv/bin/pip install -r requirements.txt
13+
venv/bin/python setup.py install

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,80 @@
11
# python-docker-cloud
2+
23
Python library for Docker Cloud
4+
5+
## Installation
6+
7+
In order to install the Docker Cloud Python library, you can use pip install:
8+
9+
pip install python-dockercloud
10+
11+
It will install a Python module called dockercloud which you can use to interface with the API.
12+
13+
## Authorization
14+
15+
The authentication can be configured in the following ways:
16+
17+
* Manually set it in your Python initialization code:
18+
19+
import dockercloud
20+
dockercloud.user = "username"
21+
dockercloud.apikey = "apikey"
22+
23+
* Login with docker cli, and the library will read the configfile automatically:
24+
25+
$ docker login
26+
27+
* Set the environment variables DOCKERCLOUD_USER and DOCKERCLOUD_APIKEY:
28+
29+
export DOCKERCLOUD_USER=username
30+
export DOCKERCLOUD_APIKEY=apikey
31+
32+
## Errors
33+
34+
Errors in the HTTP API will be returned with status codes in the 4xx and 5xx ranges.
35+
36+
The Python library will detect this status codes and raise ApiError exceptions with the error message, which should be handled by the calling application accordingly.
37+
38+
39+
## Quick examples
40+
41+
### Services
42+
43+
>>> import dockercloud
44+
>>> dockercloud.Service.list()
45+
[<dockercloud.api.service.Service object at 0x10701ca90>, <dockercloud.api.service.Service object at 0x10701ca91>]
46+
>>> service = dockercloud.Service.fetch("fee900c6-97da-46b3-a21c-e2b50ed07015")
47+
<dockercloud.api.service.Service object at 0x106c45c10>
48+
>>> service.name
49+
"my-python-app"
50+
>>> service = dockercloud.Service.create(image="dockercloud/hello-world", name="my-new- app", target_num_containers=2)
51+
>>> service.save()
52+
True
53+
>>> service.target_num_containers = 3
54+
>>> service.save()
55+
True
56+
>>> service.stop()
57+
True
58+
>>> service.start()
59+
True
60+
>>> service.delete()
61+
True
62+
63+
### Containers
64+
65+
>>> import dockercloud
66+
>>> dockercloud.Container.list()
67+
[<dockercloud.api.container.Container object at 0x10701ca90>, <dockercloud.api.container.Container object at 0x10701ca91>]
68+
>>> container = dockercloud.Container.fetch("7d6696b7-fbaf-471d-8e6b-ce7052586c24")
69+
<dockercloud.api.container.Container object at 0x10701ca90>
70+
>>> container.public_dns = "my-web-app.example.com"
71+
>>> container.save()
72+
True
73+
>>> container.stop()
74+
True
75+
>>> container.start()
76+
True
77+
>>> container.logs()
78+
"2014-03-24 23:58:08,973 CRIT Supervisor running as root (no user in config file) [...]"
79+
>>> container.delete()
80+
True

dockercloud/__init__.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import base64
2+
import logging
3+
import os
4+
5+
import requests
6+
from future.standard_library import install_aliases
7+
8+
install_aliases()
9+
10+
from dockercloud.api import auth
11+
from dockercloud.api.service import Service
12+
from dockercloud.api.container import Container
13+
from dockercloud.api.repository import Repository
14+
from dockercloud.api.node import Node
15+
from dockercloud.api.action import Action
16+
from dockercloud.api.nodecluster import NodeCluster
17+
from dockercloud.api.nodetype import NodeType
18+
from dockercloud.api.nodeprovider import Provider
19+
from dockercloud.api.noderegion import Region
20+
from dockercloud.api.tag import Tag
21+
from dockercloud.api.trigger import Trigger
22+
from dockercloud.api.stack import Stack
23+
from dockercloud.api.exceptions import ApiError, AuthError, ObjectNotFound, NonUniqueIdentifier
24+
from dockercloud.api.utils import Utils
25+
from dockercloud.api.events import Events
26+
from dockercloud.api.nodeaz import AZ
27+
28+
__version__ = '1.0.0'
29+
30+
dockercloud_auth = os.environ.get('DOCKERCLOUD_AUTH')
31+
basic_auth = auth.load_from_file("~/.docker/config.json")
32+
33+
if os.environ.get('DOCKERCLOUD_USER') and os.environ.get('DOCKERCLOUD_PASS'):
34+
basic_auth = base64.b64encode("%s:%s" % (os.environ.get('DOCKERCLOUD_USER'), os.environ.get('DOCKERCLOUD_PASS')))
35+
if os.environ.get('DOCKERCLOUD_USER') and os.environ.get('DOCKERCLOUD_APIKEY'):
36+
basic_auth = base64.b64encode("%s:%s" % (os.environ.get('DOCKERCLOUD_USER'), os.environ.get('DOCKERCLOUD_APIKEY')))
37+
38+
rest_host = os.environ.get("DOCKERCLOUD_REST_HOST") or 'https://cloud.docker.com/'
39+
stream_host = os.environ.get("DOCKERCLOUD_STREAM_HOST") or 'wss://ws.cloud.docker.com/'
40+
41+
user_agent = None
42+
43+
logging.basicConfig()
44+
logger = logging.getLogger("python-dockercloud")
45+
46+
try:
47+
requests.packages.urllib3.disable_warnings()
48+
except:
49+
pass

dockercloud/api/__init__.py

Whitespace-only changes.

dockercloud/api/action.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from __future__ import absolute_import
2+
3+
from .base import Immutable, StreamingLog
4+
5+
6+
class Action(Immutable):
7+
subsystem = 'audit'
8+
endpoint = "/action"
9+
10+
@classmethod
11+
def _pk_key(cls):
12+
return 'uuid'
13+
14+
def logs(self, tail, follow, log_handler=StreamingLog.default_log_handler):
15+
logs = StreamingLog(self.subsystem, self.endpoint, self.pk, tail, follow)
16+
logs.on_message(log_handler)
17+
logs.run_forever()
18+
19+
def cancel(self):
20+
return self._perform_action("cancel")
21+
22+
def retry(self):
23+
return self._perform_action("retry")

dockercloud/api/auth.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from __future__ import absolute_import
2+
3+
import base64
4+
import json
5+
import os
6+
7+
from requests.auth import HTTPBasicAuth
8+
9+
import dockercloud
10+
from .http import send_request
11+
12+
13+
def authenticate(username, password):
14+
verify_credential(username, password)
15+
dockercloud.basic_auth = base64.b64encode("%s:%s" % (username, password))
16+
17+
18+
def verify_credential(username, password):
19+
auth = HTTPBasicAuth(username, password)
20+
send_request("GET", "/auth", auth=auth)
21+
22+
23+
def is_authenticated():
24+
try:
25+
dockercloud.basic_auth = base64.b64encode("%s:%s" % (dockercloud.user, dockercloud.password))
26+
except:
27+
pass
28+
29+
try:
30+
dockercloud.basic_auth = base64.b64encode("%s:%s" % (dockercloud.user, dockercloud.apikey))
31+
except:
32+
pass
33+
34+
return dockercloud.dockercloud_auth or dockercloud.basic_auth
35+
36+
37+
def logout():
38+
dockercloud.dockercloud_auth = None
39+
dockercloud.basic_auth = None
40+
41+
42+
def load_from_file(f="~/.docker/config.json"):
43+
try:
44+
with open(os.path.expanduser(f)) as config_file:
45+
data = json.load(config_file)
46+
47+
return data.get("auths", {}).get("https://index.docker.io/v1/", {}).get("auth", None)
48+
except Exception:
49+
return None
50+
51+
52+
def get_auth_header():
53+
try:
54+
dockercloud.basic_auth = base64.b64encode("%s:%s" % (dockercloud.user, dockercloud.password))
55+
except:
56+
pass
57+
58+
try:
59+
dockercloud.basic_auth = base64.b64encode("%s:%s" % (dockercloud.user, dockercloud.apikey))
60+
except:
61+
pass
62+
63+
if dockercloud.dockercloud_auth:
64+
return {'Authorization': dockercloud.dockercloud_auth}
65+
if dockercloud.basic_auth:
66+
return {'Authorization': 'Basic %s' % dockercloud.basic_auth}
67+
return {}

0 commit comments

Comments
 (0)