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'): |
Filip Pytloun | 2e01a43 | 2017-02-24 17:01:11 +0100 | [diff] [blame] | 13 | try: |
Olivier Bourdon | 0470715 | 2017-02-27 15:56:21 +0100 | [diff] [blame] | 14 | inspect = json.loads(subprocess.check_output(["docker", "node", "inspect", "self"], stderr=subprocess.STDOUT).strip())[0] |
Filip Pytloun | 2e01a43 | 2017-02-24 17:01:11 +0100 | [diff] [blame] | 15 | except subprocess.CalledProcessError: |
| 16 | return None |
| 17 | |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame] | 18 | output['docker_swarm_role'] = inspect["Spec"]["Role"] |
Filip Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 19 | try: |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame] | 20 | output['docker_swarm_leader'] = inspect["ManagerStatus"]["Leader"] |
| 21 | except KeyError: |
| 22 | pass |
| 23 | |
| 24 | if output['docker_swarm_role'] == 'manager': |
Filip Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 25 | 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 29 | |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame] | 30 | 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 35 | |
Filip Pytloun | 464dca8 | 2017-02-21 19:44:06 +0100 | [diff] [blame] | 36 | 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 Pytloun | 2720a0d | 2016-11-02 14:15:55 +0100 | [diff] [blame] | 41 | |
| 42 | if output: |
| 43 | return output |
| 44 | else: |
| 45 | return None |