PageRenderTime 4ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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