Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # Find and restart lost Juju agents in a Juju2 environment.
  3. import subprocess
  4. import json
  5.  
  6. juju_output = subprocess.check_output(["juju","status","--format=json"])
  7.  
  8. the_dict = json.loads(juju_output)
  9.  
  10. to_fix = {}
  11.  
  12. for app, app_info in the_dict['applications'].items():
  13. if 'units' in app_info:
  14. for unit, unit_info in app_info['units'].items():
  15. if unit_info['juju-status']['current'] == "lost":
  16. to_fix[unit] = 'jujud-unit-{}'.format(unit.replace('/', '-'))
  17. if 'subordinates' in unit_info:
  18. for sub, sub_info in unit_info['subordinates'].items():
  19. if sub_info['juju-status']['current'] == "lost":
  20. to_fix[unit] = 'jujud-unit-{}'.format(sub.replace('/', '-'))
  21.  
  22. for machine, machine_info in the_dict['machines'].items():
  23. if machine_info['juju-status']['current'] == "down":
  24. to_fix[machine] = 'jujud-machine-{}'.format(machine)
  25. if not to_fix:
  26. print("nothing to fix, you're lucky")
  27. for where, what in to_fix.items():
  28. print("Restarting {} in {}".format(what, where))
  29. cmd = ['juju', 'ssh', where, "sudo", "service", what, "restart"]
  30. subprocess.check_call(cmd)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement