]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
f5d267f4a68277dbb42803820c6823d8477c29ef
8 import tornado
.httpclient
10 from . import accounts
11 from . import asterisk
13 from . import bugzilla
15 from . import campaigns
16 from . import database
17 from . import fireinfo
18 from . import httpclient
20 from . import messages
24 from . import ratelimit
25 from . import releases
26 from . import resolver
27 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 the cache
60 self
.cache
= cache
.Cache(self
)
62 # Initialize settings first
63 self
.settings
= settings
.Settings(self
)
65 # Initialize backend modules.
66 self
.accounts
= accounts
.Accounts(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
)
75 self
.blog
= blog
.Blog(self
)
76 self
.wiki
= wiki
.Wiki(self
)
77 self
.zeiterfassung
= zeiterfassung
.ZeiterfassungClient(self
)
79 def read_config(self
, configfile
):
80 cp
= configparser
.ConfigParser()
82 # Initialize configuration with some sensible defaults
83 cp
.readfp(DEFAULT_CONFIG
)
90 def setup_database(self
):
92 Sets up the database connection.
95 "host" : self
.config
.get("database", "server"),
96 "database" : self
.config
.get("database", "database"),
97 "user" : self
.config
.get("database", "username"),
98 "password" : self
.config
.get("database", "password"),
101 self
.db
= database
.Connection(self
, **credentials
)
104 def ssl_context(self
):
106 context
= ssl
.create_default_context()
108 # Fetch client certificate
109 certificate
= self
.settings
.get("client-certificate", None)
110 key
= self
.settings
.get("client-key", None)
112 # Apply client certificate
113 if certificate
and key
:
114 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
115 f_cert
.write(certificate
)
118 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
122 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
126 async def load_certificate(self
, certfile
, keyfile
):
127 with self
.db
.transaction():
129 with
open(certfile
) as f
:
130 self
.settings
.set("client-certificate", f
.read())
133 with
open(keyfile
) as f
:
134 self
.settings
.set("client-key", f
.read())
136 async def run_task(self
, task
, *args
, **kwargs
):
138 "accounts:delete" : self
.accounts
._delete
,
139 "announce-blog-posts" : self
.blog
.announce
,
140 "check-mirrors" : self
.mirrors
.check_all
,
141 "cleanup" : self
.cleanup
,
142 "get-all-emails" : self
.accounts
.get_all_emails
,
143 "launch-campaigns" : self
.campaigns
.launch_manually
,
144 "load-certificate" : self
.load_certificate
,
145 "run-campaigns" : self
.campaigns
.run
,
146 "scan-files" : self
.releases
.scan_files
,
147 "send-message" : self
.messages
.send_cli
,
148 "send-all-messages" : self
.messages
.queue
.send_all
,
149 "test-ldap" : self
.accounts
.test_ldap
,
150 "toot" : self
.toots
.toot
,
151 "update-blog-feeds" : self
.blog
.update_feeds
,
154 # Get the task from the list of all tasks
155 func
= tasks
.get(task
, None)
157 raise ValueError("Unknown task: %s" % task
)
160 r
= await func(*args
, **kwargs
)
162 # If any error code has been returned,
163 # we will end the program
169 return asterisk
.Asterisk(self
)
173 return campaigns
.Campaigns(self
)
177 return accounts
.Groups(self
)
181 return messages
.Messages(self
)
185 return location
.Database("/var/lib/location/database.db")
187 def get_country_name(self
, country_code
):
188 country
= self
.location
.get_country(country_code
)
194 def ratelimiter(self
):
195 return ratelimit
.RateLimiter(self
)
199 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
203 return toots
.Toots(self
)
205 async def cleanup(self
):
206 # Cleanup message queue
207 with self
.db
.transaction():
208 self
.messages
.queue
.cleanup()
210 # Cleanup in accounts
211 with self
.db
.transaction():
212 self
.accounts
.cleanup()