2 import distutils.version
9 from wizard import app, deploy, install, scripts, shell, util
10 from wizard.app import php
12 def make_filename_regex(var):
13 return 'LocalSettings.php', re.compile('^(\$' + app.expand_re(var) + r'''\s*=\s*)(.*)(;)''', re.M)
15 make_extractor = app.filename_regex_extractor(make_filename_regex)
16 make_substitution = app.filename_regex_substitution(make_filename_regex)
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'),
29 class Application(deploy.Application):
30 parametrized_files = ['LocalSettings.php', 'php.ini']
31 deprecated_keys = set(['WIZARD_IP']) | php.deprecated_keys
34 if not self._extractors:
35 self._extractors = util.dictmap(make_extractor, seed)
36 self._extractors.update(php.extractors)
37 return self._extractors
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
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"))
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")
61 return page.find("<!-- Served") != -1
62 def install(self, version, options):
64 os.unlink("LocalSettings.php")
68 os.chmod("config", 0777) # XXX: vaguely sketchy
71 'Sitename': options.title,
72 'EmergencyContact': options.email,
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,
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):
91 if not os.path.isfile("AdminSettings.php"):
92 sh.call("git", "checkout", "-q", "mediawiki-" + str(version), "--", "AdminSettings.php")
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):
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):
108 if os.path.exists(outdir):
109 util.safe_unlink(outdir)
111 outfile = os.path.join(outdir, "db.sql")
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)
119 def restore(self, deployment, backup, options):
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)
127 sh.call("mysql", *get_mysql_args(deployment), stdin=sql)
130 def get_mysql_args(d):
131 # XXX: add support for getting these out of options
133 if 'WIZARD_DBNAME' not in vars:
134 raise app.BackupFailure("Could not determine database name")
135 triplet = scripts.get_sql_credentials(vars)
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]