3
# db-relation-changed - hook to configure mediawiki when db changes
5
# Copyright (C) 2011 Canonical Ltd.
6
# Author: Clint Byrum <clint.byrum@canonical.com>
8
# Copied and modified version of wordpress db-relation-changed by
9
# Kapil Thangavelu, also Copyright 2011 Canonical
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation, either version 3 of the License, or
14
# (at your option) any later version.
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
# GNU General Public License for more details.
21
# You should have received a copy of the GNU General Public License
22
# along with this program. If not, see <http://www.gnu.org/licenses/>.
33
upload_path = "/var/www/wp-uploads"
34
config_file_path_template = "/etc/mediawiki/config-%s.php"
35
hostname_url = "http://169.254.169.254/latest/meta-data/public-hostname"
39
p = subprocess.Popen(args, close_fds=True)
42
mediawiki_template = """\
44
$path = array( $IP, "$IP/includes", "$IP/languages" );
45
set_include_path( implode( PATH_SEPARATOR, $path ) . PATH_SEPARATOR . get_include_path() );
47
require_once( "$IP/includes/DefaultSettings.php" );
49
# If PHP's memory limit is very low, some operations may fail.
50
# ini_set( 'memory_limit', '20M' );
52
if ( $wgCommandLineMode ) {
53
if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
54
die( "This script must be run from the command line\n" );
57
## Uncomment this to disable output compression
58
# $wgDisableOutputCompression = true;
60
$wgSitename = "Ensemblized Wiki";
62
## The URL base path to the directory containing the wiki;
63
## defaults for all runtime URL paths are based off of this.
64
## For more information on customizing the URLs please see:
65
## http://www.mediawiki.org/wiki/Manual:Short_URL
66
$wgScriptPath = "/mediawiki";
67
$wgScriptExtension = ".php";
69
## UPO means: this is also a user preference option
71
$wgEnableEmail = true;
72
$wgEnableUserEmail = true; # UPO
74
$wgEmergencyContact = "webmaster@localhost";
75
$wgPasswordSender = "webmaster@localhost";
77
$wgEnotifUserTalk = true; # UPO
78
$wgEnotifWatchlist = true; # UPO
79
$wgEmailAuthentication = true;
83
$wgDBserver = "%(database_host)s";
84
$wgDBname = "%(database)s";
85
$wgDBuser = "%(database_user)s";
86
$wgDBpassword = "%(database_password)s";
87
include("/etc/mediawiki/dbservers.php");
89
# MySQL specific settings
92
# MySQL table options to use during installation or update
93
$wgDBTableOptions = "ENGINE=InnoDB, DEFAULT CHARSET=binary";
95
# Experimental charset support for MySQL 4.1/5.0.
98
## Shared memory settings
99
$wgMainCacheType = CACHE_NONE;
100
$wgMemCachedServers = array();
101
include dirname(__FILE__).'/memcached_settings.php';
103
## To enable image uploads, make sure the 'images' directory
104
## is writable, then set this to true:
105
$wgEnableUploads = false;
106
$wgUseImageMagick = true;
107
$wgImageMagickConvertCommand = "/usr/bin/convert";
109
## If you use ImageMagick (or any other shell command) on a
110
## Linux server, this will need to be set to the name of an
111
## available UTF-8 locale
112
$wgShellLocale = "en_US.utf8";
114
## If you want to use image uploads under safe mode,
115
## create the directories images/archive, images/thumb and
116
## images/temp, and make them all writable. Then uncomment
117
## this, if it's not already uncommented:
118
# $wgHashedUploadDirectory = false;
120
## If you have the appropriate support software installed
121
## you can enable inline LaTeX equations:
124
$wgLocalInterwiki = strtolower( $wgSitename );
126
$wgLanguageCode = "en";
128
$wgSecretKey = "%(secret_key)s";
130
## Default skin: you can change the default skin. Use the internal symbolic
131
## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook':
132
$wgDefaultSkin = 'monobook';
134
## For attaching licensing metadata to pages, and displaying an
135
## appropriate copyright notice / icon. GNU Free Documentation
136
## License and Creative Commons licenses are supported so far.
137
# $wgEnableCreativeCommonsRdf = true;
138
$wgRightsPage = ""; # Set to the title of a wiki page that describes your license/copyright
142
# $wgRightsCode = ""; # Not yet used
144
$wgDiff3 = "/usr/bin/diff3";
146
# debian specific include:
147
if (is_file("/etc/mediawiki-extensions/extensions.php")) {
148
include( "/etc/mediawiki-extensions/extensions.php" );
151
# When you make changes to this configuration file, this will make
152
# sure that cached pages are cleared.
153
$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
156
adminsettings_template = """
158
$wgDBadminuser = '%(database_user)s';
159
$wgDBadminpassword = '%(database_password)s';
160
$wgEnableProfileInfo = false;
163
def setup_mediawiki():
166
hostname = urllib.urlopen(hostname_url).read().strip()
168
hostname = 'localhost'
169
remote_unit = os.environ.get("ENSEMBLE_REMOTE_UNIT")
171
print "remote unit", remote_unit
173
# Check we haven't already been setup.
174
#if os.path.exists(config_file_path_template % hostname):
175
# print "Already Configured, Exiting"
178
p = subprocess.Popen(["relation-get", "--format", "json"],
179
stdout=subprocess.PIPE, close_fds=True)
180
settings = json.loads(p.stdout.read().strip())
181
if len(settings) == 0:
182
print "No settings yet, cannot configure."
187
pprint.pprint(settings)
189
# Get the database settings
190
database = settings.get("database")
191
password = settings.get("password")
192
user = settings.get("user")
193
host = settings.get("host")
195
# Generate this the same way the installer does
196
secret_key = open("/dev/urandom","r").read(32).encode('hex')
198
# Ensure the remote unit has self configured.
200
"database_host": host,
201
"database": database,
202
"database_password": password,
203
"database_user": user,
204
"hostname": hostname,
205
"secret_key": secret_key}
207
# Verify we have all of our database settings.
209
for k, v in config.items():
211
print "Do not have for %r: %r" % (k, v)
215
print "Could not fetch database settings, exiting."
218
# Enable the Alias in the apache config
219
do("sed", "-ie","s,^#Alias /mediawiki,Alias /mediawiki,","/etc/mediawiki/apache.conf")
222
do("/etc/init.d/apache2", "reload")
224
# Replace "It Works" with redirector to wiki
225
ofile=tempfile.NamedTemporaryFile(dir="/var/www",delete=False)
226
os.chmod(ofile.name, 0755)
227
ofile.write("<?php header('Location: /mediawiki/');\n")
230
os.unlink("/var/www/index.php")
231
print "index.php removed"
233
print "index.php not present"
234
os.rename(ofile.name, "/var/www/index.php")
236
# This seems to take precedence
238
os.unlink("/var/www/index.html")
239
print "index.html removed"
241
print "index.html not present"
243
# Write the adminsettings
244
connection = MySQLdb.connect(user=user, host=host, passwd=password, db=database)
245
cursor = connection.cursor()
248
# Try to create the "mediawiki_ensemble_setup" table and if its already there, skip some things
249
cursor.execute("create table mediawiki_ensemble_setup (id int)")
251
except Exception as e:
252
# If we can't create that table, it has likely already been initialized
253
print 'Could not create mediawiki_ensemble_setup: ' + str(e)
256
# First time setup, we have to POST to the db and capture the LocalSettings.php
257
postargs = {'Sitename':'Ensemblized Wiki',
258
'EmergencyContact':'webmaster@localhost',
261
'SysopName':'WikiSysop',
262
'SysopPass':'change-me',
263
'SysopPass2':'change-me',
265
'Email':'email_enabled',
266
'Emailuser':'emailuser_enabled',
267
'Enotif':'enotif_disabled',
268
'Eauthent':'eauthent_enabled',
273
'DBpassword':password,
274
'DBpassword2':password,
277
install = urllib.urlopen('http://localhost/mediawiki/config/index.php', urllib.urlencode(postargs))
278
print "install URL returned ",install.getcode()
279
except Exception as e:
280
print "WARNING: could not post to installer!"
282
# Sadly, we really have to discard this LocalSettings.php and re-build
283
# it using the template, so that joins can just generate it without
284
# trying to create the whole DB.
285
#do("mv","/var/lib/mediawiki/config/LocalSettings.php","/etc/mediawiki/LocalSettings.php.generated")
287
# Write the mediawiki config - broken until dbservers.php is created
288
fh = open('/etc/mediawiki/LocalSettings.php', "w")
289
do("chmod", "0644", '/etc/mediawiki/LocalSettings.php')
290
fh.write(mediawiki_template % config)
293
# Write the AdminSettings config for updates
294
fh = open('/etc/mediawiki/AdminSettings.php', "w")
295
do("chmod", "0644", '/etc/mediawiki/AdminSettings.php')
296
fh.write(adminsettings_template % config)
299
# Refresh slave settings
300
scriptpath = os.dirname(__file__)
301
subprocess.check_call(os.path.join([scriptpath,'combine-dbservers']))