]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/base.py
analytics: Preset bot variable
[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
28e09035 12from . import analytics
05e8c602 13from . import blog
26ccb61a 14from . import bugzilla
f0a8b392 15from . import cache
d73bba54 16from . import campaigns
11347e46 17from . import database
11347e46 18from . import fireinfo
34999287 19from . import httpclient
11347e46 20from . import iuse
97e15cf6 21from . import lists
d6df53bf 22from . import messages
11347e46
MT
23from . import mirrors
24from . import netboot
25from . import nopaste
372ef119 26from . import ratelimit
11347e46 27from . import releases
440aba92 28from . import resolver
11347e46 29from . import settings
66d4b1c1 30from . import toots
440aba92 31from . import util
181d08f3 32from . import wiki
2c361abc 33from . import zeiterfassung
d6df53bf 34from .decorators import *
2c361abc 35
11347e46 36DEFAULT_CONFIG = io.StringIO("""
9ed02e3b
MT
37[global]
38debug = false
39
40data_dir =
41static_dir = %(data_dir)s/static
42templates_dir = %(data_dir)s/templates
43""")
44
a6dc0bad 45class Backend(object):
34999287
MT
46 version = 0
47
2cd9af74 48 def __init__(self, configfile, debug=False):
9068dba1
MT
49 # Read configuration file.
50 self.config = self.read_config(configfile)
9ed02e3b
MT
51
52 # Enable debug logging if configured
53 self.debug = debug or self.config.getboolean("global", "debug")
9068dba1
MT
54
55 # Setup database.
56 self.setup_database()
a6dc0bad 57
23f84bbc 58 # Create HTTPClient
34999287
MT
59 self.http_client = httpclient.HTTPClient(self)
60
f0a8b392
MT
61 # Initialize the cache
62 self.cache = cache.Cache(self)
63
4f043f79 64 # Initialize settings first
9068dba1 65 self.settings = settings.Settings(self)
9068dba1
MT
66
67 # Initialize backend modules.
a6dc0bad 68 self.accounts = accounts.Accounts(self)
28e09035 69 self.analytics = analytics.Analytics(self)
26ccb61a 70 self.bugzilla = bugzilla.Bugzilla(self)
66862195 71 self.fireinfo = fireinfo.Fireinfo(self)
9068dba1
MT
72 self.iuse = iuse.IUse(self)
73 self.mirrors = mirrors.Mirrors(self)
74 self.netboot = netboot.NetBoot(self)
66862195 75 self.nopaste = nopaste.Nopaste(self)
9068dba1 76 self.releases = releases.Releases(self)
9068dba1 77
0a6875dc 78 self.blog = blog.Blog(self)
181d08f3 79 self.wiki = wiki.Wiki(self)
2c361abc
MT
80 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
81
9068dba1
MT
82 def read_config(self, configfile):
83 cp = configparser.ConfigParser()
9ed02e3b
MT
84
85 # Initialize configuration with some sensible defaults
86 cp.readfp(DEFAULT_CONFIG)
87
88 # Parse file
9068dba1
MT
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
3082f0e9 104 self.db = database.Connection(self, **credentials)
c5ddbd67 105
59468469
MT
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
9fdf4fb7 139 async def run_task(self, task, *args, **kwargs):
c5ddbd67 140 tasks = {
26ccb61a 141 "accounts:delete" : self.accounts._delete,
aee57270
MT
142 "announce-blog-posts" : self.blog.announce,
143 "check-mirrors" : self.mirrors.check_all,
aee57270 144 "cleanup" : self.cleanup,
5bfc6729 145 "get-all-emails" : self.accounts.get_all_emails,
aee57270 146 "launch-campaigns" : self.campaigns.launch_manually,
59468469 147 "load-certificate" : self.load_certificate,
aee57270
MT
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,
aee57270 152 "test-ldap" : self.accounts.test_ldap,
66d4b1c1 153 "toot" : self.toots.toot,
aee57270 154 "update-blog-feeds" : self.blog.update_feeds,
c5ddbd67
MT
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
9fdf4fb7 163 r = await func(*args, **kwargs)
c5ddbd67
MT
164
165 # If any error code has been returned,
166 # we will end the program
167 if r:
168 raise SystemExit(r)
d6df53bf 169
19f8de1b
MT
170 @lazy_property
171 def asterisk(self):
172 return asterisk.Asterisk(self)
173
d73bba54
MT
174 @lazy_property
175 def campaigns(self):
176 return campaigns.Campaigns(self)
177
d8b04c72
MT
178 @lazy_property
179 def groups(self):
180 return accounts.Groups(self)
181
97e15cf6
MT
182 @lazy_property
183 def lists(self):
184 return lists.Lists(self)
185
d6df53bf
MT
186 @lazy_property
187 def messages(self):
188 return messages.Messages(self)
611adbfb 189
7b05edde
MT
190 @lazy_property
191 def location(self):
192 return location.Database("/var/lib/location/database.db")
193
e929ed92
MT
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
372ef119
MT
200 @lazy_property
201 def ratelimiter(self):
202 return ratelimit.RateLimiter(self)
203
440aba92
MT
204 @lazy_property
205 def resolver(self):
206 return resolver.Resolver(tries=2, timeout=2, domains=[])
207
611adbfb 208 @lazy_property
66d4b1c1
MT
209 def toots(self):
210 return toots.Toots(self)
8e69850a 211
9fdf4fb7 212 async def cleanup(self):
8e69850a
MT
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()