]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
6ac600d17b606981be3a27dbe11015daa0454d25
8 import tornado
.httpclient
10 from . import accounts
12 from . import bugzilla
13 from . import campaigns
14 from . import database
15 from . import fireinfo
16 from . import httpclient
18 from . import memcached
19 from . import messages
23 from . import ratelimit
24 from . import releases
25 from . import resolver
26 from . import settings
31 from . import zeiterfassung
32 from .decorators
import *
34 DEFAULT_CONFIG
= io
.StringIO("""
39 static_dir = %(data_dir)s/static
40 templates_dir = %(data_dir)s/templates
43 class Backend(object):
46 def __init__(self
, configfile
, debug
=False):
47 # Read configuration file.
48 self
.config
= self
.read_config(configfile
)
50 # Enable debug logging if configured
51 self
.debug
= debug
or self
.config
.getboolean("global", "debug")
57 self
.http_client
= httpclient
.HTTPClient(self
)
59 # Initialize settings first.
60 self
.settings
= settings
.Settings(self
)
61 self
.memcache
= memcached
.Memcached(self
)
63 # Initialize backend modules.
64 self
.accounts
= accounts
.Accounts(self
)
65 self
.bugzilla
= bugzilla
.Bugzilla(self
)
66 self
.fireinfo
= fireinfo
.Fireinfo(self
)
67 self
.iuse
= iuse
.IUse(self
)
68 self
.mirrors
= mirrors
.Mirrors(self
)
69 self
.netboot
= netboot
.NetBoot(self
)
70 self
.nopaste
= nopaste
.Nopaste(self
)
71 self
.releases
= releases
.Releases(self
)
72 self
.talk
= talk
.Talk(self
)
74 self
.blog
= blog
.Blog(self
)
75 self
.wiki
= wiki
.Wiki(self
)
76 self
.zeiterfassung
= zeiterfassung
.ZeiterfassungClient(self
)
78 def read_config(self
, configfile
):
79 cp
= configparser
.ConfigParser()
81 # Initialize configuration with some sensible defaults
82 cp
.readfp(DEFAULT_CONFIG
)
89 def setup_database(self
):
91 Sets up the database connection.
94 "host" : self
.config
.get("database", "server"),
95 "database" : self
.config
.get("database", "database"),
96 "user" : self
.config
.get("database", "username"),
97 "password" : self
.config
.get("database", "password"),
100 self
.db
= database
.Connection(**credentials
)
103 def ssl_context(self
):
105 context
= ssl
.create_default_context()
107 # Fetch client certificate
108 certificate
= self
.settings
.get("client-certificate", None)
109 key
= self
.settings
.get("client-key", None)
111 # Apply client certificate
112 if certificate
and key
:
113 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
114 f_cert
.write(certificate
)
117 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
121 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
125 async def load_certificate(self
, certfile
, keyfile
):
126 with self
.db
.transaction():
128 with
open(certfile
) as f
:
129 self
.settings
.set("client-certificate", f
.read())
132 with
open(keyfile
) as f
:
133 self
.settings
.set("client-key", f
.read())
135 async def run_task(self
, task
, *args
, **kwargs
):
137 "accounts:delete" : self
.accounts
._delete
,
138 "announce-blog-posts" : self
.blog
.announce
,
139 "check-mirrors" : self
.mirrors
.check_all
,
140 "cleanup" : self
.cleanup
,
141 "get-all-emails" : self
.accounts
.get_all_emails
,
142 "launch-campaigns" : self
.campaigns
.launch_manually
,
143 "load-certificate" : self
.load_certificate
,
144 "run-campaigns" : self
.campaigns
.run
,
145 "scan-files" : self
.releases
.scan_files
,
146 "send-message" : self
.messages
.send_cli
,
147 "send-all-messages" : self
.messages
.queue
.send_all
,
148 "test-ldap" : self
.accounts
.test_ldap
,
149 "toot" : self
.toots
.toot
,
150 "update-blog-feeds" : self
.blog
.update_feeds
,
153 # Get the task from the list of all tasks
154 func
= tasks
.get(task
, None)
156 raise ValueError("Unknown task: %s" % task
)
159 r
= await func(*args
, **kwargs
)
161 # If any error code has been returned,
162 # we will end the program
168 return campaigns
.Campaigns(self
)
172 return accounts
.Groups(self
)
176 return messages
.Messages(self
)
180 return location
.Database("/var/lib/location/database.db")
182 def get_country_name(self
, country_code
):
183 country
= self
.location
.get_country(country_code
)
189 def ratelimiter(self
):
190 return ratelimit
.RateLimiter(self
)
194 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
198 return toots
.Toots(self
)
200 async def cleanup(self
):
201 # Cleanup message queue
202 with self
.db
.transaction():
203 self
.messages
.queue
.cleanup()
205 # Cleanup in accounts
206 with self
.db
.transaction():
207 self
.accounts
.cleanup()