PageRenderTime 75ms CodeModel.GetById 32ms app.highlight 5ms RepoModel.GetById 36ms app.codeStats 0ms

/silverlining/mgr-scripts/clean-instances.py

https://bitbucket.org/ianb/silverlining/
Python | 43 lines | 36 code | 4 blank | 3 comment | 14 complexity | 6b32842b22eb30b75238891a972b94ec MD5 | raw file
 1#!/usr/bin/env python
 2import sys
 3sys.path.insert(0, '/usr/local/share/silverlining/lib')
 4import os
 5import optparse
 6import shutil
 7from silversupport import appdata
 8
 9parser = optparse.OptionParser(
10    usage='%prog [-n]')
11parser.add_option(
12    '-n', '--simulate',
13    action='store_true',
14    help="Show what would be removed, but don't remove it")
15   
16def remove_instance(instance_name):
17    """Removes the instance"""
18    print 'Removing unused site %s' % instance_name
19    shutil.rmtree(os.path.join('/var/www', instance_name))
20
21def remove_unused_sites():
22    """Call the script; remove all unused sites"""
23    options, args = parser.parse_args()
24    simulate = options.simulate
25    all_instances = appdata.all_app_instances()
26    used = []
27    for instance_name, hosts in sorted(all_instances.items()):
28        if hosts:
29            used.append((instance_name, hosts))
30            continue
31        if simulate:
32            print 'Would remove instance %s' % instance_name
33        else:
34            remove_instance(instance_name)
35    for instance_name, hostnames in used:
36        hostnames = [h for h, path in hostnames
37                     if ':' not in h
38                     and not h.startswith('www.')]
39        print 'Keeping instance %s (host: %s)' % (
40            instance_name, ', '.join(sorted(hostnames)))
41
42if __name__ == '__main__':
43    remove_unused_sites()