ntpdate-service.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. from comar.service import *
  3. import re
  4. serviceType = "local"
  5. serviceDesc = _({"en": "Set the date & time via NTP",
  6. "tr": "Tarih & zamanı NTP ile ayarlayın"})
  7. serviceConf = "ntpdate"
  8. serviceDefault = "conditional"
  9. # ntpdate will run if it finds a ticker in any of the configuration files
  10. NTPCONF = "/etc/ntp.conf"
  11. NTPSTEP = "/etc/ntp/step-tickers"
  12. PIDFILE = "/run/ntpdate.pid"
  13. MSG_NOSERVER = _({"en" : "NTP server not specified in %s or %s." % (NTPSTEP, NTPCONF),
  14. "tr" : "%s veya %s dosyasında hiç NTP sunucu belirtilmemiş." % (NTPSTEP, NTPCONF)})
  15. def parse_tickers():
  16. tickers = []
  17. if os.path.exists(NTPSTEP):
  18. for line in open(NTPSTEP, "r").read().strip().split("\n"):
  19. if line and not line.startswith("#"):
  20. tickers.append(line)
  21. if tickers:
  22. return tickers
  23. if os.path.exists(NTPCONF):
  24. for line in open(NTPCONF, "r").read().strip().split("\n"):
  25. if line.startswith(("server", "peer")):
  26. try:
  27. peer = line.split()[1]
  28. if not re.match("127\.127\.[0-9]+\.[0-9]+", peer):
  29. tickers.append(peer)
  30. except IndexError:
  31. pass
  32. return tickers
  33. @synchronized
  34. def start():
  35. tickers = parse_tickers()
  36. if len(tickers) == 0:
  37. fail(MSG_NOSERVER)
  38. tickers = " ".join(tickers)
  39. # Eventhough the ntpdate is a oneshot process, the pidfile hack is used to
  40. # work around COMAR's silly status() check in startService()
  41. startService(command="/usr/sbin/ntpdate",
  42. args="%s %s" % (config.get("OPTIONS", "-u ntp -s -b"), tickers),
  43. makepid=True,
  44. pidfile=PIDFILE,
  45. donotify=True)
  46. if os.path.exists(PIDFILE):
  47. os.unlink(PIDFILE)
  48. if config.get("SYNC_HWCLOCK", "no") == "yes":
  49. run("/sbin/hwclock --systohc")
  50. def ready():
  51. status = is_on()
  52. tickers = parse_tickers()
  53. if status == "on" or (status == "conditional" and tickers):
  54. start()
  55. @synchronized
  56. def stop():
  57. # There's nothing to do
  58. pass
  59. def status():
  60. # No need to supply a status as the service is oneshot
  61. return