Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #! /usr/bin/env python3
  2. import json
  3. import argparse
  4. import os
  5. from os import path
  6. class SiteGenException(Exception):
  7. error = None
  8. code = None
  9. def __init__(self, error, code):
  10. self.error = error
  11. self.code = code
  12. class SiteGen:
  13. siteConfDir = ""
  14. siteDir = ""
  15. confDir = ""
  16. hooksEnabledDir = ""
  17. hooksAvailableDir = ""
  18. templatesDir = ""
  19. certRenewTime = ""
  20. letsencryptCommand = ""
  21. certDir = ""
  22. def __init__(self, config):
  23. self.siteConfDir = config["siteConfDir"]
  24. self.siteDir = config["siteDir"]
  25. self.confDir = config["confDir"]
  26. self.hooksEnabledDir = path.join(self.confDir, "hooks-enabled")
  27. self.hooksAvailableDir = path.join(self.confDir, "hooks-available")
  28. self.templatesDir = path.join(self.confDir, "templates")
  29. self.certRenewTime = config["certRenewTime"]
  30. self.letsencryptCommand = config["letsencryptCommand"]
  31. self.certDir = config["certDir"]
  32. def get_hook_dir(self, hook_type, is_enabled):
  33. return path.join(self.hooksEnabledDir if is_enabled else self.hooksAvailableDir, hook_type)
  34. def get_hook_file(self, hook_type, hook_name, is_enabled):
  35. return path.join(self.get_hook_dir(hook_type, is_enabled), hook_name)
  36. def get_hook_files(self, hook_type, is_enabled):
  37. hook_dir = self.get_hook_dir(hook_type, is_enabled)
  38. files = os.listdir(hook_dir)
  39. files.sort()
  40. return files
  41. def is_hook_present(self, hook_type, hook_name, is_enabled):
  42. return path.isfile(self.get_hook_file(hook_type, hook_name, is_enabled))
  43. def is_hook_enabled(self, hook_type, hook_name):
  44. return self.is_hook_present(hook_type, hook_name, True)
  45. def cert_request(self, domain):
  46. pass
  47. def cert_check(self, domain):
  48. pass
  49. def cert_renew(self, domain):
  50. pass
  51. def site_create(self, domain):
  52. pass
  53. def site_remove(self, domain):
  54. pass
  55. def hook_enable(self, hook_type, hook_name):
  56. if not self.is_hook_present(hook_type, hook_name, False):
  57. raise SiteGenException("Hook is not present", 1)
  58. if self.is_hook_enabled(hook_type, hook_name):
  59. raise SiteGenException("Hook is already enabled", 0)
  60. hook_dir = self.get_hook_dir(hook_type, hook_name)
  61. if not path.isdir(hook_dir):
  62. os.makedirs(hook_dir)
  63. hook_file_available = self.get_hook_file(hook_type, hook_name, False)
  64. hook_file_enabled = self.get_hook_file(hook_type, hook_name, True)
  65. hook_relative_file = path.relpath(hook_file_available, self.get_hook_dir(hook_type, True))
  66. os.symlink(hook_relative_file, hook_file_enabled)
  67. def hook_disable(self, hook_type, hook_name):
  68. if not self.is_hook_present(hook_type, hook_name, False):
  69. raise SiteGenException("Hook is not present", 1)
  70. if not self.is_hook_enabled(hook_type, hook_name):
  71. raise SiteGenException("Hook is not enabled", 0)
  72. os.remove(self.get_hook_file(hook_type, hook_name, True))
  73. def main():
  74. parser = argparse.ArgumentParser(description='Manage apache websites and SSL certificates')
  75. parser.add_argument('--config', dest='config', default='/etc/sitegen/sitegen.json', help='Configuration file path')
  76. parser.add_argument('--cert-request', metavar='cert_request', const='', nargs='?',
  77. help='Request/renew a certificate. Request/renew all certificates if no domain is specified')
  78. parser.add_argument('--cert-check', metavar='cert_check', const='', nargs='?',
  79. help='Check if certificate needs to be renewed. Check all if no domain is specified')
  80. parser.add_argument('--cert-renew', metavar='cert_renew', const='', nargs='?',
  81. help='Renew certificate if it needs to be. Renew all that needs to be if no domain is specified')
  82. parser.add_argument('--site-create', help='Create a site configuration', metavar='site_create')
  83. parser.add_argument('--site-remove', help='Remove a site configuration', metavar='site_remove')
  84. parser.add_argument('--hook-site-enable', help='Enable a site hook', dest='site_hook_enable', metavar='hook')
  85. parser.add_argument('--hook-site-disable', help='Disable a site hook', dest='site_hook_disable', metavar='hook')
  86. parser.add_argument('--hook-cert-enable', help='Enable a certificate hook', dest='hook_cert_enable', metavar='hook')
  87. parser.add_argument('--hook-cert-disable', help='Disable a certificate hook', dest='hook_cert_disable', metavar='hook')
  88. args = parser.parse_args()
  89. with open(args.config, "r") as f:
  90. config = json.load(f)
  91. site_gen = SiteGen(config)
  92. print(site_gen.get_hook_files("site", True))
  93. try:
  94. if args.cert_request is not None:
  95. site_gen.cert_request(args.cert_request)
  96. elif args.cert_check is not None:
  97. site_gen.cert_check(args.cert_check)
  98. elif args.cert_renew is not None:
  99. site_gen.cert_renew(args.cert_renew)
  100. elif args.site_create is not None:
  101. site_gen.site_create(args.site_create)
  102. elif args.site_remove is not None:
  103. site_gen.site_remove(args.site_remove)
  104. elif args.site_hook_enable is not None:
  105. site_gen.hook_enable("site", args.site_hook_enable)
  106. elif args.site_hook_disable is not None:
  107. site_gen.hook_disable("site", args.site_hook_disable)
  108. elif args.hook_cert_enable is not None:
  109. site_gen.hook_enable("cert", args.hook_cert_enable)
  110. elif args.hook_cert_disable is not None:
  111. site_gen.hook_disable("cert", args.hook_cert_disable)
  112. else:
  113. parser.print_help()
  114. except SiteGenException as e:
  115. print(e.error)
  116. exit(e.code)
  117. if __name__ == "__main__":
  118. main()