~1chb1n/charms/trusty/keystone/next-amulet-mitaka-1601

« back to all changes in this revision

Viewing changes to hooks/lib/openstack_common.py

  • Committer: James Page
  • Date: 2013-01-07 12:37:48 UTC
  • mto: (38.18.2 avoid_restarts)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: james.page@canonical.com-20130107123748-k0nsfdofhyrmdw26
Switch haproxy configuration file generation to jinja2

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# Common python helper functions used for OpenStack charms.
4
4
 
5
5
import subprocess
 
6
import os
6
7
 
7
8
CLOUD_ARCHIVE_URL = "http://ubuntu-cloud.archive.canonical.com/ubuntu"
8
9
CLOUD_ARCHIVE_KEY_ID = '5EDB1B62EC4926EA'
194
195
 
195
196
HAPROXY_CONF = '/etc/haproxy/haproxy.cfg'
196
197
HAPROXY_DEFAULT = '/etc/default/haproxy'
197
 
HAPROXY_CONTENT = """global
198
 
    log 127.0.0.1 local0
199
 
    log 127.0.0.1 local1 notice
200
 
    maxconn 4096
201
 
    user haproxy
202
 
    group haproxy
203
 
    spread-checks 0
204
 
 
205
 
defaults
206
 
    log global
207
 
    mode http
208
 
    option httplog
209
 
    option dontlognull
210
 
    retries 3
211
 
    timeout queue 1000
212
 
    timeout connect 1000
213
 
    timeout client 1000
214
 
    timeout server 1000
215
 
 
216
 
listen stats :8888
217
 
    mode http
218
 
    stats enable
219
 
    stats hide-version
220
 
    stats realm Haproxy\ Statistics
221
 
    stats uri /
222
 
    stats auth admin:password
223
 
"""
224
 
SERVICE_FRAGMENT = """listen {0} 0.0.0.0:{1}
225
 
    balance  roundrobin
226
 
    option  tcplog
227
 
"""
228
 
SERVER_ENTRY = """    server {0} {1}:{2} check
229
 
"""
230
 
 
231
 
 
232
 
def configure_haproxy(units, service_ports):
233
 
    conf = HAPROXY_CONTENT
234
 
    for service, port in service_ports.iteritems():
235
 
        conf = conf + SERVICE_FRAGMENT.format(service,
236
 
                                              port)
237
 
        for unit, address in units.iteritems():
238
 
            conf = conf + SERVER_ENTRY.format(unit,
239
 
                                              address,
240
 
                                              port-1)
 
198
 
 
199
 
 
200
def configure_haproxy(units, service_ports, template_dir=None):
 
201
    template_dir = template_dir or 'templates'
 
202
    import jinja2
 
203
    context = {
 
204
        'units': units,
 
205
        'service_ports': service_ports
 
206
        }
 
207
    templates = jinja2.Environment(
 
208
                    loader=jinja2.FileSystemLoader(template_dir)
 
209
                    )
 
210
    template = templates.get_template(
 
211
                    os.path.basename(HAPROXY_CONF)
 
212
                    )
241
213
    with open(HAPROXY_CONF, 'w') as f:
242
 
        f.write(conf)
 
214
        f.write(template.render(context))
243
215
    with open(HAPROXY_DEFAULT, 'w') as f:
244
216
        f.write('ENABLED=1')
245