~free.ekanayaka/charms/trusty/haproxy/ciphers-support

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Charles Butler
  • Date: 2014-10-01 21:23:19 UTC
  • Revision ID: chuck@dasroot.net-20141001212319-803t8ofuo9yk1nam
Fixes for lint make target

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
from charmhelpers.contrib.charmsupport import nrpe
31
31
 
32
32
 
33
 
###############################################################################
 
33
# ##############################################################################
34
34
# Global variables
35
 
###############################################################################
 
35
# ##############################################################################
36
36
default_haproxy_config_dir = "/etc/haproxy"
37
37
default_haproxy_config = "%s/haproxy.cfg" % default_haproxy_config_dir
38
38
default_haproxy_service_config_dir = "/var/run/haproxy"
80
80
    ]
81
81
 
82
82
 
83
 
###############################################################################
 
83
# ##############################################################################
84
84
# Supporting functions
85
 
###############################################################################
 
85
# ##############################################################################
86
86
 
87
87
def comma_split(value):
88
88
    values = value.split(",")
107
107
    return template.render(vars)
108
108
 
109
109
 
110
 
#------------------------------------------------------------------------------
 
110
# ------------------------------------------------------------------------------
111
111
# enable_haproxy:  Enabled haproxy at boot time
112
 
#------------------------------------------------------------------------------
 
112
# ------------------------------------------------------------------------------
113
113
def enable_haproxy():
114
114
    default_haproxy = "/etc/default/haproxy"
115
115
    with open(default_haproxy) as f:
118
118
        f.write(enabled_haproxy)
119
119
 
120
120
 
121
 
#------------------------------------------------------------------------------
 
121
# ------------------------------------------------------------------------------
122
122
# create_haproxy_globals:  Creates the global section of the haproxy config
123
 
#------------------------------------------------------------------------------
 
123
# ------------------------------------------------------------------------------
124
124
def create_haproxy_globals():
125
125
    config_data = config_get()
126
126
    global_log = comma_split(config_data['global_log'])
143
143
    return '\n'.join(haproxy_globals)
144
144
 
145
145
 
146
 
#------------------------------------------------------------------------------
 
146
# ------------------------------------------------------------------------------
147
147
# create_haproxy_defaults:  Creates the defaults section of the haproxy config
148
 
#------------------------------------------------------------------------------
 
148
# ------------------------------------------------------------------------------
149
149
def create_haproxy_defaults():
150
150
    config_data = config_get()
151
151
    default_options = comma_split(config_data['default_options'])
162
162
    return '\n'.join(haproxy_defaults)
163
163
 
164
164
 
165
 
#------------------------------------------------------------------------------
 
165
# ------------------------------------------------------------------------------
166
166
# load_haproxy_config:  Convenience function that loads (as a string) the
167
167
#                       current haproxy configuration file.
168
168
#                       Returns a string containing the haproxy config or
169
169
#                       None
170
 
#------------------------------------------------------------------------------
 
170
# ------------------------------------------------------------------------------
171
171
def load_haproxy_config(haproxy_config_file="/etc/haproxy/haproxy.cfg"):
172
172
    if os.path.isfile(haproxy_config_file):
173
173
        return open(haproxy_config_file).read()
175
175
        return None
176
176
 
177
177
 
178
 
#------------------------------------------------------------------------------
 
178
# ------------------------------------------------------------------------------
179
179
# get_monitoring_password:  Gets the monitoring password from the
180
180
#                           haproxy config.
181
181
#                           This prevents the password from being constantly
182
182
#                           regenerated by the system.
183
 
#------------------------------------------------------------------------------
 
183
# ------------------------------------------------------------------------------
184
184
def get_monitoring_password(haproxy_config_file="/etc/haproxy/haproxy.cfg"):
185
185
    haproxy_config = load_haproxy_config(haproxy_config_file)
186
186
    if haproxy_config is None:
192
192
        return None
193
193
 
194
194
 
195
 
#------------------------------------------------------------------------------
 
195
# ------------------------------------------------------------------------------
196
196
# get_service_ports:  Convenience function that scans the existing haproxy
197
197
#                     configuration file and returns a list of the existing
198
198
#                     ports being used.  This is necessary to know which ports
199
199
#                     to open and close when exposing/unexposing a service
200
 
#------------------------------------------------------------------------------
 
200
# ------------------------------------------------------------------------------
201
201
def get_service_ports(haproxy_config_file="/etc/haproxy/haproxy.cfg"):
202
202
    stanzas = get_listen_stanzas(haproxy_config_file=haproxy_config_file)
203
203
    return tuple((int(port) for service, addr, port in stanzas))
204
204
 
205
205
 
206
 
#------------------------------------------------------------------------------
 
206
# ------------------------------------------------------------------------------
207
207
# get_listen_stanzas: Convenience function that scans the existing haproxy
208
208
#                     configuration file and returns a list of the existing
209
209
#                     listen stanzas cofnigured.
210
 
#------------------------------------------------------------------------------
 
210
# ------------------------------------------------------------------------------
211
211
def get_listen_stanzas(haproxy_config_file="/etc/haproxy/haproxy.cfg"):
212
212
    haproxy_config = load_haproxy_config(haproxy_config_file)
213
213
    if haproxy_config is None:
224
224
                   for addr, port, service in bind_stanzas)))
225
225
 
226
226
 
227
 
#------------------------------------------------------------------------------
 
227
# ------------------------------------------------------------------------------
228
228
# update_service_ports:  Convenience function that evaluate the old and new
229
229
#                        service ports to decide which ports need to be
230
230
#                        opened and which to close
231
 
#------------------------------------------------------------------------------
 
231
# ------------------------------------------------------------------------------
232
232
def update_service_ports(old_service_ports=None, new_service_ports=None):
233
233
    if old_service_ports is None or new_service_ports is None:
234
234
        return None
239
239
        open_port(port)
240
240
 
241
241
 
242
 
#------------------------------------------------------------------------------
 
242
# ------------------------------------------------------------------------------
243
243
# update_sysctl: create a sysctl.conf file from YAML-formatted 'sysctl' config
244
 
#------------------------------------------------------------------------------
 
244
# ------------------------------------------------------------------------------
245
245
def update_sysctl(config_data):
246
246
    sysctl_dict = yaml.load(config_data.get("sysctl", "{}"))
247
247
    if sysctl_dict:
252
252
        subprocess.call(["sysctl", "-p", "/etc/sysctl.d/50-haproxy.conf"])
253
253
 
254
254
 
255
 
#------------------------------------------------------------------------------
 
255
# ------------------------------------------------------------------------------
256
256
# create_listen_stanza: Function to create a generic listen section in the
257
257
#                       haproxy config
258
258
#                       service_name:  Arbitrary service name
268
268
#                                   http_status: status to handle
269
269
#                                   content: base 64 content for HAProxy to
270
270
#                                            write to socket
271
 
#------------------------------------------------------------------------------
 
271
# ------------------------------------------------------------------------------
272
272
def create_listen_stanza(service_name=None, service_ip=None,
273
273
                         service_port=None, service_options=None,
274
274
                         server_entries=None, service_errorfiles=None):
327
327
    return '\n'.join(service_config)
328
328
 
329
329
 
330
 
#------------------------------------------------------------------------------
 
330
# ------------------------------------------------------------------------------
331
331
# create_monitoring_stanza:  Function to create the haproxy monitoring section
332
332
#                            service_name: Arbitrary name
333
 
#------------------------------------------------------------------------------
 
333
# ------------------------------------------------------------------------------
334
334
def create_monitoring_stanza(service_name="haproxy_monitoring"):
335
335
    config_data = config_get()
336
336
    if config_data['enable_monitoring'] is False:
360
360
                                monitoring_config)
361
361
 
362
362
 
363
 
#------------------------------------------------------------------------------
 
363
# ------------------------------------------------------------------------------
364
364
# get_config_services:  Convenience function that returns a mapping containing
365
365
#                       all of the services configuration
366
 
#------------------------------------------------------------------------------
 
366
# ------------------------------------------------------------------------------
367
367
def get_config_services():
368
368
    config_data = config_get()
369
369
    services = {}
430
430
    seen = []
431
431
    missing = []
432
432
    for service, options in sorted(services.iteritems()):
433
 
        if not "service_host" in options:
 
433
        if "service_host" not in options:
434
434
            missing.append(options)
435
435
            continue
436
 
        if not "service_port" in options:
 
436
        if "service_port" not in options:
437
437
            missing.append(options)
438
438
            continue
439
439
        seen.append((options["service_host"], int(options["service_port"])))
448
448
    return services
449
449
 
450
450
 
451
 
#------------------------------------------------------------------------------
 
451
# ------------------------------------------------------------------------------
452
452
# get_config_service:   Convenience function that returns a dictionary
453
453
#                       of the configuration of a given service's configuration
454
 
#------------------------------------------------------------------------------
 
454
# ------------------------------------------------------------------------------
455
455
def get_config_service(service_name=None):
456
456
    return get_config_services().get(service_name, None)
457
457
 
462
462
    return os.path.exists(flag_path)
463
463
 
464
464
 
465
 
#------------------------------------------------------------------------------
 
465
# ------------------------------------------------------------------------------
466
466
# create_services:  Function that will create the services configuration
467
467
#                   from the config data and/or relation information
468
 
#------------------------------------------------------------------------------
 
468
# ------------------------------------------------------------------------------
469
469
def create_services():
470
470
    services_dict = get_config_services()
471
471
 
496
496
 
497
497
        relation_ok = True
498
498
        for required in ("port", "private-address"):
499
 
            if not required in relation_info:
 
499
            if required not in relation_info:
500
500
                log("No %s in relation data for '%s', skipping." %
501
501
                    (required, unit))
502
502
                relation_ok = False
655
655
                server_entries, errorfiles))
656
656
 
657
657
 
658
 
#------------------------------------------------------------------------------
 
658
# ------------------------------------------------------------------------------
659
659
# load_services: Convenience function that loads the service snippet
660
660
#                configuration from the filesystem.
661
 
#------------------------------------------------------------------------------
 
661
# ------------------------------------------------------------------------------
662
662
def load_services(service_name=None):
663
663
    services = ''
664
664
    if service_name is not None:
678
678
    return services
679
679
 
680
680
 
681
 
#------------------------------------------------------------------------------
 
681
# ------------------------------------------------------------------------------
682
682
# remove_services:  Convenience function that removes the configuration
683
683
#                   snippets from the filesystem.  This is necessary
684
684
#                   To ensure sync between the config/relation-data
685
685
#                   and the existing haproxy services.
686
 
#------------------------------------------------------------------------------
 
686
# ------------------------------------------------------------------------------
687
687
def remove_services(service_name=None):
688
688
    if service_name is not None:
689
689
        path = "%s/%s.service" % (default_haproxy_service_config_dir,
706
706
        return True
707
707
 
708
708
 
709
 
#------------------------------------------------------------------------------
 
709
# ------------------------------------------------------------------------------
710
710
# construct_haproxy_config:  Convenience function to write haproxy.cfg
711
711
#                            haproxy_globals, haproxy_defaults,
712
712
#                            haproxy_monitoring, haproxy_services
714
714
#                            any checks.
715
715
#                            haproxy_monitoring and haproxy_services are
716
716
#                            optional arguments
717
 
#------------------------------------------------------------------------------
 
717
# ------------------------------------------------------------------------------
718
718
def construct_haproxy_config(haproxy_globals=None,
719
719
                             haproxy_defaults=None,
720
720
                             haproxy_monitoring=None,
730
730
        haproxy_config.write(config_string)
731
731
 
732
732
 
733
 
#------------------------------------------------------------------------------
 
733
# ------------------------------------------------------------------------------
734
734
# service_haproxy:  Convenience function to start/stop/restart/reload
735
735
#                   the haproxy service
736
 
#------------------------------------------------------------------------------
 
736
# ------------------------------------------------------------------------------
737
737
def service_haproxy(action=None, haproxy_config=default_haproxy_config):
738
738
    if None in (action, haproxy_config):
739
739
        return None
745
745
    return return_value == 0
746
746
 
747
747
 
748
 
###############################################################################
 
748
# ##############################################################################
749
749
# Hook functions
750
 
###############################################################################
 
750
# ##############################################################################
751
751
def install_hook():
752
752
    # Run both during initial install and during upgrade-charm.
753
753
    if not os.path.exists(default_haproxy_service_config_dir):
969
969
        }))
970
970
 
971
971
 
972
 
###############################################################################
 
972
# ##############################################################################
973
973
# Main section
974
 
###############################################################################
 
974
# ##############################################################################
975
975
 
976
976
 
977
977
def main(hook_name):