diff --git a/imageroot/bin/redis2yml b/imageroot/bin/redis2yml index 0b84b37..a37d978 100755 --- a/imageroot/bin/redis2yml +++ b/imageroot/bin/redis2yml @@ -7,6 +7,7 @@ import os import sys +import copy import json import agent import tempfile @@ -18,7 +19,7 @@ try: except: instance = 'traefik1' -tls = { "tls": { "stores": { "default": { "defaultCertificate": { "certFile": "", "keyFile": "" } } } } } +config_tls = { "tls": { "stores": { "default": { "defaultCertificate": { "certFile": "", "keyFile": "" } } } } } middlewares = {} services = {} routers = {} @@ -28,7 +29,7 @@ rdb = agent.redis_connect(host='127.0.0.1', privileged = True) # TLS default certificate for key in ["certFile", "keyFile"]: - tls["tls"]["stores"]["default"]["defaultCertificate"][key] = rdb.get(f'{prefix}tls/stores/default/defaultCertificate/{key}') + config_tls["tls"]["stores"]["default"]["defaultCertificate"][key] = rdb.get(f'{prefix}tls/stores/default/defaultCertificate/{key}') # HTTP for kv in rdb.scan_iter(f'{prefix}http/*'): @@ -44,6 +45,8 @@ for kv in rdb.scan_iter(f'{prefix}http/*'): else: middlewares[tmp[1]] = { tmp[2]: { tmp[3]: {} } } if last.isnumeric(): + if tmp[3] not in middlewares[tmp[1]][tmp[2]]: + middlewares[tmp[1]][tmp[2]][tmp[3]] = list() middlewares[tmp[1]][tmp[2]][tmp[3]].append(value) else: middlewares[tmp[1]][tmp[2]][tmp[3]] = value @@ -78,10 +81,40 @@ for kv in rdb.scan_iter(f'{prefix}http/*'): for r in routers: if "tls" in routers[r] and (not routers[r]["tls"]["certresolver"] and not routers[r]["tls"]["domains"]): routers[r]["tls"] = {} -# TODO: delete key -rdb.close() -print(yaml.dump(tls)) +# Generate config files +os.makedirs('configs', exist_ok = True) +with open('configs/_default_cert.yml', 'w') as fpc: + fpc.write(yaml.safe_dump(config_tls, default_flow_style=False, sort_keys=False, allow_unicode=True)) + +config_middleware = {"http": {"middlewares": {'http2https-redirectscheme': middlewares.pop('http2https-redirectscheme')}}} +with open('configs/_http2https.yml', 'w') as fpm: + fpm.write(yaml.safe_dump(config_middleware, default_flow_style=False, sort_keys=False, allow_unicode=True)) + +route_api = routers.pop('ApisEndpointHttp') +route_api['priority'] = '10000000' +config_api = {"http": {"middlewares": {'ApisEndpointMw0': middlewares.pop('ApisEndpointMw0'), 'ApisEndpointMw1': middlewares.pop('ApisEndpointMw1')}, "routers": {"ApisEndpointHttp": route_api}}} +with open('configs/_api.yml', 'w') as fpm: + fpm.write(yaml.safe_dump(config_api, default_flow_style=False, sort_keys=False, allow_unicode=True)) -http = {"http": {"middlewares": middlewares, "routers": routers, "services": services}} -print(yaml.dump(http)) +route_api_server_https = routers.pop('ApiServer-https') +route_api_server_https['priority'] = '1000000' +route_api_server_http = routers.pop('ApiServer-http') +route_api_server_http['priority'] = '1000000' +config_api_server = {"http": {"middlewares": {'ApiServerMw1': middlewares.pop('ApiServerMw1'), 'ApiServerMw2': middlewares.pop('ApiServerMw2'), 'ApiServer-stripprefix': middlewares.pop('ApiServer-stripprefix')}, "routers": {"ApiServer-https": route_api_server_https, "ApiServer-https": route_api_server_http}, "services": {"ApiServer": services.pop("ApiServer")}}} +with open('configs/_api_server.yml', 'w') as fpm: + fpm.write(yaml.safe_dump(config_api_server, default_flow_style=False, sort_keys=False, allow_unicode=True)) + +for service in services: + config_service = {"http": { "routers": {}, "services": {service: services[service]}}} + for router in routers: + if router.startswith(f'{service}-'): + config_service["http"]["routers"][router] = copy.deepcopy(routers[router]) + with open(f'configs/{service}.yml', 'w') as spm: + spm.write(yaml.safe_dump(config_service, default_flow_style=False, sort_keys=False, allow_unicode=True)) + +# Cleanup redis +for kv in rdb.scan_iter(f'{prefix}*'): + rdb.delete(kv) +rdb.delete(prefix) +rdb.close() diff --git a/imageroot/bin/upgrade_to_beta2 b/imageroot/bin/upgrade_to_beta2 new file mode 100755 index 0000000..66d55e5 --- /dev/null +++ b/imageroot/bin/upgrade_to_beta2 @@ -0,0 +1,10 @@ +#!/bin/bash + +# +# Copyright (C) 2023 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +~/.config/actions/create-module/10expandconfig +~/.config/bin/redis2yaml +systemctl --user restart traefik