Import CI enrollment into private NAS runtime during bootstrap

This commit is contained in:
Codex
2026-09-29 14:17:25 +03:00
parent f775929093
commit aa84e73bd8
6 changed files with 355 additions and 28 deletions
+130 -15
View File
@@ -49,6 +49,9 @@ GITEA_CI_JOB_IMAGE = "docker.io/library/node@sha256:9887ee8a5e5aa4fb02e09f03cdf8
GITEA_CI_ENTRIES = (
"docker-compose.gitea-ci.yml", "runner.yaml", "deployment/bootstrap-v1.json",
)
GITEA_CI_V2_ENTRIES = (
"docker-compose.gitea-ci.yml", "runner.yaml", "deployment/bootstrap-v2.json",
)
GITEA_CI_SOURCE_SHA256 = {
"docker-compose.gitea-ci.yml": "851d22ae9823c30508a552ec6abb7f547ceda671f3ca77552fb7032bc807ae09",
"runner.yaml": "8634e5427986423ccd8fe18e3bf5072c53addbf17f1efc1395b3a473671d78fe",
@@ -4922,7 +4925,7 @@ def allowed_payload_path(component, rel):
die(f"path rejected: {rel}: {reason}")
if component == "gitea-ci":
if rel in GITEA_CI_ENTRIES or rel == "deployment":
if rel in GITEA_CI_ENTRIES + GITEA_CI_V2_ENTRIES or rel == "deployment":
return True
die("Gitea CI accepts only its exact bootstrap contract")
@@ -31389,16 +31392,18 @@ def rollback_map_access(root, backup_dir, entries, current_stamp):
def validate_gitea_ci_payload(payload_dir, entries, release_id):
if tuple(entries) != GITEA_CI_ENTRIES:
v2 = tuple(entries) == GITEA_CI_V2_ENTRIES
if tuple(entries) not in (GITEA_CI_ENTRIES, GITEA_CI_V2_ENTRIES):
die("Gitea CI bootstrap entry set mismatch")
if not release_id.startswith("gitea-ci-bootstrap-v1-"):
version = "v2" if v2 else "v1"
if not release_id.startswith("gitea-ci-bootstrap-" + version + "-"):
die("Gitea CI bootstrap release identity mismatch")
for rel, expected in GITEA_CI_SOURCE_SHA256.items():
if sha256_file(payload_dir / rel) != expected:
die(f"Gitea CI immutable configuration mismatch: {rel}")
descriptor = read_strict_json(payload_dir / GITEA_CI_ENTRIES[2], "Gitea CI bootstrap")
if descriptor != {
"schemaVersion": "nodedc.gitea-ci.bootstrap.v1",
descriptor = read_strict_json(payload_dir / entries[2], "Gitea CI bootstrap")
expected = {
"schemaVersion": "nodedc.gitea-ci.bootstrap." + version,
"releaseId": release_id,
"action": "bootstrap-build-runner",
"repository": "SILVER/NODEDC_DEVICE_CORE",
@@ -31407,7 +31412,10 @@ def validate_gitea_ci_payload(payload_dir, entries, release_id):
"productionDeployEnabled": False,
"publishedPorts": [],
"capacity": 1,
}:
}
if v2:
expected["enrollment"] = "import-dctouch-staging-to-root-private-runtime"
if descriptor != expected:
die("Gitea CI bootstrap descriptor mismatch")
return descriptor
@@ -31440,6 +31448,106 @@ def read_gitea_ci_enrollment():
return value["token"]
def read_gitea_ci_v2_input(runtime=False):
"""Read a bounded operator handoff or the private NAS copy, never SMB modes."""
path = (GITEA_CI_ROOT / "secrets" / "registration-token"
if runtime else GITEA_CI_ENROLLMENT)
for parent in path.parents:
if parent.is_symlink():
die("Gitea CI enrollment path contains a symlink")
try:
operator_uid = pwd.getpwnam("dctouch").pw_uid
except KeyError:
die("Gitea CI staging account dctouch is missing")
try:
fd = os.open(str(path), os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK)
except OSError:
die("Gitea CI enrollment input cannot be opened safely")
with os.fdopen(fd, "rb") as stream:
info = os.fstat(stream.fileno())
if (not stat.S_ISREG(info.st_mode) or info.st_nlink != 1
or not 1 <= info.st_size <= 2048
or info.st_uid not in ((0,) if runtime else (0, operator_uid))
or (runtime and stat.S_IMODE(info.st_mode) != 0o600)):
die("Gitea CI enrollment metadata rejected: "
f"runtime={runtime};mode={stat.S_IMODE(info.st_mode):04o};"
f"uid={info.st_uid};nlink={info.st_nlink};size={info.st_size}")
raw = stream.read(2049)
after = os.fstat(stream.fileno())
if (info.st_size, info.st_mtime_ns, info.st_ctime_ns) != (
after.st_size, after.st_mtime_ns, after.st_ctime_ns):
die("Gitea CI enrollment changed while reading")
try:
if runtime:
token = raw.decode("ascii").strip()
else:
value = json.loads(raw)
if (not isinstance(value, dict) or set(value) != {"repository", "token"}
or value["repository"] != "SILVER/NODEDC_DEVICE_CORE"):
raise ValueError()
token = value["token"]
if not isinstance(token, str) or not re.fullmatch(r"[A-Za-z0-9_-]{20,256}", token):
raise ValueError()
except (ValueError, UnicodeError):
die("Gitea CI enrollment format is invalid")
return token, info
def preflight_gitea_ci_v2_enrollment():
private = GITEA_CI_ROOT / "secrets" / "registration-token"
if GITEA_CI_ENROLLMENT.exists() or GITEA_CI_ENROLLMENT.is_symlink():
token, info = read_gitea_ci_v2_input()
if private.exists() or private.is_symlink():
if read_gitea_ci_v2_input(runtime=True)[0] != token:
die("Gitea CI existing runtime enrollment differs")
return f"import-staging;mode={stat.S_IMODE(info.st_mode):04o};uid={info.st_uid};nlink={info.st_nlink};size={info.st_size}"
read_gitea_ci_v2_input(runtime=True)
return "reuse-root-private-runtime"
def import_gitea_ci_v2_enrollment():
"""The v2 apply transition owns private creation; plan never changes rights."""
preflight_gitea_ci_v2_enrollment()
pending = GITEA_CI_ENROLLMENT.exists()
token, source_info = read_gitea_ci_v2_input(runtime=not pending)
directory = GITEA_CI_ROOT / "secrets"
directory.mkdir(mode=0o700, parents=True, exist_ok=True)
if directory.is_symlink():
die("Gitea CI private directory cannot be a symlink")
os.chown(str(directory), 0, 0)
directory.chmod(0o700)
target = directory / "registration-token"
if not target.exists():
fd, temporary = tempfile.mkstemp(prefix=".enrollment-", dir=str(directory))
try:
with os.fdopen(fd, "w") as stream:
os.fchown(stream.fileno(), 0, 0)
os.fchmod(stream.fileno(), 0o600)
stream.write(token + "\n")
stream.flush()
os.fsync(stream.fileno())
# Atomic publication without replacing an existing identity.
os.link(temporary, str(target))
finally:
os.unlink(temporary)
if read_gitea_ci_v2_input(runtime=True)[0] != token:
die("Gitea CI private enrollment import verification failed")
for path in (directory, GITEA_CI_ROOT):
fd = os.open(str(path), os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
try:
os.fsync(fd)
finally:
os.close(fd)
if pending:
current = GITEA_CI_ENROLLMENT.lstat()
if (current.st_dev, current.st_ino, current.st_mtime_ns, current.st_size) != (
source_info.st_dev, source_info.st_ino, source_info.st_mtime_ns, source_info.st_size):
die("Gitea CI enrollment staging changed before cleanup")
GITEA_CI_ENROLLMENT.unlink()
print("ci_enrollment=root-owned-private-runtime;staging=removed")
return token
def gitea_ci_container_ids(own=False):
command = [str(DOCKER), "ps", "-aq", "--no-trunc"]
if own:
@@ -31465,15 +31573,18 @@ def gitea_ci_inventory():
return result
def preflight_gitea_ci():
def preflight_gitea_ci(entries=GITEA_CI_ENTRIES):
for parent in (GITEA_CI_ROOT, *GITEA_CI_ROOT.parents):
if parent.is_symlink():
die("Gitea CI root contains a symlink")
if any((GITEA_CI_ROOT / rel).exists() for rel in GITEA_CI_ENTRIES):
if any((GITEA_CI_ROOT / rel).exists() for rel in GITEA_CI_ENTRIES + GITEA_CI_V2_ENTRIES):
die("Gitea CI bootstrap requires no installed release; use a versioned upgrade")
if gitea_ci_container_ids(own=True):
die("Gitea CI bootstrap requires no existing CI containers")
read_gitea_ci_enrollment()
if tuple(entries) == GITEA_CI_V2_ENTRIES:
preflight_gitea_ci_v2_enrollment()
else:
read_gitea_ci_enrollment()
if shutil.disk_usage(GITEA_CI_ROOT.parent).free < 8 * 1024 ** 3:
die("Gitea CI requires 8 GiB free disk before image preparation")
memory = {}
@@ -31486,8 +31597,9 @@ def preflight_gitea_ci():
return gitea_ci_inventory()
def prepare_gitea_ci():
token = read_gitea_ci_enrollment()
def prepare_gitea_ci(entries=GITEA_CI_ENTRIES):
token = (import_gitea_ci_v2_enrollment() if tuple(entries) == GITEA_CI_V2_ENTRIES
else read_gitea_ci_enrollment())
for directory in (GITEA_CI_ROOT / "data", GITEA_CI_ROOT / "secrets"):
if directory.is_symlink():
die("Gitea CI runtime directory cannot be a symlink")
@@ -31655,7 +31767,7 @@ def plan_artifact(artifact):
if manifest["component"] == "mission-core-map-access":
preflight_map_access(payload_dir, entries)
if manifest["component"] == "gitea-ci":
preflight_gitea_ci()
preflight_gitea_ci(entries)
reject_failed_artifact_replay(manifest, sha)
reject_terminal_engine_l2_failed_artifact(manifest, sha)
reject_terminal_device_plane_foundation_artifact(manifest, sha)
@@ -32246,6 +32358,9 @@ def plan_artifact(artifact):
print(f"ci_runner_image={GITEA_CI_IMAGE}")
print(f"ci_job_image={GITEA_CI_JOB_IMAGE}")
print("ci_pull=exact-digests-only;registration=separate-runtime-enrollment")
if tuple(entries) == GITEA_CI_V2_ENTRIES:
print("ci_enrollment=" + preflight_gitea_ci_v2_enrollment())
print("ci_enrollment_apply=copy-to-root0600;private-directory0700;remove-staging-after-verification")
print("ci_preserved=all-existing-containers,databases,volumes;rollback=bootstrap-source-and-runner-only")
if component == "mission-core-map-access":
print("access=local-forward:dctouch:127.0.0.1:18103")
@@ -36360,7 +36475,7 @@ def prepare_component_runtime(component, entries=None):
def run_component_runtime(component, entries, services):
if component == "gitea-ci":
prepare_gitea_ci()
prepare_gitea_ci(entries)
run_compose(component, services, entries)
return
if component == "platform" and is_platform_provider_catalog_only(entries):
@@ -38933,7 +39048,7 @@ def apply_artifact(artifact):
if component == "mission-core-map-access":
preflight_map_access(payload_dir, entries)
if component == "gitea-ci":
gitea_ci_before = preflight_gitea_ci()
gitea_ci_before = preflight_gitea_ci(entries)
artifact_only = component_artifact_only(component)
defer_bootstrap_root = is_gitea_bootstrap_slice(
component,