summaryrefslogtreecommitdiff
path: root/docker/api/swarm.py
blob: 2fc877448aa27ef88d2989fdbaa1694bc82bdfcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logging
from six.moves import http_client
from .. import utils
log = logging.getLogger(__name__)


class SwarmApiMixin(object):

    def create_swarm_spec(self, *args, **kwargs):
        return utils.SwarmSpec(*args, **kwargs)

    @utils.minimum_version('1.24')
    def init_swarm(self, advertise_addr=None, listen_addr='0.0.0.0:2377',
                   force_new_cluster=False, swarm_spec=None):
        url = self._url('/swarm/init')
        if swarm_spec is not None and not isinstance(swarm_spec, dict):
            raise TypeError('swarm_spec must be a dictionary')
        data = {
            'AdvertiseAddr': advertise_addr,
            'ListenAddr': listen_addr,
            'ForceNewCluster': force_new_cluster,
            'Spec': swarm_spec,
        }
        response = self._post_json(url, data=data)
        self._raise_for_status(response)
        return True

    @utils.minimum_version('1.24')
    def inspect_swarm(self):
        url = self._url('/swarm')
        return self._result(self._get(url), True)

    @utils.check_resource
    @utils.minimum_version('1.24')
    def inspect_node(self, node_id):
        url = self._url('/nodes/{0}', node_id)
        return self._result(self._get(url), True)

    @utils.minimum_version('1.24')
    def join_swarm(self, remote_addrs, join_token, listen_addr=None,
                   advertise_addr=None):
        data = {
            "RemoteAddrs": remote_addrs,
            "ListenAddr": listen_addr,
            "JoinToken": join_token,
            "AdvertiseAddr": advertise_addr,
        }
        url = self._url('/swarm/join')
        response = self._post_json(url, data=data)
        self._raise_for_status(response)
        return True

    @utils.minimum_version('1.24')
    def leave_swarm(self, force=False):
        url = self._url('/swarm/leave')
        response = self._post(url, params={'force': force})
        # Ignore "this node is not part of a swarm" error
        if force and response.status_code == http_client.NOT_ACCEPTABLE:
            return True
        self._raise_for_status(response)
        return True

    @utils.minimum_version('1.24')
    def nodes(self, filters=None):
        url = self._url('/nodes')
        params = {}
        if filters:
            params['filters'] = utils.convert_filters(filters)

        return self._result(self._get(url, params=params), True)

    @utils.minimum_version('1.24')
    def update_node(self, node_id, version, node_spec=None):
        url = self._url('/nodes/{0}/update?version={1}', node_id, str(version))
        res = self._post_json(url, data=node_spec)
        self._raise_for_status(res)
        return True

    @utils.minimum_version('1.24')
    def update_swarm(self, version, swarm_spec=None, rotate_worker_token=False,
                     rotate_manager_token=False):
        url = self._url('/swarm/update')
        response = self._post_json(url, data=swarm_spec, params={
            'rotateWorkerToken': rotate_worker_token,
            'rotateManagerToken': rotate_manager_token,
            'version': version
        })
        self._raise_for_status(response)
        return True