]>
Commit | Line | Data |
---|---|---|
a6dc0bad MT |
1 | #!/usr/bin/python |
2 | ||
c5ddbd67 | 3 | import configparser |
11347e46 | 4 | import io |
7b05edde | 5 | import location |
59468469 MT |
6 | import ssl |
7 | import tempfile | |
23f84bbc | 8 | import tornado.httpclient |
11347e46 MT |
9 | |
10 | from . import accounts | |
05e8c602 | 11 | from . import blog |
d73bba54 | 12 | from . import campaigns |
11347e46 | 13 | from . import database |
11347e46 MT |
14 | from . import fireinfo |
15 | from . import iuse | |
e28b082e | 16 | from . import memcached |
d6df53bf | 17 | from . import messages |
11347e46 MT |
18 | from . import mirrors |
19 | from . import netboot | |
20 | from . import nopaste | |
372ef119 | 21 | from . import ratelimit |
11347e46 | 22 | from . import releases |
440aba92 | 23 | from . import resolver |
11347e46 MT |
24 | from . import settings |
25 | from . import talk | |
611adbfb | 26 | from . import tweets |
440aba92 | 27 | from . import util |
181d08f3 | 28 | from . import wiki |
2c361abc | 29 | from . import zeiterfassung |
d6df53bf | 30 | from .decorators import * |
2c361abc | 31 | |
11347e46 | 32 | DEFAULT_CONFIG = io.StringIO(""" |
9ed02e3b MT |
33 | [global] |
34 | debug = false | |
35 | ||
36 | data_dir = | |
37 | static_dir = %(data_dir)s/static | |
38 | templates_dir = %(data_dir)s/templates | |
39 | """) | |
40 | ||
a6dc0bad | 41 | class Backend(object): |
2cd9af74 | 42 | def __init__(self, configfile, debug=False): |
9068dba1 MT |
43 | # Read configuration file. |
44 | self.config = self.read_config(configfile) | |
9ed02e3b MT |
45 | |
46 | # Enable debug logging if configured | |
47 | self.debug = debug or self.config.getboolean("global", "debug") | |
9068dba1 MT |
48 | |
49 | # Setup database. | |
50 | self.setup_database() | |
a6dc0bad | 51 | |
23f84bbc MT |
52 | # Create HTTPClient |
53 | self.http_client = tornado.httpclient.AsyncHTTPClient( | |
54 | defaults = { | |
55 | "User-Agent" : "IPFireWebApp", | |
56 | } | |
57 | ) | |
9068dba1 MT |
58 | # Initialize settings first. |
59 | self.settings = settings.Settings(self) | |
e28b082e | 60 | self.memcache = memcached.Memcached(self) |
9068dba1 MT |
61 | |
62 | # Initialize backend modules. | |
a6dc0bad | 63 | self.accounts = accounts.Accounts(self) |
66862195 | 64 | self.fireinfo = fireinfo.Fireinfo(self) |
9068dba1 MT |
65 | self.iuse = iuse.IUse(self) |
66 | self.mirrors = mirrors.Mirrors(self) | |
67 | self.netboot = netboot.NetBoot(self) | |
66862195 | 68 | self.nopaste = nopaste.Nopaste(self) |
9068dba1 | 69 | self.releases = releases.Releases(self) |
66862195 | 70 | self.talk = talk.Talk(self) |
9068dba1 | 71 | |
0a6875dc | 72 | self.blog = blog.Blog(self) |
181d08f3 | 73 | self.wiki = wiki.Wiki(self) |
2c361abc MT |
74 | self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self) |
75 | ||
9068dba1 MT |
76 | def read_config(self, configfile): |
77 | cp = configparser.ConfigParser() | |
9ed02e3b MT |
78 | |
79 | # Initialize configuration with some sensible defaults | |
80 | cp.readfp(DEFAULT_CONFIG) | |
81 | ||
82 | # Parse file | |
9068dba1 MT |
83 | cp.read(configfile) |
84 | ||
85 | return cp | |
86 | ||
87 | def setup_database(self): | |
88 | """ | |
89 | Sets up the database connection. | |
90 | """ | |
91 | credentials = { | |
92 | "host" : self.config.get("database", "server"), | |
93 | "database" : self.config.get("database", "database"), | |
94 | "user" : self.config.get("database", "username"), | |
95 | "password" : self.config.get("database", "password"), | |
96 | } | |
97 | ||
98 | self.db = database.Connection(**credentials) | |
c5ddbd67 | 99 | |
59468469 MT |
100 | @lazy_property |
101 | def ssl_context(self): | |
102 | # Create SSL context | |
103 | context = ssl.create_default_context() | |
104 | ||
105 | # Fetch client certificate | |
106 | certificate = self.settings.get("client-certificate", None) | |
107 | key = self.settings.get("client-key", None) | |
108 | ||
109 | # Apply client certificate | |
110 | if certificate and key: | |
111 | with tempfile.NamedTemporaryFile(mode="w") as f_cert: | |
112 | f_cert.write(certificate) | |
113 | f_cert.flush() | |
114 | ||
115 | with tempfile.NamedTemporaryFile(mode="w") as f_key: | |
116 | f_key.write(key) | |
117 | f_key.flush() | |
118 | ||
119 | context.load_cert_chain(f_cert.name, f_key.name) | |
120 | ||
121 | return context | |
122 | ||
123 | async def load_certificate(self, certfile, keyfile): | |
124 | with self.db.transaction(): | |
125 | # Load certificate | |
126 | with open(certfile) as f: | |
127 | self.settings.set("client-certificate", f.read()) | |
128 | ||
129 | # Load key file | |
130 | with open(keyfile) as f: | |
131 | self.settings.set("client-key", f.read()) | |
132 | ||
9fdf4fb7 | 133 | async def run_task(self, task, *args, **kwargs): |
c5ddbd67 | 134 | tasks = { |
aee57270 MT |
135 | "announce-blog-posts" : self.blog.announce, |
136 | "check-mirrors" : self.mirrors.check_all, | |
137 | "check-spam" : self.accounts.check_spam, | |
138 | "cleanup" : self.cleanup, | |
5bfc6729 | 139 | "get-all-emails" : self.accounts.get_all_emails, |
aee57270 | 140 | "launch-campaigns" : self.campaigns.launch_manually, |
59468469 | 141 | "load-certificate" : self.load_certificate, |
aee57270 MT |
142 | "run-campaigns" : self.campaigns.run, |
143 | "scan-files" : self.releases.scan_files, | |
144 | "send-message" : self.messages.send_cli, | |
145 | "send-all-messages" : self.messages.queue.send_all, | |
aee57270 MT |
146 | "test-ldap" : self.accounts.test_ldap, |
147 | "tweet" : self.tweets.tweet, | |
148 | "update-blog-feeds" : self.blog.update_feeds, | |
c5ddbd67 MT |
149 | } |
150 | ||
151 | # Get the task from the list of all tasks | |
152 | func = tasks.get(task, None) | |
153 | if not func: | |
154 | raise ValueError("Unknown task: %s" % task) | |
155 | ||
156 | # Run the task | |
9fdf4fb7 | 157 | r = await func(*args, **kwargs) |
c5ddbd67 MT |
158 | |
159 | # If any error code has been returned, | |
160 | # we will end the program | |
161 | if r: | |
162 | raise SystemExit(r) | |
d6df53bf | 163 | |
d73bba54 MT |
164 | @lazy_property |
165 | def campaigns(self): | |
166 | return campaigns.Campaigns(self) | |
167 | ||
d8b04c72 MT |
168 | @lazy_property |
169 | def groups(self): | |
170 | return accounts.Groups(self) | |
171 | ||
d6df53bf MT |
172 | @lazy_property |
173 | def messages(self): | |
174 | return messages.Messages(self) | |
611adbfb | 175 | |
7b05edde MT |
176 | @lazy_property |
177 | def location(self): | |
178 | return location.Database("/var/lib/location/database.db") | |
179 | ||
372ef119 MT |
180 | @lazy_property |
181 | def ratelimiter(self): | |
182 | return ratelimit.RateLimiter(self) | |
183 | ||
440aba92 MT |
184 | @lazy_property |
185 | def resolver(self): | |
186 | return resolver.Resolver(tries=2, timeout=2, domains=[]) | |
187 | ||
611adbfb MT |
188 | @lazy_property |
189 | def tweets(self): | |
190 | return tweets.Tweets(self) | |
8e69850a | 191 | |
9fdf4fb7 | 192 | async def cleanup(self): |
8e69850a MT |
193 | # Cleanup message queue |
194 | with self.db.transaction(): | |
195 | self.messages.queue.cleanup() | |
196 | ||
197 | # Cleanup in accounts | |
198 | with self.db.transaction(): | |
199 | self.accounts.cleanup() |