Fix docker swarm grains to work with docker 1.13
diff --git a/_grains/docker_swarm.py b/_grains/docker_swarm.py
index 8b8ecb7..65a2270 100644
--- a/_grains/docker_swarm.py
+++ b/_grains/docker_swarm.py
@@ -2,31 +2,38 @@
import os
import yaml
+import json
import subprocess
def main():
output = {}
- if os.path.exists('/var/lib/docker/swarm/control.sock'):
+
+ if os.path.exists('/var/lib/docker/swarm'):
+ inspect = json.loads(subprocess.check_output(["docker", "node", "inspect", "self"]).strip())[0]
+ output['docker_swarm_role'] = inspect["Spec"]["Role"]
try:
+ output['docker_swarm_leader'] = inspect["ManagerStatus"]["Leader"]
+ except KeyError:
+ pass
+
+ if output['docker_swarm_role'] == 'manager':
output["docker_swarm_tokens"] = {
'worker': subprocess.check_output(["docker", "swarm", "join-token", "-q", "worker"]).strip(),
'manager': subprocess.check_output(["docker", "swarm", "join-token", "-q", "manager"]).strip()
}
- except subprocess.CalledProcessError:
- pass
- if os.path.exists('/var/lib/docker/swarm/state.json'):
- with open('/var/lib/docker/swarm/state.json') as fh:
- state = yaml.load(fh)
- for key, value in state[0].iteritems():
- output["docker_swarm_%s" % key] = value
+ if os.path.exists('/var/lib/docker/swarm/state.json'):
+ with open('/var/lib/docker/swarm/state.json') as fh:
+ state = yaml.load(fh)
+ for key, value in state[0].iteritems():
+ output["docker_swarm_%s" % key] = value
- if os.path.exists('/var/lib/docker/swarm/docker-state.json'):
- with open('/var/lib/docker/swarm/docker-state.json') as fh:
- state = yaml.load(fh)
- for key, value in state.iteritems():
- output["docker_swarm_%s" % key] = value
+ if os.path.exists('/var/lib/docker/swarm/docker-state.json'):
+ with open('/var/lib/docker/swarm/docker-state.json') as fh:
+ state = yaml.load(fh)
+ for key, value in state.iteritems():
+ output["docker_swarm_%s" % key] = value
if output:
return output