]> scripts.mit.edu Git - wizard.git/blob - wizard/app/wordpress.py
Update Wordpress script to handle salt random values.
[wizard.git] / wizard / app / wordpress.py
1 import os
2 import re
3 import logging
4 import distutils
5 import urlparse
6 import hashlib
7 import sqlalchemy.exc
8
9 from wizard import app, install, resolve, sql, util
10 from wizard.app import php
11
12 def make_filename_regex_define(var):
13     """See :ref:`versioning config <seed>` for more information."""
14     return 'wp-config.php', php.re_define(var)
15
16 seed = util.dictmap(make_filename_regex_define, {
17     # these funny names are due to convention set by MediaWiki
18     'WIZARD_DBSERVER': 'DB_HOST',
19     'WIZARD_DBNAME': 'DB_NAME',
20     'WIZARD_DBUSER': 'DB_USER',
21     'WIZARD_DBPASSWORD': 'DB_PASSWORD',
22     'WIZARD_SECRETKEY': 'SECRET_KEY',
23     'WIZARD_AUTH_KEY': 'AUTH_KEY',
24     'WIZARD_SECURE_AUTH_KEY': 'SECURE_AUTH_KEY',
25     'WIZARD_LOGGED_IN_KEY': 'LOGGED_IN_KEY',
26     'WIZARD_NONCE_KEY': 'NONCE_KEY',
27     'WIZARD_AUTH_SALT': 'AUTH_SALT',
28     'WIZARD_SECURE_AUTH_SALT': 'SECURE_AUTH_SALT',
29     'WIZARD_LOGGED_IN_SALT': 'LOGGED_IN_SALT',
30     'WIZARD_NONCE_SALT': 'NONCE_SALT',
31     })
32
33 class Application(app.Application):
34     database = "mysql"
35     parametrized_files = ['wp-config.php'] + php.parametrized_files
36     extractors = app.make_extractors(seed)
37     extractors.update(php.extractors)
38     substitutions = app.make_substitutions(seed)
39     substitutions.update(php.substitutions)
40     install_schema = install.ArgSchema("db", "admin", "email", "title")
41     deprecated_keys = set(['WIZARD_SECRETKEY'])
42     random_keys = set(['WIZARD_SECRETKEY', 'WIZARD_AUTH_KEY', 'WIZARD_SECURE_AUTH_KEY', 'WIZARD_LOGGED_IN_KEY', 'WIZARD_NONCE_KEY'])
43     random_blacklist = set(['put your unique phrase here'])
44     def urlFromExtract(self, deployment):
45         try:
46             meta = sql.connect(deployment.dsn)
47             wp_options = meta.tables["wp_options"]
48             query = wp_options.select(wp_options.c.option_name == 'home')
49             return query.execute().fetchone()['option_value']
50         except sqlalchemy.exc.OperationalError:
51             return None
52     def download(self, version):
53         return "http://wordpress.org/wordpress-%s.tar.gz" % version
54     def checkConfig(self, deployment):
55         return os.path.isfile("wp-config.php")
56     def checkWeb(self, deployment):
57         return self.checkWebPage(deployment, "",
58                 outputs=["<html", "WordPress", "feed"],
59                 exclude=["Error establishing a database connection"])
60     def detectVersion(self, deployment):
61         return self.detectVersionFromFile("wp-includes/version.php", php.re_var("wp_version"))
62     def install(self, version, options):
63         util.soft_unlink("wp-config.php")
64
65         post_setup_config = {
66                 'dbhost': options.dsn.host,
67                 'uname': options.dsn.username,
68                 'dbname': options.dsn.database,
69                 'pwd': options.dsn.password,
70                 'prefix': '',
71                 'submit': 'Submit',
72                 'step': '2',
73                 }
74         post_install = {
75                 'weblog_title': options.title,
76                 'admin_email': options.email,
77                 'submit': 'Continue',
78                 'step': '2',
79                 # Version >= 3.0
80                 'user_name': options.admin_name,
81                 'admin_password': options.admin_password,
82                 'admin_password2': options.admin_password,
83                 }
84         old_mode = os.stat(".").st_mode
85         os.chmod(".", 0777) # XXX: squick squick
86
87         # we need to disable the wp_mail function in wp-includes/pluggable[-functions].php
88         pluggable_path = os.path.exists('wp-includes/pluggable.php') and 'wp-includes/pluggable.php' or 'wp-includes/pluggable-functions.php'
89         pluggable = open(pluggable_path, 'r').read()
90         wp_mail_noop = "<?php function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) { /*noop*/ } ?> \n\n"
91         pluggable_file = open(pluggable_path,'w')
92         pluggable_file.write(wp_mail_noop)
93         pluggable_file.write(pluggable)
94         pluggable_file.close()
95
96         result = install.fetch(options, "wp-admin/setup-config.php?step=2", post_setup_config)
97         logging.debug("setup-config.php output\n\n" + result)
98         result = install.fetch(options, "wp-admin/install.php?step=2", post_install)
99         logging.debug("install.php output\n\n" + result)
100         os.chmod(".", old_mode)
101         if "Finished" not in result and "Success" not in result:
102             raise app.InstallFailure()
103
104         # not sure what to do about this
105         meta = sql.connect(options.dsn)
106         wp_options = meta.tables["wp_options"]
107         wp_options.update().where(wp_options.c.option_name == 'siteurl').values(option_value=options.web_path).execute()
108         wp_options.update().where(wp_options.c.option_name == 'home').values(option_value="http://%s%s" % (options.web_host, options.web_path)).execute() # XXX: what if missing leading slash; this should be put in a function
109
110         if version < distutils.version.LooseVersion("3.0"):
111             wp_users = meta.tables["wp_users"]
112             hashed_pass = hashlib.md5(options.admin_password).hexdigest()
113             wp_users.update().where(wp_users.c.ID == 1).values(user_login=options.admin_name,user_nicename=options.admin_name,display_name=options.admin_name,user_pass=hashed_pass).execute()
114             wp_usermeta = meta.tables["wp_usermeta"]
115             wp_usermeta.delete().where(wp_usermeta.c.user_id==1 and wp_usermeta.c.meta_key == "default_password_nag").execute()
116
117         # now we can restore the wp_mail function in wp-includes/pluggable[-functions].php
118         pluggable_file = open(pluggable_path,'w')
119         pluggable_file.write(pluggable)
120         pluggable_file.close()
121
122         # replace random variable stubs with real values
123         old_config = open('wp-config.php').read()
124         def replace_with_random(s):
125             return s.replace('put your unique phrase here', util.random_key(), 1)
126         config = replace_with_random(old_config)
127         while config != old_config:
128             old_config = config
129             config = replace_with_random(config)
130         open('wp-config.php', 'w').write(config)
131
132         php.ini_replace_vars()
133     def upgrade(self, d, version, options):
134         result = d.fetch("wp-admin/upgrade.php?step=1")
135         if "Upgrade Complete" not in result and "No Upgrade Required" not in result:
136             raise app.UpgradeFailure(result)
137     def backup(self, deployment, backup_dir, options):
138         app.backup_database(backup_dir, deployment)
139     def restore(self, deployment, backup_dir, options):
140         app.restore_database(backup_dir, deployment)
141     def remove(self, deployment, options):
142         app.remove_database(deployment)
143
144 Application.resolutions = {
145 'wp-config.php': [
146     ("""
147 <<<<<<<
148
149 /** WordPress absolute path to the Wordpress directory. */
150 |||||||
151 /** WordPress absolute path to the Wordpress directory. */
152 =======
153 /** Absolute path to the WordPress directory. */
154 >>>>>>>
155 """, [0])
156 ],
157 }