]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
8 import tornado
.httpclient
10 from . import accounts
11 from . import asterisk
13 from . import bugzilla
14 from . import campaigns
15 from . import database
16 from . import fireinfo
17 from . import httpclient
19 from . import memcached
20 from . import messages
24 from . import ratelimit
25 from . import releases
26 from . import resolver
27 from . import settings
32 from . import zeiterfassung
33 from .decorators
import *
35 DEFAULT_CONFIG
= io
.StringIO("""
40 static_dir = %(data_dir)s/static
41 templates_dir = %(data_dir)s/templates
44 class Backend(object):
47 def __init__(self
, configfile
, debug
=False):
48 # Read configuration file.
49 self
.config
= self
.read_config(configfile
)
51 # Enable debug logging if configured
52 self
.debug
= debug
or self
.config
.getboolean("global", "debug")
58 self
.http_client
= httpclient
.HTTPClient(self
)
60 # Initialize settings first.
61 self
.settings
= settings
.Settings(self
)
62 self
.memcache
= memcached
.Memcached(self
)
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
)
76 self
.blog
= blog
.Blog(self
)
77 self
.wiki
= wiki
.Wiki(self
)
78 self
.zeiterfassung
= zeiterfassung
.ZeiterfassungClient(self
)
80 def read_config(self
, configfile
):
81 cp
= configparser
.ConfigParser()
83 # Initialize configuration with some sensible defaults
84 cp
.readfp(DEFAULT_CONFIG
)
91 def setup_database(self
):
93 Sets up the database connection.
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"),
102 self
.db
= database
.Connection(**credentials
)
105 def ssl_context(self
):
107 context
= ssl
.create_default_context()
109 # Fetch client certificate
110 certificate
= self
.settings
.get("client-certificate", None)
111 key
= self
.settings
.get("client-key", None)
113 # Apply client certificate
114 if certificate
and key
:
115 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
116 f_cert
.write(certificate
)
119 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
123 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
127 async def load_certificate(self
, certfile
, keyfile
):
128 with self
.db
.transaction():
130 with
open(certfile
) as f
:
131 self
.settings
.set("client-certificate", f
.read())
134 with
open(keyfile
) as f
:
135 self
.settings
.set("client-key", f
.read())
137 async def run_task(self
, task
, *args
, **kwargs
):
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
,
155 # Get the task from the list of all tasks
156 func
= tasks
.get(task
, None)
158 raise ValueError("Unknown task: %s" % task
)
161 r
= await func(*args
, **kwargs
)
163 # If any error code has been returned,
164 # we will end the program
170 return campaigns
.Campaigns(self
)
174 return accounts
.Groups(self
)
178 return messages
.Messages(self
)
182 return location
.Database("/var/lib/location/database.db")
184 def get_country_name(self
, country_code
):
185 country
= self
.location
.get_country(country_code
)
191 def ratelimiter(self
):
192 return ratelimit
.RateLimiter(self
)
196 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
200 return toots
.Toots(self
)
202 async def cleanup(self
):
203 # Cleanup message queue
204 with self
.db
.transaction():
205 self
.messages
.queue
.cleanup()
207 # Cleanup in accounts
208 with self
.db
.transaction():
209 self
.accounts
.cleanup()