]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
Support sending HTML emails
[ipfire.org.git] / src / backend / base.py
1 #!/usr/bin/python
2
3 import configparser
4 import io
5 import tornado.httpclient
6
7 from . import accounts
8 from . import blog
9 from . import campaigns
10 from . import database
11 from . import geoip
12 from . import fireinfo
13 from . import iuse
14 from . import memcached
15 from . import messages
16 from . import mirrors
17 from . import netboot
18 from . import nopaste
19 from . import ratelimit
20 from . import releases
21 from . import settings
22 from . import talk
23 from . import tweets
24 from . import wiki
25 from . import zeiterfassung
26 from .decorators import *
27
28 DEFAULT_CONFIG = io.StringIO("""
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
37 class Backend(object):
38 def __init__(self, configfile, debug=False):
39 # Read configuration file.
40 self.config = self.read_config(configfile)
41
42 # Enable debug logging if configured
43 self.debug = debug or self.config.getboolean("global", "debug")
44
45 # Setup database.
46 self.setup_database()
47
48 # Create HTTPClient
49 self.http_client = tornado.httpclient.AsyncHTTPClient(
50 defaults = {
51 "User-Agent" : "IPFireWebApp",
52 }
53 )
54 # Initialize settings first.
55 self.settings = settings.Settings(self)
56 self.memcache = memcached.Memcached(self)
57
58 # Initialize backend modules.
59 self.accounts = accounts.Accounts(self)
60 self.geoip = geoip.GeoIP(self)
61 self.fireinfo = fireinfo.Fireinfo(self)
62 self.iuse = iuse.IUse(self)
63 self.mirrors = mirrors.Mirrors(self)
64 self.netboot = netboot.NetBoot(self)
65 self.nopaste = nopaste.Nopaste(self)
66 self.releases = releases.Releases(self)
67 self.talk = talk.Talk(self)
68
69 self.blog = blog.Blog(self)
70 self.wiki = wiki.Wiki(self)
71 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
72
73 def read_config(self, configfile):
74 cp = configparser.ConfigParser()
75
76 # Initialize configuration with some sensible defaults
77 cp.readfp(DEFAULT_CONFIG)
78
79 # Parse file
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)
96
97 async def run_task(self, task, *args, **kwargs):
98 tasks = {
99 "check-mirrors" : self.mirrors.check_all,
100 "check-spam" : self.accounts.check_spam,
101 "cleanup" : self.cleanup,
102 "launch-campaigns" : self.campaigns.launch_manually,
103 "run-campaigns" : self.campaigns.run,
104 "scan-files" : self.releases.scan_files,
105 "send-message" : self.messages.send_cli,
106 "send-all-messages" : self.messages.queue.send_all,
107 "test-blacklist" : self.geoip.test_blacklist,
108 "test-ldap" : self.accounts.test_ldap,
109 "tweet" : self.tweets.tweet,
110 "update-blog-feeds" : self.blog.update_feeds,
111 }
112
113 # Get the task from the list of all tasks
114 func = tasks.get(task, None)
115 if not func:
116 raise ValueError("Unknown task: %s" % task)
117
118 # Run the task
119 r = await func(*args, **kwargs)
120
121 # If any error code has been returned,
122 # we will end the program
123 if r:
124 raise SystemExit(r)
125
126 @lazy_property
127 def campaigns(self):
128 return campaigns.Campaigns(self)
129
130 @lazy_property
131 def groups(self):
132 return accounts.Groups(self)
133
134 @lazy_property
135 def messages(self):
136 return messages.Messages(self)
137
138 @lazy_property
139 def ratelimiter(self):
140 return ratelimit.RateLimiter(self)
141
142 @lazy_property
143 def tweets(self):
144 return tweets.Tweets(self)
145
146 async def cleanup(self):
147 # Cleanup message queue
148 with self.db.transaction():
149 self.messages.queue.cleanup()
150
151 # Cleanup in accounts
152 with self.db.transaction():
153 self.accounts.cleanup()