Skip to content

Commit

Permalink
upgrade: convert redis to config files
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti committed Aug 21, 2023
1 parent ac9ba99 commit ceb98f0
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 2 deletions.
4 changes: 2 additions & 2 deletions imageroot/actions/create-module/50create
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exec 1>&2
#
# Create file provider configurations directory
#
mkdir configs
mkdir -p configs


#
Expand Down Expand Up @@ -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
104 changes: 104 additions & 0 deletions imageroot/bin/redis2yml
Original file line number Diff line number Diff line change
@@ -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()
12 changes: 12 additions & 0 deletions imageroot/update-module.d/30upgrade_to_beta2
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ceb98f0

Please sign in to comment.