]>
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 | 10 | from . import database |
11347e46 MT |
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 | 19 | from . import releases |
440aba92 | 20 | from . import resolver |
11347e46 MT |
21 | from . import settings |
22 | from . import talk | |
611adbfb | 23 | from . import tweets |
440aba92 | 24 | from . import util |
181d08f3 | 25 | from . import wiki |
2c361abc | 26 | from . import zeiterfassung |
d6df53bf | 27 | from .decorators import * |
2c361abc | 28 | |
11347e46 | 29 | DEFAULT_CONFIG = io.StringIO(""" |
9ed02e3b MT |
30 | [global] |
31 | debug = false | |
32 | ||
33 | data_dir = | |
34 | static_dir = %(data_dir)s/static | |
35 | templates_dir = %(data_dir)s/templates | |
36 | """) | |
37 | ||
a6dc0bad | 38 | class Backend(object): |
2cd9af74 | 39 | def __init__(self, configfile, debug=False): |
9068dba1 MT |
40 | # Read configuration file. |
41 | self.config = self.read_config(configfile) | |
9ed02e3b MT |
42 | |
43 | # Enable debug logging if configured | |
44 | self.debug = debug or self.config.getboolean("global", "debug") | |
9068dba1 MT |
45 | |
46 | # Setup database. | |
47 | self.setup_database() | |
a6dc0bad | 48 | |
23f84bbc MT |
49 | # Create HTTPClient |
50 | self.http_client = tornado.httpclient.AsyncHTTPClient( | |
51 | defaults = { | |
52 | "User-Agent" : "IPFireWebApp", | |
53 | } | |
54 | ) | |
9068dba1 MT |
55 | # Initialize settings first. |
56 | self.settings = settings.Settings(self) | |
e28b082e | 57 | self.memcache = memcached.Memcached(self) |
9068dba1 MT |
58 | |
59 | # Initialize backend modules. | |
a6dc0bad | 60 | self.accounts = accounts.Accounts(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 = { |
aee57270 MT |
99 | "announce-blog-posts" : self.blog.announce, |
100 | "check-mirrors" : self.mirrors.check_all, | |
101 | "check-spam" : self.accounts.check_spam, | |
102 | "cleanup" : self.cleanup, | |
5bfc6729 | 103 | "get-all-emails" : self.accounts.get_all_emails, |
aee57270 MT |
104 | "launch-campaigns" : self.campaigns.launch_manually, |
105 | "run-campaigns" : self.campaigns.run, | |
106 | "scan-files" : self.releases.scan_files, | |
107 | "send-message" : self.messages.send_cli, | |
108 | "send-all-messages" : self.messages.queue.send_all, | |
aee57270 MT |
109 | "test-ldap" : self.accounts.test_ldap, |
110 | "tweet" : self.tweets.tweet, | |
111 | "update-blog-feeds" : self.blog.update_feeds, | |
c5ddbd67 MT |
112 | } |
113 | ||
114 | # Get the task from the list of all tasks | |
115 | func = tasks.get(task, None) | |
116 | if not func: | |
117 | raise ValueError("Unknown task: %s" % task) | |
118 | ||
119 | # Run the task | |
9fdf4fb7 | 120 | r = await func(*args, **kwargs) |
c5ddbd67 MT |
121 | |
122 | # If any error code has been returned, | |
123 | # we will end the program | |
124 | if r: | |
125 | raise SystemExit(r) | |
d6df53bf | 126 | |
d73bba54 MT |
127 | @lazy_property |
128 | def campaigns(self): | |
129 | return campaigns.Campaigns(self) | |
130 | ||
d8b04c72 MT |
131 | @lazy_property |
132 | def groups(self): | |
133 | return accounts.Groups(self) | |
134 | ||
d6df53bf MT |
135 | @lazy_property |
136 | def messages(self): | |
137 | return messages.Messages(self) | |
611adbfb | 138 | |
372ef119 MT |
139 | @lazy_property |
140 | def ratelimiter(self): | |
141 | return ratelimit.RateLimiter(self) | |
142 | ||
440aba92 MT |
143 | @lazy_property |
144 | def resolver(self): | |
145 | return resolver.Resolver(tries=2, timeout=2, domains=[]) | |
146 | ||
611adbfb MT |
147 | @lazy_property |
148 | def tweets(self): | |
149 | return tweets.Tweets(self) | |
8e69850a | 150 | |
9fdf4fb7 | 151 | async def cleanup(self): |
8e69850a MT |
152 | # Cleanup message queue |
153 | with self.db.transaction(): | |
154 | self.messages.queue.cleanup() | |
155 | ||
156 | # Cleanup in accounts | |
157 | with self.db.transaction(): | |
158 | self.accounts.cleanup() |