~openstack-ubuntu-testing/charms/precise/keystone/trunk

« back to all changes in this revision

Viewing changes to hooks/lib/haproxy_utils.py

  • Committer: james.page at ubuntu
  • Date: 2014-04-29 13:05:20 UTC
  • mfrom: (12.23.23 keystone)
  • Revision ID: james.page@ubuntu.com-20140429130520-n358qfaw1vu0q1r6
Update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright 2012 Canonical Ltd.
3
 
#
4
 
# This file is sourced from lp:openstack-charm-helpers
5
 
#
6
 
# Authors:
7
 
#  James Page <james.page@ubuntu.com>
8
 
#  Adam Gandelman <adamg@ubuntu.com>
9
 
#
10
 
 
11
 
from lib.utils import (
12
 
    relation_ids,
13
 
    relation_list,
14
 
    relation_get,
15
 
    unit_get,
16
 
    reload,
17
 
    render_template)
18
 
import os
19
 
 
20
 
HAPROXY_CONF = '/etc/haproxy/haproxy.cfg'
21
 
HAPROXY_DEFAULT = '/etc/default/haproxy'
22
 
 
23
 
 
24
 
def configure_haproxy(service_ports):
25
 
    '''
26
 
    Configure HAProxy based on the current peers in the service
27
 
    cluster using the provided port map:
28
 
 
29
 
        "swift": [ 8080, 8070 ]
30
 
 
31
 
    HAproxy will also be reloaded/started if required
32
 
 
33
 
    service_ports: dict: dict of lists of [ frontend, backend ]
34
 
    '''
35
 
    cluster_hosts = {}
36
 
    cluster_hosts[os.getenv('JUJU_UNIT_NAME').replace('/', '-')] = \
37
 
        unit_get('private-address')
38
 
    for r_id in relation_ids('cluster'):
39
 
        for unit in relation_list(r_id):
40
 
            cluster_hosts[unit.replace('/', '-')] = \
41
 
                relation_get(attribute='private-address',
42
 
                             rid=r_id,
43
 
                             unit=unit)
44
 
    context = {
45
 
        'units': cluster_hosts,
46
 
        'service_ports': service_ports}
47
 
    with open(HAPROXY_CONF, 'w') as f:
48
 
        f.write(render_template(os.path.basename(HAPROXY_CONF),
49
 
                                context))
50
 
    with open(HAPROXY_DEFAULT, 'w') as f:
51
 
        f.write('ENABLED=1')
52
 
 
53
 
    reload('haproxy')