PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

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

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