Filip Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import yaml |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame^] | 5 | import json |
Filip Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 6 | import subprocess |
| 7 | |
| 8 | |
| 9 | def main(): |
| 10 | output = {} |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame^] | 11 | |
| 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 15 | try: |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame^] | 16 | output['docker_swarm_leader'] = inspect["ManagerStatus"]["Leader"] |
| 17 | except KeyError: |
| 18 | pass |
| 19 | |
| 20 | if output['docker_swarm_role'] == 'manager': |
Filip Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 21 | 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 25 | |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame^] | 26 | 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 31 | |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame^] | 32 | 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 37 | |
| 38 | if output: |
| 39 | return output |
| 40 | else: |
| 41 | return None |