~zirpu/charms/precise/haproxy/fix-stop-hook

« back to all changes in this revision

Viewing changes to examples/mediawiki/hooks/db-relation-changed

  • Committer: Clint Byrum
  • Date: 2011-02-10 23:28:05 UTC
  • Revision ID: clint@ubuntu.com-20110210232805-2z4tec7ngayoycgt
adding mediawiki example

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os
 
4
import urllib
 
5
import subprocess
 
6
import json
 
7
import time
 
8
 
 
9
change_type = os.environ.get("ENSEMBLE_CHANGE")
 
10
 
 
11
upload_path = "/var/www/wp-uploads"
 
12
config_file_path_template = "/etc/mediawiki/config-%s.php"
 
13
hostname_url = "http://169.254.169.254/latest/meta-data/public-hostname"
 
14
 
 
15
def do(*args):
 
16
    print "do", args
 
17
    p = subprocess.Popen(args, close_fds=True)
 
18
    os.waitpid(p.pid, 0)
 
19
 
 
20
mediawiki_template = """\
 
21
$path = array( $IP, "$IP/includes", "$IP/languages" );
 
22
set_include_path( implode( PATH_SEPARATOR, $path ) . PATH_SEPARATOR . get_include_path() );
 
23
 
 
24
require_once( "$IP/includes/DefaultSettings.php" );
 
25
 
 
26
# If PHP's memory limit is very low, some operations may fail.
 
27
# ini_set( 'memory_limit', '20M' );
 
28
 
 
29
if ( $wgCommandLineMode ) {
 
30
        if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
 
31
                die( "This script must be run from the command line\n" );
 
32
        }
 
33
}
 
34
## Uncomment this to disable output compression
 
35
# $wgDisableOutputCompression = true;
 
36
 
 
37
$wgSitename         = "Ensemblized Wiki";
 
38
 
 
39
## The URL base path to the directory containing the wiki;
 
40
## defaults for all runtime URL paths are based off of this.
 
41
## For more information on customizing the URLs please see:
 
42
## http://www.mediawiki.org/wiki/Manual:Short_URL
 
43
$wgScriptPath       = "/mediawiki";
 
44
$wgScriptExtension  = ".php";
 
45
 
 
46
## UPO means: this is also a user preference option
 
47
 
 
48
$wgEnableEmail      = true;
 
49
$wgEnableUserEmail  = true; # UPO
 
50
 
 
51
$wgEmergencyContact = "webmaster@localhost";
 
52
$wgPasswordSender = "webmaster@localhost";
 
53
 
 
54
$wgEnotifUserTalk = true; # UPO
 
55
$wgEnotifWatchlist = true; # UPO
 
56
$wgEmailAuthentication = true;
 
57
 
 
58
## Database settings
 
59
$wgDBtype           = "mysql";
 
60
$wgDBserver         = "%s(database_host)";
 
61
$wgDBname           = "%s(database)";
 
62
$wgDBuser           = "%s(database_user)";
 
63
$wgDBpassword       = "%s(database_password)";
 
64
 
 
65
# MySQL specific settings
 
66
$wgDBprefix         = "";
 
67
 
 
68
# MySQL table options to use during installation or update
 
69
$wgDBTableOptions   = "ENGINE=InnoDB, DEFAULT CHARSET=binary";
 
70
 
 
71
# Experimental charset support for MySQL 4.1/5.0.
 
72
$wgDBmysql5 = true;
 
73
 
 
74
## Shared memory settings
 
75
$wgMainCacheType = CACHE_NONE;
 
76
$wgMemCachedServers = array();
 
77
 
 
78
## To enable image uploads, make sure the 'images' directory
 
79
## is writable, then set this to true:
 
80
$wgEnableUploads       = false;
 
81
$wgUseImageMagick = true;
 
82
$wgImageMagickConvertCommand = "/usr/bin/convert";
 
83
 
 
84
## If you use ImageMagick (or any other shell command) on a
 
85
## Linux server, this will need to be set to the name of an
 
86
## available UTF-8 locale
 
87
$wgShellLocale = "en_US.utf8";
 
88
 
 
89
## If you want to use image uploads under safe mode,
 
90
## create the directories images/archive, images/thumb and
 
91
## images/temp, and make them all writable. Then uncomment
 
92
## this, if it's not already uncommented:
 
93
# $wgHashedUploadDirectory = false;
 
94
 
 
95
## If you have the appropriate support software installed
 
96
## you can enable inline LaTeX equations:
 
97
$wgUseTeX           = false;
 
98
 
 
99
$wgLocalInterwiki   = strtolower( $wgSitename );
 
100
 
 
101
$wgLanguageCode = "en";
 
102
 
 
103
$wgSecretKey = "%s(secret_key)";
 
104
 
 
105
## Default skin: you can change the default skin. Use the internal symbolic
 
106
## names, ie 'standard', 'nostalgia', 'cologneblue', 'monobook':
 
107
$wgDefaultSkin = 'monobook';
 
108
 
 
109
## For attaching licensing metadata to pages, and displaying an
 
110
## appropriate copyright notice / icon. GNU Free Documentation
 
111
## License and Creative Commons licenses are supported so far.
 
112
# $wgEnableCreativeCommonsRdf = true;
 
113
$wgRightsPage = ""; # Set to the title of a wiki page that describes your license/copyright
 
114
$wgRightsUrl = "";
 
115
$wgRightsText = "";
 
116
$wgRightsIcon = "";
 
117
# $wgRightsCode = ""; # Not yet used
 
118
 
 
119
$wgDiff3 = "/usr/bin/diff3";
 
120
 
 
121
# debian specific include:
 
122
if (is_file("/etc/mediawiki-extensions/extensions.php")) {
 
123
        include( "/etc/mediawiki-extensions/extensions.php" );
 
124
}
 
125
 
 
126
# When you make changes to this configuration file, this will make
 
127
# sure that cached pages are cleared.
 
128
$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
 
129
"""
 
130
 
 
131
adminsettings_template = """
 
132
<?php
 
133
$wgDBadminuser = '%s(database_admin_user)';
 
134
$wgDBadminpassword = '%s(database_admin_password)';
 
135
$wgEnableProfileInfo = false;
 
136
"""
 
137
 
 
138
def setup_mediawiki():
 
139
 
 
140
    hostname = urllib.urlopen(hostname_url).read().strip()
 
141
    remote_unit = os.environ.get("ENSEMBLE_REMOTE_UNIT")
 
142
 
 
143
    print "remote unit", remote_unit
 
144
 
 
145
    # Check we haven't already been setup.
 
146
    if os.path.exists(config_file_path_template % hostname):
 
147
        print "Already Configured, Exiting"
 
148
        return
 
149
 
 
150
    p = subprocess.Popen(["relation-get", "--format", "json", remote_unit],
 
151
                         stdout=subprocess.PIPE, close_fds=True)
 
152
    settings = json.loads(p.stdout.read().strip())
 
153
 
 
154
    print "Settings"
 
155
    import pprint
 
156
    pprint.pprint(settings)
 
157
 
 
158
    # Get the database settings
 
159
    database = settings.get("database")
 
160
    password = settings.get("password")
 
161
    user = settings.get("user")
 
162
    host = settings.get("host")
 
163
 
 
164
    # Ensure the remote unit has self configured.
 
165
    config = {
 
166
        "database_host": host,
 
167
        "database": database,
 
168
        "database_password": password,
 
169
        "database_user": user,
 
170
        "hostname": hostname,
 
171
        "secret_key": "wp"}
 
172
 
 
173
    # Verify we have all of our database settings.
 
174
    broken = False
 
175
    for k, v in config.items():
 
176
        if not v:
 
177
            print "Do not have for %r: %r" % (k, v)
 
178
            broken = True
 
179
 
 
180
    if broken:
 
181
        print "Could not fetch database settings, exiting."
 
182
        return
 
183
 
 
184
    # Write the adminsettings
 
185
    fh = open('/etc/mediawiki/AdminSettings.php', "w")
 
186
    do("chmod", "0644", '/etc/mediawiki/AdminSettings.php')
 
187
    fh.write(adminsettings_template % config)
 
188
    fh.close()
 
189
 
 
190
    # Write the mediawiki config
 
191
    fh = open('/etc/mediawiki/LocalSettings.php', "w")
 
192
    do("chmod", "0644", '/etc/mediawiki/LocalSettings.php')
 
193
    fh.write(mediawiki_template % config)
 
194
    fh.close()
 
195
 
 
196
    # Enable the Alias in the apache config
 
197
    do("sed", "-ie","'s,^#Alias /mediawiki,Alias /mediawiki,' /etc/mediawiki/apache.conf")
 
198
 
 
199
    # Restart apache
 
200
    do("/etc/init.d/apache2", "reload")
 
201
 
 
202
print change_type
 
203
 
 
204
 
 
205
if change_type == "modified":
 
206
    setup_mediawiki()