kcode-k1-daemon-code-services 0.1.5

Open the daemon's process-owned RustCode and WebCode services
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#![doc = include_str!("../Documentation.md")]

use kcode_k1_groups::K1Groups;
use kcode_k1_objects::K1Objects;
use kcode_k1_peering::K1Peering;
use kcode_k1_rust_code_ktool_service::RustCodeKtoolService;
use kcode_k1_rust_coding::{RustCodingConfig, RustCodingConfigValues};
use kcode_k1_rust_projection::K1RustProjection;
use kcode_k1_txn_ordering::K1TxnOrdering;
use kcode_k1_web_code_ktool_service::{K1WebCodeKtoolService, ServiceConfig, ServiceRevisions};
use kcode_k1_web_code_workspace::K1WebCodeWorkspace;
use kcode_k1_web_podman::{WebPodman, WebPodmanConfig};
use kcode_k1_web_projection::K1WebProjection;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::sync::Arc;
use std::time::Duration;

const CONTAINERFILE: &str = include_str!("assets/Containerfile");
const RUST_SCHEMA: &str = "k1-rust-code-cache-v1";
const RUST_TOOLCHAIN_POLICY: &str = "rust-1.97-k1-code-tools-v1";
const RUST_CHECK_POLICY: &str = "k1-rust-code-check-v1";
const RUST_COMMAND_POLICY: &str = "k1-rust-code-command-v1";
const WEB_BOOT: &str = "k1-web-code-boot-v1";
const WEB_SCHEMA: &str = "k1-web-code-cache-v1";
const WEB_ROUTE: &str = "k1-web-code-routes-v1";
const WEB_HARNESS: &str = "k1-web-code-harness-v1";
const WEB_CHECK_POLICY: &str = "k1-web-code-check-v1";
const CHECK_DEADLINE: Duration = Duration::from_secs(225);

pub struct CodeServices {
    rust: Arc<RustCodeKtoolService>,
    web: K1WebCodeKtoolService,
}

impl CodeServices {
    #[allow(clippy::too_many_arguments)]
    pub fn open(
        state_root: &Path,
        ordering: Arc<K1TxnOrdering>,
        peering: Arc<K1Peering>,
        groups: Arc<K1Groups>,
        objects: Arc<K1Objects>,
        web_projection: Arc<K1WebProjection>,
    ) -> Result<Self, String> {
        let paths = CodePaths::open(state_root)?;
        let podman = resolve_podman()?;
        let image = tool_image();
        ensure_tool_image(&podman, &image, &paths.tool_build)?;
        let checker = materialize_checker(&podman, &image, &paths.checker)?;
        let (chromium, chromium_version) = discover_chromium(&podman, &image)?;
        let rust_config = rust_config(podman.clone().into_os_string(), image.clone())?;
        let web_config = WebPodmanConfig {
            podman,
            image,
            checker,
            chromium,
            chromium_version,
            cpu_millis: 0,
            memory_bytes: 0,
            pids_limit: 0,
            tmpfs_bytes: 0,
            shm_bytes: 0,
            checker_timeout: CHECK_DEADLINE,
            wall_timeout: CHECK_DEADLINE,
        };
        WebPodman::new(web_config.clone())
            .map_err(|error| format!("Web Podman configuration: {error}"))?;

        let workspaces = Arc::new(
            K1WebCodeWorkspace::open(Arc::clone(&ordering), Arc::clone(&peering))
                .map_err(|error| format!("open Web code workspaces: {error}"))?,
        );
        let rust_projection =
            K1RustProjection::open(paths.rust_projection, paths.rust_control, ordering, peering)
                .map(Arc::new)
                .map_err(|error| format!("open Rust projection: {error}"))?;
        let rust = Arc::new(RustCodeKtoolService::new(
            paths.rust_cache,
            rust_config,
            rust_projection,
            Arc::clone(&objects),
            Arc::clone(&groups),
        ));
        let web = K1WebCodeKtoolService::new(
            ServiceConfig::new(
                paths.web_cache,
                paths.web_projection,
                web_revisions(),
                web_config,
            ),
            groups,
            objects,
            web_projection,
            workspaces,
        );
        Ok(Self { rust, web })
    }

    pub fn into_parts(self) -> (Arc<RustCodeKtoolService>, K1WebCodeKtoolService) {
        (self.rust, self.web)
    }
}

struct CodePaths {
    rust_projection: PathBuf,
    rust_control: PathBuf,
    rust_cache: PathBuf,
    web_projection: PathBuf,
    web_cache: PathBuf,
    tool_build: PathBuf,
    checker: PathBuf,
}

impl CodePaths {
    fn open(state_root: &Path) -> Result<Self, String> {
        let rust = state_root.join("rust");
        let web = state_root.join("web");
        let tools = state_root.join("code-tools");
        for path in [&rust, &web, &tools] {
            ensure_directory(path)?;
        }
        let value = Self {
            rust_projection: rust.join("projection"),
            rust_control: rust.join("control"),
            rust_cache: rust.join("code-cache"),
            web_projection: web.join("projection"),
            web_cache: web.join("code-cache"),
            tool_build: tools.join("image-build"),
            checker: tools.join("kcode-k1-web-checker"),
        };
        for path in [
            &value.rust_projection,
            &value.rust_control,
            &value.rust_cache,
            &value.web_projection,
            &value.web_cache,
        ] {
            ensure_directory(path)?;
        }
        Ok(value)
    }
}

fn resolve_podman() -> Result<PathBuf, String> {
    let path =
        std::env::var_os("PATH").ok_or_else(|| "find podman: PATH is unavailable".to_owned())?;
    resolve_executable("podman", std::env::split_paths(&path))
        .ok_or_else(|| "find executable podman on PATH".to_owned())
}

fn resolve_executable(name: &str, paths: impl IntoIterator<Item = PathBuf>) -> Option<PathBuf> {
    paths.into_iter().find_map(|directory| {
        let candidate = directory.join(name);
        executable(&candidate)
            .then(|| fs::canonicalize(candidate).ok())
            .flatten()
            .filter(|path| path.is_absolute())
    })
}

#[cfg(unix)]
fn executable(path: &Path) -> bool {
    use std::os::unix::fs::PermissionsExt as _;
    fs::metadata(path)
        .is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
}

#[cfg(not(unix))]
fn executable(path: &Path) -> bool {
    fs::metadata(path).is_ok_and(|metadata| metadata.is_file())
}

fn tool_image() -> String {
    let mut hash = 0xcbf29ce484222325_u64;
    for byte in CONTAINERFILE.bytes() {
        hash ^= u64::from(byte);
        hash = hash.wrapping_mul(0x100000001b3);
    }
    format!(
        "localhost/kcode-k1-code-tools:{}-{hash:016x}",
        env!("CARGO_PKG_VERSION")
    )
}

fn ensure_tool_image(podman: &Path, image: &str, build_root: &Path) -> Result<(), String> {
    let mut inspect = Command::new(podman);
    inspect.arg("image").arg("exists").arg(image);
    let inspected = run(inspect, "inspect K1 code tool image")?;
    if inspected.status.success() {
        return Ok(());
    }
    if inspected.status.code() != Some(1) {
        return Err(command_failure("inspect K1 code tool image", inspected));
    }

    if build_root.exists() {
        fs::remove_dir_all(build_root).map_err(|error| {
            format!(
                "replace code-tool image build directory {}: {error}",
                build_root.display()
            )
        })?;
    }
    fs::create_dir(build_root).map_err(|error| {
        format!(
            "create code-tool image build directory {}: {error}",
            build_root.display()
        )
    })?;
    let containerfile = build_root.join("Containerfile");
    fs::write(&containerfile, CONTAINERFILE).map_err(|error| {
        format!(
            "write embedded Containerfile {}: {error}",
            containerfile.display()
        )
    })?;
    let mut build = Command::new(podman);
    build
        .arg("build")
        .arg("--tag")
        .arg(image)
        .arg("--file")
        .arg(&containerfile)
        .arg(build_root);
    let built = run(build, "build K1 code tool image")?;
    let _ = fs::remove_dir_all(build_root);
    if built.status.success() {
        Ok(())
    } else {
        Err(command_failure("build K1 code tool image", built))
    }
}

fn materialize_checker(podman: &Path, image: &str, destination: &Path) -> Result<PathBuf, String> {
    let mut command = Command::new(podman);
    command
        .arg("run")
        .arg("--rm")
        .arg("--network=none")
        .arg("--pull=never")
        .arg("--entrypoint=/bin/cat")
        .arg(image)
        .arg("/usr/local/cargo/bin/kcode-k1-web-checker");
    let output = run(command, "extract K1 Web checker")?;
    if !output.status.success() {
        return Err(command_failure("extract K1 Web checker", output));
    }
    if output.stdout.is_empty() {
        return Err("extract K1 Web checker: image returned an empty executable".to_owned());
    }
    let staging = destination.with_extension("new");
    fs::write(&staging, output.stdout)
        .map_err(|error| format!("write Web checker {}: {error}", staging.display()))?;
    set_executable(&staging)?;
    fs::rename(&staging, destination)
        .map_err(|error| format!("install Web checker {}: {error}", destination.display()))?;
    fs::canonicalize(destination).map_err(|error| {
        format!(
            "canonicalize Web checker {}: {error}",
            destination.display()
        )
    })
}

#[cfg(unix)]
fn set_executable(path: &Path) -> Result<(), String> {
    use std::os::unix::fs::PermissionsExt as _;
    fs::set_permissions(path, fs::Permissions::from_mode(0o755))
        .map_err(|error| format!("make Web checker executable {}: {error}", path.display()))
}

#[cfg(not(unix))]
fn set_executable(_path: &Path) -> Result<(), String> {
    Ok(())
}

fn discover_chromium(podman: &Path, image: &str) -> Result<(PathBuf, String), String> {
    let mut command = Command::new(podman);
    command
        .arg("run")
        .arg("--rm")
        .arg("--network=none")
        .arg("--pull=never")
        .arg("--entrypoint=/bin/sh")
        .arg(image)
        .arg("-c")
        .arg("command -v chromium; chromium --version");
    let output = run(command, "discover Chromium in K1 code tool image")?;
    if !output.status.success() {
        return Err(command_failure(
            "discover Chromium in K1 code tool image",
            output,
        ));
    }
    let text = String::from_utf8(output.stdout)
        .map_err(|_| "discover Chromium: image output was not UTF-8".to_owned())?;
    let mut lines = text.lines().filter(|line| !line.trim().is_empty());
    let path = PathBuf::from(
        lines
            .next()
            .ok_or_else(|| "discover Chromium: executable path was absent".to_owned())?,
    );
    let version = lines
        .next()
        .ok_or_else(|| "discover Chromium: version was absent".to_owned())?
        .to_owned();
    if !path.is_absolute() {
        return Err("discover Chromium: executable path was not absolute".to_owned());
    }
    Ok((path, version))
}

fn rust_config(podman_program: OsString, image: String) -> Result<RustCodingConfig, String> {
    RustCodingConfig::new(RustCodingConfigValues {
        schema_id: RUST_SCHEMA.into(),
        toolchain_policy: RUST_TOOLCHAIN_POLICY.into(),
        image,
        rust_toolchain: "1.97".into(),
        check_policy: RUST_CHECK_POLICY.into(),
        target_triple: rust_target()?.into(),
        command_policy: RUST_COMMAND_POLICY.into(),
        podman_program,
    })
    .map_err(|error| format!("Rust coding configuration: {error}"))
}

fn rust_target() -> Result<&'static str, String> {
    match std::env::consts::ARCH {
        "x86_64" => Ok("x86_64-unknown-linux-gnu"),
        "aarch64" => Ok("aarch64-unknown-linux-gnu"),
        architecture => Err(format!(
            "unsupported Rust code host architecture: {architecture}"
        )),
    }
}

fn web_revisions() -> ServiceRevisions {
    ServiceRevisions {
        boot: WEB_BOOT.into(),
        schema: WEB_SCHEMA.into(),
        route: WEB_ROUTE.into(),
        harness: WEB_HARNESS.into(),
        check_policy: WEB_CHECK_POLICY.into(),
    }
}

fn run(mut command: Command, label: &str) -> Result<Output, String> {
    command
        .output()
        .map_err(|error| format!("{label}: could not start command: {error}"))
}

fn command_failure(label: &str, output: Output) -> String {
    format!(
        "{label} exited with {}\n--- stdout ---\n{}\n--- stderr ---\n{}",
        output
            .status
            .code()
            .map_or_else(|| "signal".to_owned(), |code| code.to_string()),
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    )
}

fn ensure_directory(path: &Path) -> Result<(), String> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if metadata.is_dir() && !metadata.file_type().is_symlink() => Ok(()),
        Ok(_) => Err(format!(
            "code-service path is not an ordinary directory: {}",
            path.display()
        )),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => fs::create_dir(path)
            .map_err(|error| format!("create code-service directory {}: {error}", path.display())),
        Err(error) => Err(format!(
            "inspect code-service directory {}: {error}",
            path.display()
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fixed_paths_are_beneath_state_root() {
        let root = tempfile::tempdir().unwrap();
        let paths = CodePaths::open(root.path()).unwrap();
        assert_eq!(paths.rust_projection, root.path().join("rust/projection"));
        assert_eq!(paths.web_cache, root.path().join("web/code-cache"));
        assert_eq!(
            paths.checker,
            root.path().join("code-tools/kcode-k1-web-checker")
        );
    }

    #[cfg(unix)]
    #[test]
    fn podman_resolver_accepts_only_an_executable_file() {
        use std::os::unix::fs::PermissionsExt as _;

        let root = tempfile::tempdir().unwrap();
        let podman = root.path().join("podman");
        fs::write(&podman, b"#!/bin/sh\nexit 0\n").unwrap();
        fs::set_permissions(&podman, fs::Permissions::from_mode(0o700)).unwrap();
        assert_eq!(
            resolve_executable("podman", [root.path().to_path_buf()]),
            Some(fs::canonicalize(&podman).unwrap())
        );
        fs::set_permissions(&podman, fs::Permissions::from_mode(0o600)).unwrap();
        assert!(resolve_executable("podman", [root.path().to_path_buf()]).is_none());
    }

    #[test]
    fn tool_image_owns_the_full_runtime_without_environment_configuration() {
        assert!(CONTAINERFILE.contains("rust:1.97-bookworm"));
        assert!(CONTAINERFILE.contains("chromium"));
        assert!(CONTAINERFILE.contains("kcode-k1-web-checker --version 0.1.1"));
        assert!(tool_image().starts_with("localhost/kcode-k1-code-tools:0.1.5-"));
        assert_eq!(CHECK_DEADLINE, Duration::from_secs(225));
    }
}