~george-edison55/charms/precise/uwsgi/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python

import json
import os

from charmhelpers import fetch
from charmhelpers.core import hookenv

#####################
# UTILITY FUNCTIONS #
#####################

UWSGI_CONFIG_DIR = '/etc/uwsgi/apps-enabled'

def update_uwsgi_app_config(app, config):
    """Update the specified uWSGI app with the new config."""
    with open(os.path.join(UWSGI_CONFIG_DIR, app), 'r+') as f:
        app_config = json.loads(f.read())
        app_config['uwsgi'].update({
            'processes': config['uwsgi_processes'],
            'threads':   config['uwsgi_threads'],
            'uid':       config['uwsgi_uid'],
            'gid':       config['uwsgi_gid'],
        })
        f.seek(0)
        f.write(json.dumps(app_config))
        f.truncate()

def update_all_uwsgi_apps(config):
    """Write the updated config to all uWSGI apps."""
    for app in os.listdir(UWSGI_CONFIG_DIR):
        update_uwsgi_app_config(app, config)

##################
# HOOK FUNCTIONS #
##################

@hooks.hook('install', 'upgrade-charm')
def install():
    """Install or upgrade the charm."""
    fetch.apt_update()
    fetch.apt_install(['uwsgi',])

@hooks.hook('config-changed')
def config_changed():
    """Update all configuration files."""
    update_all_uwsgi_files(hookenv.config())