Преглед на файлове

hooks management

tags/v2.0.0
Robin Thoni преди 8 години
родител
ревизия
0e90f827e3
променени са 33 файла, в които са добавени 213 реда и са изтрити 11 реда
  1. 159
    0
      sitegen.py
  2. 7
    0
      sitegen/hooks-available/cert/000-print
  3. 13
    0
      sitegen/hooks-available/site/000-print
  4. 8
    1
      sitegen/hooks-available/site/050-letsencrypt
  5. 10
    0
      sitegen/hooks-available/site/100-chown
  6. 8
    0
      sitegen/hooks-available/site/200-a2ensite
  7. 8
    0
      sitegen/hooks-available/site/300-reload
  8. 0
    3
      sitegen/hooks.available/100-chown
  9. 0
    1
      sitegen/hooks.available/200-a2ensite
  10. 0
    1
      sitegen/hooks.available/300-reload
  11. 0
    1
      sitegen/hooks.d/000-print
  12. 0
    1
      sitegen/hooks.d/050-letsencrypt
  13. 0
    1
      sitegen/hooks.d/100-chown
  14. 0
    1
      sitegen/hooks.d/200-a2ensite
  15. 0
    1
      sitegen/hooks.d/300-reload
  16. 0
    0
      sitegen/templates/default.conf
  17. 0
    0
      sitegen/templates/default.include
  18. 0
    0
      sitegen/templates/docker.http.conf
  19. 0
    0
      sitegen/templates/docker.http.include
  20. 0
    0
      sitegen/templates/docker.https.conf
  21. 0
    0
      sitegen/templates/docker.https.include
  22. 0
    0
      sitegen/templates/http.conf
  23. 0
    0
      sitegen/templates/http.include
  24. 0
    0
      sitegen/templates/https.conf
  25. 0
    0
      sitegen/templates/https.include
  26. 0
    0
      sitegen/templates/laravel.http.conf
  27. 0
    0
      sitegen/templates/laravel.http.include
  28. 0
    0
      sitegen/templates/laravel.https.conf
  29. 0
    0
      sitegen/templates/laravel.https.include
  30. 0
    0
      sitegen/templates/reverse.http.conf
  31. 0
    0
      sitegen/templates/reverse.http.include
  32. 0
    0
      sitegen/templates/reverse.https.conf
  33. 0
    0
      sitegen/templates/reverse.https.include

+ 159
- 0
sitegen.py Целия файл

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

sitegen/hooks.available/000-print → sitegen/hooks-available/cert/000-print Целия файл

@@ -1,3 +1,10 @@
1
+host="${1}"
2
+cert_file="${2}"
3
+key_file="${3}"
4
+conf_include="${4}"
5
+site_conf="${5}"
6
+site_include="${6}"
7
+
1 8
 echo "Host: ${host}"
2 9
 echo "Root Document: ${root_dir}"
3 10
 echo "Http Configuration Source: ${conf_conf}"

+ 13
- 0
sitegen/hooks-available/site/000-print Целия файл

@@ -0,0 +1,13 @@
1
+host="${1}"
2
+root_dir="${2}"
3
+conf_conf="${3}"
4
+conf_include="${4}"
5
+site_conf="${5}"
6
+site_include="${6}"
7
+
8
+echo "Host: ${host}"
9
+echo "Root Document: ${root_dir}"
10
+echo "Http Configuration Source: ${conf_conf}"
11
+echo "Site Configuration Source: ${conf_include}"
12
+echo "Http Configuration Destination: ${site_conf}"
13
+echo "Site Configuration Destination: ${site_include}"

sitegen/hooks.available/050-letsencrypt → sitegen/hooks-available/site/050-letsencrypt Целия файл

@@ -1,8 +1,15 @@
1
+host="${1}"
2
+root_dir="${2}"
3
+conf_conf="${3}"
4
+conf_include="${4}"
5
+site_conf="${5}"
6
+site_include="${6}"
7
+
1 8
 count=$(grep -ci SSLCertificateFile ${site_conf})
2 9
 if [ "${count}" -ge 1 ]
3 10
 then
4 11
   echo "SSL found; generating certificate..."
5
-  generate-ssl-cert "${host}"
12
+  sitegen --cert-request "${host}"
6 13
 else
7 14
   echo "No SSL found; doing nothing"
8 15
 fi

+ 10
- 0
sitegen/hooks-available/site/100-chown Целия файл

@@ -0,0 +1,10 @@
1
+host="${1}"
2
+root_dir="${2}"
3
+conf_conf="${3}"
4
+conf_include="${4}"
5
+site_conf="${5}"
6
+site_include="${6}"
7
+
8
+user=$(logname)
9
+echo "chown to ${user}"
10
+chown ${user}:${user} ${root_dir}

+ 8
- 0
sitegen/hooks-available/site/200-a2ensite Целия файл

@@ -0,0 +1,8 @@
1
+host="${1}"
2
+root_dir="${2}"
3
+conf_conf="${3}"
4
+conf_include="${4}"
5
+site_conf="${5}"
6
+site_include="${6}"
7
+
8
+a2ensite "${host}.conf"

+ 8
- 0
sitegen/hooks-available/site/300-reload Целия файл

@@ -0,0 +1,8 @@
1
+host="${1}"
2
+root_dir="${2}"
3
+conf_conf="${3}"
4
+conf_include="${4}"
5
+site_conf="${5}"
6
+site_include="${6}"
7
+
8
+service apache2 reload

+ 0
- 3
sitegen/hooks.available/100-chown Целия файл

@@ -1,3 +0,0 @@
1
-user=$(logname)
2
-echo "chown to ${user}"
3
-chown ${user}:${user} ${root_dir}

+ 0
- 1
sitegen/hooks.available/200-a2ensite Целия файл

@@ -1 +0,0 @@
1
-a2ensite "${host}.conf"

+ 0
- 1
sitegen/hooks.available/300-reload Целия файл

@@ -1 +0,0 @@
1
-service apache2 reload

+ 0
- 1
sitegen/hooks.d/000-print Целия файл

@@ -1 +0,0 @@
1
-../hooks.available/000-print

+ 0
- 1
sitegen/hooks.d/050-letsencrypt Целия файл

@@ -1 +0,0 @@
1
-../hooks.available/050-letsencrypt

+ 0
- 1
sitegen/hooks.d/100-chown Целия файл

@@ -1 +0,0 @@
1
-../hooks.available/100-chown

+ 0
- 1
sitegen/hooks.d/200-a2ensite Целия файл

@@ -1 +0,0 @@
1
-../hooks.available/200-a2ensite

+ 0
- 1
sitegen/hooks.d/300-reload Целия файл

@@ -1 +0,0 @@
1
-../hooks.available/300-reload

sitegen/default.conf → sitegen/templates/default.conf Целия файл


sitegen/default.include → sitegen/templates/default.include Целия файл


sitegen/docker.http.conf → sitegen/templates/docker.http.conf Целия файл


sitegen/docker.http.include → sitegen/templates/docker.http.include Целия файл


sitegen/docker.https.conf → sitegen/templates/docker.https.conf Целия файл


sitegen/docker.https.include → sitegen/templates/docker.https.include Целия файл


sitegen/http.conf → sitegen/templates/http.conf Целия файл


sitegen/http.include → sitegen/templates/http.include Целия файл


sitegen/https.conf → sitegen/templates/https.conf Целия файл


sitegen/https.include → sitegen/templates/https.include Целия файл


sitegen/laravel.http.conf → sitegen/templates/laravel.http.conf Целия файл


sitegen/laravel.http.include → sitegen/templates/laravel.http.include Целия файл


sitegen/laravel.https.conf → sitegen/templates/laravel.https.conf Целия файл


sitegen/laravel.https.include → sitegen/templates/laravel.https.include Целия файл


sitegen/reverse.http.conf → sitegen/templates/reverse.http.conf Целия файл


sitegen/reverse.http.include → sitegen/templates/reverse.http.include Целия файл


sitegen/reverse.https.conf → sitegen/templates/reverse.https.conf Целия файл


sitegen/reverse.https.include → sitegen/templates/reverse.https.include Целия файл


Loading…
Отказ
Запис