remdb 0.3.103__py3-none-any.whl → 0.3.118__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.
Potentially problematic release.
This version of remdb might be problematic. Click here for more details.
- rem/agentic/context.py +28 -24
- rem/agentic/mcp/tool_wrapper.py +29 -3
- rem/agentic/otel/setup.py +92 -4
- rem/agentic/providers/pydantic_ai.py +88 -18
- rem/agentic/schema.py +358 -21
- rem/agentic/tools/rem_tools.py +3 -3
- rem/api/main.py +85 -16
- rem/api/mcp_router/resources.py +1 -1
- rem/api/mcp_router/server.py +18 -4
- rem/api/mcp_router/tools.py +383 -16
- rem/api/routers/admin.py +218 -1
- rem/api/routers/chat/completions.py +30 -3
- rem/api/routers/chat/streaming.py +143 -3
- rem/api/routers/feedback.py +12 -319
- rem/api/routers/query.py +360 -0
- rem/api/routers/shared_sessions.py +13 -13
- rem/cli/commands/README.md +237 -64
- rem/cli/commands/cluster.py +1300 -0
- rem/cli/commands/configure.py +1 -3
- rem/cli/commands/db.py +354 -143
- rem/cli/commands/process.py +14 -8
- rem/cli/commands/schema.py +92 -45
- rem/cli/main.py +27 -6
- rem/models/core/rem_query.py +5 -2
- rem/models/entities/shared_session.py +2 -28
- rem/registry.py +10 -4
- rem/services/content/service.py +30 -8
- rem/services/embeddings/api.py +4 -4
- rem/services/embeddings/worker.py +16 -16
- rem/services/postgres/README.md +151 -26
- rem/services/postgres/__init__.py +2 -1
- rem/services/postgres/diff_service.py +531 -0
- rem/services/postgres/pydantic_to_sqlalchemy.py +427 -129
- rem/services/postgres/schema_generator.py +205 -4
- rem/services/postgres/service.py +6 -6
- rem/services/rem/parser.py +44 -9
- rem/services/rem/service.py +36 -2
- rem/services/session/reload.py +1 -1
- rem/settings.py +56 -7
- rem/sql/background_indexes.sql +19 -24
- rem/sql/migrations/001_install.sql +252 -69
- rem/sql/migrations/002_install_models.sql +2171 -593
- rem/sql/migrations/003_optional_extensions.sql +326 -0
- rem/sql/migrations/004_cache_system.sql +548 -0
- rem/utils/__init__.py +18 -0
- rem/utils/date_utils.py +2 -2
- rem/utils/schema_loader.py +17 -13
- rem/utils/sql_paths.py +146 -0
- rem/workers/__init__.py +2 -1
- rem/workers/unlogged_maintainer.py +463 -0
- {remdb-0.3.103.dist-info → remdb-0.3.118.dist-info}/METADATA +149 -76
- {remdb-0.3.103.dist-info → remdb-0.3.118.dist-info}/RECORD +54 -48
- rem/sql/migrations/003_seed_default_user.sql +0 -48
- {remdb-0.3.103.dist-info → remdb-0.3.118.dist-info}/WHEEL +0 -0
- {remdb-0.3.103.dist-info → remdb-0.3.118.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,1300 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Cluster management commands for deploying REM to Kubernetes.
|
|
3
|
+
|
|
4
|
+
Usage:
|
|
5
|
+
rem cluster init # Initialize cluster config
|
|
6
|
+
rem cluster generate # Generate all manifests (including SQL ConfigMap)
|
|
7
|
+
rem cluster setup-ssm # Create required SSM parameters
|
|
8
|
+
rem cluster validate # Validate deployment prerequisites
|
|
9
|
+
rem cluster env check # Validate .env for cluster deployment
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
import shutil
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
import tarfile
|
|
17
|
+
import tempfile
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from urllib.error import HTTPError
|
|
20
|
+
from urllib.request import urlopen, Request
|
|
21
|
+
|
|
22
|
+
import click
|
|
23
|
+
import yaml
|
|
24
|
+
from loguru import logger
|
|
25
|
+
|
|
26
|
+
# Default GitHub repo for manifest releases
|
|
27
|
+
DEFAULT_MANIFESTS_REPO = "anthropics/remstack"
|
|
28
|
+
DEFAULT_MANIFESTS_ASSET = "manifests.tar.gz"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def get_current_version() -> str:
|
|
32
|
+
"""Get current installed version of remdb."""
|
|
33
|
+
try:
|
|
34
|
+
from importlib.metadata import version
|
|
35
|
+
return version("remdb")
|
|
36
|
+
except Exception:
|
|
37
|
+
return "latest"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def download_manifests(version: str, output_dir: Path, repo: str = DEFAULT_MANIFESTS_REPO) -> bool:
|
|
41
|
+
"""
|
|
42
|
+
Download manifests tarball from GitHub releases.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
version: Release tag (e.g., "v1.2.3" or "latest")
|
|
46
|
+
output_dir: Directory to extract manifests to
|
|
47
|
+
repo: GitHub repo in "owner/repo" format
|
|
48
|
+
|
|
49
|
+
Returns:
|
|
50
|
+
True if successful, False otherwise
|
|
51
|
+
"""
|
|
52
|
+
# Construct GitHub release URL
|
|
53
|
+
# For "latest", GitHub redirects to the actual latest release
|
|
54
|
+
if version == "latest":
|
|
55
|
+
base_url = f"https://github.com/{repo}/releases/latest/download"
|
|
56
|
+
else:
|
|
57
|
+
# Ensure version has 'v' prefix for GitHub tags
|
|
58
|
+
if not version.startswith("v"):
|
|
59
|
+
version = f"v{version}"
|
|
60
|
+
base_url = f"https://github.com/{repo}/releases/download/{version}"
|
|
61
|
+
|
|
62
|
+
url = f"{base_url}/{DEFAULT_MANIFESTS_ASSET}"
|
|
63
|
+
|
|
64
|
+
click.echo(f"Downloading manifests from: {url}")
|
|
65
|
+
|
|
66
|
+
try:
|
|
67
|
+
# Create request with user-agent (GitHub requires it)
|
|
68
|
+
request = Request(url, headers={"User-Agent": "remdb-cli"})
|
|
69
|
+
|
|
70
|
+
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) as tmp_file:
|
|
71
|
+
tmp_path = Path(tmp_file.name)
|
|
72
|
+
|
|
73
|
+
# Download with progress indication
|
|
74
|
+
with urlopen(request, timeout=60) as response:
|
|
75
|
+
total_size = response.headers.get("Content-Length")
|
|
76
|
+
if total_size:
|
|
77
|
+
total_size = int(total_size)
|
|
78
|
+
click.echo(f"Size: {total_size / 1024 / 1024:.1f} MB")
|
|
79
|
+
|
|
80
|
+
# Read in chunks
|
|
81
|
+
chunk_size = 8192
|
|
82
|
+
downloaded = 0
|
|
83
|
+
while True:
|
|
84
|
+
chunk = response.read(chunk_size)
|
|
85
|
+
if not chunk:
|
|
86
|
+
break
|
|
87
|
+
tmp_file.write(chunk)
|
|
88
|
+
downloaded += len(chunk)
|
|
89
|
+
if total_size:
|
|
90
|
+
pct = (downloaded / total_size) * 100
|
|
91
|
+
click.echo(f"\r Downloading: {pct:.0f}%", nl=False)
|
|
92
|
+
|
|
93
|
+
click.echo() # Newline after progress
|
|
94
|
+
|
|
95
|
+
# Extract tarball
|
|
96
|
+
click.echo(f"Extracting to: {output_dir}")
|
|
97
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
98
|
+
|
|
99
|
+
with tarfile.open(tmp_path, "r:gz") as tar:
|
|
100
|
+
# Extract all files
|
|
101
|
+
tar.extractall(output_dir)
|
|
102
|
+
|
|
103
|
+
# Clean up temp file
|
|
104
|
+
tmp_path.unlink()
|
|
105
|
+
|
|
106
|
+
click.secho("✓ Manifests downloaded successfully", fg="green")
|
|
107
|
+
return True
|
|
108
|
+
|
|
109
|
+
except HTTPError as e:
|
|
110
|
+
if e.code == 404:
|
|
111
|
+
click.secho(f"✗ Release not found: {version}", fg="red")
|
|
112
|
+
click.echo(f" Check available releases at: https://github.com/{repo}/releases")
|
|
113
|
+
else:
|
|
114
|
+
click.secho(f"✗ Download failed: HTTP {e.code}", fg="red")
|
|
115
|
+
return False
|
|
116
|
+
except Exception as e:
|
|
117
|
+
click.secho(f"✗ Download failed: {e}", fg="red")
|
|
118
|
+
return False
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def get_manifests_dir() -> Path:
|
|
122
|
+
"""Get the manifests directory from the remstack repo."""
|
|
123
|
+
# Walk up from CLI to find manifests/
|
|
124
|
+
current = Path(__file__).resolve()
|
|
125
|
+
for parent in current.parents:
|
|
126
|
+
manifests = parent / "manifests"
|
|
127
|
+
if manifests.exists():
|
|
128
|
+
return manifests
|
|
129
|
+
# Try relative to cwd
|
|
130
|
+
cwd_manifests = Path.cwd() / "manifests"
|
|
131
|
+
if cwd_manifests.exists():
|
|
132
|
+
return cwd_manifests
|
|
133
|
+
raise click.ClickException("Could not find manifests directory. Run from remstack root.")
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def load_cluster_config(config_path: Path | None) -> dict:
|
|
137
|
+
"""Load cluster configuration from YAML file or defaults."""
|
|
138
|
+
if config_path and config_path.exists():
|
|
139
|
+
with open(config_path) as f:
|
|
140
|
+
return yaml.safe_load(f)
|
|
141
|
+
|
|
142
|
+
# Try default location
|
|
143
|
+
manifests = get_manifests_dir()
|
|
144
|
+
default_config = manifests / "cluster-config.yaml"
|
|
145
|
+
if default_config.exists():
|
|
146
|
+
with open(default_config) as f:
|
|
147
|
+
return yaml.safe_load(f)
|
|
148
|
+
|
|
149
|
+
# Return minimal defaults
|
|
150
|
+
return {
|
|
151
|
+
"project": {"name": "rem", "environment": "staging", "namespace": "rem"},
|
|
152
|
+
"aws": {"region": "us-east-1", "ssmPrefix": "/rem"},
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@click.command()
|
|
157
|
+
@click.option(
|
|
158
|
+
"--output",
|
|
159
|
+
"-o",
|
|
160
|
+
type=click.Path(path_type=Path),
|
|
161
|
+
default=None,
|
|
162
|
+
help="Output path for config file (default: ./manifests/cluster-config.yaml)",
|
|
163
|
+
)
|
|
164
|
+
@click.option(
|
|
165
|
+
"--manifests-dir",
|
|
166
|
+
"-m",
|
|
167
|
+
type=click.Path(path_type=Path),
|
|
168
|
+
default=None,
|
|
169
|
+
help="Path to manifests directory (default: ./manifests)",
|
|
170
|
+
)
|
|
171
|
+
@click.option(
|
|
172
|
+
"--project-name",
|
|
173
|
+
default="rem",
|
|
174
|
+
help="Project name prefix (default: rem)",
|
|
175
|
+
)
|
|
176
|
+
@click.option(
|
|
177
|
+
"--git-repo",
|
|
178
|
+
default="https://github.com/anthropics/remstack.git",
|
|
179
|
+
help="Git repository URL for ArgoCD",
|
|
180
|
+
)
|
|
181
|
+
@click.option(
|
|
182
|
+
"--region",
|
|
183
|
+
default="us-east-1",
|
|
184
|
+
help="AWS region (default: us-east-1)",
|
|
185
|
+
)
|
|
186
|
+
@click.option(
|
|
187
|
+
"--manifest-version",
|
|
188
|
+
default=None,
|
|
189
|
+
help="Manifest release version to download (e.g., v0.5.0). Default: latest",
|
|
190
|
+
)
|
|
191
|
+
@click.option(
|
|
192
|
+
"-y", "--yes",
|
|
193
|
+
is_flag=True,
|
|
194
|
+
help="Auto-confirm manifest download without prompting",
|
|
195
|
+
)
|
|
196
|
+
def init(
|
|
197
|
+
output: Path | None,
|
|
198
|
+
manifests_dir: Path | None,
|
|
199
|
+
project_name: str,
|
|
200
|
+
git_repo: str,
|
|
201
|
+
region: str,
|
|
202
|
+
manifest_version: str | None,
|
|
203
|
+
yes: bool,
|
|
204
|
+
):
|
|
205
|
+
"""
|
|
206
|
+
Initialize a new cluster configuration file.
|
|
207
|
+
|
|
208
|
+
Creates a cluster-config.yaml with your project settings that can be
|
|
209
|
+
used with other `rem cluster` commands.
|
|
210
|
+
|
|
211
|
+
If manifests are not found locally, offers to download them from
|
|
212
|
+
the GitHub releases matching your installed remdb version.
|
|
213
|
+
|
|
214
|
+
Examples:
|
|
215
|
+
rem cluster init
|
|
216
|
+
rem cluster init --project-name myapp --git-repo https://github.com/myorg/myrepo.git
|
|
217
|
+
rem cluster init -o my-cluster.yaml
|
|
218
|
+
rem cluster init -y # Auto-download manifests without prompting
|
|
219
|
+
rem cluster init --manifest-version v0.5.0 # Download specific manifest version
|
|
220
|
+
"""
|
|
221
|
+
# Determine manifests directory
|
|
222
|
+
if manifests_dir is None:
|
|
223
|
+
manifests_dir = Path.cwd() / "manifests"
|
|
224
|
+
|
|
225
|
+
# Check if manifests exist
|
|
226
|
+
manifests_exist = manifests_dir.exists() and (manifests_dir / "cluster-config.yaml").exists()
|
|
227
|
+
|
|
228
|
+
if not manifests_exist:
|
|
229
|
+
# Manifests not found - offer to download
|
|
230
|
+
click.echo()
|
|
231
|
+
click.echo(f"Manifests not found at: {manifests_dir}")
|
|
232
|
+
click.echo()
|
|
233
|
+
|
|
234
|
+
# Determine version to download
|
|
235
|
+
if manifest_version is None:
|
|
236
|
+
manifest_version = "latest"
|
|
237
|
+
|
|
238
|
+
click.echo(f"Manifest version: {manifest_version}")
|
|
239
|
+
click.echo(f"remdb version: {get_current_version()}")
|
|
240
|
+
|
|
241
|
+
# Prompt or auto-confirm
|
|
242
|
+
if yes or click.confirm(f"Download manifests ({manifest_version})?", default=True):
|
|
243
|
+
click.echo()
|
|
244
|
+
success = download_manifests(manifest_version, manifests_dir.parent)
|
|
245
|
+
if not success:
|
|
246
|
+
click.echo()
|
|
247
|
+
click.secho("Failed to download manifests. You can:", fg="yellow")
|
|
248
|
+
click.echo(" 1. Clone the repo: git clone https://github.com/anthropics/remstack.git")
|
|
249
|
+
click.echo(" 2. Download manually from: https://github.com/anthropics/remstack/releases")
|
|
250
|
+
click.echo(" 3. Specify existing manifests: rem cluster init --manifests-dir /path/to/manifests")
|
|
251
|
+
raise click.Abort()
|
|
252
|
+
click.echo()
|
|
253
|
+
else:
|
|
254
|
+
click.echo()
|
|
255
|
+
click.secho("Skipping manifest download.", fg="yellow")
|
|
256
|
+
click.echo("You can download later or specify a path with --manifests-dir")
|
|
257
|
+
click.echo()
|
|
258
|
+
|
|
259
|
+
# Set output path
|
|
260
|
+
if output is None:
|
|
261
|
+
output = manifests_dir / "cluster-config.yaml"
|
|
262
|
+
|
|
263
|
+
# Check if config file exists
|
|
264
|
+
if output.exists():
|
|
265
|
+
if not click.confirm(f"{output} already exists. Overwrite?"):
|
|
266
|
+
raise click.Abort()
|
|
267
|
+
|
|
268
|
+
# Read template if it exists
|
|
269
|
+
template_path = manifests_dir / "cluster-config.yaml"
|
|
270
|
+
if template_path.exists() and template_path != output:
|
|
271
|
+
with open(template_path) as f:
|
|
272
|
+
config = yaml.safe_load(f) or {}
|
|
273
|
+
else:
|
|
274
|
+
config = {}
|
|
275
|
+
|
|
276
|
+
# Update with provided values
|
|
277
|
+
if "project" not in config:
|
|
278
|
+
config["project"] = {}
|
|
279
|
+
config["project"]["name"] = project_name
|
|
280
|
+
config["project"]["namespace"] = project_name
|
|
281
|
+
|
|
282
|
+
if "git" not in config:
|
|
283
|
+
config["git"] = {}
|
|
284
|
+
config["git"]["repoURL"] = git_repo
|
|
285
|
+
|
|
286
|
+
if "aws" not in config:
|
|
287
|
+
config["aws"] = {}
|
|
288
|
+
config["aws"]["region"] = region
|
|
289
|
+
config["aws"]["ssmPrefix"] = f"/{project_name}"
|
|
290
|
+
|
|
291
|
+
# Write config
|
|
292
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
293
|
+
with open(output, "w") as f:
|
|
294
|
+
yaml.dump(config, f, default_flow_style=False, sort_keys=False)
|
|
295
|
+
|
|
296
|
+
click.secho(f"✓ Created cluster config: {output}", fg="green")
|
|
297
|
+
click.echo()
|
|
298
|
+
click.echo("Next steps:")
|
|
299
|
+
click.echo(f" 1. Edit {output} to customize settings")
|
|
300
|
+
click.echo(" 2. Deploy CDK infrastructure: cd manifests/infra/cdk-eks && cdk deploy")
|
|
301
|
+
click.echo(" 3. Run: rem cluster setup-ssm")
|
|
302
|
+
click.echo(" 4. Run: rem cluster generate")
|
|
303
|
+
click.echo(" 5. Run: rem cluster validate")
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
@click.command("setup-ssm")
|
|
307
|
+
@click.option(
|
|
308
|
+
"--config",
|
|
309
|
+
"-c",
|
|
310
|
+
type=click.Path(exists=True, path_type=Path),
|
|
311
|
+
help="Path to cluster config file",
|
|
312
|
+
)
|
|
313
|
+
@click.option(
|
|
314
|
+
"--dry-run",
|
|
315
|
+
is_flag=True,
|
|
316
|
+
help="Show commands without executing",
|
|
317
|
+
)
|
|
318
|
+
@click.option(
|
|
319
|
+
"--force",
|
|
320
|
+
is_flag=True,
|
|
321
|
+
help="Overwrite existing parameters",
|
|
322
|
+
)
|
|
323
|
+
def setup_ssm(config: Path | None, dry_run: bool, force: bool):
|
|
324
|
+
"""
|
|
325
|
+
Create required SSM parameters in AWS.
|
|
326
|
+
|
|
327
|
+
Creates the following parameters under the configured SSM prefix:
|
|
328
|
+
- /postgres/username (String)
|
|
329
|
+
- /postgres/password (SecureString, auto-generated)
|
|
330
|
+
- /llm/anthropic-api-key (SecureString, placeholder)
|
|
331
|
+
- /llm/openai-api-key (SecureString, placeholder)
|
|
332
|
+
|
|
333
|
+
Optional Phoenix parameters:
|
|
334
|
+
- /phoenix/api-key (SecureString, auto-generated)
|
|
335
|
+
- /phoenix/secret (SecureString, auto-generated)
|
|
336
|
+
|
|
337
|
+
Examples:
|
|
338
|
+
rem cluster setup-ssm
|
|
339
|
+
rem cluster setup-ssm --config my-cluster.yaml
|
|
340
|
+
rem cluster setup-ssm --dry-run
|
|
341
|
+
"""
|
|
342
|
+
import secrets
|
|
343
|
+
|
|
344
|
+
cfg = load_cluster_config(config)
|
|
345
|
+
prefix = cfg.get("aws", {}).get("ssmPrefix", "/rem")
|
|
346
|
+
region = cfg.get("aws", {}).get("region", "us-east-1")
|
|
347
|
+
|
|
348
|
+
click.echo()
|
|
349
|
+
click.echo("SSM Parameter Setup")
|
|
350
|
+
click.echo("=" * 60)
|
|
351
|
+
click.echo(f"Prefix: {prefix}")
|
|
352
|
+
click.echo(f"Region: {region}")
|
|
353
|
+
click.echo()
|
|
354
|
+
|
|
355
|
+
# Define parameters to create
|
|
356
|
+
parameters = [
|
|
357
|
+
# Required
|
|
358
|
+
(f"{prefix}/postgres/username", "remuser", "String", "PostgreSQL username"),
|
|
359
|
+
(f"{prefix}/postgres/password", secrets.token_urlsafe(24), "SecureString", "PostgreSQL password"),
|
|
360
|
+
# LLM keys - placeholders that user must fill in
|
|
361
|
+
(f"{prefix}/llm/anthropic-api-key", "REPLACE_WITH_YOUR_KEY", "SecureString", "Anthropic API key"),
|
|
362
|
+
(f"{prefix}/llm/openai-api-key", "REPLACE_WITH_YOUR_KEY", "SecureString", "OpenAI API key"),
|
|
363
|
+
# Phoenix - auto-generated
|
|
364
|
+
(f"{prefix}/phoenix/api-key", secrets.token_hex(16), "SecureString", "Phoenix API key"),
|
|
365
|
+
(f"{prefix}/phoenix/secret", secrets.token_hex(32), "SecureString", "Phoenix session secret"),
|
|
366
|
+
]
|
|
367
|
+
|
|
368
|
+
for name, value, param_type, description in parameters:
|
|
369
|
+
# Check if exists
|
|
370
|
+
check_cmd = ["aws", "ssm", "get-parameter", "--name", name, "--region", region]
|
|
371
|
+
|
|
372
|
+
if not dry_run:
|
|
373
|
+
result = subprocess.run(check_cmd, capture_output=True)
|
|
374
|
+
exists = result.returncode == 0
|
|
375
|
+
|
|
376
|
+
if exists and not force:
|
|
377
|
+
click.echo(f" ⏭ {name} (exists, skipping)")
|
|
378
|
+
continue
|
|
379
|
+
|
|
380
|
+
# Create/update parameter
|
|
381
|
+
put_cmd = [
|
|
382
|
+
"aws", "ssm", "put-parameter",
|
|
383
|
+
"--name", name,
|
|
384
|
+
"--value", value if "REPLACE" not in value else value,
|
|
385
|
+
"--type", param_type,
|
|
386
|
+
"--region", region,
|
|
387
|
+
"--overwrite" if force else "",
|
|
388
|
+
"--description", description,
|
|
389
|
+
]
|
|
390
|
+
# Remove empty strings
|
|
391
|
+
put_cmd = [c for c in put_cmd if c]
|
|
392
|
+
|
|
393
|
+
if dry_run:
|
|
394
|
+
display_value = "***" if param_type == "SecureString" else value
|
|
395
|
+
click.echo(f" Would create: {name} = {display_value}")
|
|
396
|
+
else:
|
|
397
|
+
try:
|
|
398
|
+
subprocess.run(put_cmd, check=True, capture_output=True)
|
|
399
|
+
click.secho(f" ✓ {name}", fg="green")
|
|
400
|
+
except subprocess.CalledProcessError as e:
|
|
401
|
+
if "ParameterAlreadyExists" in str(e.stderr):
|
|
402
|
+
click.echo(f" ⏭ {name} (exists)")
|
|
403
|
+
else:
|
|
404
|
+
click.secho(f" ✗ {name}: {e.stderr.decode()}", fg="red")
|
|
405
|
+
|
|
406
|
+
click.echo()
|
|
407
|
+
if dry_run:
|
|
408
|
+
click.secho("Dry run - no parameters created", fg="yellow")
|
|
409
|
+
else:
|
|
410
|
+
click.secho("✓ SSM parameters configured", fg="green")
|
|
411
|
+
click.echo()
|
|
412
|
+
click.echo("IMPORTANT: Update placeholder API keys:")
|
|
413
|
+
click.echo(f" aws ssm put-parameter --name {prefix}/llm/anthropic-api-key --value 'sk-...' --type SecureString --overwrite")
|
|
414
|
+
click.echo(f" aws ssm put-parameter --name {prefix}/llm/openai-api-key --value 'sk-...' --type SecureString --overwrite")
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def _generate_sql_configmap(project_name: str, namespace: str, output_dir: Path) -> None:
|
|
418
|
+
"""
|
|
419
|
+
Generate SQL init ConfigMap from migration files.
|
|
420
|
+
|
|
421
|
+
Called by `cluster generate` to include SQL migrations in the manifest generation.
|
|
422
|
+
"""
|
|
423
|
+
from ...utils.sql_paths import get_package_migrations_dir
|
|
424
|
+
|
|
425
|
+
sql_dir = get_package_migrations_dir()
|
|
426
|
+
|
|
427
|
+
if not sql_dir.exists():
|
|
428
|
+
click.secho(f" ⚠ SQL directory not found: {sql_dir}", fg="yellow")
|
|
429
|
+
click.echo(" Run 'rem db schema generate' to create migrations")
|
|
430
|
+
return
|
|
431
|
+
|
|
432
|
+
# Read all SQL files in sorted order
|
|
433
|
+
sql_files = {}
|
|
434
|
+
for sql_file in sorted(sql_dir.glob("*.sql")):
|
|
435
|
+
content = sql_file.read_text(encoding="utf-8")
|
|
436
|
+
sql_files[sql_file.name] = content
|
|
437
|
+
|
|
438
|
+
if not sql_files:
|
|
439
|
+
click.secho(" ⚠ No SQL files found in migrations directory", fg="yellow")
|
|
440
|
+
return
|
|
441
|
+
|
|
442
|
+
# Generate ConfigMap YAML
|
|
443
|
+
configmap = {
|
|
444
|
+
"apiVersion": "v1",
|
|
445
|
+
"kind": "ConfigMap",
|
|
446
|
+
"metadata": {
|
|
447
|
+
"name": f"{project_name}-postgres-init-sql",
|
|
448
|
+
"namespace": namespace,
|
|
449
|
+
"labels": {
|
|
450
|
+
"app.kubernetes.io/name": f"{project_name}-postgres",
|
|
451
|
+
"app.kubernetes.io/component": "init-sql",
|
|
452
|
+
},
|
|
453
|
+
},
|
|
454
|
+
"data": sql_files,
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
output = output_dir / "application" / "rem-stack" / "components" / "postgres" / "postgres-init-configmap.yaml"
|
|
458
|
+
output.parent.mkdir(parents=True, exist_ok=True)
|
|
459
|
+
|
|
460
|
+
with open(output, "w") as f:
|
|
461
|
+
f.write("# Auto-generated by: rem cluster generate\n")
|
|
462
|
+
f.write("# Do not edit manually - regenerate with 'rem cluster generate'\n")
|
|
463
|
+
f.write("#\n")
|
|
464
|
+
f.write("# Source files:\n")
|
|
465
|
+
for name in sql_files:
|
|
466
|
+
f.write(f"# - rem/sql/migrations/{name}\n")
|
|
467
|
+
f.write("#\n")
|
|
468
|
+
yaml.dump(configmap, f, default_flow_style=False, sort_keys=False)
|
|
469
|
+
|
|
470
|
+
click.secho(f" ✓ Generated {output.name} ({len(sql_files)} SQL files)", fg="green")
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
@click.command()
|
|
474
|
+
@click.option(
|
|
475
|
+
"--config",
|
|
476
|
+
"-c",
|
|
477
|
+
type=click.Path(exists=True, path_type=Path),
|
|
478
|
+
help="Path to cluster config file",
|
|
479
|
+
)
|
|
480
|
+
def validate(config: Path | None):
|
|
481
|
+
"""
|
|
482
|
+
Validate deployment prerequisites.
|
|
483
|
+
|
|
484
|
+
Checks:
|
|
485
|
+
1. kubectl connectivity
|
|
486
|
+
2. Required namespaces exist
|
|
487
|
+
3. Platform operators installed (ESO, CNPG, KEDA)
|
|
488
|
+
4. ClusterSecretStores configured
|
|
489
|
+
5. SSM parameters exist
|
|
490
|
+
6. Pod Identity associations
|
|
491
|
+
|
|
492
|
+
Examples:
|
|
493
|
+
rem cluster validate
|
|
494
|
+
rem cluster validate --config my-cluster.yaml
|
|
495
|
+
"""
|
|
496
|
+
cfg = load_cluster_config(config)
|
|
497
|
+
project_name = cfg.get("project", {}).get("name", "rem")
|
|
498
|
+
namespace = cfg.get("project", {}).get("namespace", project_name)
|
|
499
|
+
region = cfg.get("aws", {}).get("region", "us-east-1")
|
|
500
|
+
ssm_prefix = cfg.get("aws", {}).get("ssmPrefix", f"/{project_name}")
|
|
501
|
+
|
|
502
|
+
click.echo()
|
|
503
|
+
click.echo("REM Cluster Validation")
|
|
504
|
+
click.echo("=" * 60)
|
|
505
|
+
click.echo(f"Project: {project_name}")
|
|
506
|
+
click.echo(f"Namespace: {namespace}")
|
|
507
|
+
click.echo(f"Region: {region}")
|
|
508
|
+
click.echo()
|
|
509
|
+
|
|
510
|
+
errors = []
|
|
511
|
+
warnings = []
|
|
512
|
+
|
|
513
|
+
# 1. Check kubectl connectivity
|
|
514
|
+
click.echo("1. Kubernetes connectivity")
|
|
515
|
+
try:
|
|
516
|
+
result = subprocess.run(
|
|
517
|
+
["kubectl", "cluster-info"],
|
|
518
|
+
capture_output=True,
|
|
519
|
+
timeout=10,
|
|
520
|
+
)
|
|
521
|
+
if result.returncode == 0:
|
|
522
|
+
click.secho(" ✓ kubectl connected", fg="green")
|
|
523
|
+
else:
|
|
524
|
+
errors.append("kubectl not connected to cluster")
|
|
525
|
+
click.secho(" ✗ kubectl not connected", fg="red")
|
|
526
|
+
except Exception as e:
|
|
527
|
+
errors.append(f"kubectl error: {e}")
|
|
528
|
+
click.secho(f" ✗ kubectl error: {e}", fg="red")
|
|
529
|
+
|
|
530
|
+
# 2. Check platform operators
|
|
531
|
+
click.echo()
|
|
532
|
+
click.echo("2. Platform operators")
|
|
533
|
+
operators = [
|
|
534
|
+
("external-secrets-system", "external-secrets", "External Secrets Operator"),
|
|
535
|
+
("cnpg-system", "cnpg-controller-manager", "CloudNativePG"),
|
|
536
|
+
("keda", "keda-operator", "KEDA"),
|
|
537
|
+
]
|
|
538
|
+
|
|
539
|
+
for ns, deployment, name in operators:
|
|
540
|
+
try:
|
|
541
|
+
result = subprocess.run(
|
|
542
|
+
["kubectl", "get", "deployment", deployment, "-n", ns],
|
|
543
|
+
capture_output=True,
|
|
544
|
+
)
|
|
545
|
+
if result.returncode == 0:
|
|
546
|
+
click.secho(f" ✓ {name}", fg="green")
|
|
547
|
+
else:
|
|
548
|
+
warnings.append(f"{name} not found in {ns}")
|
|
549
|
+
click.secho(f" ⚠ {name} not found", fg="yellow")
|
|
550
|
+
except Exception:
|
|
551
|
+
warnings.append(f"Could not check {name}")
|
|
552
|
+
click.secho(f" ⚠ Could not check {name}", fg="yellow")
|
|
553
|
+
|
|
554
|
+
# 3. Check ClusterSecretStores
|
|
555
|
+
click.echo()
|
|
556
|
+
click.echo("3. ClusterSecretStores")
|
|
557
|
+
stores = ["aws-parameter-store", "kubernetes-secrets"]
|
|
558
|
+
|
|
559
|
+
for store in stores:
|
|
560
|
+
try:
|
|
561
|
+
result = subprocess.run(
|
|
562
|
+
["kubectl", "get", "clustersecretstore", store],
|
|
563
|
+
capture_output=True,
|
|
564
|
+
)
|
|
565
|
+
if result.returncode == 0:
|
|
566
|
+
click.secho(f" ✓ {store}", fg="green")
|
|
567
|
+
else:
|
|
568
|
+
warnings.append(f"ClusterSecretStore {store} not found")
|
|
569
|
+
click.secho(f" ⚠ {store} not found", fg="yellow")
|
|
570
|
+
except Exception:
|
|
571
|
+
warnings.append(f"Could not check ClusterSecretStore {store}")
|
|
572
|
+
click.secho(f" ⚠ Could not check {store}", fg="yellow")
|
|
573
|
+
|
|
574
|
+
# 4. Check SSM parameters
|
|
575
|
+
click.echo()
|
|
576
|
+
click.echo("4. SSM parameters")
|
|
577
|
+
required_params = [
|
|
578
|
+
f"{ssm_prefix}/postgres/username",
|
|
579
|
+
f"{ssm_prefix}/postgres/password",
|
|
580
|
+
]
|
|
581
|
+
optional_params = [
|
|
582
|
+
f"{ssm_prefix}/llm/anthropic-api-key",
|
|
583
|
+
f"{ssm_prefix}/llm/openai-api-key",
|
|
584
|
+
]
|
|
585
|
+
|
|
586
|
+
for param in required_params:
|
|
587
|
+
try:
|
|
588
|
+
result = subprocess.run(
|
|
589
|
+
["aws", "ssm", "get-parameter", "--name", param, "--region", region],
|
|
590
|
+
capture_output=True,
|
|
591
|
+
)
|
|
592
|
+
if result.returncode == 0:
|
|
593
|
+
click.secho(f" ✓ {param}", fg="green")
|
|
594
|
+
else:
|
|
595
|
+
errors.append(f"Required SSM parameter missing: {param}")
|
|
596
|
+
click.secho(f" ✗ {param} (required)", fg="red")
|
|
597
|
+
except Exception as e:
|
|
598
|
+
errors.append(f"Could not check SSM: {e}")
|
|
599
|
+
click.secho(f" ✗ AWS CLI error: {e}", fg="red")
|
|
600
|
+
break
|
|
601
|
+
|
|
602
|
+
for param in optional_params:
|
|
603
|
+
try:
|
|
604
|
+
result = subprocess.run(
|
|
605
|
+
["aws", "ssm", "get-parameter", "--name", param, "--region", region],
|
|
606
|
+
capture_output=True,
|
|
607
|
+
)
|
|
608
|
+
if result.returncode == 0:
|
|
609
|
+
# Check if it's a placeholder
|
|
610
|
+
output = result.stdout.decode()
|
|
611
|
+
if "REPLACE_WITH" in output:
|
|
612
|
+
warnings.append(f"SSM parameter is placeholder: {param}")
|
|
613
|
+
click.secho(f" ⚠ {param} (placeholder)", fg="yellow")
|
|
614
|
+
else:
|
|
615
|
+
click.secho(f" ✓ {param}", fg="green")
|
|
616
|
+
else:
|
|
617
|
+
warnings.append(f"Optional SSM parameter missing: {param}")
|
|
618
|
+
click.secho(f" ⚠ {param} (optional)", fg="yellow")
|
|
619
|
+
except Exception:
|
|
620
|
+
pass # Already reported AWS CLI issues
|
|
621
|
+
|
|
622
|
+
# Summary
|
|
623
|
+
click.echo()
|
|
624
|
+
click.echo("=" * 60)
|
|
625
|
+
|
|
626
|
+
if errors:
|
|
627
|
+
click.secho(f"✗ Validation failed with {len(errors)} error(s)", fg="red")
|
|
628
|
+
for error in errors:
|
|
629
|
+
click.echo(f" - {error}")
|
|
630
|
+
raise click.Abort()
|
|
631
|
+
elif warnings:
|
|
632
|
+
click.secho(f"⚠ Validation passed with {len(warnings)} warning(s)", fg="yellow")
|
|
633
|
+
for warning in warnings:
|
|
634
|
+
click.echo(f" - {warning}")
|
|
635
|
+
else:
|
|
636
|
+
click.secho("✓ All checks passed", fg="green")
|
|
637
|
+
|
|
638
|
+
click.echo()
|
|
639
|
+
click.echo("Ready to deploy:")
|
|
640
|
+
click.echo(f" kubectl apply -f manifests/application/rem-stack/argocd-staging.yaml")
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
@click.command()
|
|
644
|
+
@click.option(
|
|
645
|
+
"--config",
|
|
646
|
+
"-c",
|
|
647
|
+
type=click.Path(exists=True, path_type=Path),
|
|
648
|
+
help="Path to cluster config file",
|
|
649
|
+
)
|
|
650
|
+
@click.option(
|
|
651
|
+
"--output-dir",
|
|
652
|
+
"-o",
|
|
653
|
+
type=click.Path(path_type=Path),
|
|
654
|
+
default=None,
|
|
655
|
+
help="Output directory for generated manifests",
|
|
656
|
+
)
|
|
657
|
+
def generate(config: Path | None, output_dir: Path | None):
|
|
658
|
+
"""
|
|
659
|
+
Generate Kubernetes manifests from cluster config.
|
|
660
|
+
|
|
661
|
+
Reads cluster-config.yaml and generates/updates:
|
|
662
|
+
- ArgoCD Application manifests
|
|
663
|
+
- ClusterSecretStore configurations
|
|
664
|
+
- SQL init ConfigMap (from rem/sql/migrations/*.sql)
|
|
665
|
+
- Kustomization patches
|
|
666
|
+
|
|
667
|
+
Examples:
|
|
668
|
+
rem cluster generate
|
|
669
|
+
rem cluster generate --config my-cluster.yaml
|
|
670
|
+
"""
|
|
671
|
+
cfg = load_cluster_config(config)
|
|
672
|
+
project_name = cfg.get("project", {}).get("name", "rem")
|
|
673
|
+
namespace = cfg.get("project", {}).get("namespace", project_name)
|
|
674
|
+
region = cfg.get("aws", {}).get("region", "us-east-1")
|
|
675
|
+
git_repo = cfg.get("git", {}).get("repoURL", "")
|
|
676
|
+
git_branch = cfg.get("git", {}).get("targetRevision", "main")
|
|
677
|
+
|
|
678
|
+
if output_dir is None:
|
|
679
|
+
output_dir = get_manifests_dir()
|
|
680
|
+
|
|
681
|
+
click.echo()
|
|
682
|
+
click.echo("Generating Manifests from Config")
|
|
683
|
+
click.echo("=" * 60)
|
|
684
|
+
click.echo(f"Project: {project_name}")
|
|
685
|
+
click.echo(f"Namespace: {namespace}")
|
|
686
|
+
click.echo(f"Git: {git_repo}@{git_branch}")
|
|
687
|
+
click.echo(f"Output: {output_dir}")
|
|
688
|
+
click.echo()
|
|
689
|
+
|
|
690
|
+
# Update ArgoCD application
|
|
691
|
+
argocd_app = output_dir / "application" / "rem-stack" / "argocd-staging.yaml"
|
|
692
|
+
if argocd_app.exists():
|
|
693
|
+
with open(argocd_app) as f:
|
|
694
|
+
content = f.read()
|
|
695
|
+
|
|
696
|
+
# Update git repo URL
|
|
697
|
+
if "repoURL:" in content:
|
|
698
|
+
import re
|
|
699
|
+
content = re.sub(
|
|
700
|
+
r'repoURL:.*',
|
|
701
|
+
f'repoURL: {git_repo}',
|
|
702
|
+
content,
|
|
703
|
+
)
|
|
704
|
+
content = re.sub(
|
|
705
|
+
r'namespace: rem\b',
|
|
706
|
+
f'namespace: {namespace}',
|
|
707
|
+
content,
|
|
708
|
+
)
|
|
709
|
+
|
|
710
|
+
with open(argocd_app, "w") as f:
|
|
711
|
+
f.write(content)
|
|
712
|
+
click.secho(f" ✓ Updated {argocd_app.name}", fg="green")
|
|
713
|
+
|
|
714
|
+
# Update ClusterSecretStore region
|
|
715
|
+
css = output_dir / "platform" / "external-secrets" / "cluster-secret-store.yaml"
|
|
716
|
+
if css.exists():
|
|
717
|
+
with open(css) as f:
|
|
718
|
+
content = f.read()
|
|
719
|
+
|
|
720
|
+
if "region:" in content:
|
|
721
|
+
import re
|
|
722
|
+
content = re.sub(
|
|
723
|
+
r'region:.*',
|
|
724
|
+
f'region: {region}',
|
|
725
|
+
content,
|
|
726
|
+
)
|
|
727
|
+
|
|
728
|
+
with open(css, "w") as f:
|
|
729
|
+
f.write(content)
|
|
730
|
+
click.secho(f" ✓ Updated {css.name}", fg="green")
|
|
731
|
+
|
|
732
|
+
# Generate SQL init ConfigMap from migrations
|
|
733
|
+
_generate_sql_configmap(project_name, namespace, output_dir)
|
|
734
|
+
|
|
735
|
+
click.echo()
|
|
736
|
+
click.secho("✓ Manifests generated", fg="green")
|
|
737
|
+
click.echo()
|
|
738
|
+
click.echo("Next steps:")
|
|
739
|
+
click.echo(" 1. Review generated manifests")
|
|
740
|
+
click.echo(" 2. Commit changes to git")
|
|
741
|
+
click.echo(" 3. Deploy: kubectl apply -f manifests/application/rem-stack/argocd-staging.yaml")
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
# =============================================================================
|
|
745
|
+
# Environment Configuration Commands (rem cluster env ...)
|
|
746
|
+
# =============================================================================
|
|
747
|
+
|
|
748
|
+
@click.group()
|
|
749
|
+
def env():
|
|
750
|
+
"""
|
|
751
|
+
Environment configuration management.
|
|
752
|
+
|
|
753
|
+
Commands for validating and generating Kubernetes ConfigMaps
|
|
754
|
+
from local .env files, ensuring consistency between local
|
|
755
|
+
development and cluster deployments.
|
|
756
|
+
|
|
757
|
+
Examples:
|
|
758
|
+
rem cluster env check # Validate .env for staging
|
|
759
|
+
rem cluster env check --env prod # Validate for production
|
|
760
|
+
rem cluster env generate # Generate ConfigMap from .env
|
|
761
|
+
rem cluster env diff # Compare .env with cluster
|
|
762
|
+
"""
|
|
763
|
+
pass
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
# Patterns that indicate localhost/development values inappropriate for cluster
|
|
767
|
+
LOCALHOST_PATTERNS = [
|
|
768
|
+
"localhost",
|
|
769
|
+
"127.0.0.1",
|
|
770
|
+
"0.0.0.0",
|
|
771
|
+
"host.docker.internal",
|
|
772
|
+
]
|
|
773
|
+
|
|
774
|
+
# Required env vars for each environment
|
|
775
|
+
# These align with rem-config ConfigMap structure in manifests/application/rem-stack/base/kustomization.yaml
|
|
776
|
+
ENV_REQUIREMENTS = {
|
|
777
|
+
"staging": {
|
|
778
|
+
"required": [
|
|
779
|
+
"ENVIRONMENT",
|
|
780
|
+
"AWS_REGION",
|
|
781
|
+
"S3__BUCKET_NAME",
|
|
782
|
+
],
|
|
783
|
+
"recommended": [
|
|
784
|
+
"LLM__ANTHROPIC_API_KEY",
|
|
785
|
+
"LLM__OPENAI_API_KEY",
|
|
786
|
+
"LLM__DEFAULT_MODEL",
|
|
787
|
+
"OTEL_COLLECTOR_ENDPOINT",
|
|
788
|
+
"OTEL__ENABLED",
|
|
789
|
+
"LOG_LEVEL",
|
|
790
|
+
"AUTH__ENABLED",
|
|
791
|
+
"MODELS__IMPORT_MODULES",
|
|
792
|
+
],
|
|
793
|
+
"no_localhost": [
|
|
794
|
+
"POSTGRES__CONNECTION_STRING",
|
|
795
|
+
"OTEL_COLLECTOR_ENDPOINT",
|
|
796
|
+
"S3__ENDPOINT_URL",
|
|
797
|
+
],
|
|
798
|
+
},
|
|
799
|
+
"prod": {
|
|
800
|
+
"required": [
|
|
801
|
+
"ENVIRONMENT",
|
|
802
|
+
"AWS_REGION",
|
|
803
|
+
"S3__BUCKET_NAME",
|
|
804
|
+
"AUTH__ENABLED",
|
|
805
|
+
],
|
|
806
|
+
"recommended": [
|
|
807
|
+
"LLM__ANTHROPIC_API_KEY",
|
|
808
|
+
"LLM__OPENAI_API_KEY",
|
|
809
|
+
"LLM__DEFAULT_MODEL",
|
|
810
|
+
"OTEL_COLLECTOR_ENDPOINT",
|
|
811
|
+
"OTEL__ENABLED",
|
|
812
|
+
"LOG_LEVEL",
|
|
813
|
+
"AUTH__SESSION_SECRET",
|
|
814
|
+
"MODELS__IMPORT_MODULES",
|
|
815
|
+
],
|
|
816
|
+
"no_localhost": [
|
|
817
|
+
"POSTGRES__CONNECTION_STRING",
|
|
818
|
+
"OTEL_COLLECTOR_ENDPOINT",
|
|
819
|
+
"S3__ENDPOINT_URL",
|
|
820
|
+
"AUTH__GOOGLE__REDIRECT_URI",
|
|
821
|
+
"AUTH__MICROSOFT__REDIRECT_URI",
|
|
822
|
+
],
|
|
823
|
+
},
|
|
824
|
+
"local": {
|
|
825
|
+
"required": [
|
|
826
|
+
"ENVIRONMENT",
|
|
827
|
+
],
|
|
828
|
+
"recommended": [
|
|
829
|
+
"LLM__ANTHROPIC_API_KEY",
|
|
830
|
+
"LLM__OPENAI_API_KEY",
|
|
831
|
+
"MODELS__IMPORT_MODULES",
|
|
832
|
+
],
|
|
833
|
+
"no_localhost": [], # localhost is fine for local
|
|
834
|
+
},
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
def load_env_file(env_path: Path) -> dict[str, str]:
|
|
839
|
+
"""Load environment variables from a .env file."""
|
|
840
|
+
env_vars = {}
|
|
841
|
+
if not env_path.exists():
|
|
842
|
+
return env_vars
|
|
843
|
+
|
|
844
|
+
with open(env_path) as f:
|
|
845
|
+
for line in f:
|
|
846
|
+
line = line.strip()
|
|
847
|
+
# Skip comments and empty lines
|
|
848
|
+
if not line or line.startswith("#"):
|
|
849
|
+
continue
|
|
850
|
+
# Parse KEY=value
|
|
851
|
+
if "=" in line:
|
|
852
|
+
key, _, value = line.partition("=")
|
|
853
|
+
key = key.strip()
|
|
854
|
+
value = value.strip()
|
|
855
|
+
# Remove quotes if present
|
|
856
|
+
if value and value[0] in ('"', "'") and value[-1] == value[0]:
|
|
857
|
+
value = value[1:-1]
|
|
858
|
+
env_vars[key] = value
|
|
859
|
+
|
|
860
|
+
return env_vars
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
def has_localhost(value: str) -> bool:
|
|
864
|
+
"""Check if a value contains localhost-like patterns."""
|
|
865
|
+
value_lower = value.lower()
|
|
866
|
+
return any(pattern in value_lower for pattern in LOCALHOST_PATTERNS)
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
@env.command("check")
|
|
870
|
+
@click.option(
|
|
871
|
+
"--env-file",
|
|
872
|
+
"-f",
|
|
873
|
+
type=click.Path(exists=True, path_type=Path),
|
|
874
|
+
default=None,
|
|
875
|
+
help="Path to .env file (default: .env in current directory)",
|
|
876
|
+
)
|
|
877
|
+
@click.option(
|
|
878
|
+
"--environment",
|
|
879
|
+
"--env",
|
|
880
|
+
"-e",
|
|
881
|
+
type=click.Choice(["local", "staging", "prod"]),
|
|
882
|
+
default="staging",
|
|
883
|
+
help="Target environment to validate against (default: staging)",
|
|
884
|
+
)
|
|
885
|
+
@click.option(
|
|
886
|
+
"--strict",
|
|
887
|
+
is_flag=True,
|
|
888
|
+
help="Treat warnings as errors",
|
|
889
|
+
)
|
|
890
|
+
def env_check(env_file: Path | None, environment: str, strict: bool):
|
|
891
|
+
"""
|
|
892
|
+
Validate .env file for a target environment.
|
|
893
|
+
|
|
894
|
+
Checks that environment variables are appropriate for the target
|
|
895
|
+
deployment environment (local, staging, prod).
|
|
896
|
+
|
|
897
|
+
Validates:
|
|
898
|
+
- Required variables are set
|
|
899
|
+
- No localhost values in cluster configs
|
|
900
|
+
- Recommended variables for the environment
|
|
901
|
+
- Placeholder values that need updating
|
|
902
|
+
|
|
903
|
+
Examples:
|
|
904
|
+
rem cluster env check # Check .env for staging
|
|
905
|
+
rem cluster env check --env prod # Check for production
|
|
906
|
+
rem cluster env check -f backend/.env # Check specific file
|
|
907
|
+
rem cluster env check --strict # Fail on warnings
|
|
908
|
+
"""
|
|
909
|
+
# Find .env file
|
|
910
|
+
if env_file is None:
|
|
911
|
+
# Try common locations
|
|
912
|
+
for candidate in [Path(".env"), Path("application/backend/.env"), Path("backend/.env")]:
|
|
913
|
+
if candidate.exists():
|
|
914
|
+
env_file = candidate
|
|
915
|
+
break
|
|
916
|
+
|
|
917
|
+
if env_file is None or not env_file.exists():
|
|
918
|
+
click.secho("✗ No .env file found", fg="red")
|
|
919
|
+
click.echo()
|
|
920
|
+
click.echo("Specify path with: rem cluster env check -f /path/to/.env")
|
|
921
|
+
raise click.Abort()
|
|
922
|
+
|
|
923
|
+
click.echo()
|
|
924
|
+
click.echo(f"Environment Config Check: {environment}")
|
|
925
|
+
click.echo("=" * 60)
|
|
926
|
+
click.echo(f"File: {env_file}")
|
|
927
|
+
click.echo()
|
|
928
|
+
|
|
929
|
+
# Load env vars
|
|
930
|
+
env_vars = load_env_file(env_file)
|
|
931
|
+
|
|
932
|
+
if not env_vars:
|
|
933
|
+
click.secho("✗ No environment variables found in file", fg="red")
|
|
934
|
+
raise click.Abort()
|
|
935
|
+
|
|
936
|
+
click.echo(f"Found {len(env_vars)} variables")
|
|
937
|
+
click.echo()
|
|
938
|
+
|
|
939
|
+
requirements = ENV_REQUIREMENTS.get(environment, ENV_REQUIREMENTS["staging"])
|
|
940
|
+
errors = []
|
|
941
|
+
warnings = []
|
|
942
|
+
|
|
943
|
+
# Check required variables
|
|
944
|
+
click.echo("Required variables:")
|
|
945
|
+
for var in requirements["required"]:
|
|
946
|
+
if var in env_vars and env_vars[var]:
|
|
947
|
+
click.secho(f" ✓ {var}", fg="green")
|
|
948
|
+
else:
|
|
949
|
+
errors.append(f"Missing required: {var}")
|
|
950
|
+
click.secho(f" ✗ {var} (missing or empty)", fg="red")
|
|
951
|
+
|
|
952
|
+
# Check for localhost in cluster configs
|
|
953
|
+
if requirements["no_localhost"]:
|
|
954
|
+
click.echo()
|
|
955
|
+
click.echo("Localhost check (should not contain localhost for cluster):")
|
|
956
|
+
for var in requirements["no_localhost"]:
|
|
957
|
+
if var in env_vars:
|
|
958
|
+
value = env_vars[var]
|
|
959
|
+
if has_localhost(value):
|
|
960
|
+
errors.append(f"Localhost value in {var}: {value}")
|
|
961
|
+
click.secho(f" ✗ {var} contains localhost: {value[:50]}...", fg="red")
|
|
962
|
+
else:
|
|
963
|
+
click.secho(f" ✓ {var}", fg="green")
|
|
964
|
+
else:
|
|
965
|
+
click.echo(f" - {var} (not set)")
|
|
966
|
+
|
|
967
|
+
# Check recommended variables
|
|
968
|
+
click.echo()
|
|
969
|
+
click.echo("Recommended variables:")
|
|
970
|
+
for var in requirements["recommended"]:
|
|
971
|
+
if var in env_vars:
|
|
972
|
+
value = env_vars[var]
|
|
973
|
+
# Check for placeholder values
|
|
974
|
+
if "REPLACE" in value or "YOUR_" in value or value == "":
|
|
975
|
+
warnings.append(f"Placeholder value: {var}")
|
|
976
|
+
click.secho(f" ⚠ {var} (placeholder value)", fg="yellow")
|
|
977
|
+
else:
|
|
978
|
+
click.secho(f" ✓ {var}", fg="green")
|
|
979
|
+
else:
|
|
980
|
+
warnings.append(f"Missing recommended: {var}")
|
|
981
|
+
click.secho(f" ⚠ {var} (not set)", fg="yellow")
|
|
982
|
+
|
|
983
|
+
# Check ENVIRONMENT value matches target
|
|
984
|
+
click.echo()
|
|
985
|
+
click.echo("Environment consistency:")
|
|
986
|
+
env_value = env_vars.get("ENVIRONMENT", "")
|
|
987
|
+
if env_value == environment or (environment == "local" and env_value == "development"):
|
|
988
|
+
click.secho(f" ✓ ENVIRONMENT={env_value} (matches target)", fg="green")
|
|
989
|
+
elif env_value:
|
|
990
|
+
warnings.append(f"ENVIRONMENT mismatch: {env_value} != {environment}")
|
|
991
|
+
click.secho(f" ⚠ ENVIRONMENT={env_value} (target is {environment})", fg="yellow")
|
|
992
|
+
|
|
993
|
+
# Summary
|
|
994
|
+
click.echo()
|
|
995
|
+
click.echo("=" * 60)
|
|
996
|
+
|
|
997
|
+
if errors:
|
|
998
|
+
click.secho(f"✗ Check failed with {len(errors)} error(s)", fg="red")
|
|
999
|
+
for error in errors:
|
|
1000
|
+
click.echo(f" - {error}")
|
|
1001
|
+
raise click.Abort()
|
|
1002
|
+
elif warnings:
|
|
1003
|
+
if strict:
|
|
1004
|
+
click.secho(f"✗ Check failed with {len(warnings)} warning(s) (strict mode)", fg="red")
|
|
1005
|
+
for warning in warnings:
|
|
1006
|
+
click.echo(f" - {warning}")
|
|
1007
|
+
raise click.Abort()
|
|
1008
|
+
else:
|
|
1009
|
+
click.secho(f"⚠ Check passed with {len(warnings)} warning(s)", fg="yellow")
|
|
1010
|
+
else:
|
|
1011
|
+
click.secho(f"✓ All checks passed for {environment}", fg="green")
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
@env.command("generate")
|
|
1015
|
+
@click.option(
|
|
1016
|
+
"--env-file",
|
|
1017
|
+
"-f",
|
|
1018
|
+
type=click.Path(exists=True, path_type=Path),
|
|
1019
|
+
default=None,
|
|
1020
|
+
help="Path to .env file",
|
|
1021
|
+
)
|
|
1022
|
+
@click.option(
|
|
1023
|
+
"--output",
|
|
1024
|
+
"-o",
|
|
1025
|
+
type=click.Path(path_type=Path),
|
|
1026
|
+
default=None,
|
|
1027
|
+
help="Output path for ConfigMap YAML",
|
|
1028
|
+
)
|
|
1029
|
+
@click.option(
|
|
1030
|
+
"--name",
|
|
1031
|
+
default="rem-config",
|
|
1032
|
+
help="ConfigMap name (default: rem-config)",
|
|
1033
|
+
)
|
|
1034
|
+
@click.option(
|
|
1035
|
+
"--namespace",
|
|
1036
|
+
"-n",
|
|
1037
|
+
default="siggy",
|
|
1038
|
+
help="Kubernetes namespace (default: siggy)",
|
|
1039
|
+
)
|
|
1040
|
+
@click.option(
|
|
1041
|
+
"--exclude-secrets",
|
|
1042
|
+
is_flag=True,
|
|
1043
|
+
default=True,
|
|
1044
|
+
help="Exclude secret values (API keys, passwords) - default: True",
|
|
1045
|
+
)
|
|
1046
|
+
@click.option(
|
|
1047
|
+
"--apply",
|
|
1048
|
+
is_flag=True,
|
|
1049
|
+
help="Apply ConfigMap directly to cluster",
|
|
1050
|
+
)
|
|
1051
|
+
def env_generate(
|
|
1052
|
+
env_file: Path | None,
|
|
1053
|
+
output: Path | None,
|
|
1054
|
+
name: str,
|
|
1055
|
+
namespace: str,
|
|
1056
|
+
exclude_secrets: bool,
|
|
1057
|
+
apply: bool,
|
|
1058
|
+
):
|
|
1059
|
+
"""
|
|
1060
|
+
Generate Kubernetes ConfigMap from .env file.
|
|
1061
|
+
|
|
1062
|
+
Converts local .env file to a Kubernetes ConfigMap YAML,
|
|
1063
|
+
optionally excluding sensitive values (API keys, passwords).
|
|
1064
|
+
|
|
1065
|
+
Secret values should be managed via ExternalSecrets/SSM, not ConfigMaps.
|
|
1066
|
+
|
|
1067
|
+
Examples:
|
|
1068
|
+
rem cluster env generate # Generate from .env
|
|
1069
|
+
rem cluster env generate -o configmap.yaml # Custom output path
|
|
1070
|
+
rem cluster env generate --apply # Apply to cluster
|
|
1071
|
+
"""
|
|
1072
|
+
# Secret patterns to exclude
|
|
1073
|
+
secret_patterns = [
|
|
1074
|
+
"API_KEY",
|
|
1075
|
+
"SECRET",
|
|
1076
|
+
"PASSWORD",
|
|
1077
|
+
"TOKEN",
|
|
1078
|
+
"CREDENTIAL",
|
|
1079
|
+
]
|
|
1080
|
+
|
|
1081
|
+
# Find .env file
|
|
1082
|
+
if env_file is None:
|
|
1083
|
+
for candidate in [Path(".env"), Path("application/backend/.env"), Path("backend/.env")]:
|
|
1084
|
+
if candidate.exists():
|
|
1085
|
+
env_file = candidate
|
|
1086
|
+
break
|
|
1087
|
+
|
|
1088
|
+
if env_file is None or not env_file.exists():
|
|
1089
|
+
click.secho("✗ No .env file found", fg="red")
|
|
1090
|
+
raise click.Abort()
|
|
1091
|
+
|
|
1092
|
+
click.echo()
|
|
1093
|
+
click.echo("Generate ConfigMap from .env")
|
|
1094
|
+
click.echo("=" * 60)
|
|
1095
|
+
click.echo(f"Source: {env_file}")
|
|
1096
|
+
click.echo(f"ConfigMap: {name}")
|
|
1097
|
+
click.echo(f"Namespace: {namespace}")
|
|
1098
|
+
click.echo()
|
|
1099
|
+
|
|
1100
|
+
# Load env vars
|
|
1101
|
+
env_vars = load_env_file(env_file)
|
|
1102
|
+
|
|
1103
|
+
# Filter out secrets if requested
|
|
1104
|
+
config_data = {}
|
|
1105
|
+
excluded = []
|
|
1106
|
+
|
|
1107
|
+
for key, value in env_vars.items():
|
|
1108
|
+
# Check if this looks like a secret
|
|
1109
|
+
is_secret = any(pattern in key.upper() for pattern in secret_patterns)
|
|
1110
|
+
|
|
1111
|
+
if exclude_secrets and is_secret:
|
|
1112
|
+
excluded.append(key)
|
|
1113
|
+
else:
|
|
1114
|
+
config_data[key] = value
|
|
1115
|
+
|
|
1116
|
+
click.echo(f"Variables to include: {len(config_data)}")
|
|
1117
|
+
if excluded:
|
|
1118
|
+
click.echo(f"Excluded (secrets): {len(excluded)}")
|
|
1119
|
+
for key in excluded[:5]:
|
|
1120
|
+
click.echo(f" - {key}")
|
|
1121
|
+
if len(excluded) > 5:
|
|
1122
|
+
click.echo(f" ... and {len(excluded) - 5} more")
|
|
1123
|
+
|
|
1124
|
+
# Generate ConfigMap
|
|
1125
|
+
configmap = {
|
|
1126
|
+
"apiVersion": "v1",
|
|
1127
|
+
"kind": "ConfigMap",
|
|
1128
|
+
"metadata": {
|
|
1129
|
+
"name": name,
|
|
1130
|
+
"namespace": namespace,
|
|
1131
|
+
"labels": {
|
|
1132
|
+
"app.kubernetes.io/managed-by": "rem-cli",
|
|
1133
|
+
},
|
|
1134
|
+
},
|
|
1135
|
+
"data": config_data,
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
# Output
|
|
1139
|
+
if output is None:
|
|
1140
|
+
output = Path(f"{name}-configmap.yaml")
|
|
1141
|
+
|
|
1142
|
+
with open(output, "w") as f:
|
|
1143
|
+
f.write(f"# Generated by: rem cluster env generate\n")
|
|
1144
|
+
f.write(f"# Source: {env_file}\n")
|
|
1145
|
+
f.write(f"# Date: {__import__('datetime').datetime.utcnow().isoformat()}Z\n")
|
|
1146
|
+
f.write("#\n")
|
|
1147
|
+
if excluded:
|
|
1148
|
+
f.write("# Excluded secrets (use ExternalSecrets for these):\n")
|
|
1149
|
+
for key in excluded:
|
|
1150
|
+
f.write(f"# - {key}\n")
|
|
1151
|
+
f.write("#\n")
|
|
1152
|
+
yaml.dump(configmap, f, default_flow_style=False, sort_keys=False)
|
|
1153
|
+
|
|
1154
|
+
click.echo()
|
|
1155
|
+
click.secho(f"✓ Generated: {output}", fg="green")
|
|
1156
|
+
|
|
1157
|
+
if apply:
|
|
1158
|
+
click.echo()
|
|
1159
|
+
click.echo("Applying to cluster...")
|
|
1160
|
+
try:
|
|
1161
|
+
subprocess.run(["kubectl", "apply", "-f", str(output)], check=True)
|
|
1162
|
+
click.secho("✓ ConfigMap applied", fg="green")
|
|
1163
|
+
except subprocess.CalledProcessError as e:
|
|
1164
|
+
click.secho(f"✗ Failed to apply: {e}", fg="red")
|
|
1165
|
+
raise click.Abort()
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
@env.command("diff")
|
|
1169
|
+
@click.option(
|
|
1170
|
+
"--env-file",
|
|
1171
|
+
"-f",
|
|
1172
|
+
type=click.Path(exists=True, path_type=Path),
|
|
1173
|
+
default=None,
|
|
1174
|
+
help="Path to .env file",
|
|
1175
|
+
)
|
|
1176
|
+
@click.option(
|
|
1177
|
+
"--configmap",
|
|
1178
|
+
"-c",
|
|
1179
|
+
default="rem-config",
|
|
1180
|
+
help="ConfigMap name to compare (default: rem-config)",
|
|
1181
|
+
)
|
|
1182
|
+
@click.option(
|
|
1183
|
+
"--namespace",
|
|
1184
|
+
"-n",
|
|
1185
|
+
default="siggy",
|
|
1186
|
+
help="Kubernetes namespace (default: siggy)",
|
|
1187
|
+
)
|
|
1188
|
+
def env_diff(env_file: Path | None, configmap: str, namespace: str):
|
|
1189
|
+
"""
|
|
1190
|
+
Compare local .env with cluster ConfigMap.
|
|
1191
|
+
|
|
1192
|
+
Shows differences between local environment configuration
|
|
1193
|
+
and what's deployed in the Kubernetes cluster.
|
|
1194
|
+
|
|
1195
|
+
Examples:
|
|
1196
|
+
rem cluster env diff # Compare with rem-config
|
|
1197
|
+
rem cluster env diff -c my-config # Compare with custom ConfigMap
|
|
1198
|
+
rem cluster env diff -n production # Compare in different namespace
|
|
1199
|
+
"""
|
|
1200
|
+
# Find .env file
|
|
1201
|
+
if env_file is None:
|
|
1202
|
+
for candidate in [Path(".env"), Path("application/backend/.env"), Path("backend/.env")]:
|
|
1203
|
+
if candidate.exists():
|
|
1204
|
+
env_file = candidate
|
|
1205
|
+
break
|
|
1206
|
+
|
|
1207
|
+
if env_file is None or not env_file.exists():
|
|
1208
|
+
click.secho("✗ No .env file found", fg="red")
|
|
1209
|
+
raise click.Abort()
|
|
1210
|
+
|
|
1211
|
+
click.echo()
|
|
1212
|
+
click.echo("Compare .env with Cluster ConfigMap")
|
|
1213
|
+
click.echo("=" * 60)
|
|
1214
|
+
click.echo(f"Local: {env_file}")
|
|
1215
|
+
click.echo(f"Cluster: {configmap} (namespace: {namespace})")
|
|
1216
|
+
click.echo()
|
|
1217
|
+
|
|
1218
|
+
# Load local env
|
|
1219
|
+
local_vars = load_env_file(env_file)
|
|
1220
|
+
|
|
1221
|
+
# Get cluster ConfigMap
|
|
1222
|
+
try:
|
|
1223
|
+
result = subprocess.run(
|
|
1224
|
+
["kubectl", "get", "configmap", configmap, "-n", namespace, "-o", "yaml"],
|
|
1225
|
+
capture_output=True,
|
|
1226
|
+
check=True,
|
|
1227
|
+
)
|
|
1228
|
+
cluster_cm = yaml.safe_load(result.stdout.decode())
|
|
1229
|
+
cluster_vars = cluster_cm.get("data", {})
|
|
1230
|
+
except subprocess.CalledProcessError:
|
|
1231
|
+
click.secho(f"✗ ConfigMap {configmap} not found in {namespace}", fg="red")
|
|
1232
|
+
click.echo()
|
|
1233
|
+
click.echo("Generate and apply with:")
|
|
1234
|
+
click.echo(f" rem cluster env generate --name {configmap} --namespace {namespace} --apply")
|
|
1235
|
+
raise click.Abort()
|
|
1236
|
+
|
|
1237
|
+
# Compare
|
|
1238
|
+
local_keys = set(local_vars.keys())
|
|
1239
|
+
cluster_keys = set(cluster_vars.keys())
|
|
1240
|
+
|
|
1241
|
+
only_local = local_keys - cluster_keys
|
|
1242
|
+
only_cluster = cluster_keys - local_keys
|
|
1243
|
+
common = local_keys & cluster_keys
|
|
1244
|
+
|
|
1245
|
+
# Check for differences in common keys
|
|
1246
|
+
different = []
|
|
1247
|
+
for key in common:
|
|
1248
|
+
if local_vars[key] != cluster_vars[key]:
|
|
1249
|
+
different.append(key)
|
|
1250
|
+
|
|
1251
|
+
# Report
|
|
1252
|
+
if only_local:
|
|
1253
|
+
click.echo(f"Only in local .env ({len(only_local)}):")
|
|
1254
|
+
for key in sorted(only_local)[:10]:
|
|
1255
|
+
click.secho(f" + {key}", fg="green")
|
|
1256
|
+
if len(only_local) > 10:
|
|
1257
|
+
click.echo(f" ... and {len(only_local) - 10} more")
|
|
1258
|
+
click.echo()
|
|
1259
|
+
|
|
1260
|
+
if only_cluster:
|
|
1261
|
+
click.echo(f"Only in cluster ({len(only_cluster)}):")
|
|
1262
|
+
for key in sorted(only_cluster)[:10]:
|
|
1263
|
+
click.secho(f" - {key}", fg="red")
|
|
1264
|
+
if len(only_cluster) > 10:
|
|
1265
|
+
click.echo(f" ... and {len(only_cluster) - 10} more")
|
|
1266
|
+
click.echo()
|
|
1267
|
+
|
|
1268
|
+
if different:
|
|
1269
|
+
click.echo(f"Different values ({len(different)}):")
|
|
1270
|
+
for key in sorted(different)[:10]:
|
|
1271
|
+
click.secho(f" ~ {key}", fg="yellow")
|
|
1272
|
+
# Show truncated values (hide secrets)
|
|
1273
|
+
if "SECRET" not in key.upper() and "KEY" not in key.upper() and "PASSWORD" not in key.upper():
|
|
1274
|
+
local_val = local_vars[key][:30] + "..." if len(local_vars[key]) > 30 else local_vars[key]
|
|
1275
|
+
cluster_val = cluster_vars[key][:30] + "..." if len(cluster_vars[key]) > 30 else cluster_vars[key]
|
|
1276
|
+
click.echo(f" local: {local_val}")
|
|
1277
|
+
click.echo(f" cluster: {cluster_val}")
|
|
1278
|
+
if len(different) > 10:
|
|
1279
|
+
click.echo(f" ... and {len(different) - 10} more")
|
|
1280
|
+
click.echo()
|
|
1281
|
+
|
|
1282
|
+
# Summary
|
|
1283
|
+
click.echo("=" * 60)
|
|
1284
|
+
if not only_local and not only_cluster and not different:
|
|
1285
|
+
click.secho("✓ Local .env matches cluster ConfigMap", fg="green")
|
|
1286
|
+
else:
|
|
1287
|
+
total_diff = len(only_local) + len(only_cluster) + len(different)
|
|
1288
|
+
click.secho(f"⚠ Found {total_diff} difference(s)", fg="yellow")
|
|
1289
|
+
click.echo()
|
|
1290
|
+
click.echo("To sync local → cluster:")
|
|
1291
|
+
click.echo(f" rem cluster env generate --name {configmap} --namespace {namespace} --apply")
|
|
1292
|
+
|
|
1293
|
+
|
|
1294
|
+
def register_commands(cluster_group):
|
|
1295
|
+
"""Register all cluster commands."""
|
|
1296
|
+
cluster_group.add_command(init)
|
|
1297
|
+
cluster_group.add_command(setup_ssm)
|
|
1298
|
+
cluster_group.add_command(validate)
|
|
1299
|
+
cluster_group.add_command(generate)
|
|
1300
|
+
cluster_group.add_command(env)
|