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