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