]> scripts.mit.edu Git - wizard.git/blob - wizard/app/mediawiki.py
Implement 'wizard blacklist', tweaks.
[wizard.git] / wizard / app / mediawiki.py
1 import re
2 import distutils.version
3 import os
4 import datetime
5 import logging
6 import shlex
7 import shutil
8
9 from wizard import app, deploy, install, scripts, shell, util
10 from wizard.app import php
11
12 def make_filename_regex(var):
13     return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
14
15 make_extractor = app.filename_regex_extractor(make_filename_regex)
16 make_substitution = app.filename_regex_substitution(make_filename_regex)
17 seed = {
18         'WIZARD_IP': 'IP', # obsolete, remove after we're done
19         'WIZARD_SITENAME': 'wgSitename',
20         'WIZARD_SCRIPTPATH': 'wgScriptPath',
21         'WIZARD_EMERGENCYCONTACT': ('wgEmergencyContact', 'wgPasswordSender'),
22         'WIZARD_DBSERVER': 'wgDBserver',
23         'WIZARD_DBNAME': 'wgDBname',
24         'WIZARD_DBUSER': 'wgDBuser',
25         'WIZARD_DBPASSWORD': 'wgDBpassword',
26         'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
27         }
28
29 class Application(deploy.Application):
30     parametrized_files = ['LocalSettings.php', 'php.ini']
31     deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
32     @property
33     def extractors(self):
34         if not self._extractors:
35             self._extractors = util.dictmap(make_extractor, seed)
36             self._extractors.update(php.extractors)
37         return self._extractors
38     @property
39     def substitutions(self):
40         if not self._substitutions:
41             self._substitutions = util.dictkmap(make_substitution, seed)
42             self._substitutions.update(php.substitutions)
43         return self._substitutions
44     @property
45     def install_handler(self):
46         handler = install.ArgHandler("mysql", "admin", "email")
47         handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
48         return handler
49     def checkConfig(self, deployment):
50         return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
51     def detectVersion(self, deployment):
52         contents = deployment.read("includes/DefaultSettings.php")
53         regex = make_filename_regex("wgVersion")[1]
54         match = regex.search(contents)
55         if not match: return None
56         return distutils.version.LooseVersion(match.group(2)[1:-1])
57     def checkWeb(self, d, out=None):
58         page = d.fetch("/index.php?title=Main_Page")
59         if type(out) is list:
60             out.append(page)
61         return page.find("<!-- Served") != -1
62     def install(self, version, options):
63         try:
64             os.unlink("LocalSettings.php")
65         except OSError:
66             pass
67
68         os.chmod("config", 0777) # XXX: vaguely sketchy
69
70         postdata = {
71             'Sitename': options.title,
72             'EmergencyContact': options.email,
73             'LanguageCode': 'en',
74             'DBserver': options.mysql_host,
75             'DBname': options.mysql_db,
76             'DBuser': options.mysql_user,
77             'DBpassword': options.mysql_password,
78             'DBpassword2': options.mysql_password,
79             'defaultEmail': options.email,
80             'SysopName': options.admin_name,
81             'SysopPass': options.admin_password,
82             'SysopPass2': options.admin_password,
83             }
84         result = install.fetch(options, '/config/index.php', post=postdata)
85         if options.verbose: print result
86         if result.find("Installation successful") == -1:
87             raise install.Failure()
88         os.rename('config/LocalSettings.php', 'LocalSettings.php')
89     def upgrade(self, d, version, options):
90         sh = shell.Shell()
91         if not os.path.isfile("AdminSettings.php"):
92             sh.call("git", "checkout", "mediawiki-" + str(version), "--", "AdminSettings.php")
93         try:
94             result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
95         except shell.CallError as e:
96             raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
97         results = result.rstrip().split()
98         if not results or not results[-1] == "Done.":
99             raise app.UpgradeFailure(result)
100     def backup(self, deployment, options):
101         sh = shell.Shell()
102         # XXX: duplicate code, refactor, also, race condition
103         backupdir = os.path.join(".scripts", "backups")
104         backup = str(deployment.version) + "-" + datetime.date.today().isoformat()
105         outdir = os.path.join(backupdir, backup)
106         if not os.path.exists(backupdir):
107             os.mkdir(backupdir)
108         if os.path.exists(outdir):
109             util.safe_unlink(outdir)
110         os.mkdir(outdir)
111         outfile = os.path.join(outdir, "db.sql")
112         try:
113             sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
114             sh.call("gzip", "--best", outfile)
115         except shell.CallError as e:
116             shutil.rmtree(outdir)
117             raise app.BackupFailure(e.stderr)
118         return backup
119     def restore(self, deployment, backup, options):
120         sh = shell.Shell()
121         backup_dir = os.path.join(".scripts", "backups", backup)
122         if not os.path.exists(backup_dir):
123             raise app.RestoreFailure("Backup %s doesn't exist", backup)
124         sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
125         sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
126         sql.seek(0)
127         sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
128         sql.close()
129
130 def get_mysql_args(d):
131     # XXX: add support for getting these out of options
132     vars = d.extract()
133     if 'WIZARD_DBNAME' not in vars:
134         raise app.BackupFailure("Could not determine database name")
135     triplet = scripts.get_sql_credentials(vars)
136     args = []
137     if triplet is not None:
138         server, user, password = triplet
139         args += ["-h", server, "-u", user, "-p" + password]
140     name = shlex.split(vars['WIZARD_DBNAME'])[0]
141     args.append(name)
142     return args