blob: 8dd6dc78e83d122c4863eeaccefab941608617e0 [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'):
Filip Pytloun2e01a432017-02-24 17:01:11 +010013 try:
Olivier Bourdon04707152017-02-27 15:56:21 +010014 inspect = json.loads(subprocess.check_output(["docker", "node", "inspect", "self"], stderr=subprocess.STDOUT).strip())[0]
Filip Pytloun2e01a432017-02-24 17:01:11 +010015 except subprocess.CalledProcessError:
16 return None
17
Filip Pytloun464dca82017-02-21 19:44:06 +010018 output['docker_swarm_role'] = inspect["Spec"]["Role"]
Filip Pytloun2720a0d2016-11-02 14:15:55 +010019 try:
Filip Pytloun464dca82017-02-21 19:44:06 +010020 output['docker_swarm_leader'] = inspect["ManagerStatus"]["Leader"]
21 except KeyError:
22 pass
23
24 if output['docker_swarm_role'] == 'manager':
Filip Pytloun2720a0d2016-11-02 14:15:55 +010025 output["docker_swarm_tokens"] = {
26 'worker': subprocess.check_output(["docker", "swarm", "join-token", "-q", "worker"]).strip(),
27 'manager': subprocess.check_output(["docker", "swarm", "join-token", "-q", "manager"]).strip()
28 }
Filip Pytloun2720a0d2016-11-02 14:15:55 +010029
Filip Pytloun464dca82017-02-21 19:44:06 +010030 if os.path.exists('/var/lib/docker/swarm/state.json'):
31 with open('/var/lib/docker/swarm/state.json') as fh:
32 state = yaml.load(fh)
33 for key, value in state[0].iteritems():
34 output["docker_swarm_%s" % key] = value
Filip Pytloun2720a0d2016-11-02 14:15:55 +010035
Filip Pytloun464dca82017-02-21 19:44:06 +010036 if os.path.exists('/var/lib/docker/swarm/docker-state.json'):
37 with open('/var/lib/docker/swarm/docker-state.json') as fh:
38 state = yaml.load(fh)
39 for key, value in state.iteritems():
40 output["docker_swarm_%s" % key] = value
Filip Pytloun2720a0d2016-11-02 14:15:55 +010041
42 if output:
43 return output
44 else:
45 return None