]> git.ipfire.org Git - ipfire.org.git/blob - src/backend/base.py
Migrate to libloc
[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 fireinfo
12 from . import iuse
13 from . import memcached
14 from . import messages
15 from . import mirrors
16 from . import netboot
17 from . import nopaste
18 from . import ratelimit
19 from . import releases
20 from . import resolver
21 from . import settings
22 from . import talk
23 from . import tweets
24 from . import util
25 from . import wiki
26 from . import zeiterfassung
27 from .decorators import *
28
29 DEFAULT_CONFIG = io.StringIO("""
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
38 class Backend(object):
39 def __init__(self, configfile, debug=False):
40 # Read configuration file.
41 self.config = self.read_config(configfile)
42
43 # Enable debug logging if configured
44 self.debug = debug or self.config.getboolean("global", "debug")
45
46 # Setup database.
47 self.setup_database()
48
49 # Create HTTPClient
50 self.http_client = tornado.httpclient.AsyncHTTPClient(
51 defaults = {
52 "User-Agent" : "IPFireWebApp",
53 }
54 )
55 # Initialize settings first.
56 self.settings = settings.Settings(self)
57 self.memcache = memcached.Memcached(self)
58
59 # Initialize backend modules.
60 self.accounts = accounts.Accounts(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 "announce-blog-posts" : self.blog.announce,
100 "check-mirrors" : self.mirrors.check_all,
101 "check-spam" : self.accounts.check_spam,
102 "cleanup" : self.cleanup,
103 "get-all-emails" : self.accounts.get_all_emails,
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,
109 "test-ldap" : self.accounts.test_ldap,
110 "tweet" : self.tweets.tweet,
111 "update-blog-feeds" : self.blog.update_feeds,
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
120 r = await func(*args, **kwargs)
121
122 # If any error code has been returned,
123 # we will end the program
124 if r:
125 raise SystemExit(r)
126
127 @lazy_property
128 def campaigns(self):
129 return campaigns.Campaigns(self)
130
131 @lazy_property
132 def groups(self):
133 return accounts.Groups(self)
134
135 @lazy_property
136 def messages(self):
137 return messages.Messages(self)
138
139 @lazy_property
140 def ratelimiter(self):
141 return ratelimit.RateLimiter(self)
142
143 @lazy_property
144 def resolver(self):
145 return resolver.Resolver(tries=2, timeout=2, domains=[])
146
147 @lazy_property
148 def tweets(self):
149 return tweets.Tweets(self)
150
151 async def cleanup(self):
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()