~cprov/tanuki-continuous-deployer/local-provider

« back to all changes in this revision

Viewing changes to reaper.py

  • Committer: Thomi Richards
  • Date: 2015-07-17 06:02:44 UTC
  • Revision ID: thomi.richards@canonical.com-20150717060244-jngqosrn11qcx8ns
RemoveĀ unusedĀ code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python3
2
 
#
3
 
# Copyright (C) 2015 Canonical
4
 
#
5
 
# This program is free software: you can redistribute it and/or modify
6
 
# it under the terms of the GNU General Public License as published by
7
 
# the Free Software Foundation, either version 3 of the License, or
8
 
# (at your option) any later version.
9
 
#
10
 
# This program is distributed in the hope that it will be useful,
11
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
# GNU General Public License for more details.
14
 
#
15
 
# You should have received a copy of the GNU General Public License
16
 
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
#
18
 
"""CI Automation: Destroys an environment upon given its SHA1 value."""
19
 
 
20
 
import argparse
21
 
import glob
22
 
import os
23
 
import sys
24
 
 
25
 
 
26
 
from ci_automation import (
27
 
    juju,
28
 
    mojo,
29
 
    swift,
30
 
)
31
 
 
32
 
 
33
 
def main():
34
 
    parser = argparse.ArgumentParser(
35
 
        description=('Destroys the juju environment of a given identifier')
36
 
    )
37
 
    parser.add_argument('--project', default='mojo-stg-ue-ci-engineering')
38
 
    parser.add_argument('--base',
39
 
                        default=os.path.expanduser('~/juju-environments/'))
40
 
    parser.add_argument('identifier', metavar='IDENTIFIER')
41
 
    args = parser.parse_args()
42
 
 
43
 
    glob_pattern = os.path.join(args.base, '*{}'.format(args.identifier))
44
 
    matching = glob.glob(glob_pattern)
45
 
    if not matching:
46
 
        print("Nothing to do - no files matching pattern %r" % glob_pattern)
47
 
        # We are happy if it's already destroyed.
48
 
        return 0
49
 
 
50
 
    try:
51
 
        [env_dir] = matching
52
 
    except ValueError:
53
 
        print('Environments collision ? %r', matching)
54
 
        return 1
55
 
 
56
 
    name = os.path.basename(env_dir)
57
 
    if juju.deployment_has_floating_ips(name, args.base):
58
 
        print("""\
59
 
Cowardly refusing to reap that, since it has floating IP addresses assigned.
60
 
 
61
 
Don't do what Thomi did and destroy in-use production services!
62
 
Assign all floating IP addresses to a new deployment, then call reaper.py
63
 
again.
64
 
        """)
65
 
        return 1
66
 
 
67
 
    juju.destroy_all_services(name, args.base)
68
 
    print('Services destroyed')
69
 
    juju.destroy_environment(name, args.base)
70
 
    print('Environment {} destroyed'.format(name))
71
 
    control_bucket = "juju-{}".format(name)
72
 
    swift.delete(control_bucket)
73
 
    print('Swift control bucket {} deleted'.format(control_bucket))
74
 
    mojo.workspace_destroy(args.project, name)
75
 
    print('Mojo Workspace {}/{} destroyed'.format(args.project, name))
76
 
 
77
 
    return 0
78
 
 
79
 
 
80
 
if __name__ == '__main__':
81
 
    sys.exit(main())