]>
Commit | Line | Data |
---|---|---|
a6dc0bad MT |
1 | #!/usr/bin/python |
2 | ||
c5ddbd67 | 3 | import configparser |
11347e46 | 4 | import io |
23f84bbc | 5 | import tornado.httpclient |
11347e46 MT |
6 | |
7 | from . import accounts | |
05e8c602 | 8 | from . import blog |
d73bba54 | 9 | from . import campaigns |
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 | 96 | |
9fdf4fb7 | 97 | async def run_task(self, task, *args, **kwargs): |
c5ddbd67 | 98 | tasks = { |
b2059099 | 99 | "check-mirrors" : self.mirrors.check_all, |
06dfa22d | 100 | "check-spam" : self.accounts.check_spam, |
8e69850a | 101 | "cleanup" : self.cleanup, |
d73bba54 MT |
102 | "launch-campaigns" : self.campaigns.launch_manually, |
103 | "run-campaigns" : self.campaigns.run, | |
e136d583 | 104 | "scan-files" : self.releases.scan_files, |
d6df53bf | 105 | "send-all-messages" : self.messages.queue.send_all, |
8f94e19f | 106 | "test-blacklist" : self.geoip.test_blacklist, |
30e11b1b | 107 | "test-ldap" : self.accounts.test_ldap, |
611adbfb | 108 | "tweet" : self.tweets.tweet, |
c5ddbd67 MT |
109 | "update-blog-feeds" : self.blog.update_feeds, |
110 | } | |
111 | ||
112 | # Get the task from the list of all tasks | |
113 | func = tasks.get(task, None) | |
114 | if not func: | |
115 | raise ValueError("Unknown task: %s" % task) | |
116 | ||
117 | # Run the task | |
9fdf4fb7 | 118 | r = await func(*args, **kwargs) |
c5ddbd67 MT |
119 | |
120 | # If any error code has been returned, | |
121 | # we will end the program | |
122 | if r: | |
123 | raise SystemExit(r) | |
d6df53bf | 124 | |
d73bba54 MT |
125 | @lazy_property |
126 | def campaigns(self): | |
127 | return campaigns.Campaigns(self) | |
128 | ||
d8b04c72 MT |
129 | @lazy_property |
130 | def groups(self): | |
131 | return accounts.Groups(self) | |
132 | ||
d6df53bf MT |
133 | @lazy_property |
134 | def messages(self): | |
135 | return messages.Messages(self) | |
611adbfb | 136 | |
372ef119 MT |
137 | @lazy_property |
138 | def ratelimiter(self): | |
139 | return ratelimit.RateLimiter(self) | |
140 | ||
611adbfb MT |
141 | @lazy_property |
142 | def tweets(self): | |
143 | return tweets.Tweets(self) | |
8e69850a | 144 | |
9fdf4fb7 | 145 | async def cleanup(self): |
8e69850a MT |
146 | # Cleanup message queue |
147 | with self.db.transaction(): | |
148 | self.messages.queue.cleanup() | |
149 | ||
150 | # Cleanup in accounts | |
151 | with self.db.transaction(): | |
152 | self.accounts.cleanup() |