Skip to content

Instantly share code, notes, and snippets.

@cmars
Created January 7, 2014 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmars/8306216 to your computer and use it in GitHub Desktop.
Save cmars/8306216 to your computer and use it in GitHub Desktop.
This "juju nuke" plugin will not only destroy-service, it will also destroy-machine --force all machines running units of that service. Normally, juju does not reuse machines when destroying and redeploying services. This is useful when you're debugging on an OpenStack or AWS and tired of leaking instances when redeploying. This script is called…
#!/usr/bin/env python
import json
import os
import sys
from subprocess import Popen, PIPE
def die(msg):
print >>sys.stderr, msg
os._exit(1)
if __name__ == '__main__':
p = Popen(["juju", "status", "--format", "json"], stdin=None, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
if p.returncode:
die("Failed to execute 'juju status': %s" % (err))
data = json.loads(out)
match_services = set(sys.argv[1:])
if not match_services:
die("Usage: %s <service1> [<service2> .. <serviceN>]" % (sys.argv[0]))
nuke_machines = set()
nuke_services = set()
for service_name, service_info in data.get('services', {}).iteritems():
if service_name in match_services:
nuke_services.add(service_name)
for unit_name, unit_info in service_info.get('units', {}).iteritems():
if unit_info.get('machine'):
nuke_machines.add(unit_info['machine'])
for m in nuke_machines:
p = Popen(["juju", "destroy-machine", "--force", m], stdin=None, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
if p.returncode:
print >>sys.stderr, "Failed to destroy-machine %s: %s" % (m, err)
for s in nuke_services:
p = Popen(["juju", "destroy-service", s], stdin=None, stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
if p.returncode:
print >>sys.stderr, "Failed to destroy-service %s: %s" % (s, err)
print "Destroyed machines: %s" % (', '.join(sorted(list(nuke_machines))))
print "Destroyed services: %s" % (', '.join(sorted(list(nuke_services))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment