]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/base.py
ipfire.org-webapp: Wrap everything into asyncio.run()
[ipfire.org.git] / src / backend / base.py
CommitLineData
a6dc0bad
MT
1#!/usr/bin/python
2
c5ddbd67 3import configparser
11347e46 4import io
7b05edde 5import location
59468469
MT
6import ssl
7import tempfile
23f84bbc 8import tornado.httpclient
11347e46
MT
9
10from . import accounts
d6c41da2 11from . import asterisk
05e8c602 12from . import blog
26ccb61a 13from . import bugzilla
d73bba54 14from . import campaigns
11347e46 15from . import database
11347e46 16from . import fireinfo
34999287 17from . import httpclient
11347e46 18from . import iuse
e28b082e 19from . import memcached
d6df53bf 20from . import messages
11347e46
MT
21from . import mirrors
22from . import netboot
23from . import nopaste
372ef119 24from . import ratelimit
11347e46 25from . import releases
440aba92 26from . import resolver
11347e46 27from . import settings
66d4b1c1 28from . import toots
440aba92 29from . import util
181d08f3 30from . import wiki
2c361abc 31from . import zeiterfassung
d6df53bf 32from .decorators import *
2c361abc 33
11347e46 34DEFAULT_CONFIG = io.StringIO("""
9ed02e3b
MT
35[global]
36debug = false
37
38data_dir =
39static_dir = %(data_dir)s/static
40templates_dir = %(data_dir)s/templates
41""")
42
a6dc0bad 43class 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)
d6c41da2 65 self.asterisk = asterisk.Asterisk(self)
26ccb61a 66 self.bugzilla = bugzilla.Bugzilla(self)
66862195 67 self.fireinfo = fireinfo.Fireinfo(self)
9068dba1
MT
68 self.iuse = iuse.IUse(self)
69 self.mirrors = mirrors.Mirrors(self)
70 self.netboot = netboot.NetBoot(self)
66862195 71 self.nopaste = nopaste.Nopaste(self)
9068dba1 72 self.releases = releases.Releases(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 148 "test-ldap" : self.accounts.test_ldap,
66d4b1c1 149 "toot" : self.toots.toot,
aee57270 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 196 @lazy_property
66d4b1c1
MT
197 def toots(self):
198 return toots.Toots(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()