~erik-lonroth/charm-haproxy/charmcraft_build

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: mergebot at canonical
  • Author(s): "Martin Hilton"
  • Date: 2019-05-28 15:21:02 UTC
  • mfrom: (108.5.5 add-userlists-support)
  • Revision ID: mergebot@juju-139df4-prod-is-toolbox-0.canonical.com-20190528152102-rwlpjzbxh61n2sdy
Add userlist support

Reviewed-on: https://code.launchpad.net/~martin-hilton/charm-haproxy/add-userlists-support/+merge/367581
Reviewed-by: Joel Sing <joel.sing@canonical.com>
Reviewed-by: Stuart Bishop <stuart.bishop@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
196
196
 
197
197
 
198
198
# -----------------------------------------------------------------------------
 
199
# create_haproxy_userlists:  Creates the userlist sections of the haproxy
 
200
# config
 
201
# -----------------------------------------------------------------------------
 
202
def create_haproxy_userlists(userlists=None):
 
203
    if userlists is None:
 
204
        userlists = config_get()["userlists"]
 
205
    userlists = yaml.safe_load(userlists)
 
206
    if not userlists:
 
207
        return ''
 
208
    result = []
 
209
    for l in userlists:
 
210
        for userlist, v in l.items():
 
211
            result.append('userlist ' + userlist)
 
212
            for group in v['groups']:
 
213
                    result.append('    group ' + group)
 
214
            for user in v['users']:
 
215
                    result.append('    user ' + user)
 
216
    return '\n'.join(result)
 
217
 
 
218
 
 
219
# -----------------------------------------------------------------------------
199
220
# load_haproxy_config:  Convenience function that loads (as a string) the
200
221
#                       current haproxy configuration file.
201
222
#                       Returns a string containing the haproxy config or
566
587
    config_data = config_get()
567
588
    seen = []
568
589
    missing = []
569
 
    for service, options in sorted(services.iteritems()):
 
590
    for service, options in sorted(services.items()):
570
591
        if "service_host" not in options:
571
592
            missing.append(options)
572
593
            continue
692
713
                                'server_options', [])))
693
714
 
694
715
    has_servers = False
695
 
    for service_name, service in services_dict.iteritems():
 
716
    for service_name, service in services_dict.items():
696
717
        if service.get("servers", []):
697
718
            has_servers = True
698
719
 
738
759
 
739
760
    unit_name = os.environ["JUJU_UNIT_NAME"].replace("/", "-")
740
761
    private_address = unit_get("private-address")
741
 
    for service_name, peer_service in peer_services.iteritems():
 
762
    for service_name, peer_service in peer_services.items():
742
763
        original_service = services_dict[service_name]
743
764
 
744
765
        # If the original service has timeout settings, copy them over to the
884
905
def construct_haproxy_config(haproxy_globals=None,
885
906
                             haproxy_defaults=None,
886
907
                             haproxy_monitoring=None,
887
 
                             haproxy_services=None):
 
908
                             haproxy_services=None,
 
909
                             haproxy_userlists=None):
888
910
    if None in (haproxy_globals, haproxy_defaults):
889
911
        return
890
912
    with open(default_haproxy_config, 'w') as haproxy_config:
891
913
        config_string = ''
892
 
        for config in (haproxy_globals, haproxy_defaults, haproxy_monitoring,
 
914
        for config in (haproxy_globals, haproxy_defaults, haproxy_userlists,
 
915
                       haproxy_monitoring,
893
916
                       haproxy_services):
894
917
            if config is not None:
895
918
                config_string += config + '\n\n'
957
980
 
958
981
    old_stanzas = get_listen_stanzas()
959
982
    haproxy_globals = create_haproxy_globals()
 
983
    haproxy_userlists = create_haproxy_userlists()
960
984
    haproxy_defaults = create_haproxy_defaults()
961
985
    if config_data['enable_monitoring'] is True:
962
986
        haproxy_monitoring = create_monitoring_stanza()
975
999
    construct_haproxy_config(haproxy_globals,
976
1000
                             haproxy_defaults,
977
1001
                             haproxy_monitoring,
978
 
                             haproxy_services)
 
1002
                             haproxy_services,
 
1003
                             haproxy_userlists)
979
1004
 
980
1005
    write_metrics_cronjob(metrics_script_path,
981
1006
                          metrics_cronjob_path)