]>
Commit | Line | Data |
---|---|---|
a6dc0bad MT |
1 | #!/usr/bin/python |
2 | ||
c5ddbd67 | 3 | import configparser |
11347e46 | 4 | import io |
c5ddbd67 | 5 | import tornado.gen |
11347e46 MT |
6 | |
7 | from . import accounts | |
05e8c602 | 8 | from . import blog |
11347e46 MT |
9 | from . import database |
10 | from . import geoip | |
11 | from . import fireinfo | |
12 | from . import iuse | |
e28b082e | 13 | from . import memcached |
d6df53bf | 14 | from . import messages |
11347e46 MT |
15 | from . import mirrors |
16 | from . import netboot | |
17 | from . import nopaste | |
372ef119 | 18 | from . import ratelimit |
11347e46 MT |
19 | from . import releases |
20 | from . import settings | |
21 | from . import talk | |
611adbfb | 22 | from . import tweets |
181d08f3 | 23 | from . import wiki |
2c361abc | 24 | from . import zeiterfassung |
d6df53bf | 25 | from .decorators import * |
2c361abc | 26 | |
11347e46 | 27 | DEFAULT_CONFIG = io.StringIO(""" |
9ed02e3b MT |
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 | ||
a6dc0bad | 36 | class Backend(object): |
2cd9af74 | 37 | def __init__(self, configfile, debug=False): |
9068dba1 MT |
38 | # Read configuration file. |
39 | self.config = self.read_config(configfile) | |
9ed02e3b MT |
40 | |
41 | # Enable debug logging if configured | |
42 | self.debug = debug or self.config.getboolean("global", "debug") | |
9068dba1 MT |
43 | |
44 | # Setup database. | |
45 | self.setup_database() | |
a6dc0bad | 46 | |
9068dba1 MT |
47 | # Initialize settings first. |
48 | self.settings = settings.Settings(self) | |
e28b082e | 49 | self.memcache = memcached.Memcached(self) |
9068dba1 MT |
50 | |
51 | # Initialize backend modules. | |
a6dc0bad | 52 | self.accounts = accounts.Accounts(self) |
9068dba1 | 53 | self.geoip = geoip.GeoIP(self) |
66862195 | 54 | self.fireinfo = fireinfo.Fireinfo(self) |
9068dba1 MT |
55 | self.iuse = iuse.IUse(self) |
56 | self.mirrors = mirrors.Mirrors(self) | |
57 | self.netboot = netboot.NetBoot(self) | |
66862195 | 58 | self.nopaste = nopaste.Nopaste(self) |
9068dba1 | 59 | self.releases = releases.Releases(self) |
66862195 | 60 | self.talk = talk.Talk(self) |
9068dba1 | 61 | |
0a6875dc | 62 | self.blog = blog.Blog(self) |
181d08f3 | 63 | self.wiki = wiki.Wiki(self) |
2c361abc MT |
64 | self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self) |
65 | ||
9068dba1 MT |
66 | def read_config(self, configfile): |
67 | cp = configparser.ConfigParser() | |
9ed02e3b MT |
68 | |
69 | # Initialize configuration with some sensible defaults | |
70 | cp.readfp(DEFAULT_CONFIG) | |
71 | ||
72 | # Parse file | |
9068dba1 MT |
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) | |
c5ddbd67 MT |
89 | |
90 | @tornado.gen.coroutine | |
91 | def run_task(self, task, *args, **kwargs): | |
92 | tasks = { | |
b2059099 | 93 | "check-mirrors" : self.mirrors.check_all, |
8e69850a | 94 | "cleanup" : self.cleanup, |
e136d583 | 95 | "scan-files" : self.releases.scan_files, |
d6df53bf | 96 | "send-all-messages" : self.messages.queue.send_all, |
611adbfb | 97 | "tweet" : self.tweets.tweet, |
c5ddbd67 MT |
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) | |
d6df53bf MT |
113 | |
114 | @lazy_property | |
115 | def messages(self): | |
116 | return messages.Messages(self) | |
611adbfb | 117 | |
372ef119 MT |
118 | @lazy_property |
119 | def ratelimiter(self): | |
120 | return ratelimit.RateLimiter(self) | |
121 | ||
611adbfb MT |
122 | @lazy_property |
123 | def tweets(self): | |
124 | return tweets.Tweets(self) | |
8e69850a MT |
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() |