]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
7 import tornado
.httpclient
11 from . import campaigns
12 from . import database
13 from . import fireinfo
15 from . import memcached
16 from . import messages
20 from . import ratelimit
21 from . import releases
22 from . import resolver
23 from . import settings
28 from . import zeiterfassung
29 from .decorators
import *
31 DEFAULT_CONFIG
= io
.StringIO("""
36 static_dir = %(data_dir)s/static
37 templates_dir = %(data_dir)s/templates
40 class Backend(object):
41 def __init__(self
, configfile
, debug
=False):
42 # Read configuration file.
43 self
.config
= self
.read_config(configfile
)
45 # Enable debug logging if configured
46 self
.debug
= debug
or self
.config
.getboolean("global", "debug")
52 self
.http_client
= tornado
.httpclient
.AsyncHTTPClient(
54 "User-Agent" : "IPFireWebApp",
57 # Initialize settings first.
58 self
.settings
= settings
.Settings(self
)
59 self
.memcache
= memcached
.Memcached(self
)
61 # Initialize backend modules.
62 self
.accounts
= accounts
.Accounts(self
)
63 self
.fireinfo
= fireinfo
.Fireinfo(self
)
64 self
.iuse
= iuse
.IUse(self
)
65 self
.mirrors
= mirrors
.Mirrors(self
)
66 self
.netboot
= netboot
.NetBoot(self
)
67 self
.nopaste
= nopaste
.Nopaste(self
)
68 self
.releases
= releases
.Releases(self
)
69 self
.talk
= talk
.Talk(self
)
71 self
.blog
= blog
.Blog(self
)
72 self
.wiki
= wiki
.Wiki(self
)
73 self
.zeiterfassung
= zeiterfassung
.ZeiterfassungClient(self
)
75 def read_config(self
, configfile
):
76 cp
= configparser
.ConfigParser()
78 # Initialize configuration with some sensible defaults
79 cp
.readfp(DEFAULT_CONFIG
)
86 def setup_database(self
):
88 Sets up the database connection.
91 "host" : self
.config
.get("database", "server"),
92 "database" : self
.config
.get("database", "database"),
93 "user" : self
.config
.get("database", "username"),
94 "password" : self
.config
.get("database", "password"),
97 self
.db
= database
.Connection(**credentials
)
100 def ssl_context(self
):
102 context
= ssl
.create_default_context()
104 # Fetch client certificate
105 certificate
= self
.settings
.get("client-certificate", None)
106 key
= self
.settings
.get("client-key", None)
108 # Apply client certificate
109 if certificate
and key
:
110 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
111 f_cert
.write(certificate
)
114 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
118 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
122 async def load_certificate(self
, certfile
, keyfile
):
123 with self
.db
.transaction():
125 with
open(certfile
) as f
:
126 self
.settings
.set("client-certificate", f
.read())
129 with
open(keyfile
) as f
:
130 self
.settings
.set("client-key", f
.read())
132 async def run_task(self
, task
, *args
, **kwargs
):
134 "announce-blog-posts" : self
.blog
.announce
,
135 "check-mirrors" : self
.mirrors
.check_all
,
136 "check-spam" : self
.accounts
.check_spam
,
137 "cleanup" : self
.cleanup
,
138 "get-all-emails" : self
.accounts
.get_all_emails
,
139 "launch-campaigns" : self
.campaigns
.launch_manually
,
140 "load-certificate" : self
.load_certificate
,
141 "run-campaigns" : self
.campaigns
.run
,
142 "scan-files" : self
.releases
.scan_files
,
143 "send-message" : self
.messages
.send_cli
,
144 "send-all-messages" : self
.messages
.queue
.send_all
,
145 "test-ldap" : self
.accounts
.test_ldap
,
146 "tweet" : self
.tweets
.tweet
,
147 "update-blog-feeds" : self
.blog
.update_feeds
,
150 # Get the task from the list of all tasks
151 func
= tasks
.get(task
, None)
153 raise ValueError("Unknown task: %s" % task
)
156 r
= await func(*args
, **kwargs
)
158 # If any error code has been returned,
159 # we will end the program
165 return campaigns
.Campaigns(self
)
169 return accounts
.Groups(self
)
173 return messages
.Messages(self
)
176 def ratelimiter(self
):
177 return ratelimit
.RateLimiter(self
)
181 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
185 return tweets
.Tweets(self
)
187 async def cleanup(self
):
188 # Cleanup message queue
189 with self
.db
.transaction():
190 self
.messages
.queue
.cleanup()
192 # Cleanup in accounts
193 with self
.db
.transaction():
194 self
.accounts
.cleanup()