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