-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade: convert redis to config files
- Loading branch information
1 parent
ac9ba99
commit ceb98f0
Showing
3 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |