]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
075546e014a2f88f65fdae37ef5f7092a0a7c284
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import configparser
4 import io
5 import tornado.gen
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 # Initialize settings first.
48 self.settings = settings.Settings(self)
49 self.memcache = memcached.Memcached(self)
50
51 # Initialize backend modules.
52 self.accounts = accounts.Accounts(self)
53 self.geoip = geoip.GeoIP(self)
54 self.fireinfo = fireinfo.Fireinfo(self)
55 self.iuse = iuse.IUse(self)
56 self.mirrors = mirrors.Mirrors(self)
57 self.netboot = netboot.NetBoot(self)
58 self.nopaste = nopaste.Nopaste(self)
59 self.releases = releases.Releases(self)
60 self.talk = talk.Talk(self)
61
62 self.blog = blog.Blog(self)
63 self.wiki = wiki.Wiki(self)
64 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
65
66 def read_config(self, configfile):
67 cp = configparser.ConfigParser()
68
69 # Initialize configuration with some sensible defaults
70 cp.readfp(DEFAULT_CONFIG)
71
72 # Parse file
73 cp.read(configfile)
74
75 return cp
76
77 def setup_database(self):
78 """
79 Sets up the database connection.
80 """
81 credentials = {
82 "host" : self.config.get("database", "server"),
83 "database" : self.config.get("database", "database"),
84 "user" : self.config.get("database", "username"),
85 "password" : self.config.get("database", "password"),
86 }
87
88 self.db = database.Connection(**credentials)
89
90 @tornado.gen.coroutine
91 def run_task(self, task, *args, **kwargs):
92 tasks = {
93 "check-mirrors" : self.mirrors.check_all,
94 "cleanup" : self.cleanup,
95 "scan-files" : self.releases.scan_files,
96 "send-all-messages" : self.messages.queue.send_all,
97 "test-blacklist" : self.geoip.test_blacklist,
98 "test-ldap" : self.accounts.test_ldap,
99 "tweet" : self.tweets.tweet,
100 "update-blog-feeds" : self.blog.update_feeds,
101 }
102
103 # Get the task from the list of all tasks
104 func = tasks.get(task, None)
105 if not func:
106 raise ValueError("Unknown task: %s" % task)
107
108 # Run the task
109 r = yield func(*args, **kwargs)
110
111 # If any error code has been returned,
112 # we will end the program
113 if r:
114 raise SystemExit(r)
115
116 @lazy_property
117 def messages(self):
118 return messages.Messages(self)
119
120 @lazy_property
121 def ratelimiter(self):
122 return ratelimit.RateLimiter(self)
123
124 @lazy_property
125 def tweets(self):
126 return tweets.Tweets(self)
127
128 @tornado.gen.coroutine
129 def cleanup(self):
130 # Cleanup message queue
131 with self.db.transaction():
132 self.messages.queue.cleanup()
133
134 # Cleanup in accounts
135 with self.db.transaction():
136 self.accounts.cleanup()