PageRenderTime 183ms CodeModel.GetById 80ms app.highlight 6ms RepoModel.GetById 94ms app.codeStats 0ms

/silverlining/mgr-scripts/remove-host.py

https://bitbucket.org/ianb/silverlining/
Python | 53 lines | 47 code | 4 blank | 2 comment | 22 complexity | e449d2fffcd933ca993af146a5cd75f8 MD5 | raw file
 1#!/usr/bin/env python
 2import sys
 3sys.path.insert(0, '/usr/local/share/silverlining/lib')
 4import optparse
 5from silversupport import appdata
 6import fnmatch
 7import re
 8
 9parser = optparse.OptionParser(
10    usage='%prog HOSTS')
11parser.add_option(
12    '--keep-prev', action='store_true',
13    help='Keep the prev.* host')
14
15
16def main():
17    options, args = parser.parse_args()
18    locations = []
19    existing = appdata.all_app_instances()
20    for location in args:
21        hostname, path = appdata.normalize_location(location, empty_path=None)
22        if '*' in location:
23            hostname_match = re.compile(fnmatch.translate(hostname), re.I)
24            if path is not None:
25                path_match = re.compile(fnmatch.translate(path))
26            else:
27                path_match = None
28            for instance_name, ex_location in existing.iteritems():
29                for ex_hostname, ex_path in ex_location:
30                    if ((ex_hostname == 'disabled' and hostname != 'disabled')
31                        or (ex_hostname == 'not-found' and hostname != 'not-found')):
32                        # Only allow explicit disabling of these hosts
33                        continue
34                    if (hostname_match.match(ex_hostname)
35                        and (path_match is None or path_match.search(ex_path))):
36                        locations.append((ex_hostname, ex_path))
37                        break
38        else:
39            locations.append((hostname, path))
40
41    for hostname, path in locations:
42        removed = appdata.remove_host(hostname, path=path, keep_prev=options.keep_prev)
43        if not removed:
44            print 'No entries found matching %s' % (hostname + (path or ''))
45        elif len(removed) == 1:
46            print 'Removed %s: %s' % (hostname + (path or ''), removed[0].split('|')[0])
47        else:
48            print 'Removed %s:' % (hostname + (path or ''))
49            for line in removed:
50                print '  %s' % line.split('|')[0]
51
52if __name__ == '__main__':
53    main()