]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
a3daa358c0ab4eb86489c48d72f74ed41faf9043
9 import tornado
.httpclient
11 from . import accounts
12 from . import asterisk
13 from . import analytics
15 from . import bugzilla
17 from . import campaigns
18 from . import database
19 from . import fireinfo
20 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 return self
.config
.get("global", "environment")
105 def setup_database(self
):
107 Sets up the database connection.
110 "host" : self
.config
.get("database", "server"),
111 "database" : self
.config
.get("database", "database"),
112 "user" : self
.config
.get("database", "username"),
113 "password" : self
.config
.get("database", "password"),
116 self
.db
= database
.Connection(self
, **credentials
)
119 def ssl_context(self
):
121 context
= ssl
.create_default_context()
123 # Fetch client certificate
124 certificate
= self
.settings
.get("client-certificate", None)
125 key
= self
.settings
.get("client-key", None)
127 # Apply client certificate
128 if certificate
and key
:
129 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
130 f_cert
.write(certificate
)
133 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
137 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
141 async def load_certificate(self
, certfile
, keyfile
):
142 with self
.db
.transaction():
144 with
open(certfile
) as f
:
145 self
.settings
.set("client-certificate", f
.read())
148 with
open(keyfile
) as f
:
149 self
.settings
.set("client-key", f
.read())
151 async def run_task(self
, task
, *args
, **kwargs
):
153 "accounts:delete" : self
.accounts
._delete
,
154 "announce-blog-posts" : self
.blog
.announce
,
155 "check-mirrors" : self
.mirrors
.check_all
,
156 "cleanup" : self
.cleanup
,
157 "get-all-emails" : self
.accounts
.get_all_emails
,
158 "launch-campaigns" : self
.campaigns
.launch_manually
,
159 "load-certificate" : self
.load_certificate
,
160 "run-campaigns" : self
.campaigns
.run
,
161 "scan-files" : self
.releases
.scan_files
,
162 "send-message" : self
.messages
.send_cli
,
163 "send-all-messages" : self
.messages
.queue
.send_all
,
164 "test-ldap" : self
.accounts
.test_ldap
,
165 "toot" : self
.toots
.toot
,
166 "update-blog-feeds" : self
.blog
.update_feeds
,
169 # Get the task from the list of all tasks
170 func
= tasks
.get(task
, None)
172 raise ValueError("Unknown task: %s" % task
)
174 # Check if we are running in production
175 if not self
.environment
== "production":
176 log
.warning("Refusing to run task '%s' in '%s' environment" % (task
, self
.environment
))
180 r
= await func(*args
, **kwargs
)
182 # If any error code has been returned,
183 # we will end the program
189 return asterisk
.Asterisk(self
)
193 return campaigns
.Campaigns(self
)
197 return accounts
.Groups(self
)
201 return lists
.Lists(self
)
205 return messages
.Messages(self
)
209 return location
.Database("/var/lib/location/database.db")
211 def get_country_name(self
, country_code
):
213 country
= self
.location
.get_country(country_code
)
215 # In case the country code was invalid, we return it again
225 def ratelimiter(self
):
226 return ratelimit
.RateLimiter(self
)
230 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
234 return toots
.Toots(self
)
236 async def cleanup(self
):
237 # Cleanup message queue
238 with self
.db
.transaction():
239 self
.messages
.queue
.cleanup()
241 # Cleanup in accounts
242 with self
.db
.transaction():
243 self
.accounts
.cleanup()
246 with self
.db
.transaction():
247 self
.nopaste
.cleanup()