~viswesn/juju-ci-tools/aws_boto3

610 by Curtis Hovey
Added script to clean up after lxc, and it can exit with 1 when juju left dirt.
1
#!/bin/bash
2
set -eux
3
4
EXIT_CODE=0
5
ERROR_WHEN_DIRTY='false'
6
while [[ "${1-}" != "" && $1 =~ ^-.*  ]]; do
7
    case $1 in
8
        --error-when-dirty)
9
            ERROR_WHEN_DIRTY="true"
10
            ;;
11
    esac
12
    shift
13
done
14
15
16
RUNNING_SERVICES=$(
17
    sudo lxc-ls --fancy |
18
    grep $USER-juju |
19
    grep RUNNING |
20
    cut -d ' ' -f 1)
21
[[ -n "$RUNNING_SERVICES" && $ERROR_WHEN_DIRTY == 'true' ]] && EXIT_CODE=1
22
for service in $RUNNING_SERVICES; do
23
    sudo lxc-stop -n $service
24
done
25
26
27
STOPPED_SERVICES=$(
28
    sudo lxc-ls --fancy |
29
    grep $USER-juju |
30
    grep STOPPED |
31
    cut -d ' ' -f 1)
32
[[ -n "$STOPPED_SERVICES" && $ERROR_WHEN_DIRTY == 'true' ]] && EXIT_CODE=1
33
for service in $RUNNING_SERVICES; do
34
    sudo lxc-destroy -n $service
35
    if [[ -e /etc/lxc/auto/$service ]]; then
36
        sudo rm -r /etc/lxc/auto/$service
37
    fi
38
done
39
40
41
RUNNING_TEMPLATES=$(
42
    sudo lxc-ls --fancy |
43
    grep juju-.*-template |
44
    grep RUNNING |
45
    cut -d ' ' -f 1)
46
[[ -n "$RUNNING_TEMPLATES" && $ERROR_WHEN_DIRTY == 'true' ]] && EXIT_CODE=1
47
for template in $RUNNING_TEMPLATES; do
48
    sudo lxc-stop -n $template
49
done
50
51
52
set +e
53
STALE_LOCKS=$(find /var/lib/juju/locks -maxdepth 1 -type d -name 'juju-*')
54
set -e
55
[[ -n "$STALE_LOCKS" && $ERROR_WHEN_DIRTY == 'true' ]] && EXIT_CODE=1
56
for lock in $STALE_LOCKS; do
57
    sudo rm -r $lock
58
done
59
echo "cleaned"
60
exit $EXIT_CODE