Skip to content

Commit

Permalink
upgrade: manual migration from redis to yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Aug 21, 2023
1 parent 081997a commit 4665817
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
47 changes: 40 additions & 7 deletions imageroot/bin/redis2yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import os
import sys
import copy
import json
import agent
import tempfile
Expand All @@ -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 = {}
Expand All @@ -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/*'):
Expand All @@ -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
Expand Down Expand Up @@ -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'] = '1000000'
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()
10 changes: 10 additions & 0 deletions imageroot/bin/upgrade_to_beta2
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4665817

Please sign in to comment.