gitops-configserver 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gitops_configserver-1.0.1/LICENSE +2 -0
- gitops_configserver-1.0.1/MANIFEST.in +6 -0
- gitops_configserver-1.0.1/PKG-INFO +56 -0
- gitops_configserver-1.0.1/README.md +39 -0
- gitops_configserver-1.0.1/VERSION +1 -0
- gitops_configserver-1.0.1/gitops_configserver/__init__.py +14 -0
- gitops_configserver-1.0.1/gitops_configserver/cli.py +87 -0
- gitops_configserver-1.0.1/gitops_configserver/config.py +45 -0
- gitops_configserver-1.0.1/gitops_configserver/configs/__init__.py +1 -0
- gitops_configserver-1.0.1/gitops_configserver/configs/views.py +22 -0
- gitops_configserver-1.0.1/gitops_configserver/extensions.py +8 -0
- gitops_configserver-1.0.1/gitops_configserver/git.py +90 -0
- gitops_configserver-1.0.1/gitops_configserver/server.py +15 -0
- gitops_configserver-1.0.1/gitops_configserver/templates_rendering.py +169 -0
- gitops_configserver-1.0.1/gitops_configserver/utils.py +39 -0
- gitops_configserver-1.0.1/gitops_configserver/workflow.py +71 -0
- gitops_configserver-1.0.1/gitops_configserver.egg-info/PKG-INFO +56 -0
- gitops_configserver-1.0.1/gitops_configserver.egg-info/SOURCES.txt +26 -0
- gitops_configserver-1.0.1/gitops_configserver.egg-info/dependency_links.txt +1 -0
- gitops_configserver-1.0.1/gitops_configserver.egg-info/requires.txt +5 -0
- gitops_configserver-1.0.1/gitops_configserver.egg-info/top_level.txt +8 -0
- gitops_configserver-1.0.1/pyproject.toml +23 -0
- gitops_configserver-1.0.1/pytest.ini +4 -0
- gitops_configserver-1.0.1/requirements.txt +5 -0
- gitops_configserver-1.0.1/setup.cfg +4 -0
- gitops_configserver-1.0.1/tests/__init__.py +0 -0
- gitops_configserver-1.0.1/tests/test_rendering.py +153 -0
- gitops_configserver-1.0.1/tests/test_variables_resolver.py +63 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: gitops-configserver
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Author-email: Piotr Przetacznik <piotr.przetacznik+gitops@gmail.com>
|
|
5
|
+
License: All rights reserved by Piotr Przetacznik.
|
|
6
|
+
MIT License.
|
|
7
|
+
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: pytest
|
|
13
|
+
Requires-Dist: Jinja2
|
|
14
|
+
Requires-Dist: pyyaml
|
|
15
|
+
Requires-Dist: Flask
|
|
16
|
+
Requires-Dist: flask-restful
|
|
17
|
+
|
|
18
|
+
# GitOps Configserver
|
|
19
|
+
|
|
20
|
+
[](https://github.com/pprzetacznik/gitops-configserver/actions/workflows/test.yml)
|
|
21
|
+
|
|
22
|
+
Inspired by puppet, kustomized and GitOps practices.
|
|
23
|
+
|
|
24
|
+
## Planned features
|
|
25
|
+
|
|
26
|
+
* multitenant templates
|
|
27
|
+
* hieradata variables
|
|
28
|
+
* flask rest service
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
$ python -m gitops_server.cli config_gen -h
|
|
34
|
+
usage: cli.py config_gen [-h] --config_dir CONFIG_DIR
|
|
35
|
+
|
|
36
|
+
options:
|
|
37
|
+
-h, --help show this help message and exit
|
|
38
|
+
--config_dir CONFIG_DIR
|
|
39
|
+
Config directory
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
$ python -m gitops_server.cli server --config_dir=config
|
|
44
|
+
...
|
|
45
|
+
$ curl http://localhost:8002/configs
|
|
46
|
+
{"tenants":["tenant1"]}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Setting up GitHub tokens
|
|
50
|
+
|
|
51
|
+
* Go to Setting -> Developer Settings -> Fine-grained personal access tokens
|
|
52
|
+
* Create a token with following settings:
|
|
53
|
+
* `Only select repositories` and select your repository
|
|
54
|
+
* `Repository permissions` and select `Content`
|
|
55
|
+
|
|
56
|
+
Set the token as `$GH_PAT` in your local environment.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# GitOps Configserver
|
|
2
|
+
|
|
3
|
+
[](https://github.com/pprzetacznik/gitops-configserver/actions/workflows/test.yml)
|
|
4
|
+
|
|
5
|
+
Inspired by puppet, kustomized and GitOps practices.
|
|
6
|
+
|
|
7
|
+
## Planned features
|
|
8
|
+
|
|
9
|
+
* multitenant templates
|
|
10
|
+
* hieradata variables
|
|
11
|
+
* flask rest service
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
$ python -m gitops_server.cli config_gen -h
|
|
17
|
+
usage: cli.py config_gen [-h] --config_dir CONFIG_DIR
|
|
18
|
+
|
|
19
|
+
options:
|
|
20
|
+
-h, --help show this help message and exit
|
|
21
|
+
--config_dir CONFIG_DIR
|
|
22
|
+
Config directory
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
$ python -m gitops_server.cli server --config_dir=config
|
|
27
|
+
...
|
|
28
|
+
$ curl http://localhost:8002/configs
|
|
29
|
+
{"tenants":["tenant1"]}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Setting up GitHub tokens
|
|
33
|
+
|
|
34
|
+
* Go to Setting -> Developer Settings -> Fine-grained personal access tokens
|
|
35
|
+
* Create a token with following settings:
|
|
36
|
+
* `Only select repositories` and select your repository
|
|
37
|
+
* `Repository permissions` and select `Content`
|
|
38
|
+
|
|
39
|
+
Set the token as `$GH_PAT` in your local environment.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1.0.1
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from os.path import join, dirname
|
|
2
|
+
from importlib import metadata
|
|
3
|
+
from importlib.metadata import PackageNotFoundError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def read(fname):
|
|
7
|
+
with open(join(dirname(__file__), fname)) as f:
|
|
8
|
+
return f.read()
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
__version__ = metadata.version("gitops-configserver")
|
|
13
|
+
except PackageNotFoundError:
|
|
14
|
+
__version__ = read(join("..", "VERSION")).strip()
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from argparse import Namespace, ArgumentParser
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
import logging
|
|
4
|
+
from gitops_configserver.config import setup_logger, Config
|
|
5
|
+
from gitops_configserver.server import ConfigServer
|
|
6
|
+
from gitops_configserver.workflow import Workflow
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AbstractArgumentsHandler(ABC):
|
|
12
|
+
"""Command pattern interface"""
|
|
13
|
+
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def execute(self, args) -> None:
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def add_parser(self, subparsers):
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ConfigGeneratorHandler(AbstractArgumentsHandler):
|
|
24
|
+
def execute(self, args):
|
|
25
|
+
config = Config()
|
|
26
|
+
config.CONFIG_DIR = args.config_dir
|
|
27
|
+
Workflow(config).execute()
|
|
28
|
+
print("HELLO WORLD")
|
|
29
|
+
|
|
30
|
+
def add_parser(self, subparsers):
|
|
31
|
+
parser_config_generator = subparsers.add_parser("config_gen")
|
|
32
|
+
parser_config_generator.set_defaults(func=self.execute)
|
|
33
|
+
parser_config_generator.add_argument(
|
|
34
|
+
"--config_dir",
|
|
35
|
+
type=str,
|
|
36
|
+
help="Config directory",
|
|
37
|
+
dest="config_dir",
|
|
38
|
+
required=True,
|
|
39
|
+
)
|
|
40
|
+
parser_config_generator.add_argument(
|
|
41
|
+
"--environment",
|
|
42
|
+
type=str,
|
|
43
|
+
help="Environment",
|
|
44
|
+
dest="environment",
|
|
45
|
+
default="dev",
|
|
46
|
+
required=False,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ConfigServerHandler(AbstractArgumentsHandler):
|
|
51
|
+
def execute(self, args):
|
|
52
|
+
config = Config()
|
|
53
|
+
config.CONFIG_DIR = args.config_dir
|
|
54
|
+
config_server = ConfigServer(config)
|
|
55
|
+
config_server.start()
|
|
56
|
+
|
|
57
|
+
def add_parser(self, subparsers):
|
|
58
|
+
parser_config_generator = subparsers.add_parser("server")
|
|
59
|
+
parser_config_generator.set_defaults(func=self.execute)
|
|
60
|
+
parser_config_generator.add_argument(
|
|
61
|
+
"--config_dir",
|
|
62
|
+
type=str,
|
|
63
|
+
help="Config directory",
|
|
64
|
+
dest="config_dir",
|
|
65
|
+
required=True,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def parse_arguments() -> Namespace:
|
|
70
|
+
parser = ArgumentParser(
|
|
71
|
+
description="GitOps multitenancy templates generator"
|
|
72
|
+
)
|
|
73
|
+
subparsers = parser.add_subparsers(required=True)
|
|
74
|
+
handlers = [ConfigGeneratorHandler, ConfigServerHandler]
|
|
75
|
+
for handler in handlers:
|
|
76
|
+
handler().add_parser(subparsers)
|
|
77
|
+
return parser.parse_args()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def main() -> None:
|
|
81
|
+
setup_logger()
|
|
82
|
+
args = parse_arguments()
|
|
83
|
+
args.func(args)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
main()
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from os import getenv, path
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
import logging
|
|
4
|
+
import logging.config
|
|
5
|
+
from gitops_configserver.utils import file_exists, load_yaml, create_dir
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def setup_logger():
|
|
9
|
+
if file_exists(Config.LOGGER_CONFIG_FILE):
|
|
10
|
+
config = load_yaml(Config.LOGGER_CONFIG_FILE)
|
|
11
|
+
logging.config.dictConfig(config)
|
|
12
|
+
else:
|
|
13
|
+
create_dir("logs")
|
|
14
|
+
logging.basicConfig(
|
|
15
|
+
level=logging.DEBUG,
|
|
16
|
+
format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
|
|
17
|
+
datefmt="%Y-%m-%d %H:%M:%S",
|
|
18
|
+
filename=datetime.now().strftime("logs/logs_%Y_%m_%d_%H_%M.log"),
|
|
19
|
+
filemode="w",
|
|
20
|
+
)
|
|
21
|
+
console = logging.StreamHandler()
|
|
22
|
+
console.setLevel(logging.DEBUG)
|
|
23
|
+
formatter = logging.Formatter(
|
|
24
|
+
"%(name)-12s: %(levelname)-8s %(message)s"
|
|
25
|
+
)
|
|
26
|
+
console.setFormatter(formatter)
|
|
27
|
+
logging.getLogger("").addHandler(console)
|
|
28
|
+
logger = logging.getLogger(__name__)
|
|
29
|
+
logger.info("Started")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Config:
|
|
33
|
+
LOGS_VERBOSE = True
|
|
34
|
+
|
|
35
|
+
LOGGER_CONFIG_FILE = getenv(
|
|
36
|
+
"GITOPS_CONFIGSERVER__LOGGER_CONFIG_FILE",
|
|
37
|
+
path.join("resources", "logger.yaml"),
|
|
38
|
+
)
|
|
39
|
+
CONFIG_DIR = getenv("GITOPS_CONFIGSERVER__CONFIG_DIR")
|
|
40
|
+
TARGET_DIR = getenv("GITOPS_CONFIGSERVER__TARGET_DIR", "target")
|
|
41
|
+
|
|
42
|
+
HOST = getenv("GITOPS_CONFIGSERVER__HOST", "0.0.0.0")
|
|
43
|
+
PORT = getenv("GITOPS_CONFIGSERVER__PORT", "8002")
|
|
44
|
+
|
|
45
|
+
GH_PAT = getenv("GH_PAT")
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from gitops_configserver.configs import views
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from os.path import join
|
|
2
|
+
from flask import Blueprint, jsonify, request, abort
|
|
3
|
+
from flask_restful import Resource
|
|
4
|
+
from gitops_configserver.extensions import config_provider
|
|
5
|
+
from gitops_configserver.utils import load_yaml
|
|
6
|
+
|
|
7
|
+
blueprint = Blueprint("configs", __name__)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ConfigsAPI(Resource):
|
|
11
|
+
def get(self):
|
|
12
|
+
config = config_provider.app.config["config"]
|
|
13
|
+
filename = join(config.CONFIG_DIR, "index.yml")
|
|
14
|
+
index = load_yaml(filename)
|
|
15
|
+
return jsonify(index)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
blueprint.add_url_rule(
|
|
19
|
+
"/configs",
|
|
20
|
+
view_func=ConfigsAPI.as_view("configs"),
|
|
21
|
+
methods=["GET"],
|
|
22
|
+
)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from os.path import join, dirname
|
|
2
|
+
from subprocess import run
|
|
3
|
+
from logging import getLogger
|
|
4
|
+
from json import dumps
|
|
5
|
+
from gitops_configserver.config import Config
|
|
6
|
+
from gitops_configserver.utils import (
|
|
7
|
+
remove_dir_with_content,
|
|
8
|
+
copy_file,
|
|
9
|
+
create_dir,
|
|
10
|
+
write_to_file,
|
|
11
|
+
timestamp,
|
|
12
|
+
)
|
|
13
|
+
from gitops_configserver import __version__
|
|
14
|
+
|
|
15
|
+
logger = getLogger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class GitWrapper:
|
|
19
|
+
def __init__(self, config: Config):
|
|
20
|
+
self.config = config
|
|
21
|
+
|
|
22
|
+
def clone(self, repo_url: str, repo_dir: str):
|
|
23
|
+
command = f"git clone {repo_url} {repo_dir}"
|
|
24
|
+
logger.info(f"run: {command}")
|
|
25
|
+
run(command, shell=True, check=True)
|
|
26
|
+
|
|
27
|
+
def stage_file(self, filename: str, cwd: str):
|
|
28
|
+
command = f"git add {filename}"
|
|
29
|
+
logger.info(f"run: {command}")
|
|
30
|
+
run(command, shell=True, check=True, cwd=cwd)
|
|
31
|
+
|
|
32
|
+
def set_committer(self, cwd: str):
|
|
33
|
+
command = 'git config --local user.name "GitHub Actions Bot"'
|
|
34
|
+
run(command, shell=True, check=True, cwd=cwd)
|
|
35
|
+
command = 'git config --local user.email "github-bot@speedwell.pl"'
|
|
36
|
+
run(command, shell=True, check=True, cwd=cwd)
|
|
37
|
+
|
|
38
|
+
def commit(self, message: str, cwd: str):
|
|
39
|
+
command = f'git commit -m "{message}"'
|
|
40
|
+
logger.info(f"run: {command}")
|
|
41
|
+
run(command, shell=True, check=True, cwd=cwd)
|
|
42
|
+
|
|
43
|
+
def push(self, cwd: str):
|
|
44
|
+
command = f"git push"
|
|
45
|
+
logger.info(f"run: {command}")
|
|
46
|
+
run(command, shell=True, check=True, cwd=cwd)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class GitProvisioner:
|
|
50
|
+
def __init__(self, config: Config, git_wrapper: GitWrapper) -> None:
|
|
51
|
+
self.config = config
|
|
52
|
+
self.git_wrapper = git_wrapper
|
|
53
|
+
|
|
54
|
+
def provision(self, repositories: dict) -> dict:
|
|
55
|
+
for repository_name, repository_value in repositories.items():
|
|
56
|
+
index_file = {
|
|
57
|
+
"files": [],
|
|
58
|
+
"gitops_configserver": __version__,
|
|
59
|
+
"timestamp": timestamp(),
|
|
60
|
+
}
|
|
61
|
+
if repository_value.get("type") == "github":
|
|
62
|
+
repo_dir = join(self.config.TARGET_DIR, repository_name)
|
|
63
|
+
remove_dir_with_content(repo_dir)
|
|
64
|
+
repo_url = repository_value.get("url")
|
|
65
|
+
repo_full_url = f"https://{self.config.GH_PAT}@{repo_url}"
|
|
66
|
+
self.git_wrapper.clone(repo_full_url, repo_dir)
|
|
67
|
+
for file_entry in repository_value.get("files", []):
|
|
68
|
+
destination_filepath = join(
|
|
69
|
+
repo_dir, file_entry["destination_filename"]
|
|
70
|
+
)
|
|
71
|
+
create_dir(dirname(destination_filepath))
|
|
72
|
+
copy_file(file_entry["tmp_path"], destination_filepath)
|
|
73
|
+
self.git_wrapper.stage_file(
|
|
74
|
+
file_entry["destination_filename"], repo_dir
|
|
75
|
+
)
|
|
76
|
+
index_file["files"].append(
|
|
77
|
+
file_entry["destination_filename"]
|
|
78
|
+
)
|
|
79
|
+
write_to_file(
|
|
80
|
+
join(repo_dir, ".gitops_configserver"),
|
|
81
|
+
dumps(index_file, indent=4),
|
|
82
|
+
)
|
|
83
|
+
index_file["files"].append(
|
|
84
|
+
join(repo_dir, ".gitops_configserver")
|
|
85
|
+
)
|
|
86
|
+
self.git_wrapper.stage_file(".gitops_configserver", repo_dir)
|
|
87
|
+
self.git_wrapper.set_committer(repo_dir)
|
|
88
|
+
self.git_wrapper.commit("chore: Update configs", repo_dir)
|
|
89
|
+
self.git_wrapper.push(repo_dir)
|
|
90
|
+
return index_file
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from flask import Flask
|
|
2
|
+
from gitops_configserver import configs
|
|
3
|
+
from gitops_configserver.extensions import config_provider
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class ConfigServer:
|
|
7
|
+
def __init__(self, config):
|
|
8
|
+
self.config = config
|
|
9
|
+
|
|
10
|
+
def start(self):
|
|
11
|
+
app = Flask(__name__)
|
|
12
|
+
config_provider.init_app(app)
|
|
13
|
+
app.register_blueprint(configs.views.blueprint)
|
|
14
|
+
app.config["config"] = self.config
|
|
15
|
+
app.run(host=self.config.HOST, port=self.config.PORT)
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
from itertools import product
|
|
2
|
+
from os.path import join, dirname
|
|
3
|
+
import logging
|
|
4
|
+
import jinja2
|
|
5
|
+
from yaml import dump, safe_load
|
|
6
|
+
from gitops_configserver.config import Config
|
|
7
|
+
from gitops_configserver.utils import create_dir, read_file, write_to_file
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
logger = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TenantsConfigLoader:
|
|
14
|
+
def __init__(self, config):
|
|
15
|
+
self.config = config
|
|
16
|
+
|
|
17
|
+
def index(self):
|
|
18
|
+
filepath = "index.yml"
|
|
19
|
+
return self._load_yaml_file(filepath)
|
|
20
|
+
|
|
21
|
+
def tenant(self, tenant_name):
|
|
22
|
+
filepath = join(tenant_name, "index.yml")
|
|
23
|
+
return self._load_yaml_file(filepath)
|
|
24
|
+
|
|
25
|
+
def variables(self, tenant_name):
|
|
26
|
+
filepath = join(tenant_name, "variables", "defaults.yml")
|
|
27
|
+
return self._load_yaml_file(filepath)
|
|
28
|
+
|
|
29
|
+
def _load_yaml_file(self, filename):
|
|
30
|
+
filepath = join(self.config.CONFIG_DIR, filename)
|
|
31
|
+
with open(filepath, "r") as f:
|
|
32
|
+
content_dict = safe_load(f.read())
|
|
33
|
+
return content_dict
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class TemplatesRendering:
|
|
37
|
+
|
|
38
|
+
def __init__(self, config: Config):
|
|
39
|
+
self.config = config
|
|
40
|
+
self.tenants_config_loader = TenantsConfigLoader(config)
|
|
41
|
+
self.jinja_env = jinja2.Environment(
|
|
42
|
+
block_start_string=r"\BLOCK{",
|
|
43
|
+
block_end_string="}",
|
|
44
|
+
variable_start_string="{",
|
|
45
|
+
variable_end_string="}",
|
|
46
|
+
comment_start_string=r"\#{",
|
|
47
|
+
comment_end_string="}",
|
|
48
|
+
line_statement_prefix="%%",
|
|
49
|
+
line_comment_prefix="%#",
|
|
50
|
+
trim_blocks=True,
|
|
51
|
+
autoescape=False,
|
|
52
|
+
)
|
|
53
|
+
self.jinja_env.filters.update(
|
|
54
|
+
{"to_yaml": lambda data: dump(data).strip()}
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
def render(self, tenant_name) -> dict:
|
|
58
|
+
tenant_variables = self.tenants_config_loader.variables(tenant_name)
|
|
59
|
+
tenant_config = self.tenants_config_loader.tenant(tenant_name)
|
|
60
|
+
|
|
61
|
+
templates_dict = {
|
|
62
|
+
"repositories": tenant_config.get("repositories", {}),
|
|
63
|
+
"files": [],
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for template_config in tenant_config.get("configs"):
|
|
67
|
+
template_variables_mapping = template_config.get("variables", [])
|
|
68
|
+
tpl_variables = VariablesResolver.resolve_for_template(
|
|
69
|
+
template_variables_mapping, tenant_variables
|
|
70
|
+
)
|
|
71
|
+
logger.info(f"tpl_variables: {tpl_variables}")
|
|
72
|
+
variants = self._get_variants(template_config, tenant_variables)
|
|
73
|
+
|
|
74
|
+
for variant in variants:
|
|
75
|
+
template_content = read_file(
|
|
76
|
+
join(
|
|
77
|
+
self.config.CONFIG_DIR,
|
|
78
|
+
tenant_name,
|
|
79
|
+
"templates",
|
|
80
|
+
template_config.get("template_file"),
|
|
81
|
+
)
|
|
82
|
+
)
|
|
83
|
+
logger.info(f"template_content: {template_content}")
|
|
84
|
+
|
|
85
|
+
rendered_content = self._render_template(
|
|
86
|
+
template_content, tpl_variables
|
|
87
|
+
)
|
|
88
|
+
logger.info(f"rendered_content: {rendered_content}")
|
|
89
|
+
|
|
90
|
+
destination_filename = TemplateResolver.resolve(
|
|
91
|
+
template_config.get("destination_filename", ""),
|
|
92
|
+
{
|
|
93
|
+
**tpl_variables,
|
|
94
|
+
"environment": template_config.get("environment"),
|
|
95
|
+
"matrix": variant,
|
|
96
|
+
},
|
|
97
|
+
)
|
|
98
|
+
destination_filepath = join(
|
|
99
|
+
self.config.TARGET_DIR, tenant_name, destination_filename
|
|
100
|
+
)
|
|
101
|
+
logger.info(f"destination_filepath: {destination_filepath}")
|
|
102
|
+
create_dir(dirname(destination_filepath))
|
|
103
|
+
write_to_file(destination_filepath, rendered_content)
|
|
104
|
+
|
|
105
|
+
templates_dict["files"] += [
|
|
106
|
+
{
|
|
107
|
+
"tmp_path": destination_filepath,
|
|
108
|
+
"destination_filename": destination_filename,
|
|
109
|
+
"repo": template_config.get(
|
|
110
|
+
"destination_repo", "local"
|
|
111
|
+
),
|
|
112
|
+
}
|
|
113
|
+
]
|
|
114
|
+
|
|
115
|
+
return templates_dict
|
|
116
|
+
|
|
117
|
+
def _get_variants(
|
|
118
|
+
self, template_config: dict, tenant_variables: dict
|
|
119
|
+
) -> list:
|
|
120
|
+
matrix = template_config.get("matrix", {})
|
|
121
|
+
matrix_include = template_config.get("matrix_include", [])
|
|
122
|
+
resolved_variants = MatrixResolver.resolve(matrix, tenant_variables)
|
|
123
|
+
variants = resolved_variants + matrix_include
|
|
124
|
+
return variants
|
|
125
|
+
|
|
126
|
+
def _render_template(self, template: str, variables: dict) -> str:
|
|
127
|
+
new_template = self.jinja_env.from_string(template)
|
|
128
|
+
return new_template.render(**variables)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class TemplateResolver:
|
|
132
|
+
@staticmethod
|
|
133
|
+
def resolve(template_content, variables) -> str:
|
|
134
|
+
env = jinja2.Environment()
|
|
135
|
+
template = env.from_string(template_content)
|
|
136
|
+
output = template.render(**variables)
|
|
137
|
+
return output
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class MatrixResolver:
|
|
141
|
+
@staticmethod
|
|
142
|
+
def resolve(matrix, tenant_variables) -> list:
|
|
143
|
+
env = jinja2.Environment()
|
|
144
|
+
for key, value in matrix.items():
|
|
145
|
+
if isinstance(value, str):
|
|
146
|
+
template = env.from_string(value)
|
|
147
|
+
output = template.render(**tenant_variables)
|
|
148
|
+
matrix[key] = safe_load(output)
|
|
149
|
+
|
|
150
|
+
if not matrix:
|
|
151
|
+
return [{"_variant": "default"}]
|
|
152
|
+
keys, values = zip(*matrix.items())
|
|
153
|
+
permutations_dicts = [dict(zip(keys, v)) for v in product(*values)]
|
|
154
|
+
return permutations_dicts
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class VariablesResolver:
|
|
158
|
+
@staticmethod
|
|
159
|
+
def resolve_for_template(
|
|
160
|
+
template_variables_mapping: list, tenant_variables: dict
|
|
161
|
+
) -> dict:
|
|
162
|
+
resolved_dict = {}
|
|
163
|
+
for template_variable_item in template_variables_mapping:
|
|
164
|
+
key = template_variable_item.get("tpl_variable")
|
|
165
|
+
value = tenant_variables.get(
|
|
166
|
+
template_variable_item.get("tenant_variable")
|
|
167
|
+
)
|
|
168
|
+
resolved_dict[key] = value
|
|
169
|
+
return resolved_dict
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from datetime import datetime, timezone
|
|
2
|
+
from os import makedirs, path
|
|
3
|
+
from shutil import rmtree, copyfile
|
|
4
|
+
from yaml import safe_load
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def create_dir(path):
|
|
8
|
+
makedirs(path, exist_ok=True)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def file_exists(fname):
|
|
12
|
+
return path.isfile(fname)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def read_file(filepath):
|
|
16
|
+
with open(filepath, "r") as f:
|
|
17
|
+
return f.read()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def write_to_file(filepath, content):
|
|
21
|
+
with open(filepath, "w") as f:
|
|
22
|
+
f.write(content)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def remove_dir_with_content(dirpath):
|
|
26
|
+
rmtree(dirpath, ignore_errors=True)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def load_yaml(filepath):
|
|
30
|
+
with open(filepath, "r") as f:
|
|
31
|
+
return safe_load(f.read())
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def copy_file(src, dst):
|
|
35
|
+
copyfile(src, dst)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def timestamp():
|
|
39
|
+
return datetime.now(timezone.utc).isoformat()
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from logging import getLogger
|
|
3
|
+
from gitops_configserver.templates_rendering import (
|
|
4
|
+
TemplatesRendering,
|
|
5
|
+
TenantsConfigLoader,
|
|
6
|
+
)
|
|
7
|
+
from gitops_configserver.git import GitProvisioner, GitWrapper
|
|
8
|
+
from gitops_configserver.utils import create_dir
|
|
9
|
+
|
|
10
|
+
logger = getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class WorkflowCommand(ABC):
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def execute(self, request: dict = None) -> None:
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TemplatesRenderingCommand(WorkflowCommand):
|
|
20
|
+
def __init__(self, config):
|
|
21
|
+
self.config = config
|
|
22
|
+
self.tenants_config_loader = TenantsConfigLoader(config)
|
|
23
|
+
|
|
24
|
+
def execute(self, request: dict = None) -> dict:
|
|
25
|
+
create_dir(self.config.TARGET_DIR)
|
|
26
|
+
tenants_list = self.tenants_config_loader.index().get("tenants", [])
|
|
27
|
+
repo_files = {}
|
|
28
|
+
for tenant_name in tenants_list:
|
|
29
|
+
templates_rendering = TemplatesRendering(self.config)
|
|
30
|
+
repositories_files = templates_rendering.render(tenant_name)
|
|
31
|
+
repo_files[tenant_name] = repositories_files
|
|
32
|
+
logger.info(f"repo_files: {repo_files}")
|
|
33
|
+
return repo_files
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class GitRepositoriesCommitterCommand(WorkflowCommand):
|
|
37
|
+
def __init__(self, config):
|
|
38
|
+
self.config = config
|
|
39
|
+
self.tenants_config_loader = TenantsConfigLoader(config)
|
|
40
|
+
self.git_wrapper = GitWrapper(self.config)
|
|
41
|
+
|
|
42
|
+
def execute(self, request: dict = None) -> dict:
|
|
43
|
+
tenants_list = self.tenants_config_loader.index().get("tenants", [])
|
|
44
|
+
global_repositories = self.tenants_config_loader.index().get(
|
|
45
|
+
"repositories", []
|
|
46
|
+
)
|
|
47
|
+
for repository_name, repository_value in global_repositories.items():
|
|
48
|
+
global_repositories[repository_name]["files"] = []
|
|
49
|
+
for tenant_name in tenants_list:
|
|
50
|
+
# tenant_config = self.tenants_config_loader.tenant(tenant_name)
|
|
51
|
+
for file_entry in request[tenant_name]["files"]:
|
|
52
|
+
if file_entry.get("repo") in global_repositories:
|
|
53
|
+
global_repositories[repository_name]["files"] += [
|
|
54
|
+
file_entry
|
|
55
|
+
]
|
|
56
|
+
logger.info(f"global_repositories: {global_repositories}")
|
|
57
|
+
git_provisioner = GitProvisioner(self.config, self.git_wrapper)
|
|
58
|
+
result = git_provisioner.provision(global_repositories)
|
|
59
|
+
return result
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Workflow:
|
|
63
|
+
def __init__(self, config):
|
|
64
|
+
self.config = config
|
|
65
|
+
self.request = {}
|
|
66
|
+
|
|
67
|
+
def execute(self) -> None:
|
|
68
|
+
commands = [TemplatesRenderingCommand, GitRepositoriesCommitterCommand]
|
|
69
|
+
for command in commands:
|
|
70
|
+
response = command(self.config).execute(self.request)
|
|
71
|
+
self.request = response
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: gitops-configserver
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Author-email: Piotr Przetacznik <piotr.przetacznik+gitops@gmail.com>
|
|
5
|
+
License: All rights reserved by Piotr Przetacznik.
|
|
6
|
+
MIT License.
|
|
7
|
+
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
10
|
+
Description-Content-Type: text/markdown
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Requires-Dist: pytest
|
|
13
|
+
Requires-Dist: Jinja2
|
|
14
|
+
Requires-Dist: pyyaml
|
|
15
|
+
Requires-Dist: Flask
|
|
16
|
+
Requires-Dist: flask-restful
|
|
17
|
+
|
|
18
|
+
# GitOps Configserver
|
|
19
|
+
|
|
20
|
+
[](https://github.com/pprzetacznik/gitops-configserver/actions/workflows/test.yml)
|
|
21
|
+
|
|
22
|
+
Inspired by puppet, kustomized and GitOps practices.
|
|
23
|
+
|
|
24
|
+
## Planned features
|
|
25
|
+
|
|
26
|
+
* multitenant templates
|
|
27
|
+
* hieradata variables
|
|
28
|
+
* flask rest service
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
$ python -m gitops_server.cli config_gen -h
|
|
34
|
+
usage: cli.py config_gen [-h] --config_dir CONFIG_DIR
|
|
35
|
+
|
|
36
|
+
options:
|
|
37
|
+
-h, --help show this help message and exit
|
|
38
|
+
--config_dir CONFIG_DIR
|
|
39
|
+
Config directory
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
$ python -m gitops_server.cli server --config_dir=config
|
|
44
|
+
...
|
|
45
|
+
$ curl http://localhost:8002/configs
|
|
46
|
+
{"tenants":["tenant1"]}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Setting up GitHub tokens
|
|
50
|
+
|
|
51
|
+
* Go to Setting -> Developer Settings -> Fine-grained personal access tokens
|
|
52
|
+
* Create a token with following settings:
|
|
53
|
+
* `Only select repositories` and select your repository
|
|
54
|
+
* `Repository permissions` and select `Content`
|
|
55
|
+
|
|
56
|
+
Set the token as `$GH_PAT` in your local environment.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
VERSION
|
|
5
|
+
pyproject.toml
|
|
6
|
+
pytest.ini
|
|
7
|
+
requirements.txt
|
|
8
|
+
gitops_configserver/__init__.py
|
|
9
|
+
gitops_configserver/cli.py
|
|
10
|
+
gitops_configserver/config.py
|
|
11
|
+
gitops_configserver/extensions.py
|
|
12
|
+
gitops_configserver/git.py
|
|
13
|
+
gitops_configserver/server.py
|
|
14
|
+
gitops_configserver/templates_rendering.py
|
|
15
|
+
gitops_configserver/utils.py
|
|
16
|
+
gitops_configserver/workflow.py
|
|
17
|
+
gitops_configserver.egg-info/PKG-INFO
|
|
18
|
+
gitops_configserver.egg-info/SOURCES.txt
|
|
19
|
+
gitops_configserver.egg-info/dependency_links.txt
|
|
20
|
+
gitops_configserver.egg-info/requires.txt
|
|
21
|
+
gitops_configserver.egg-info/top_level.txt
|
|
22
|
+
gitops_configserver/configs/__init__.py
|
|
23
|
+
gitops_configserver/configs/views.py
|
|
24
|
+
tests/__init__.py
|
|
25
|
+
tests/test_rendering.py
|
|
26
|
+
tests/test_variables_resolver.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "gitops-configserver"
|
|
3
|
+
authors = [
|
|
4
|
+
{name = "Piotr Przetacznik", email = "piotr.przetacznik+gitops@gmail.com"}
|
|
5
|
+
]
|
|
6
|
+
license = {file = "LICENSE"}
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
dynamic = ["version", "dependencies"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Programming Language :: Python :: 3.12",
|
|
11
|
+
"Programming Language :: Python :: 3.13",
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
[build-system]
|
|
15
|
+
requires = ["setuptools>=61.0"]
|
|
16
|
+
build-backend = "setuptools.build_meta"
|
|
17
|
+
|
|
18
|
+
[tool.setuptools.packages]
|
|
19
|
+
find = {}
|
|
20
|
+
|
|
21
|
+
[tool.setuptools.dynamic]
|
|
22
|
+
version = {file = "VERSION"}
|
|
23
|
+
dependencies = { file = ["requirements.txt"] }
|
|
File without changes
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from os.path import join
|
|
2
|
+
from pytest import fixture
|
|
3
|
+
from gitops_configserver.config import Config
|
|
4
|
+
from gitops_configserver.workflow import TemplatesRenderingCommand
|
|
5
|
+
from gitops_configserver.utils import read_file, remove_dir_with_content
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@fixture
|
|
9
|
+
def config():
|
|
10
|
+
config = Config()
|
|
11
|
+
config.CONFIG_DIR = "resources/test_config"
|
|
12
|
+
config.TARGET_DIR = "target_test_configserver"
|
|
13
|
+
return config
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@fixture
|
|
17
|
+
def templates_rendering_command(config):
|
|
18
|
+
yield TemplatesRenderingCommand(config)
|
|
19
|
+
# remove_dir_with_content(config.TARGET_DIR)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def test_rendering(templates_rendering_command, config):
|
|
23
|
+
repo_files = templates_rendering_command.execute()
|
|
24
|
+
assert repo_files == {
|
|
25
|
+
"tenant1": {
|
|
26
|
+
"repositories": {
|
|
27
|
+
"test_repo1": {"type": "local", "url": "local_path"}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
{
|
|
31
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-10-python3.10-prod.yml",
|
|
32
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-10-python3.10-prod.yml",
|
|
33
|
+
"repo": "gitops-configserver-tests",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-10-python3.11-prod.yml",
|
|
37
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-10-python3.11-prod.yml",
|
|
38
|
+
"repo": "gitops-configserver-tests",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-10-python3.12-prod.yml",
|
|
42
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-10-python3.12-prod.yml",
|
|
43
|
+
"repo": "gitops-configserver-tests",
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-12-python3.10-prod.yml",
|
|
47
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-12-python3.10-prod.yml",
|
|
48
|
+
"repo": "gitops-configserver-tests",
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-12-python3.11-prod.yml",
|
|
52
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-12-python3.11-prod.yml",
|
|
53
|
+
"repo": "gitops-configserver-tests",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-12-python3.12-prod.yml",
|
|
57
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-12-python3.12-prod.yml",
|
|
58
|
+
"repo": "gitops-configserver-tests",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-14-python3.10-prod.yml",
|
|
62
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-14-python3.10-prod.yml",
|
|
63
|
+
"repo": "gitops-configserver-tests",
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-14-python3.11-prod.yml",
|
|
67
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-14-python3.11-prod.yml",
|
|
68
|
+
"repo": "gitops-configserver-tests",
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-14-python3.12-prod.yml",
|
|
72
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-14-python3.12-prod.yml",
|
|
73
|
+
"repo": "gitops-configserver-tests",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-10-python3.10-prod.yml",
|
|
77
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-10-python3.10-prod.yml",
|
|
78
|
+
"repo": "gitops-configserver-tests",
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-10-python3.11-prod.yml",
|
|
82
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-10-python3.11-prod.yml",
|
|
83
|
+
"repo": "gitops-configserver-tests",
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-10-python3.12-prod.yml",
|
|
87
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-10-python3.12-prod.yml",
|
|
88
|
+
"repo": "gitops-configserver-tests",
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-12-python3.10-prod.yml",
|
|
92
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-12-python3.10-prod.yml",
|
|
93
|
+
"repo": "gitops-configserver-tests",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-12-python3.11-prod.yml",
|
|
97
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-12-python3.11-prod.yml",
|
|
98
|
+
"repo": "gitops-configserver-tests",
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-12-python3.12-prod.yml",
|
|
102
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-12-python3.12-prod.yml",
|
|
103
|
+
"repo": "gitops-configserver-tests",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-14-python3.10-prod.yml",
|
|
107
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-14-python3.10-prod.yml",
|
|
108
|
+
"repo": "gitops-configserver-tests",
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-14-python3.11-prod.yml",
|
|
112
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-14-python3.11-prod.yml",
|
|
113
|
+
"repo": "gitops-configserver-tests",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-20.04-14-python3.12-prod.yml",
|
|
117
|
+
"destination_filename": "asdf/config1-ubuntu-20.04-14-python3.12-prod.yml",
|
|
118
|
+
"repo": "gitops-configserver-tests",
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
"tmp_path": "target_test_configserver/tenant1/asdf/config1-ubuntu-22.04-10-python3.7-prod.yml",
|
|
122
|
+
"destination_filename": "asdf/config1-ubuntu-22.04-10-python3.7-prod.yml",
|
|
123
|
+
"repo": "gitops-configserver-tests",
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
"tmp_path": "target_test_configserver/tenant1/config1-prod.yml",
|
|
127
|
+
"destination_filename": "config1-prod.yml",
|
|
128
|
+
"repo": "test_repo1",
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
print(repo_files)
|
|
134
|
+
content = read_file(
|
|
135
|
+
join(
|
|
136
|
+
config.TARGET_DIR,
|
|
137
|
+
"tenant1",
|
|
138
|
+
"asdf",
|
|
139
|
+
"config1-ubuntu-22.04-10-python3.12-prod.yml",
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
assert (
|
|
143
|
+
content
|
|
144
|
+
== """- my_config:
|
|
145
|
+
- var1: aaa1.default
|
|
146
|
+
- var2: bbb1.default
|
|
147
|
+
- var3: ccc1.default
|
|
148
|
+
- lll: |
|
|
149
|
+
- abc: 1
|
|
150
|
+
def: 2
|
|
151
|
+
ghi: 3
|
|
152
|
+
- mmm: 4"""
|
|
153
|
+
)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from gitops_configserver.templates_rendering import (
|
|
2
|
+
VariablesResolver,
|
|
3
|
+
MatrixResolver,
|
|
4
|
+
)
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_variables_resolver():
|
|
8
|
+
template_variables_mapping = [
|
|
9
|
+
{"tenant_variable": "aaa", "tpl_variable": "template.var1"},
|
|
10
|
+
{"tenant_variable": "bbb", "tpl_variable": "template.var2"},
|
|
11
|
+
{"tenant_variable": "ccc", "tpl_variable": "template.var3"},
|
|
12
|
+
]
|
|
13
|
+
tenant_variables = {
|
|
14
|
+
"aaa": "aaa1.default",
|
|
15
|
+
"bbb": "bbb1.default",
|
|
16
|
+
"ccc": "ccc1.default",
|
|
17
|
+
}
|
|
18
|
+
resolved_variables = VariablesResolver.resolve_for_template(
|
|
19
|
+
template_variables_mapping,
|
|
20
|
+
tenant_variables,
|
|
21
|
+
)
|
|
22
|
+
assert resolved_variables == {
|
|
23
|
+
"template.var1": "aaa1.default",
|
|
24
|
+
"template.var2": "bbb1.default",
|
|
25
|
+
"template.var3": "ccc1.default",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_matrix_resolver():
|
|
30
|
+
tenant_variables = {
|
|
31
|
+
"aaa": "aaa1.default",
|
|
32
|
+
"bbb": "bbb1.default",
|
|
33
|
+
"ccc": "ccc1.default",
|
|
34
|
+
"python_version": ["3.11", "3.12"],
|
|
35
|
+
}
|
|
36
|
+
matrix = {
|
|
37
|
+
"os": ["ubuntu-22.04", "ubuntu-20.04"],
|
|
38
|
+
"version": ["10"],
|
|
39
|
+
"python_version": "{{ python_version }}",
|
|
40
|
+
}
|
|
41
|
+
resolved_variants = MatrixResolver.resolve(matrix, tenant_variables)
|
|
42
|
+
assert resolved_variants == [
|
|
43
|
+
{
|
|
44
|
+
"os": "ubuntu-22.04",
|
|
45
|
+
"python_version": "3.11",
|
|
46
|
+
"version": "10",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"os": "ubuntu-22.04",
|
|
50
|
+
"python_version": "3.12",
|
|
51
|
+
"version": "10",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"os": "ubuntu-20.04",
|
|
55
|
+
"python_version": "3.11",
|
|
56
|
+
"version": "10",
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
"os": "ubuntu-20.04",
|
|
60
|
+
"python_version": "3.12",
|
|
61
|
+
"version": "10",
|
|
62
|
+
},
|
|
63
|
+
]
|