]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
f85d2ad891c88c112e57e95235dceabeb2c7cb3b
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import configparser
4 import io
5 import tornado.httpclient
6
7 from . import accounts
8 from . import blog
9 from . import database
10 from . import geoip
11 from . import fireinfo
12 from . import iuse
13 from . import memcached
14 from . import messages
15 from . import mirrors
16 from . import netboot
17 from . import nopaste
18 from . import ratelimit
19 from . import releases
20 from . import settings
21 from . import talk
22 from . import tweets
23 from . import wiki
24 from . import zeiterfassung
25 from .decorators import *
26
27 DEFAULT_CONFIG = io.StringIO("""
28 [global]
29 debug = false
30
31 data_dir =
32 static_dir = %(data_dir)s/static
33 templates_dir = %(data_dir)s/templates
34 """)
35
36 class Backend(object):
37 def __init__(self, configfile, debug=False):
38 # Read configuration file.
39 self.config = self.read_config(configfile)
40
41 # Enable debug logging if configured
42 self.debug = debug or self.config.getboolean("global", "debug")
43
44 # Setup database.
45 self.setup_database()
46
47 # Create HTTPClient
48 self.http_client = tornado.httpclient.AsyncHTTPClient(
49 defaults = {
50 "User-Agent" : "IPFireWebApp",
51 }
52 )
53 # Initialize settings first.
54 self.settings = settings.Settings(self)
55 self.memcache = memcached.Memcached(self)
56
57 # Initialize backend modules.
58 self.accounts = accounts.Accounts(self)
59 self.geoip = geoip.GeoIP(self)
60 self.fireinfo = fireinfo.Fireinfo(self)
61 self.iuse = iuse.IUse(self)
62 self.mirrors = mirrors.Mirrors(self)
63 self.netboot = netboot.NetBoot(self)
64 self.nopaste = nopaste.Nopaste(self)
65 self.releases = releases.Releases(self)
66 self.talk = talk.Talk(self)
67
68 self.blog = blog.Blog(self)
69 self.wiki = wiki.Wiki(self)
70 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
71
72 def read_config(self, configfile):
73 cp = configparser.ConfigParser()
74
75 # Initialize configuration with some sensible defaults
76 cp.readfp(DEFAULT_CONFIG)
77
78 # Parse file
79 cp.read(configfile)
80
81 return cp
82
83 def setup_database(self):
84 """
85 Sets up the database connection.
86 """
87 credentials = {
88 "host" : self.config.get("database", "server"),
89 "database" : self.config.get("database", "database"),
90 "user" : self.config.get("database", "username"),
91 "password" : self.config.get("database", "password"),
92 }
93
94 self.db = database.Connection(**credentials)
95
96 async def run_task(self, task, *args, **kwargs):
97 tasks = {
98 "check-mirrors" : self.mirrors.check_all,
99 "check-spam" : self.accounts.check_spam,
100 "cleanup" : self.cleanup,
101 "scan-files" : self.releases.scan_files,
102 "send-all-messages" : self.messages.queue.send_all,
103 "test-blacklist" : self.geoip.test_blacklist,
104 "test-ldap" : self.accounts.test_ldap,
105 "tweet" : self.tweets.tweet,
106 "update-blog-feeds" : self.blog.update_feeds,
107 }
108
109 # Get the task from the list of all tasks
110 func = tasks.get(task, None)
111 if not func:
112 raise ValueError("Unknown task: %s" % task)
113
114 # Run the task
115 r = await func(*args, **kwargs)
116
117 # If any error code has been returned,
118 # we will end the program
119 if r:
120 raise SystemExit(r)
121
122 @lazy_property
123 def groups(self):
124 return accounts.Groups(self)
125
126 @lazy_property
127 def messages(self):
128 return messages.Messages(self)
129
130 @lazy_property
131 def ratelimiter(self):
132 return ratelimit.RateLimiter(self)
133
134 @lazy_property
135 def tweets(self):
136 return tweets.Tweets(self)
137
138 async def cleanup(self):
139 # Cleanup message queue
140 with self.db.transaction():
141 self.messages.queue.cleanup()
142
143 # Cleanup in accounts
144 with self.db.transaction():
145 self.accounts.cleanup()