]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
8 import tornado
.httpclient
10 from . import accounts
12 from . import campaigns
13 from . import database
14 from . import fireinfo
16 from . import memcached
17 from . import messages
21 from . import ratelimit
22 from . import releases
23 from . import resolver
24 from . import settings
29 from . import zeiterfassung
30 from .decorators
import *
32 DEFAULT_CONFIG
= io
.StringIO("""
37 static_dir = %(data_dir)s/static
38 templates_dir = %(data_dir)s/templates
41 class Backend(object):
42 def __init__(self
, configfile
, debug
=False):
43 # Read configuration file.
44 self
.config
= self
.read_config(configfile
)
46 # Enable debug logging if configured
47 self
.debug
= debug
or self
.config
.getboolean("global", "debug")
53 self
.http_client
= tornado
.httpclient
.AsyncHTTPClient(
55 "User-Agent" : "IPFireWebApp",
58 # Initialize settings first.
59 self
.settings
= settings
.Settings(self
)
60 self
.memcache
= memcached
.Memcached(self
)
62 # Initialize backend modules.
63 self
.accounts
= accounts
.Accounts(self
)
64 self
.fireinfo
= fireinfo
.Fireinfo(self
)
65 self
.iuse
= iuse
.IUse(self
)
66 self
.mirrors
= mirrors
.Mirrors(self
)
67 self
.netboot
= netboot
.NetBoot(self
)
68 self
.nopaste
= nopaste
.Nopaste(self
)
69 self
.releases
= releases
.Releases(self
)
70 self
.talk
= talk
.Talk(self
)
72 self
.blog
= blog
.Blog(self
)
73 self
.wiki
= wiki
.Wiki(self
)
74 self
.zeiterfassung
= zeiterfassung
.ZeiterfassungClient(self
)
76 def read_config(self
, configfile
):
77 cp
= configparser
.ConfigParser()
79 # Initialize configuration with some sensible defaults
80 cp
.readfp(DEFAULT_CONFIG
)
87 def setup_database(self
):
89 Sets up the database connection.
92 "host" : self
.config
.get("database", "server"),
93 "database" : self
.config
.get("database", "database"),
94 "user" : self
.config
.get("database", "username"),
95 "password" : self
.config
.get("database", "password"),
98 self
.db
= database
.Connection(**credentials
)
101 def ssl_context(self
):
103 context
= ssl
.create_default_context()
105 # Fetch client certificate
106 certificate
= self
.settings
.get("client-certificate", None)
107 key
= self
.settings
.get("client-key", None)
109 # Apply client certificate
110 if certificate
and key
:
111 with tempfile
.NamedTemporaryFile(mode
="w") as f_cert
:
112 f_cert
.write(certificate
)
115 with tempfile
.NamedTemporaryFile(mode
="w") as f_key
:
119 context
.load_cert_chain(f_cert
.name
, f_key
.name
)
123 async def load_certificate(self
, certfile
, keyfile
):
124 with self
.db
.transaction():
126 with
open(certfile
) as f
:
127 self
.settings
.set("client-certificate", f
.read())
130 with
open(keyfile
) as f
:
131 self
.settings
.set("client-key", f
.read())
133 async def run_task(self
, task
, *args
, **kwargs
):
135 "announce-blog-posts" : self
.blog
.announce
,
136 "check-mirrors" : self
.mirrors
.check_all
,
137 "check-spam" : self
.accounts
.check_spam
,
138 "cleanup" : self
.cleanup
,
139 "get-all-emails" : self
.accounts
.get_all_emails
,
140 "launch-campaigns" : self
.campaigns
.launch_manually
,
141 "load-certificate" : self
.load_certificate
,
142 "run-campaigns" : self
.campaigns
.run
,
143 "scan-files" : self
.releases
.scan_files
,
144 "send-message" : self
.messages
.send_cli
,
145 "send-all-messages" : self
.messages
.queue
.send_all
,
146 "test-ldap" : self
.accounts
.test_ldap
,
147 "tweet" : self
.tweets
.tweet
,
148 "update-blog-feeds" : self
.blog
.update_feeds
,
151 # Get the task from the list of all tasks
152 func
= tasks
.get(task
, None)
154 raise ValueError("Unknown task: %s" % task
)
157 r
= await func(*args
, **kwargs
)
159 # If any error code has been returned,
160 # we will end the program
166 return campaigns
.Campaigns(self
)
170 return accounts
.Groups(self
)
174 return messages
.Messages(self
)
178 return location
.Database("/var/lib/location/database.db")
180 def get_country_name(self
, country_code
):
181 country
= self
.location
.get_country(country_code
)
187 def ratelimiter(self
):
188 return ratelimit
.RateLimiter(self
)
192 return resolver
.Resolver(tries
=2, timeout
=2, domains
=[])
196 return tweets
.Tweets(self
)
198 async def cleanup(self
):
199 # Cleanup message queue
200 with self
.db
.transaction():
201 self
.messages
.queue
.cleanup()
203 # Cleanup in accounts
204 with self
.db
.transaction():
205 self
.accounts
.cleanup()