]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
location: Disable caching for the index page
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import configparser
4 import io
5 import ssl
6 import tempfile
7 import tornado.httpclient
8
9 from . import accounts
10 from . import blog
11 from . import campaigns
12 from . import database
13 from . import fireinfo
14 from . import iuse
15 from . import memcached
16 from . import messages
17 from . import mirrors
18 from . import netboot
19 from . import nopaste
20 from . import ratelimit
21 from . import releases
22 from . import resolver
23 from . import settings
24 from . import talk
25 from . import tweets
26 from . import util
27 from . import wiki
28 from . import zeiterfassung
29 from .decorators import *
30
31 DEFAULT_CONFIG = io.StringIO("""
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
40 class Backend(object):
41 def __init__(self, configfile, debug=False):
42 # Read configuration file.
43 self.config = self.read_config(configfile)
44
45 # Enable debug logging if configured
46 self.debug = debug or self.config.getboolean("global", "debug")
47
48 # Setup database.
49 self.setup_database()
50
51 # Create HTTPClient
52 self.http_client = tornado.httpclient.AsyncHTTPClient(
53 defaults = {
54 "User-Agent" : "IPFireWebApp",
55 }
56 )
57 # Initialize settings first.
58 self.settings = settings.Settings(self)
59 self.memcache = memcached.Memcached(self)
60
61 # Initialize backend modules.
62 self.accounts = accounts.Accounts(self)
63 self.fireinfo = fireinfo.Fireinfo(self)
64 self.iuse = iuse.IUse(self)
65 self.mirrors = mirrors.Mirrors(self)
66 self.netboot = netboot.NetBoot(self)
67 self.nopaste = nopaste.Nopaste(self)
68 self.releases = releases.Releases(self)
69 self.talk = talk.Talk(self)
70
71 self.blog = blog.Blog(self)
72 self.wiki = wiki.Wiki(self)
73 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
74
75 def read_config(self, configfile):
76 cp = configparser.ConfigParser()
77
78 # Initialize configuration with some sensible defaults
79 cp.readfp(DEFAULT_CONFIG)
80
81 # Parse file
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)
98
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
132 async def run_task(self, task, *args, **kwargs):
133 tasks = {
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,
138 "get-all-emails" : self.accounts.get_all_emails,
139 "launch-campaigns" : self.campaigns.launch_manually,
140 "load-certificate" : self.load_certificate,
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,
145 "test-ldap" : self.accounts.test_ldap,
146 "tweet" : self.tweets.tweet,
147 "update-blog-feeds" : self.blog.update_feeds,
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
156 r = await func(*args, **kwargs)
157
158 # If any error code has been returned,
159 # we will end the program
160 if r:
161 raise SystemExit(r)
162
163 @lazy_property
164 def campaigns(self):
165 return campaigns.Campaigns(self)
166
167 @lazy_property
168 def groups(self):
169 return accounts.Groups(self)
170
171 @lazy_property
172 def messages(self):
173 return messages.Messages(self)
174
175 @lazy_property
176 def ratelimiter(self):
177 return ratelimit.RateLimiter(self)
178
179 @lazy_property
180 def resolver(self):
181 return resolver.Resolver(tries=2, timeout=2, domains=[])
182
183 @lazy_property
184 def tweets(self):
185 return tweets.Tweets(self)
186
187 async def cleanup(self):
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()