loglimit.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python2
  2. # -*- coding:utf-8 -*-
  3. import sh
  4. import os
  5. from .hook import JobHook
  6. from datetime import datetime
  7. class LogLimitHook(JobHook):
  8. def __init__(self, limit=10):
  9. self.limit = limit
  10. def before_job(self, *args, **kwargs):
  11. pass
  12. def after_job(self, *args, **kwargs):
  13. pass
  14. def before_exec(self, provider, ctx={}, *args, **kwargs):
  15. log_dir = provider.log_dir
  16. self.ensure_log_dir(log_dir)
  17. log_file = provider.log_file.format(
  18. date=datetime.now().strftime("%Y-%m-%d_%H-%M"))
  19. ctx['log_file'] = log_file
  20. if log_file == "/dev/null":
  21. return
  22. log_link = os.path.join(log_dir, "latest")
  23. ctx['log_link'] = log_link
  24. lfiles = [os.path.join(log_dir, lfile)
  25. for lfile in os.listdir(log_dir)
  26. if lfile.startswith(provider.name)]
  27. lfiles_set = set(lfiles)
  28. # sort to get the newest 10 files
  29. lfiles_ts = sorted(
  30. [(os.path.getmtime(lfile), lfile) for lfile in lfiles],
  31. key=lambda x: x[0],
  32. reverse=True)
  33. lfiles_keep = set([x[1] for x in lfiles_ts[:self.limit]])
  34. lfiles_rm = lfiles_set - lfiles_keep
  35. # remove old files
  36. for lfile in lfiles_rm:
  37. try:
  38. sh.rm(lfile)
  39. except:
  40. pass
  41. # create a soft link
  42. self.create_link(log_link, log_file)
  43. def after_exec(self, status=None, ctx={}, *args, **kwargs):
  44. log_file = ctx.get('log_file', None)
  45. log_link = ctx.get('log_link', None)
  46. if log_file == "/dev/null":
  47. return
  48. if status == "fail":
  49. log_file_save = log_file + ".fail"
  50. try:
  51. sh.mv(log_file, log_file_save)
  52. except:
  53. pass
  54. self.create_link(log_link, log_file_save)
  55. def ensure_log_dir(self, log_dir):
  56. if not os.path.exists(log_dir):
  57. sh.mkdir("-p", log_dir)
  58. def create_link(self, log_link, log_file):
  59. if log_link == log_file:
  60. return
  61. if not (log_link and log_file):
  62. return
  63. if os.path.lexists(log_link):
  64. try:
  65. sh.rm(log_link)
  66. except:
  67. return
  68. try:
  69. sh.ln('-s', log_file, log_link)
  70. except:
  71. return
  72. # vim: ts=4 sw=4 sts=4 expandtab