~james-page/charms/oneiric/mysql/charm-tester

« back to all changes in this revision

Viewing changes to formulas/mediawiki/hooks/cache-relation-changed

  • Committer: Francis J. Lacoste
  • Date: 2011-05-27 15:21:22 UTC
  • Revision ID: francis.lacoste@canonical.com-20110527152122-l8akt2g34hwz1zpj
Split out from principia-tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
import subprocess
3
 
import os
4
 
import tempfile
5
 
import pprint
6
 
import json
7
 
 
8
 
# all changes mean we should regen the file
9
 
memcached_ips = []
10
 
rl = subprocess.Popen("relation-list",stdout=subprocess.PIPE)
11
 
for memcached_unit in rl.stdout:
12
 
    p = subprocess.Popen(["relation-get", "--format", "json", "-", memcached_unit],
13
 
            stdout=subprocess.PIPE, close_fds=True)
14
 
    settings = json.loads(p.stdout.read().strip())
15
 
    try:
16
 
        if len(settings['ip']) and len(settings['port']):
17
 
            memcached_ips.append("'"+settings['ip']+":"+settings['port']+"'")
18
 
    except KeyError:
19
 
        print memcached_unit + " has no settings. ["+str(settings)+"]"
20
 
 
21
 
rl.wait()
22
 
 
23
 
pprint.pprint(memcached_ips)
24
 
 
25
 
memcache_template = """<?php
26
 
$wgMainCacheType = CACHE_MEMCACHED;
27
 
$wgMemCachedServers = array(%s);
28
 
$wgSessionsInMemcached = true;
29
 
"""
30
 
 
31
 
with tempfile.NamedTemporaryFile(dir='/etc/mediawiki',delete=False) as settings:
32
 
    subprocess.call(["chmod","0644",settings.name])
33
 
    if len(memcached_ips):
34
 
        settings.write(memcache_template % ','.join(memcached_ips))
35
 
    else:
36
 
        settings.write("<?php\n#No Memcached Servers\n")
37
 
 
38
 
    try:
39
 
        os.unlink('/etc/mediawiki/memcached_settings.php.old')
40
 
    except:
41
 
        pass
42
 
    os.rename('/etc/mediawiki/memcached_settings.php','/etc/mediawiki/memcached_settings.php.old')
43
 
    os.rename(settings.name, '/etc/mediawiki/memcached_settings.php')