]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
cfe7a78fdc4c2bdc014fd86ed42a20cd3fab7636
10 import tornado
.httpclient
12 from . import accounts
13 from . import asterisk
14 from . import analytics
16 from . import bugzilla
18 from . import campaigns
19 from . import database
20 from . import fireinfo
21 from . import httpclient
23 from . import messages
27 from . import ratelimit
28 from . import releases
29 from . import resolver
30 from . import settings
34 from . import zeiterfassung
35 from .decorators
import *
37 DEFAULT_CONFIG
= io
.StringIO("""
43 static_dir = %(data_dir)s/static
44 templates_dir = %(data_dir)s/templates
48 log
= logging
.getLogger(__name__
)
50 class Backend(object):
53 def __init__(self
, configfile
, debug
=False):
54 # Read configuration file.
55 self
.config
= self
.read_config(configfile
)
57 # Enable debug logging if configured
58 self
.debug
= debug
or self
.config
.getboolean("global", "debug")
64 self
.http_client
= httpclient
.HTTPClient(self
)
66 # Initialize the cache
67 self
.cache
= cache
.Cache(self
)
69 # Initialize settings first
70 self
.settings
= settings
.Settings(self
)
72 # Initialize backend modules.
73 self
.accounts
= accounts
.Accounts(self
)
74 self
.analytics
= analytics
.Analytics(self
)
75 self
.bugzilla
= bugzilla
.Bugzilla(self
)
76 self
.fireinfo
= fireinfo
.Fireinfo(self
)
77 self
.iuse
= iuse
.IUse(self
)
78 self
.mirrors
= mirrors
.Mirrors(self
)
79 self
.netboot
= netboot
.NetBoot(self
)
80 self
.nopaste
= nopaste
.Nopaste(self
)
81 self
.releases
= releases
.Releases(self
)
83 self
.blog
= blog
.Blog(self
)
84 self
.wiki
= wiki
.Wiki(self
)
85 self
.zeiterfassung
= zeiterfassung
.ZeiterfassungClient(self
)
87 def read_config(self
, configfile
):
88 cp
= configparser
.ConfigParser()
90 # Initialize configuration with some sensible defaults
91 cp
.readfp(DEFAULT_CONFIG
)
99 def environment(self
):
101 Returns whether this is running in "production" or "testing"
103 # Fetch from the environment
105 return os
.environ
["ENVIRONMENT"]
109 # Fall back to the configuration
110 return self
.config
.get("global", "environment")
112 def setup_database(self
):
114 Sets up the database connection.
117 "host" : self
.config
.get("database", "server"),
118 "database" : self
.config
.get("database", "database"),
119 "user" : self
.config
.get("database", "username"),
120 "password" : self
.config
.get("database", "password"),
123 self
.db
= database
.Connection(self
, **credentials
)
126 def ssl_context(self
):
128 context
= ssl
.create_default_context()
130 # Fetch client certificate
131 certificate
= self
.settings
.get("client-certificate", None)
132 key
= self
.settings
.get("client-key", None)
134 # Apply client certificate
135 if certificate
and key
:
136 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
137 f_cert
.write(certificate
)
140 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
144 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
148 async def load_certificate(self
, certfile
, keyfile
):
149 with self
.db
.transaction():
151 with
open(certfile
) as f
:
152 self
.settings
.set("client-certificate", f
.read())
155 with
open(keyfile
) as f
:
156 self
.settings
.set("client-key", f
.read())
158 async def run_task(self
, task
, *args
, **kwargs
):
160 "accounts:delete" : self
.accounts
._delete
,
161 "announce-blog-posts" : self
.blog
.announce
,
162 "campaigns:donate" : self
.campaigns
.donate
,
163 "campaigns:send" : self
.campaigns
.send
,
164 "check-mirrors" : self
.mirrors
.check_all
,
165 "cleanup" : self
.cleanup
,
166 "get-all-emails" : self
.accounts
.get_all_emails
,
167 "load-certificate" : self
.load_certificate
,
168 "scan-files" : self
.releases
.scan_files
,
169 "send-message" : self
.messages
.send_cli
,
170 "send-all-messages" : self
.messages
.queue
.send_all
,
171 "test-ldap" : self
.accounts
.test_ldap
,
172 "toot" : self
.toots
.toot
,
173 "update-blog-feeds" : self
.blog
.update_feeds
,
176 # Get the task from the list of all tasks
177 func
= tasks
.get(task
, None)
179 raise ValueError("Unknown task: %s" % task
)
181 # Check if we are running in production
182 if not self
.environment
== "production":
183 log
.warning("Refusing to run task '%s' in '%s' environment" % (task
, self
.environment
))
187 with self
.db
.transaction():
188 r
= await func(*args
, **kwargs
)
190 # If any error code has been returned,
191 # we will end the program
197 return asterisk
.Asterisk(self
)
201 return campaigns
.Campaigns(self
)
205 return accounts
.Groups(self
)
209 return messages
.Messages(self
)
213 return location
.Database("/var/lib/location/database.db")
215 def get_country_name(self
, country_code
):
217 country
= self
.location
.get_country(country_code
)
219 # In case the country code was invalid, we return it again
229 def ratelimiter(self
):
230 return ratelimit
.RateLimiter(self
)
234 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
238 return toots
.Toots(self
)
240 async def cleanup(self
):
241 # Cleanup message queue
242 with self
.db
.transaction():
243 self
.messages
.queue
.cleanup()
245 # Cleanup in accounts
246 with self
.db
.transaction():
247 self
.accounts
.cleanup()
250 with self
.db
.transaction():
251 self
.nopaste
.cleanup()