chopratejas--headroom
0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
177 行
6.1 KiB
Python
177 行
6.1 KiB
Python
"""Tool-target configuration for persistent deployments."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from headroom import fsutil
|
|
from headroom._subprocess import run
|
|
from headroom.providers.install_registry import (
|
|
apply_provider_scope_mutations,
|
|
revert_provider_scope_mutation,
|
|
)
|
|
|
|
from .models import ConfigScope, DeploymentManifest, ManagedMutation
|
|
from .paths import (
|
|
unix_system_env_targets,
|
|
unix_user_env_targets,
|
|
)
|
|
|
|
_ENV_MARKER_START = "# >>> headroom persistent env >>>"
|
|
_ENV_MARKER_END = "# <<< headroom persistent env <<<"
|
|
_ENV_PATTERN = re.compile(
|
|
re.escape(_ENV_MARKER_START) + r".*?" + re.escape(_ENV_MARKER_END),
|
|
re.DOTALL,
|
|
)
|
|
|
|
|
|
def _merge_marker_block(file_path: Path, block: str, pattern: re.Pattern[str], marker: str) -> str:
|
|
if file_path.exists():
|
|
existing = fsutil.read_text(file_path)
|
|
if marker in existing:
|
|
return pattern.sub(block, existing)
|
|
return existing.rstrip() + "\n\n" + block + "\n"
|
|
return block + "\n"
|
|
|
|
|
|
def _env_block(values: dict[str, str]) -> str:
|
|
lines = [_ENV_MARKER_START]
|
|
for name, value in values.items():
|
|
lines.append(f'export {name}="{value}"')
|
|
lines.append(_ENV_MARKER_END)
|
|
return "\n".join(lines)
|
|
|
|
|
|
def _powershell_literal(value: str) -> str:
|
|
return "'" + value.replace("'", "''") + "'"
|
|
|
|
|
|
def _unix_scope_values(manifest: DeploymentManifest) -> dict[str, str]:
|
|
merged = dict(manifest.base_env)
|
|
for env_map in manifest.tool_envs.values():
|
|
merged.update(env_map)
|
|
return merged
|
|
|
|
|
|
def _apply_unix_env_scope(manifest: DeploymentManifest) -> list[ManagedMutation]:
|
|
values = _unix_scope_values(manifest)
|
|
block = _env_block(values)
|
|
if manifest.scope == ConfigScope.USER.value:
|
|
targets = unix_user_env_targets()
|
|
else:
|
|
targets = unix_system_env_targets()
|
|
mutations: list[ManagedMutation] = []
|
|
for path in targets:
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
merged = _merge_marker_block(path, block, _ENV_PATTERN, _ENV_MARKER_START)
|
|
fsutil.write_text(path, merged)
|
|
mutations.append(ManagedMutation(target="env", kind="shell-block", path=str(path)))
|
|
return mutations
|
|
|
|
|
|
def _remove_unix_env_scope(mutations: list[ManagedMutation]) -> None:
|
|
for mutation in mutations:
|
|
if mutation.kind != "shell-block" or not mutation.path:
|
|
continue
|
|
path = Path(mutation.path)
|
|
if not path.exists():
|
|
continue
|
|
content = fsutil.read_text(path)
|
|
if _ENV_MARKER_START not in content:
|
|
continue
|
|
fsutil.write_text(path, _ENV_PATTERN.sub("", content).strip() + "\n")
|
|
|
|
|
|
def _apply_windows_env_scope(manifest: DeploymentManifest) -> list[ManagedMutation]:
|
|
scope_name = "Machine" if manifest.scope == ConfigScope.SYSTEM.value else "User"
|
|
merged = _unix_scope_values(manifest)
|
|
mutations: list[ManagedMutation] = []
|
|
for name, value in merged.items():
|
|
previous = run(
|
|
[
|
|
"powershell",
|
|
"-NoProfile",
|
|
"-Command",
|
|
f"$value = [Environment]::GetEnvironmentVariable({_powershell_literal(name)},{_powershell_literal(scope_name)}); "
|
|
"if ($null -eq $value) { '__HEADROOM_UNSET__' } else { $value }",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
).stdout.strip()
|
|
command = [
|
|
"powershell",
|
|
"-NoProfile",
|
|
"-Command",
|
|
f"[Environment]::SetEnvironmentVariable({_powershell_literal(name)},{_powershell_literal(value)},{_powershell_literal(scope_name)})",
|
|
]
|
|
subprocess.run(command, check=True)
|
|
mutations.append(
|
|
ManagedMutation(
|
|
target="env",
|
|
kind="windows-env",
|
|
data={
|
|
"name": name,
|
|
"scope": scope_name,
|
|
"previous": None if previous == "__HEADROOM_UNSET__" else previous,
|
|
},
|
|
)
|
|
)
|
|
return mutations
|
|
|
|
|
|
def _remove_windows_env_scope(mutations: list[ManagedMutation]) -> None:
|
|
for mutation in mutations:
|
|
if mutation.kind != "windows-env":
|
|
continue
|
|
name = mutation.data.get("name")
|
|
if not isinstance(name, str):
|
|
raise ValueError("Windows environment mutation is missing a variable name")
|
|
scope_name = mutation.data.get("scope", "User")
|
|
if not isinstance(scope_name, str):
|
|
raise ValueError("Windows environment mutation is missing a valid scope")
|
|
previous = mutation.data.get("previous")
|
|
if previous is None:
|
|
value_literal = "$null"
|
|
else:
|
|
value_literal = _powershell_literal(previous)
|
|
command = [
|
|
"powershell",
|
|
"-NoProfile",
|
|
"-Command",
|
|
f"[Environment]::SetEnvironmentVariable({_powershell_literal(name)},{value_literal},{_powershell_literal(scope_name)})",
|
|
]
|
|
subprocess.run(command, check=True)
|
|
|
|
|
|
def apply_mutations(manifest: DeploymentManifest) -> list[ManagedMutation]:
|
|
"""Apply provider/user/system configuration for a deployment."""
|
|
|
|
mutations: list[ManagedMutation] = []
|
|
if manifest.scope in {ConfigScope.USER.value, ConfigScope.SYSTEM.value}:
|
|
if os.name == "nt":
|
|
mutations.extend(_apply_windows_env_scope(manifest))
|
|
else:
|
|
mutations.extend(_apply_unix_env_scope(manifest))
|
|
mutations.extend(apply_provider_scope_mutations(manifest))
|
|
return mutations
|
|
|
|
return [*mutations, *apply_provider_scope_mutations(manifest)]
|
|
|
|
|
|
def revert_mutations(manifest: DeploymentManifest) -> None:
|
|
"""Undo the stored mutations for a deployment."""
|
|
|
|
if manifest.scope in {ConfigScope.USER.value, ConfigScope.SYSTEM.value}:
|
|
shell_mutations = [m for m in manifest.mutations if m.target == "env"]
|
|
if os.name == "nt":
|
|
_remove_windows_env_scope(shell_mutations)
|
|
else:
|
|
_remove_unix_env_scope(shell_mutations)
|
|
|
|
for mutation in manifest.mutations:
|
|
revert_provider_scope_mutation(manifest, mutation)
|