blob: 65a227068f09396fbd69de7aa648d4b85fc64c7f [file] [log] [blame]
Filip Pytloun2720a0d2016-11-02 14:15:55 +01001#!/usr/bin/env python
2
3import os
4import yaml
Filip Pytloun464dca82017-02-21 19:44:06 +01005import json
Filip Pytloun2720a0d2016-11-02 14:15:55 +01006import subprocess
7
8
9def main():
10 output = {}
Filip Pytloun464dca82017-02-21 19:44:06 +010011
12 if os.path.exists('/var/lib/docker/swarm'):
13 inspect = json.loads(subprocess.check_output(["docker", "node", "inspect", "self"]).strip())[0]
14 output['docker_swarm_role'] = inspect["Spec"]["Role"]
Filip Pytloun2720a0d2016-11-02 14:15:55 +010015 try:
Filip Pytloun464dca82017-02-21 19:44:06 +010016 output['docker_swarm_leader'] = inspect["ManagerStatus"]["Leader"]
17 except KeyError:
18 pass
19
20 if output['docker_swarm_role'] == 'manager':
Filip Pytloun2720a0d2016-11-02 14:15:55 +010021 output["docker_swarm_tokens"] = {
22 'worker': subprocess.check_output(["docker", "swarm", "join-token", "-q", "worker"]).strip(),
23 'manager': subprocess.check_output(["docker", "swarm", "join-token", "-q", "manager"]).strip()
24 }
Filip Pytloun2720a0d2016-11-02 14:15:55 +010025
Filip Pytloun464dca82017-02-21 19:44:06 +010026 if os.path.exists('/var/lib/docker/swarm/state.json'):
27 with open('/var/lib/docker/swarm/state.json') as fh:
28 state = yaml.load(fh)
29 for key, value in state[0].iteritems():
30 output["docker_swarm_%s" % key] = value
Filip Pytloun2720a0d2016-11-02 14:15:55 +010031
Filip Pytloun464dca82017-02-21 19:44:06 +010032 if os.path.exists('/var/lib/docker/swarm/docker-state.json'):
33 with open('/var/lib/docker/swarm/docker-state.json') as fh:
34 state = yaml.load(fh)
35 for key, value in state.iteritems():
36 output["docker_swarm_%s" % key] = value
Filip Pytloun2720a0d2016-11-02 14:15:55 +010037
38 if output:
39 return output
40 else:
41 return None