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