import re
import distutils.version
import os
-import datetime
+import lxml.cssselect
+import lxml.etree
+import StringIO
import logging
-import shlex
-import shutil
-from wizard import app, deploy, install, resolve, scripts, shell, util
+from wizard import app, install, resolve, shell, util
from wizard.app import php
def make_filename_regex(var):
- return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
+ return 'LocalSettings.php', php.re_var(var)
-make_extractor = app.filename_regex_extractor(make_filename_regex)
-make_substitution = app.filename_regex_substitution(make_filename_regex)
-seed = {
+seed = util.dictmap(make_filename_regex, {
'WIZARD_IP': 'IP', # obsolete, remove after we're done
'WIZARD_SITENAME': 'wgSitename',
'WIZARD_SCRIPTPATH': 'wgScriptPath',
'WIZARD_DBUSER': 'wgDBuser',
'WIZARD_DBPASSWORD': 'wgDBpassword',
'WIZARD_SECRETKEY': ('wgSecretKey', 'wgProxyKey'),
- }
+ })
-resolutions = {
+class Application(app.Application):
+ database = "mysql"
+ parametrized_files = ['LocalSettings.php'] + php.parametrized_files
+ deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
+ extractors = app.make_extractors(seed)
+ extractors.update(php.extractors)
+ substitutions = app.make_substitutions(seed)
+ substitutions.update(php.substitutions)
+ install_schema = install.ArgSchema("db", "admin", "email", "title")
+ def checkConfig(self, deployment):
+ return os.path.isfile("LocalSettings.php")
+ def detectVersion(self, deployment):
+ return self.detectVersionFromFile("includes/DefaultSettings.php", php.re_var("wgVersion"))
+ def checkWeb(self, deployment):
+ return self.checkWebPage(deployment, "/index.php?title=Main_Page", "<!-- Served")
+ def prepareMerge(self, deployment):
+ resolve.fix_newlines("LocalSettings.php")
+ def install(self, version, options):
+ util.soft_unlink("LocalSettings.php")
+ os.chmod("config", 0777) # XXX: vaguely sketchy
+
+ postdata = {
+ 'Sitename': options.title,
+ 'EmergencyContact': options.email,
+ 'LanguageCode': 'en',
+ 'DBserver': options.dsn.host,
+ 'DBname': options.dsn.database,
+ 'DBuser': options.dsn.username,
+ 'DBpassword': options.dsn.password,
+ 'DBpassword2': options.dsn.password,
+ 'defaultEmail': options.email,
+ 'SysopName': options.admin_name,
+ 'SysopPass': options.admin_password,
+ 'SysopPass2': options.admin_password,
+ }
+ result = install.fetch(options, '/config/index.php', post=postdata)
+ result_etree = lxml.etree.parse(StringIO.StringIO(result), lxml.etree.HTMLParser())
+ selector = lxml.cssselect.CSSSelector(".error")
+ error_messages = [e.text for e in selector(result_etree)]
+ logging.debug("Installation output:\n\n" + result)
+ if result.find("Installation successful") == -1:
+ if not error_messages:
+ raise app.InstallFailure()
+ else:
+ raise app.RecoverableInstallFailure(error_messages)
+ os.rename('config/LocalSettings.php', 'LocalSettings.php')
+ def upgrade(self, d, version, options):
+ if not os.path.isfile("AdminSettings.php"):
+ shell.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
+ try:
+ result = shell.eval("php", "maintenance/update.php", "--quick", log=True)
+ except shell.CallError as e:
+ raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
+ results = result.rstrip().split()
+ if not results or not results[-1] == "Done.":
+ raise app.UpgradeFailure(result)
+ def backup(self, deployment, backup_dir, options):
+ app.backup_database(backup_dir, deployment)
+ def restore(self, deployment, backup_dir, options):
+ app.restore_database(backup_dir, deployment)
+ def remove(self, deployment, options):
+ app.remove_database(deployment)
+ def researchFilter(self, filename, added, deleted):
+ if filename == "LocalSettings.php":
+ return added == deleted == 10 or added == deleted == 9
+ elif filename == "AdminSettings.php":
+ return added == 0 and deleted == 20
+ elif filename == "config/index.php" or filename == "config/index.php5":
+ return added == 0
+ return False
+
+Application.resolutions = {
'LocalSettings.php': [
("""
<<<<<<<
]
}
-class Application(deploy.Application):
- parametrized_files = ['LocalSettings.php', 'php.ini']
- deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
- @property
- def extractors(self):
- if not self._extractors:
- self._extractors = util.dictmap(make_extractor, seed)
- self._extractors.update(php.extractors)
- return self._extractors
- @property
- def substitutions(self):
- if not self._substitutions:
- self._substitutions = util.dictkmap(make_substitution, seed)
- self._substitutions.update(php.substitutions)
- return self._substitutions
- @property
- def install_handler(self):
- handler = install.ArgHandler("mysql", "admin", "email")
- handler.add(install.Arg("title", help="Title of your new MediaWiki install"))
- return handler
- def checkConfig(self, deployment):
- return os.path.isfile(os.path.join(deployment.location, "LocalSettings.php"))
- def detectVersion(self, deployment):
- contents = deployment.read("includes/DefaultSettings.php")
- regex = make_filename_regex("wgVersion")[1]
- match = regex.search(contents)
- if not match: return None
- return distutils.version.LooseVersion(match.group(2)[1:-1])
- def checkWeb(self, d, out=None):
- page = d.fetch("/index.php?title=Main_Page")
- if type(out) is list:
- out.append(page)
- return page.find("<!-- Served") != -1
- def prepareMerge(self, dir):
- # XXX: this should be factored out
- old_contents = open("LocalSettings.php", "r").read()
- contents = old_contents
- while "\r\n" in contents:
- contents = contents.replace("\r\n", "\n")
- contents = contents.replace("\r", "\n")
- if contents != old_contents:
- logging.info("Converted LocalSettings.php to UNIX file endings")
- open("LocalSettings.php", "w").write(contents)
- def resolveConflicts(self, dir):
- # XXX: this is pretty generic
- resolved = True
- sh = shell.Shell()
- for status in sh.eval("git", "ls-files", "--unmerged").splitlines():
- file = status.split()[-1]
- if file in resolutions:
- contents = open(file, "r").read()
- for spec, result in resolutions[file]:
- old_contents = contents
- contents = resolve.resolve(contents, spec, result)
- if old_contents != contents:
- logging.info("Did resolution with spec:\n" + spec)
- open(file, "w").write(contents)
- if not resolve.is_conflict(contents):
- sh.call("git", "add", file)
- else:
- resolved = False
- else:
- resolved = False
- return resolved
- def install(self, version, options):
- try:
- os.unlink("LocalSettings.php")
- except OSError:
- pass
-
- os.chmod("config", 0777) # XXX: vaguely sketchy
-
- postdata = {
- 'Sitename': options.title,
- 'EmergencyContact': options.email,
- 'LanguageCode': 'en',
- 'DBserver': options.mysql_host,
- 'DBname': options.mysql_db,
- 'DBuser': options.mysql_user,
- 'DBpassword': options.mysql_password,
- 'DBpassword2': options.mysql_password,
- 'defaultEmail': options.email,
- 'SysopName': options.admin_name,
- 'SysopPass': options.admin_password,
- 'SysopPass2': options.admin_password,
- }
- result = install.fetch(options, '/config/index.php', post=postdata)
- if options.verbose: print result
- if result.find("Installation successful") == -1:
- raise install.Failure()
- os.rename('config/LocalSettings.php', 'LocalSettings.php')
- def upgrade(self, d, version, options):
- sh = shell.Shell()
- if not os.path.isfile("AdminSettings.php"):
- sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
- try:
- result = sh.eval("php", "maintenance/update.php", "--quick", log=True)
- except shell.CallError as e:
- raise app.UpgradeFailure("Update script returned non-zero exit code\nSTDOUT: %s\nSTDERR: %s" % (e.stdout, e.stderr))
- results = result.rstrip().split()
- if not results or not results[-1] == "Done.":
- raise app.UpgradeFailure(result)
- def backup(self, deployment, options):
- sh = shell.Shell()
- # XXX: duplicate code, refactor, also, race condition
- backupdir = os.path.join(".scripts", "backups")
- backup = str(deployment.version) + "-" + datetime.date.today().isoformat()
- outdir = os.path.join(backupdir, backup)
- if not os.path.exists(backupdir):
- os.mkdir(backupdir)
- if os.path.exists(outdir):
- util.safe_unlink(outdir)
- os.mkdir(outdir)
- outfile = os.path.join(outdir, "db.sql")
- try:
- sh.call("mysqldump", "--compress", "-r", outfile, *get_mysql_args(deployment))
- sh.call("gzip", "--best", outfile)
- except shell.CallError as e:
- shutil.rmtree(outdir)
- raise app.BackupFailure(e.stderr)
- return backup
- def restore(self, deployment, backup, options):
- sh = shell.Shell()
- backup_dir = os.path.join(".scripts", "backups", backup)
- if not os.path.exists(backup_dir):
- raise app.RestoreFailure("Backup %s doesn't exist", backup)
- sql = open(os.path.join(backup_dir, "db.sql"), 'w+')
- sh.call("gunzip", "-c", os.path.join(backup_dir, "db.sql.gz"), stdout=sql)
- sql.seek(0)
- sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
- sql.close()
-
-def get_mysql_args(d):
- # XXX: add support for getting these out of options
- vars = d.extract()
- if 'WIZARD_DBNAME' not in vars:
- raise app.BackupFailure("Could not determine database name")
- triplet = scripts.get_sql_credentials(vars)
- args = []
- if triplet is not None:
- server, user, password = triplet
- args += ["-h", server, "-u", user, "-p" + password]
- name = shlex.split(vars['WIZARD_DBNAME'])[0]
- args.append(name)
- return args