From ceb98f06ea69574a037ab67e1ffe873ab3485253 Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Thu, 10 Aug 2023 16:08:57 +0200 Subject: [PATCH] upgrade: convert redis to config files --- imageroot/actions/create-module/50create | 4 +- imageroot/bin/redis2yml | 104 +++++++++++++++++++ imageroot/update-module.d/30upgrade_to_beta2 | 12 +++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100755 imageroot/bin/redis2yml create mode 100755 imageroot/update-module.d/30upgrade_to_beta2 diff --git a/imageroot/actions/create-module/50create b/imageroot/actions/create-module/50create index 25299e7..a75cade 100755 --- a/imageroot/actions/create-module/50create +++ b/imageroot/actions/create-module/50create @@ -13,7 +13,7 @@ exec 1>&2 # # Create file provider configurations directory # -mkdir configs +mkdir -p configs # @@ -110,7 +110,7 @@ http: EOF # Create uploaded certificates folder -mkdir custom_certificates +mkdir -p custom_certificates # Enable and start the services systemctl --user enable --now traefik.service diff --git a/imageroot/bin/redis2yml b/imageroot/bin/redis2yml new file mode 100755 index 0000000..5b3eddc --- /dev/null +++ b/imageroot/bin/redis2yml @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 + +# +# Copyright (C) 2023 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +import os +import sys +import copy +import json +import agent +import tempfile + +import yaml + +try: + instance = sys.argv[1] +except: + instance = 'traefik1' + +middlewares = {} +services = {} +routers = {} + +prefix = f'module/{instance}/kv/' +rdb = agent.redis_connect(host='127.0.0.1', privileged = True) + +# HTTP +for kv in rdb.scan_iter(f'{prefix}http/*'): + value = rdb.get(kv) + key = kv.removeprefix(f'{prefix}http/') + tmp = key.split("/") + + last = kv[kv.rfind('/')+1:] + if tmp[0] == "middlewares": + if tmp[1] not in middlewares: + if last.isnumeric(): + middlewares[tmp[1]] = { tmp[2]: { tmp[3]: list() } } + 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 + elif tmp[0] == "services": + if tmp[1] not in services: + services[tmp[1]] = { "loadBalancer": { "servers": list() } } + services[tmp[1]]["loadBalancer"]["servers"].append({"url": value}) + elif tmp[0] == "routers": + if tmp[1] not in routers: + if tmp[1].endswith("https") or tmp[1].startswith("certificate-"): + routers[tmp[1]] = {"middlewares": list(), "entrypoints": list(), "tls": { "certresolver": {}, "domains": list()}} + else: + routers[tmp[1]] = {"middlewares": list(), "entrypoints": list()} + if len(tmp) == 3: + # module/traefik1/kv/http/routers/instance1-https/rule + if tmp[2] != "tls": # ignore module/traefik1/kv/http/routers/instance1-https/tls + routers[tmp[1]][tmp[2]] = value + elif len(tmp) == 4: + if tmp[2] == "tls": + # module/traefik1/kv/http/routers/instance1-http/tls/certresolver + routers[tmp[1]]['tls']['certresolver'] = value + else: + # module/traefik1/kv/http/routers/instance1-http/middlewares/0 + if tmp[2] == "entryPoints": + tmp[2] = tmp[2].lower() + routers[tmp[1]][tmp[2]].append(value) + else: + # module/traefik1/kv/http/routers/instance1-https/tls/domains/0/main + routers[tmp[1]]['tls']['domains'].append({last: value}) + +# Cleanup routes using default cert +for r in routers: + if "tls" in routers[r] and (not routers[r]["tls"]["certresolver"] and not routers[r]["tls"]["domains"]): + routers[r]["tls"] = {} + +# Remove well-known configs +try: + middlewares.pop('http2https-redirectscheme') + middlewares.pop('ApisEndpointMw0') + middlewares.pop('ApisEndpointMw1') + routers.pop('ApisEndpointHttp') + routers.pop('ApiServer-https') + routers.pop('ApiServer-http') + services.pop('ApiServer') +except: + pass + +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(f'module/{instance}/kv') +rdb.close() diff --git a/imageroot/update-module.d/30upgrade_to_beta2 b/imageroot/update-module.d/30upgrade_to_beta2 new file mode 100755 index 0000000..49226f8 --- /dev/null +++ b/imageroot/update-module.d/30upgrade_to_beta2 @@ -0,0 +1,12 @@ +#!/bin/bash + +# +# Copyright (C) 2023 Nethesis S.r.l. +# SPDX-License-Identifier: GPL-3.0-or-later +# + +# Create static configuration, ignore warnings +~/.config/actions/create-module/10expandconfig 2>/dev/null +~/.config/actions/create-module/50create 2>/dev/null +# Convert existing configuration to yaml +~/.config/bin/redis2yml