gitops-configserver 1.0.1__py3-none-any.whl
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.
- build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/__init__.py +14 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/cli.py +51 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/config.py +27 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/__init__.py +14 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/cli.py +51 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/config.py +27 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/__init__.py +14 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/cli.py +51 -0
- build/lib/build/lib/build/lib/build/lib/build/lib/gitops_mt/config.py +27 -0
- build/lib/build/lib/build/lib/build/lib/gitops_mt/__init__.py +14 -0
- build/lib/build/lib/build/lib/build/lib/gitops_mt/cli.py +51 -0
- build/lib/build/lib/build/lib/build/lib/gitops_mt/config.py +27 -0
- build/lib/build/lib/build/lib/gitops_mt/__init__.py +14 -0
- build/lib/build/lib/build/lib/gitops_mt/cli.py +51 -0
- build/lib/build/lib/build/lib/gitops_mt/config.py +27 -0
- build/lib/build/lib/gitops_mt/__init__.py +14 -0
- build/lib/build/lib/gitops_mt/cli.py +51 -0
- build/lib/build/lib/gitops_mt/config.py +27 -0
- build/lib/gitops_mt/__init__.py +14 -0
- build/lib/gitops_mt/cli.py +51 -0
- build/lib/gitops_mt/config.py +27 -0
- build/lib/tests/__init__.py +0 -0
- build/lib/tests/test_rendering.py +153 -0
- build/lib/tests/test_variables_resolver.py +63 -0
- gitops_configserver/__init__.py +14 -0
- gitops_configserver/cli.py +87 -0
- gitops_configserver/config.py +45 -0
- gitops_configserver/configs/__init__.py +1 -0
- gitops_configserver/configs/views.py +22 -0
- gitops_configserver/extensions.py +8 -0
- gitops_configserver/git.py +90 -0
- gitops_configserver/server.py +15 -0
- gitops_configserver/templates_rendering.py +169 -0
- gitops_configserver/utils.py +39 -0
- gitops_configserver/workflow.py +71 -0
- gitops_configserver-1.0.1.dist-info/LICENSE +2 -0
- gitops_configserver-1.0.1.dist-info/METADATA +56 -0
- gitops_configserver-1.0.1.dist-info/RECORD +46 -0
- gitops_configserver-1.0.1.dist-info/WHEEL +5 -0
- gitops_configserver-1.0.1.dist-info/top_level.txt +8 -0
- gitops_mt/__init__.py +14 -0
- gitops_mt/cli.py +51 -0
- gitops_mt/config.py +27 -0
- tests/__init__.py +0 -0
- tests/test_rendering.py +153 -0
- tests/test_variables_resolver.py +63 -0
|
@@ -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
|
+
]
|
|
@@ -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
|