]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
6ac600d17b606981be3a27dbe11015daa0454d25
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import configparser
4 import io
5 import location
6 import ssl
7 import tempfile
8 import tornado.httpclient
9
10 from . import accounts
11 from . import blog
12 from . import bugzilla
13 from . import campaigns
14 from . import database
15 from . import fireinfo
16 from . import httpclient
17 from . import iuse
18 from . import memcached
19 from . import messages
20 from . import mirrors
21 from . import netboot
22 from . import nopaste
23 from . import ratelimit
24 from . import releases
25 from . import resolver
26 from . import settings
27 from . import talk
28 from . import toots
29 from . import util
30 from . import wiki
31 from . import zeiterfassung
32 from .decorators import *
33
34 DEFAULT_CONFIG = io.StringIO("""
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
43 class Backend(object):
44 version = 0
45
46 def __init__(self, configfile, debug=False):
47 # Read configuration file.
48 self.config = self.read_config(configfile)
49
50 # Enable debug logging if configured
51 self.debug = debug or self.config.getboolean("global", "debug")
52
53 # Setup database.
54 self.setup_database()
55
56 # Create HTTPClient
57 self.http_client = httpclient.HTTPClient(self)
58
59 # Initialize settings first.
60 self.settings = settings.Settings(self)
61 self.memcache = memcached.Memcached(self)
62
63 # Initialize backend modules.
64 self.accounts = accounts.Accounts(self)
65 self.bugzilla = bugzilla.Bugzilla(self)
66 self.fireinfo = fireinfo.Fireinfo(self)
67 self.iuse = iuse.IUse(self)
68 self.mirrors = mirrors.Mirrors(self)
69 self.netboot = netboot.NetBoot(self)
70 self.nopaste = nopaste.Nopaste(self)
71 self.releases = releases.Releases(self)
72 self.talk = talk.Talk(self)
73
74 self.blog = blog.Blog(self)
75 self.wiki = wiki.Wiki(self)
76 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
77
78 def read_config(self, configfile):
79 cp = configparser.ConfigParser()
80
81 # Initialize configuration with some sensible defaults
82 cp.readfp(DEFAULT_CONFIG)
83
84 # Parse file
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)
101
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
135 async def run_task(self, task, *args, **kwargs):
136 tasks = {
137 "accounts:delete" : self.accounts._delete,
138 "announce-blog-posts" : self.blog.announce,
139 "check-mirrors" : self.mirrors.check_all,
140 "cleanup" : self.cleanup,
141 "get-all-emails" : self.accounts.get_all_emails,
142 "launch-campaigns" : self.campaigns.launch_manually,
143 "load-certificate" : self.load_certificate,
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,
148 "test-ldap" : self.accounts.test_ldap,
149 "toot" : self.toots.toot,
150 "update-blog-feeds" : self.blog.update_feeds,
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
159 r = await func(*args, **kwargs)
160
161 # If any error code has been returned,
162 # we will end the program
163 if r:
164 raise SystemExit(r)
165
166 @lazy_property
167 def campaigns(self):
168 return campaigns.Campaigns(self)
169
170 @lazy_property
171 def groups(self):
172 return accounts.Groups(self)
173
174 @lazy_property
175 def messages(self):
176 return messages.Messages(self)
177
178 @lazy_property
179 def location(self):
180 return location.Database("/var/lib/location/database.db")
181
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
188 @lazy_property
189 def ratelimiter(self):
190 return ratelimit.RateLimiter(self)
191
192 @lazy_property
193 def resolver(self):
194 return resolver.Resolver(tries=2, timeout=2, domains=[])
195
196 @lazy_property
197 def toots(self):
198 return toots.Toots(self)
199
200 async def cleanup(self):
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()