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