Remove Juju Local Environment Cleanly

UPDATE 2015-01-28: Now taking into account recent changes to Juju 1.20+

If you’re frequently hacking on Juju source code or just creating and destroying local environments often, you may find the following snippet I came up with handy.

It’s supposed to be run as root, like `sudo cleanup-juju` (assuming it’s somewhere in your ‘$PATH’ and it’s executable). Just change `LOCAL_ENV=localenv` to whatever your local environment name is (from `$HOME/.juju/environments.yaml` – usually `local`) and run it. Twice, if needed (when `mongodb` and/or `jujud` agents are busy), to clean up:

  • Any leftover gocheck or mgo test directories in `/tmp` (these tend to accumulate over time and waste space)
  • Juju run-time temporary directories (e.g. /tmp/juju-worker-deployer<random-number>/)
  • Mongo socket files created by mongod run from juju tests (port other than the default 27017)
  • Old-style upstart “auto start” configuration (in juju 1.17 and later we use user upstart jobs for that)
  • Any juju or juju-lxc logs
  • Any lxc containers created by juju for that local environment
  • The .jenv file of the environment itself
  • Kill any `mongod` or `jujud` processes related to the local environment (for this to work it’s important you pick a unique name for your environment, so that `ps xa | grep <envname>` does not return processes not belonging to the environment – i.e. “local” is a bit too generic, I use “localenv”)
#!/bin/bash

# Change this to match the name of your environment type:local in
#  ~/.juju/environments.yaml
LOCAL_ENV=localenv

[[ $UID != 0 ]] && echo "$0 must be run as root" && exit -1

pkill -9 -f $LOCAL_ENV
pkill -9 -u $USER mongod
find /tmp -name 'mongodb-*.sock' -not -name 'mongodb-27017.sock' -delete

cleanup=(
"/tmp/gocheck-*"
"/tmp/test-mgo*"
"/tmp/juju-*"
"$HOME/.juju/${LOCAL_ENV}"
"/etc/lxc/auto/*"
"/var/log/juju*"
"/var/lib/juju*"
"/var/lib/lxc/${SUDO_USER}*"
"/var/lib/lxc/juju-*"
"/var/log/lxc/${SUDO_USER}*"
"/var/log/upstart/juju*"
"/var/log/upstart/lxc-instance-*"
"${HOME}/.juju/environments/${LOCAL_ENV}.jenv"
)

for f in "${cleanup[@]}"; do
  rm -fr $f
done

I hope you find this useful, especially if your `juju destroy-environment localenv` fails for some reason to clean up after itself, or if `juju bootstrap` fails and leaves the environment in a bad state.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.