inmanta-module-git-ops 0.4.0__tar.gz → 0.5.0__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.
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/CHANGELOG.md +5 -0
- {inmanta_module_git_ops-0.4.0/inmanta_module_git_ops.egg-info → inmanta_module_git_ops-0.5.0}/PKG-INFO +1 -1
- inmanta_module_git_ops-0.5.0/inmanta_git_ops/cli.py +593 -0
- inmanta_module_git_ops-0.5.0/inmanta_git_ops/const.py +81 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0/inmanta_module_git_ops.egg-info}/PKG-INFO +1 -1
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/__init__.py +5 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/processors.py +6 -2
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/setup.cfg +1 -1
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/slice.py +50 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/store.py +109 -5
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/setup.cfg +1 -1
- inmanta_module_git_ops-0.5.0/tests/test_cli.py +209 -0
- inmanta_module_git_ops-0.5.0/tests/test_slice_schema.py +211 -0
- inmanta_module_git_ops-0.4.0/inmanta_git_ops/cli.py +0 -287
- inmanta_module_git_ops-0.4.0/inmanta_git_ops/const.py +0 -41
- inmanta_module_git_ops-0.4.0/tests/test_cli.py +0 -48
- inmanta_module_git_ops-0.4.0/tests/test_slice_schema.py +0 -88
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/MANIFEST.in +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/README.md +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/README.md +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/.editorconfig +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/.gitignore +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/MANIFEST.in +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/README.md +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/files/.gitkeep +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/files/slices/fs.json +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/inmanta_plugins/example/__init__.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/inmanta_plugins/example/slices/__init__.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/inmanta_plugins/example/slices/fs.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/inmanta_plugins/example/slices/recursive.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/inmanta_plugins/example/slices/simple.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/model/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/model/slices/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/model/slices/fs/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/model/slices/fs/unroll.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/model/slices/recursive/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/model/slices/simple/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/pyproject.toml +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/requirements.dev.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/requirements.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/setup.cfg +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/templates/.gitkeep +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/tests/test_basics.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/docs/example/tests/test_unroll.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_git_ops/__init__.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_git_ops/generator.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_git_ops/py.typed +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_module_git_ops.egg-info/SOURCES.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_module_git_ops.egg-info/dependency_links.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_module_git_ops.egg-info/entry_points.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_module_git_ops.egg-info/not-zip-safe +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_module_git_ops.egg-info/requires.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_module_git_ops.egg-info/top_level.txt +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/config.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/files/.gitkeep +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/model/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/model/processors.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/model/slice/_init.cf +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/py.typed +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/inmanta_plugins/git_ops/templates/.gitkeep +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/pyproject.toml +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/pytest_inmanta_git_ops/__init__.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/pytest_inmanta_git_ops/plugin.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/pytest_inmanta_git_ops/project.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/pytest_inmanta_git_ops/py.typed +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/pytest_inmanta_git_ops/slice.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/tests/test_basics.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/tests/test_example.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/tests/test_migrations.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/tests/test_processors.py +0 -0
- {inmanta_module_git_ops-0.4.0 → inmanta_module_git_ops-0.5.0}/tests/test_unroll.py +0 -0
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## v0.5.0 - 2026-06-06
|
|
4
|
+
|
|
5
|
+
- Add `git-ops project slice` command group to create, list and inspect the slices of a project.
|
|
6
|
+
- Address some compiler warnings in plugin annotations
|
|
7
|
+
|
|
3
8
|
## v0.4.0 - 2026-05-30
|
|
4
9
|
|
|
5
10
|
- Add support for polymorphism in embedded slice definition
|
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright 2026 Guillaume Everarts de Velp
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
Contact: edvgui@gmail.com
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import importlib
|
|
20
|
+
import json
|
|
21
|
+
import logging
|
|
22
|
+
import os
|
|
23
|
+
import pathlib
|
|
24
|
+
import re
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
import tempfile
|
|
28
|
+
from collections.abc import Mapping, Sequence
|
|
29
|
+
|
|
30
|
+
import click
|
|
31
|
+
import texttable
|
|
32
|
+
|
|
33
|
+
from inmanta.const import ENVIRON_FORCE_TTY
|
|
34
|
+
from inmanta.module import ModuleV2, Project
|
|
35
|
+
from inmanta_git_ops import const
|
|
36
|
+
from inmanta_plugins.git_ops.store import SLICE_STORE_REGISTRY
|
|
37
|
+
|
|
38
|
+
LOGGER = logging.getLogger(__name__)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@click.group()
|
|
42
|
+
@click.option(
|
|
43
|
+
"--log-level",
|
|
44
|
+
type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR"]),
|
|
45
|
+
default="INFO",
|
|
46
|
+
)
|
|
47
|
+
def cli(log_level: str) -> None:
|
|
48
|
+
"""
|
|
49
|
+
Inmanta module git_ops CLI tool.
|
|
50
|
+
"""
|
|
51
|
+
logging.basicConfig(level=log_level)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
MODULE: ModuleV2 | None = None
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@cli.group("module")
|
|
58
|
+
@click.option(
|
|
59
|
+
"--module-path",
|
|
60
|
+
type=click.Path(exists=True, file_okay=False),
|
|
61
|
+
help="Path to the module to operate on.",
|
|
62
|
+
envvar="INMANTA_GIT_OPS_MODULE_PATH",
|
|
63
|
+
default=".",
|
|
64
|
+
show_default=True,
|
|
65
|
+
show_envvar=True,
|
|
66
|
+
)
|
|
67
|
+
def module(
|
|
68
|
+
module_path: str,
|
|
69
|
+
) -> None:
|
|
70
|
+
"""
|
|
71
|
+
Commands to manage the module containing slice definitions.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
global MODULE
|
|
75
|
+
MODULE = ModuleV2.from_path(module_path)
|
|
76
|
+
if MODULE is None:
|
|
77
|
+
raise click.BadParameter(f"Could not load module from path {module_path}")
|
|
78
|
+
|
|
79
|
+
# The generator needs the module to be marked as editable
|
|
80
|
+
MODULE._is_editable_install = True
|
|
81
|
+
LOGGER.debug("Found module %s at path %s", MODULE.name, MODULE._path)
|
|
82
|
+
|
|
83
|
+
for file in pathlib.Path(MODULE.get_plugin_dir()).rglob("*.py"):
|
|
84
|
+
# For each python file in the module, try to import it to load the slice definitions
|
|
85
|
+
relative_path = file.relative_to(MODULE.get_plugin_dir())
|
|
86
|
+
module_name = f"inmanta_plugins.{MODULE.name}." + relative_path.with_suffix(
|
|
87
|
+
""
|
|
88
|
+
).as_posix().replace("/", ".")
|
|
89
|
+
LOGGER.debug(f"Importing module {module_name} from {file}")
|
|
90
|
+
importlib.import_module(module_name)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
@module.command("generate")
|
|
94
|
+
@click.option(
|
|
95
|
+
"--explicit-parent-relations",
|
|
96
|
+
is_flag=True,
|
|
97
|
+
help="Whether to generate explicit parent relations in the model.",
|
|
98
|
+
envvar="INMANTA_GIT_OPS_EXPLICIT_PARENT_RELATIONS",
|
|
99
|
+
default=False,
|
|
100
|
+
show_envvar=True,
|
|
101
|
+
)
|
|
102
|
+
def generate(explicit_parent_relations: bool) -> None:
|
|
103
|
+
"""
|
|
104
|
+
Generate the model for the slices defined in the input module.
|
|
105
|
+
"""
|
|
106
|
+
from inmanta_git_ops import generator
|
|
107
|
+
|
|
108
|
+
if explicit_parent_relations:
|
|
109
|
+
generator.EXPLICIT_PARENT_RELATIONS = True
|
|
110
|
+
|
|
111
|
+
assert MODULE is not None
|
|
112
|
+
builder = generator.get_module_builder(MODULE.name)
|
|
113
|
+
|
|
114
|
+
# Collect the schema for all registered slice stores and generate the corresponding entities
|
|
115
|
+
slices = [store.schema.entity_schema() for store in SLICE_STORE_REGISTRY.values()]
|
|
116
|
+
[generator.get_entity(s, slice_root=True) for s in slices]
|
|
117
|
+
|
|
118
|
+
builder.upgrade_existing_module(MODULE, fix_linting=False)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@module.group("store")
|
|
122
|
+
def store() -> None:
|
|
123
|
+
"""
|
|
124
|
+
Commands to manage slice stores.
|
|
125
|
+
"""
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@store.command("list")
|
|
130
|
+
@click.option(
|
|
131
|
+
"--format",
|
|
132
|
+
type=click.Choice(["table", "json"]),
|
|
133
|
+
default="table",
|
|
134
|
+
help="Output format",
|
|
135
|
+
)
|
|
136
|
+
def list_stores(format: str) -> None:
|
|
137
|
+
"""
|
|
138
|
+
List all registered slice stores.
|
|
139
|
+
"""
|
|
140
|
+
if format == "json":
|
|
141
|
+
stores = [
|
|
142
|
+
{
|
|
143
|
+
"name": store.name,
|
|
144
|
+
"path": store._folder,
|
|
145
|
+
"description": (store.schema.__doc__ or "").strip(),
|
|
146
|
+
"python_type": f"{store.schema.__module__}.{store.schema.__name__}",
|
|
147
|
+
}
|
|
148
|
+
for store in SLICE_STORE_REGISTRY.values()
|
|
149
|
+
]
|
|
150
|
+
click.echo(json.dumps(stores, indent=2))
|
|
151
|
+
return
|
|
152
|
+
|
|
153
|
+
elif format == "table":
|
|
154
|
+
table = texttable.Texttable()
|
|
155
|
+
table.header(["Name", "Path", "Description"])
|
|
156
|
+
|
|
157
|
+
for store in SLICE_STORE_REGISTRY.values():
|
|
158
|
+
description = store.schema.__doc__ or ""
|
|
159
|
+
description = description.strip().split("\n")[
|
|
160
|
+
0
|
|
161
|
+
] # Get the first line of the docstring
|
|
162
|
+
table.add_row([store.name, store._folder, description])
|
|
163
|
+
|
|
164
|
+
click.echo(table.draw())
|
|
165
|
+
|
|
166
|
+
else:
|
|
167
|
+
raise click.BadParameter(f"Unsupported format {format}")
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
@store.command("schema")
|
|
171
|
+
@click.option(
|
|
172
|
+
"--store",
|
|
173
|
+
type=str,
|
|
174
|
+
required=True,
|
|
175
|
+
help="The name of the store to get the schema for.",
|
|
176
|
+
)
|
|
177
|
+
def schema(store: str) -> None:
|
|
178
|
+
"""
|
|
179
|
+
Print the JSON schema for the given store.
|
|
180
|
+
"""
|
|
181
|
+
if store not in SLICE_STORE_REGISTRY:
|
|
182
|
+
raise click.BadParameter(f"Store {store} is not registered")
|
|
183
|
+
|
|
184
|
+
schema = SLICE_STORE_REGISTRY[store].schema
|
|
185
|
+
click.echo(json.dumps(schema.model_json_schema(), indent=2))
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
PROJECT: Project | None = None
|
|
189
|
+
INMANTA_ARGS: list[str] = []
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
@cli.group("project")
|
|
193
|
+
@click.option(
|
|
194
|
+
"--inmanta-arg",
|
|
195
|
+
multiple=True,
|
|
196
|
+
help="Additional arguments to pass to the inmanta command.",
|
|
197
|
+
)
|
|
198
|
+
def project(inmanta_arg: list[str]) -> None:
|
|
199
|
+
"""
|
|
200
|
+
Commands to manage the current Inmanta project.
|
|
201
|
+
"""
|
|
202
|
+
global PROJECT
|
|
203
|
+
PROJECT = Project.get()
|
|
204
|
+
|
|
205
|
+
INMANTA_ARGS.extend(inmanta_arg)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def inmanta_compile_command(inmanta_compile_arg: Sequence[str]) -> list[str]:
|
|
209
|
+
"""
|
|
210
|
+
Construct the command line to run a compile on the current project.
|
|
211
|
+
|
|
212
|
+
:param inmanta_compile_arg: Additional arguments to pass to the inmanta
|
|
213
|
+
compile command.
|
|
214
|
+
"""
|
|
215
|
+
return [
|
|
216
|
+
sys.executable,
|
|
217
|
+
"-m",
|
|
218
|
+
"inmanta.app",
|
|
219
|
+
*INMANTA_ARGS,
|
|
220
|
+
"compile",
|
|
221
|
+
*inmanta_compile_arg,
|
|
222
|
+
]
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def run_compile(
|
|
226
|
+
inmanta_compile_arg: Sequence[str],
|
|
227
|
+
*,
|
|
228
|
+
compile_mode: str,
|
|
229
|
+
) -> None:
|
|
230
|
+
"""
|
|
231
|
+
Run a compile on the current project, in a subprocess, with the given
|
|
232
|
+
compile mode.
|
|
233
|
+
|
|
234
|
+
:param inmanta_compile_arg: Additional arguments to pass to the inmanta
|
|
235
|
+
compile command.
|
|
236
|
+
:param compile_mode: The compile mode the compile should run in.
|
|
237
|
+
"""
|
|
238
|
+
subprocess.run(
|
|
239
|
+
inmanta_compile_command(inmanta_compile_arg),
|
|
240
|
+
check=True,
|
|
241
|
+
env={**os.environ, const.COMPILE_MODE_ENV_VAR: compile_mode},
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
# Environment variables, defined by inmanta-core, with which the user can
|
|
246
|
+
# set the logging configuration of the compiler.
|
|
247
|
+
COMPILER_LOGGING_CONTENT_ENV_VAR = "INMANTA_LOGGING_COMPILER_CONTENT"
|
|
248
|
+
COMPILER_LOGGING_ENV_VARS = [
|
|
249
|
+
"INMANTA_LOGGING_COMPILER",
|
|
250
|
+
COMPILER_LOGGING_CONTENT_ENV_VAR,
|
|
251
|
+
"INMANTA_LOGGING_COMPILER_TMPL",
|
|
252
|
+
"INMANTA_CONFIG_LOGGING_CONFIG",
|
|
253
|
+
"INMANTA_CONFIG_LOGGING_CONFIG_CONTENT",
|
|
254
|
+
"INMANTA_CONFIG_LOGGING_CONFIG_TMPL",
|
|
255
|
+
]
|
|
256
|
+
|
|
257
|
+
# Pattern matching the headers of the summary that the compiler prints at the
|
|
258
|
+
# end of every compile, and which shouldn't be part of a slice command output.
|
|
259
|
+
COMPILE_SUMMARY_HEADER_REGEX = re.compile(
|
|
260
|
+
r"=+ (SUCCESS|COMPILATION FAILURE|EXPORT FAILURE|EXCEPTION TRACE) =+"
|
|
261
|
+
)
|
|
262
|
+
ANSI_ESCAPE_SEQUENCE_REGEX = re.compile(r"\x1b\[[0-9;]*m")
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def slice_compile_logging_config() -> str:
|
|
266
|
+
"""
|
|
267
|
+
Build the logging configuration for the slice command compiles: all the
|
|
268
|
+
compiler logs are sent to stderr, so that the compiler doesn't log
|
|
269
|
+
anything on stdout. The logs are colored, like the default console
|
|
270
|
+
logging of the compiler, when stderr is a tty. The inmanta.logging
|
|
271
|
+
logger is restricted to errors because it warns about the default cli
|
|
272
|
+
logging options it ignores when a logging config is provided.
|
|
273
|
+
"""
|
|
274
|
+
on_tty = sys.stderr.isatty()
|
|
275
|
+
return json.dumps(
|
|
276
|
+
{
|
|
277
|
+
"version": 1,
|
|
278
|
+
"disable_existing_loggers": False,
|
|
279
|
+
"formatters": {
|
|
280
|
+
"console": {
|
|
281
|
+
"()": "inmanta.logging.MultiLineFormatter",
|
|
282
|
+
"fmt": (
|
|
283
|
+
"%(log_color)s%(name)-15s%(levelname)-8s%(reset)s%(blue)s%(message)s"
|
|
284
|
+
if on_tty
|
|
285
|
+
else "%(name)-15s%(levelname)-8s%(message)s"
|
|
286
|
+
),
|
|
287
|
+
"log_colors": (
|
|
288
|
+
{
|
|
289
|
+
"DEBUG": "cyan",
|
|
290
|
+
"INFO": "green",
|
|
291
|
+
"WARNING": "yellow",
|
|
292
|
+
"ERROR": "red",
|
|
293
|
+
"CRITICAL": "red",
|
|
294
|
+
}
|
|
295
|
+
if on_tty
|
|
296
|
+
else None
|
|
297
|
+
),
|
|
298
|
+
"reset": on_tty,
|
|
299
|
+
"no_color": not on_tty,
|
|
300
|
+
"keep_logger_names": False,
|
|
301
|
+
}
|
|
302
|
+
},
|
|
303
|
+
"handlers": {
|
|
304
|
+
"console": {
|
|
305
|
+
"class": "logging.StreamHandler",
|
|
306
|
+
"formatter": "console",
|
|
307
|
+
"level": "WARNING",
|
|
308
|
+
"stream": "ext://sys.stderr",
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
"loggers": {
|
|
312
|
+
"inmanta.logging": {"level": "ERROR"},
|
|
313
|
+
},
|
|
314
|
+
"root": {"handlers": ["console"], "level": "WARNING"},
|
|
315
|
+
}
|
|
316
|
+
)
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
def run_slice_command_compile(
|
|
320
|
+
inmanta_compile_arg: Sequence[str],
|
|
321
|
+
*,
|
|
322
|
+
compile_mode: str,
|
|
323
|
+
env: Mapping[str, str],
|
|
324
|
+
) -> object:
|
|
325
|
+
"""
|
|
326
|
+
Run a slice command compile on the current project. The compiler is
|
|
327
|
+
configured to log to stderr, and any remaining compile output on stdout
|
|
328
|
+
is forwarded to stderr, without the compile summary banner. The result
|
|
329
|
+
of the command, written to the output file by the corresponding
|
|
330
|
+
finalizer, is read back and returned.
|
|
331
|
+
|
|
332
|
+
:param inmanta_compile_arg: Additional arguments to pass to the inmanta
|
|
333
|
+
compile command.
|
|
334
|
+
:param compile_mode: The compile mode the compile should run in.
|
|
335
|
+
:param env: Additional environment variables to pass to the compile.
|
|
336
|
+
"""
|
|
337
|
+
with tempfile.TemporaryDirectory() as tmp_dir:
|
|
338
|
+
output_file = pathlib.Path(tmp_dir) / "output.json"
|
|
339
|
+
compile_env = {
|
|
340
|
+
**os.environ,
|
|
341
|
+
const.COMPILE_MODE_ENV_VAR: compile_mode,
|
|
342
|
+
**env,
|
|
343
|
+
const.OUTPUT_FILE_ENV_VAR: str(output_file),
|
|
344
|
+
}
|
|
345
|
+
if not any(var in os.environ for var in COMPILER_LOGGING_ENV_VARS):
|
|
346
|
+
# The user didn't configure the compiler logging, make sure the
|
|
347
|
+
# compiler doesn't log anything on stdout
|
|
348
|
+
compile_env[COMPILER_LOGGING_CONTENT_ENV_VAR] = (
|
|
349
|
+
slice_compile_logging_config()
|
|
350
|
+
)
|
|
351
|
+
if sys.stderr.isatty():
|
|
352
|
+
# The compile output goes through a pipe, let the compiler know
|
|
353
|
+
# its output still ends up on a tty, so it keeps its colors
|
|
354
|
+
compile_env[ENVIRON_FORCE_TTY] = "true"
|
|
355
|
+
|
|
356
|
+
with subprocess.Popen(
|
|
357
|
+
inmanta_compile_command(inmanta_compile_arg),
|
|
358
|
+
env=compile_env,
|
|
359
|
+
stdout=subprocess.PIPE,
|
|
360
|
+
stderr=subprocess.STDOUT,
|
|
361
|
+
text=True,
|
|
362
|
+
) as process:
|
|
363
|
+
assert process.stdout is not None
|
|
364
|
+
for line in process.stdout:
|
|
365
|
+
plain_line = ANSI_ESCAPE_SEQUENCE_REGEX.sub("", line).strip()
|
|
366
|
+
if COMPILE_SUMMARY_HEADER_REGEX.fullmatch(plain_line):
|
|
367
|
+
# Drop the compile summary banner from the output
|
|
368
|
+
continue
|
|
369
|
+
sys.stderr.write(line)
|
|
370
|
+
|
|
371
|
+
if process.returncode != 0:
|
|
372
|
+
raise click.ClickException("The compile failed (see logs above)")
|
|
373
|
+
|
|
374
|
+
if not output_file.exists():
|
|
375
|
+
raise click.ClickException(
|
|
376
|
+
"The compile didn't emit any result for the slice command. "
|
|
377
|
+
"Make sure the project model imports the git_ops module."
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
return json.loads(output_file.read_text())
|
|
381
|
+
|
|
382
|
+
|
|
383
|
+
@project.command("update")
|
|
384
|
+
@click.option(
|
|
385
|
+
"--inmanta-compile-arg",
|
|
386
|
+
multiple=True,
|
|
387
|
+
help="Additional arguments to pass to the inmanta compile command.",
|
|
388
|
+
)
|
|
389
|
+
def update(inmanta_compile_arg: list[str]) -> None:
|
|
390
|
+
"""
|
|
391
|
+
Update the source slices.
|
|
392
|
+
|
|
393
|
+
Read each source slice, and update their content if processors need to resolve some values.
|
|
394
|
+
Verify that the input data is correct, don't export any resource to the orchestrator.
|
|
395
|
+
"""
|
|
396
|
+
run_compile(inmanta_compile_arg, compile_mode=const.COMPILE_UPDATE)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
@project.command("sync")
|
|
400
|
+
@click.option(
|
|
401
|
+
"--inmanta-compile-arg",
|
|
402
|
+
multiple=True,
|
|
403
|
+
help="Additional arguments to pass to the inmanta compile command.",
|
|
404
|
+
)
|
|
405
|
+
def sync(inmanta_compile_arg: list[str]) -> None:
|
|
406
|
+
"""
|
|
407
|
+
Commit the source slices.
|
|
408
|
+
|
|
409
|
+
Read each source slice, and if they have a more recent version or are deleted, update the slice store accordingly
|
|
410
|
+
by emitting a newer version of the slice or marking it as deleted. This will make sure the slice store is in sync
|
|
411
|
+
with the source slices, and that the orchestrator will receive the expected resources when doing the next export.
|
|
412
|
+
"""
|
|
413
|
+
run_compile(inmanta_compile_arg, compile_mode=const.COMPILE_SYNC)
|
|
414
|
+
|
|
415
|
+
|
|
416
|
+
@project.command("prune")
|
|
417
|
+
@click.option(
|
|
418
|
+
"--inmanta-compile-arg",
|
|
419
|
+
multiple=True,
|
|
420
|
+
help="Additional arguments to pass to the inmanta compile command.",
|
|
421
|
+
)
|
|
422
|
+
def prune(inmanta_compile_arg: list[str]) -> None:
|
|
423
|
+
"""
|
|
424
|
+
Prune the slice store.
|
|
425
|
+
|
|
426
|
+
Remove from the slice store all active slices which have a more recent version
|
|
427
|
+
or which are deleted.
|
|
428
|
+
"""
|
|
429
|
+
run_compile(inmanta_compile_arg, compile_mode=const.COMPILE_PRUNE)
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
@project.group("slice")
|
|
433
|
+
def slice() -> None:
|
|
434
|
+
"""
|
|
435
|
+
Commands to manage individual slices of the current Inmanta project.
|
|
436
|
+
"""
|
|
437
|
+
pass
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
@slice.command("create")
|
|
441
|
+
@click.option(
|
|
442
|
+
"--store",
|
|
443
|
+
type=str,
|
|
444
|
+
help="The name of the store in which the slice should be created.",
|
|
445
|
+
envvar=const.SLICE_STORE_ENV_VAR,
|
|
446
|
+
prompt=True,
|
|
447
|
+
show_envvar=True,
|
|
448
|
+
)
|
|
449
|
+
@click.option(
|
|
450
|
+
"--name",
|
|
451
|
+
type=str,
|
|
452
|
+
help="The name of the slice to create, used as the source file name.",
|
|
453
|
+
envvar=const.SLICE_NAME_ENV_VAR,
|
|
454
|
+
prompt=True,
|
|
455
|
+
show_envvar=True,
|
|
456
|
+
)
|
|
457
|
+
@click.option(
|
|
458
|
+
"--extension",
|
|
459
|
+
type=click.Choice(["json", "yaml"]),
|
|
460
|
+
default="json",
|
|
461
|
+
help="The format of the created slice file.",
|
|
462
|
+
envvar=const.SLICE_EXTENSION_ENV_VAR,
|
|
463
|
+
show_default=True,
|
|
464
|
+
show_envvar=True,
|
|
465
|
+
)
|
|
466
|
+
@click.option(
|
|
467
|
+
"--inmanta-compile-arg",
|
|
468
|
+
multiple=True,
|
|
469
|
+
help="Additional arguments to pass to the inmanta compile command.",
|
|
470
|
+
)
|
|
471
|
+
def create(
|
|
472
|
+
store: str,
|
|
473
|
+
name: str,
|
|
474
|
+
extension: str,
|
|
475
|
+
inmanta_compile_arg: list[str],
|
|
476
|
+
) -> None:
|
|
477
|
+
"""
|
|
478
|
+
Scaffold a new source slice file for the given store.
|
|
479
|
+
|
|
480
|
+
The created file contains all the properties of the store's schema: the
|
|
481
|
+
required ones with a placeholder value that should be replaced by the user,
|
|
482
|
+
the others pre-filled with their default value. The path of the created
|
|
483
|
+
file is printed to stdout.
|
|
484
|
+
"""
|
|
485
|
+
path = run_slice_command_compile(
|
|
486
|
+
inmanta_compile_arg,
|
|
487
|
+
compile_mode=const.COMPILE_SLICE_CREATE,
|
|
488
|
+
env={
|
|
489
|
+
const.SLICE_STORE_ENV_VAR: store,
|
|
490
|
+
const.SLICE_NAME_ENV_VAR: name,
|
|
491
|
+
const.SLICE_EXTENSION_ENV_VAR: extension,
|
|
492
|
+
},
|
|
493
|
+
)
|
|
494
|
+
click.echo(path)
|
|
495
|
+
|
|
496
|
+
|
|
497
|
+
@slice.command("inspect")
|
|
498
|
+
@click.option(
|
|
499
|
+
"--store",
|
|
500
|
+
type=str,
|
|
501
|
+
help="The name of the store in which the slice is defined.",
|
|
502
|
+
envvar=const.SLICE_STORE_ENV_VAR,
|
|
503
|
+
prompt=True,
|
|
504
|
+
show_envvar=True,
|
|
505
|
+
)
|
|
506
|
+
@click.option(
|
|
507
|
+
"--name",
|
|
508
|
+
type=str,
|
|
509
|
+
help="The name of the slice to inspect.",
|
|
510
|
+
envvar=const.SLICE_NAME_ENV_VAR,
|
|
511
|
+
prompt=True,
|
|
512
|
+
show_envvar=True,
|
|
513
|
+
)
|
|
514
|
+
@click.option(
|
|
515
|
+
"--inmanta-compile-arg",
|
|
516
|
+
multiple=True,
|
|
517
|
+
help="Additional arguments to pass to the inmanta compile command.",
|
|
518
|
+
)
|
|
519
|
+
def inspect(store: str, name: str, inmanta_compile_arg: list[str]) -> None:
|
|
520
|
+
"""
|
|
521
|
+
Dump the fully-resolved view of a single slice as JSON.
|
|
522
|
+
|
|
523
|
+
The output matches the view of the slice during an update compile: the merged
|
|
524
|
+
current and previous attributes, with operation/path markers, and the version
|
|
525
|
+
the slice would be assigned.
|
|
526
|
+
"""
|
|
527
|
+
result = run_slice_command_compile(
|
|
528
|
+
inmanta_compile_arg,
|
|
529
|
+
compile_mode=const.COMPILE_SLICE_INSPECT,
|
|
530
|
+
env={
|
|
531
|
+
const.SLICE_STORE_ENV_VAR: store,
|
|
532
|
+
const.SLICE_NAME_ENV_VAR: name,
|
|
533
|
+
},
|
|
534
|
+
)
|
|
535
|
+
click.echo(json.dumps(result, indent=2))
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
@slice.command("list")
|
|
539
|
+
@click.option(
|
|
540
|
+
"--store",
|
|
541
|
+
type=str,
|
|
542
|
+
default=None,
|
|
543
|
+
help="Only list the slices of the store with this name.",
|
|
544
|
+
envvar=const.SLICE_STORE_ENV_VAR,
|
|
545
|
+
show_envvar=True,
|
|
546
|
+
)
|
|
547
|
+
@click.option(
|
|
548
|
+
"--format",
|
|
549
|
+
type=click.Choice(["table", "json"]),
|
|
550
|
+
default="table",
|
|
551
|
+
help="Output format",
|
|
552
|
+
)
|
|
553
|
+
@click.option(
|
|
554
|
+
"--inmanta-compile-arg",
|
|
555
|
+
multiple=True,
|
|
556
|
+
help="Additional arguments to pass to the inmanta compile command.",
|
|
557
|
+
)
|
|
558
|
+
def list_slices(
|
|
559
|
+
store: str | None,
|
|
560
|
+
format: str,
|
|
561
|
+
inmanta_compile_arg: list[str],
|
|
562
|
+
) -> None:
|
|
563
|
+
"""
|
|
564
|
+
List the slices of the current project, optionally filtered by store.
|
|
565
|
+
|
|
566
|
+
Slices present in either the source folder or the active store are listed,
|
|
567
|
+
with the version they would be assigned during an update compile.
|
|
568
|
+
"""
|
|
569
|
+
slices = run_slice_command_compile(
|
|
570
|
+
inmanta_compile_arg,
|
|
571
|
+
compile_mode=const.COMPILE_SLICE_LIST,
|
|
572
|
+
env={const.SLICE_STORE_ENV_VAR: store} if store is not None else {},
|
|
573
|
+
)
|
|
574
|
+
|
|
575
|
+
if format == "json":
|
|
576
|
+
click.echo(json.dumps(slices, indent=2))
|
|
577
|
+
return
|
|
578
|
+
|
|
579
|
+
elif format == "table":
|
|
580
|
+
table = texttable.Texttable()
|
|
581
|
+
table.header(["Store", "Name", "Version", "Deleted"])
|
|
582
|
+
|
|
583
|
+
for s in slices:
|
|
584
|
+
table.add_row([s["store_name"], s["name"], s["version"], s["deleted"]])
|
|
585
|
+
|
|
586
|
+
click.echo(table.draw())
|
|
587
|
+
|
|
588
|
+
else:
|
|
589
|
+
raise click.BadParameter(f"Unsupported format {format}")
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
if __name__ == "__main__":
|
|
593
|
+
cli()
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright 2025 Guillaume Everarts de Velp
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
Contact: edvgui@gmail.com
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import os
|
|
20
|
+
import typing
|
|
21
|
+
|
|
22
|
+
import pydantic
|
|
23
|
+
|
|
24
|
+
type CompileMode = typing.Literal[
|
|
25
|
+
"update",
|
|
26
|
+
"sync",
|
|
27
|
+
"export",
|
|
28
|
+
"prune",
|
|
29
|
+
"slice-create",
|
|
30
|
+
"slice-list",
|
|
31
|
+
"slice-inspect",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
COMPILE_UPDATE = "update"
|
|
36
|
+
COMPILE_SYNC = "sync"
|
|
37
|
+
COMPILE_EXPORT = "export"
|
|
38
|
+
COMPILE_PRUNE = "prune"
|
|
39
|
+
COMPILE_EMPTY = "empty"
|
|
40
|
+
COMPILE_SLICE_CREATE = "slice-create"
|
|
41
|
+
COMPILE_SLICE_LIST = "slice-list"
|
|
42
|
+
COMPILE_SLICE_INSPECT = "slice-inspect"
|
|
43
|
+
|
|
44
|
+
# The compile modes triggered by the `git-ops project slice` commands. During
|
|
45
|
+
# these compiles, no slice is emitted in the model, the command logic is
|
|
46
|
+
# executed in a finalizer instead.
|
|
47
|
+
COMPILE_SLICE_COMMANDS = [
|
|
48
|
+
COMPILE_SLICE_CREATE,
|
|
49
|
+
COMPILE_SLICE_LIST,
|
|
50
|
+
COMPILE_SLICE_INSPECT,
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
COMPILE_MODE_ENV_VAR = "INMANTA_GIT_OPS_COMPILE_MODE"
|
|
54
|
+
COMPILE_MODE_ADAPTER = pydantic.TypeAdapter(CompileMode)
|
|
55
|
+
COMPILE_MODE = COMPILE_MODE_ADAPTER.validate_python(
|
|
56
|
+
os.getenv(COMPILE_MODE_ENV_VAR, COMPILE_EXPORT)
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Options for the slice command compiles, passed to the compile subprocess
|
|
60
|
+
# by the cli, the same way the compile mode is.
|
|
61
|
+
SLICE_STORE_ENV_VAR = "INMANTA_GIT_OPS_SLICE_STORE"
|
|
62
|
+
SLICE_STORE: str | None = os.getenv(SLICE_STORE_ENV_VAR)
|
|
63
|
+
|
|
64
|
+
SLICE_NAME_ENV_VAR = "INMANTA_GIT_OPS_SLICE_NAME"
|
|
65
|
+
SLICE_NAME: str | None = os.getenv(SLICE_NAME_ENV_VAR)
|
|
66
|
+
|
|
67
|
+
SLICE_EXTENSION_ENV_VAR = "INMANTA_GIT_OPS_SLICE_EXTENSION"
|
|
68
|
+
SLICE_EXTENSION: str = os.getenv(SLICE_EXTENSION_ENV_VAR, "json")
|
|
69
|
+
|
|
70
|
+
# File in which the result of a slice command should be written by the
|
|
71
|
+
# finalizer executing it, so that the cli process can read it back.
|
|
72
|
+
OUTPUT_FILE_ENV_VAR = "INMANTA_GIT_OPS_OUTPUT_FILE"
|
|
73
|
+
OUTPUT_FILE: str | None = os.getenv(OUTPUT_FILE_ENV_VAR)
|
|
74
|
+
|
|
75
|
+
SLICE_CREATE = "create"
|
|
76
|
+
SLICE_UPDATE = "update"
|
|
77
|
+
SLICE_DELETE = "delete"
|
|
78
|
+
|
|
79
|
+
# Placeholder used for all the required property values of a newly scaffolded
|
|
80
|
+
# slice, which the user must replace with real values.
|
|
81
|
+
SLICE_PLACEHOLDER = "<REPLACE_THIS>"
|