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