]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
40cc70a0ee74e02837ee1e324c95fe490b2eedc5
[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 "tweet" : self.tweets.tweet,
98 "update-blog-feeds" : self.blog.update_feeds,
99 }
100
101 # Get the task from the list of all tasks
102 func = tasks.get(task, None)
103 if not func:
104 raise ValueError("Unknown task: %s" % task)
105
106 # Run the task
107 r = yield func(*args, **kwargs)
108
109 # If any error code has been returned,
110 # we will end the program
111 if r:
112 raise SystemExit(r)
113
114 @lazy_property
115 def messages(self):
116 return messages.Messages(self)
117
118 @lazy_property
119 def ratelimiter(self):
120 return ratelimit.RateLimiter(self)
121
122 @lazy_property
123 def tweets(self):
124 return tweets.Tweets(self)
125
126 @tornado.gen.coroutine
127 def cleanup(self):
128 # Cleanup message queue
129 with self.db.transaction():
130 self.messages.queue.cleanup()
131
132 # Cleanup in accounts
133 with self.db.transaction():
134 self.accounts.cleanup()