~lazypower/charms/trusty/haproxy/trunk

« back to all changes in this revision

Viewing changes to hooks/hooks.py

[jseutter] adds support for the backend service to specify errorfiles in the service configuration. If errorfiles are supplied, the haproxy charm will write them to /var/lib/haproxy/<service_name>/<http status>.html and configure haproxy to use them.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
 
3
import base64
3
4
import glob
4
5
import os
5
6
import re
34
35
default_haproxy_config_dir = "/etc/haproxy"
35
36
default_haproxy_config = "%s/haproxy.cfg" % default_haproxy_config_dir
36
37
default_haproxy_service_config_dir = "/var/run/haproxy"
 
38
default_haproxy_lib_dir = "/var/lib/haproxy"
37
39
service_affecting_packages = ['haproxy']
38
40
 
39
41
dupe_options = [
247
249
#                                         server_ip
248
250
#                                         server_port
249
251
#                                         server_options
 
252
#                       errorfiles: List of dicts
 
253
#                                   http_status: status to handle
 
254
#                                   content: base 64 content for HAProxy to
 
255
#                                            write to socket
250
256
#------------------------------------------------------------------------------
251
257
def create_listen_stanza(service_name=None, service_ip=None,
252
258
                         service_port=None, service_options=None,
253
 
                         server_entries=None):
 
259
                         server_entries=None, service_errorfiles=None):
254
260
    if service_name is None or service_ip is None or service_port is None:
255
261
        return None
256
262
    fe_options = []
284
290
    service_config.append("backend %s" % (service_name,))
285
291
    service_config.extend("    %s" % service_option.strip()
286
292
                          for service_option in be_options)
 
293
    if service_errorfiles is not None:
 
294
        for errorfile in service_errorfiles:
 
295
            path = os.path.join(default_haproxy_lib_dir,
 
296
                                "service_%s" % service_name,
 
297
                                "%s.http" % errorfile["http_status"])
 
298
            service_config.append(
 
299
                "    errorfile %s %s" % (errorfile["http_status"], path))
287
300
    if isinstance(server_entries, (list, tuple)):
288
301
        for (server_name, server_ip, server_port,
289
302
             server_options) in server_entries:
596
609
        log("Service: %s" % service_key)
597
610
        server_entries = service_config.get('servers')
598
611
 
 
612
        errorfiles = service_config.get('errorfiles', [])
 
613
        for errorfile in errorfiles:
 
614
            service_name = services_dict[service_key]['service_name']
 
615
            path = os.path.join(default_haproxy_lib_dir,
 
616
                                "service_%s" % service_name)
 
617
            if not os.path.exists(path):
 
618
                os.makedirs(path)
 
619
            full_path = os.path.join(path, "%s.http" % errorfile["http_status"])
 
620
            with open(full_path, 'w') as f:
 
621
                f.write(base64.b64decode(errorfile["content"]))
 
622
 
599
623
        service_name = service_config["service_name"]
600
624
        if not os.path.exists(default_haproxy_service_config_dir):
601
625
            os.mkdir(default_haproxy_service_config_dir, 0600)
606
630
                service_config['service_host'],
607
631
                service_config['service_port'],
608
632
                service_config['service_options'],
609
 
                server_entries))
 
633
                server_entries, errorfiles))
610
634
 
611
635
 
612
636
#------------------------------------------------------------------------------
613
 
# load_services: Convenience function that load the service snippet
 
637
# load_services: Convenience function that loads the service snippet
614
638
#                configuration from the filesystem.
615
639
#------------------------------------------------------------------------------
616
640
def load_services(service_name=None):