~james/core

core/shuttle/src/activation.rs -rw-r--r-- 10.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
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
use crate::command::{CaptureOutput, OutKind, Spec, run_capture, spawn_streaming};
use crate::nix_config::{SPINDLE_RUN_DIR, nix_executable};
use crate::protocol::{self, Message, v1};
use anyhow::{Context, Result};
use std::fs;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tokio::sync::mpsc::Sender;
use tracing::{info, warn};

const USER_CONFIG_DIR: &str = "/run/spindle/user-config";
const DEVSHELL_ENV_PATH: &str = "/run/spindle/devshell-env.sh";
const DEVSHELL_DRV: &str = "/etc/spindle/devshell-drv";
const MIN_BUILD_TIME: Duration = Duration::from_secs(1);

pub async fn run(id: String, req: v1::ActivateConfig, out: Sender<Message>) {
    let config_key = req.config_key.clone();
    let result = activate(&id, &req, &out).await;
    let msg = Message {
        id,
        activate_config_result: Some(v1::ActivateConfigResult {
            config_key,
            toplevel: (result.as_ref())
                .map(|p| p.to_string_lossy().into_owned())
                .unwrap_or_default(),
            error: protocol::error_or_empty(result.err().map(|e| format!("{e:#}"))),
        }),
        ..Default::default()
    };
    let _ = out.send(msg).await;
}

async fn activate(id: &str, req: &v1::ActivateConfig, out: &Sender<Message>) -> Result<PathBuf> {
    let need_build = req.toplevel.is_empty();
    let requested =
        (req.timeout_seconds > 0).then(|| Duration::from_secs(req.timeout_seconds.into()));
    let build_timeout = requested.unwrap_or(Duration::from_secs(10 * 60));
    let build_deadline = Instant::now() + build_timeout;
    let timeout = if need_build {
        build_timeout
    } else {
        requested.unwrap_or(Duration::from_secs(2 * 60))
    };

    let toplevel = if need_build {
        build_toplevel(id, req, timeout, out).await?
    } else {
        realise_or_build(
            realise_toplevel(id, &req.toplevel, timeout, out),
            build_deadline,
            |left| build_toplevel(id, req, left, out),
        )
        .await?
    };

    if !toplevel.starts_with("/nix/store/") {
        anyhow::bail!("config toplevel {toplevel:?} is not a nix store path");
    }

    switch_to_configuration(&toplevel, timeout).await?;
    write_devshell_env(timeout).await?;
    info!(
        config_key = %req.config_key,
        base_config_hash = %req.base_config_hash,
        ?toplevel,
        "activated NixOS config"
    );
    Ok(toplevel)
}

// the cache can evict part of the closure, so a failed realise falls back to building
async fn realise_or_build<B: Future<Output = Result<PathBuf>>>(
    realise: impl Future<Output = Result<PathBuf>>,
    build_deadline: Instant,
    build: impl FnOnce(Duration) -> B,
) -> Result<PathBuf> {
    match realise.await {
        Ok(toplevel) => Ok(toplevel),
        Err(error) => {
            let left = build_deadline.saturating_duration_since(Instant::now());
            if left < MIN_BUILD_TIME {
                return Err(
                    error.context("no time left to build the config after realising failed")
                );
            }
            warn!("realising cached config failed, building it: {error:#}");
            build(left).await
        }
    }
}

async fn write_devshell_env(timeout: Duration) -> Result<()> {
    let pointer = Path::new(DEVSHELL_DRV);
    if !pointer.exists() {
        let _ = fs::remove_file(DEVSHELL_ENV_PATH);
        return Ok(());
    }
    let drv = fs::read_to_string(pointer)
        .with_context(|| format!("read {pointer:?}"))?
        .trim()
        .to_owned();

    info!(
        ?drv,
        "running nix print-dev-env for dependencies devshell..."
    );
    let output = run_capture(
        Spec::new(nix_executable())
            .args(["print-dev-env", "--show-trace", &drv])
            .cwd(SPINDLE_RUN_DIR)
            .timeout(timeout),
    )
    .await?;

    if !output.success() {
        anyhow::bail!(
            "nix print-dev-env failed: exit={} error={:?} output={}",
            output.exit.exit_code,
            output.exit.error,
            output.combined_lossy(),
        );
    }

    fs::write(DEVSHELL_ENV_PATH, &output.stdout)
        .with_context(|| format!("write {DEVSHELL_ENV_PATH}"))?;
    info!(path = %DEVSHELL_ENV_PATH, "wrote devshell env");
    Ok(())
}

async fn build_toplevel(
    id: &str,
    req: &v1::ActivateConfig,
    timeout: Duration,
    out: &Sender<Message>,
) -> Result<PathBuf> {
    let user_config = (req.user_config.is_empty())
        .then_some("{}")
        .unwrap_or_else(|| &req.user_config);

    info!("writing user config to {USER_CONFIG_DIR}/config.json");
    write_user_config(user_config).context("write user config")?;

    info!("running nix build command for user config toplevel...");
    let output = run_streaming_stderr(
        Spec::new(nix_executable())
            .args([
                "build",
                "--no-link",
                "--show-trace",
                "--json",
                "--file",
                "/etc/spindle/nixos/default.nix",
            ])
            .cwd(SPINDLE_RUN_DIR)
            .timeout(timeout),
        id,
        out,
    )
    .await?;

    if !output.success() {
        anyhow::bail!(
            "nix config build failed: exit={} error={:?} output={}",
            output.exit.exit_code,
            output.exit.error,
            output.combined_lossy(),
        );
    }

    #[derive(Debug, serde::Deserialize)]
    struct NixBuildResult {
        outputs: NixBuildOutputs,
    }
    #[derive(Debug, serde::Deserialize)]
    struct NixBuildOutputs {
        out: PathBuf,
    }
    let [result] = serde_json::from_slice::<[NixBuildResult; 1]>(&output.stdout)
        .context("parse nix build --json output")?;
    Ok(result.outputs.out)
}

fn write_user_config(user_config: &str) -> Result<()> {
    fs::create_dir_all(USER_CONFIG_DIR).with_context(|| format!("create {USER_CONFIG_DIR}"))?;

    let config_path = format!("{USER_CONFIG_DIR}/config.json");
    fs::write(&config_path, user_config).with_context(|| format!("write {config_path}"))?;
    Ok(())
}

async fn realise_toplevel(
    id: &str,
    toplevel: &str,
    timeout: Duration,
    out: &Sender<Message>,
) -> Result<PathBuf> {
    if !toplevel.starts_with("/nix/store/") {
        anyhow::bail!("cached config toplevel {toplevel:?} is not a nix store path");
    }
    let output = run_streaming_stderr(
        Spec::new(nix_executable())
            .args(["build", "--no-link", "--show-trace", toplevel])
            .timeout(timeout),
        id,
        out,
    )
    .await?;
    if !output.success() {
        anyhow::bail!(
            "realise cached config failed: exit={} error={:?} output={}",
            output.exit.exit_code,
            output.exit.error,
            output.combined_lossy(),
        );
    }

    Ok(PathBuf::from(toplevel))
}

// streams stderr but captures stdout
async fn run_streaming_stderr(
    spec: Spec,
    id: &str,
    out: &Sender<Message>,
) -> Result<CaptureOutput> {
    let running = spawn_streaming(spec)?;
    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    let (mut events, exit_task) = running.into_parts();

    while let Some(event) = events.recv().await {
        match event.kind {
            OutKind::Stdout => stdout.extend_from_slice(&event.data),
            OutKind::Stderr => {
                stderr.extend_from_slice(&event.data);
                let _ = out
                    .send(Message {
                        id: id.to_owned(),
                        exec_stderr: Some(v1::ExecStderr {
                            data: event.data.into(),
                        }),
                        ..Default::default()
                    })
                    .await;
            }
        }
    }

    let exit = exit_task
        .await
        .unwrap_or_else(|error| Err(anyhow::anyhow!("command supervisor failed: {error}")))?;
    Ok(CaptureOutput {
        exit,
        stdout,
        stderr,
    })
}

async fn switch_to_configuration(toplevel: &Path, timeout: Duration) -> Result<()> {
    info!("switching to new configuration: {:?}", toplevel);
    let switch = toplevel.join("bin/switch-to-configuration");
    let output = run_capture(Spec::new(switch).args(["test"]).timeout(timeout)).await?;
    if !output.success() {
        anyhow::bail!(
            "switch-to-configuration test failed: exit={} error={:?} output={}",
            output.exit.exit_code,
            output.exit.error,
            output.combined_lossy(),
        );
    }
    Ok(())
}

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

    #[tokio::test]
    async fn keeps_the_cached_toplevel_when_it_realises() {
        let toplevel = realise_or_build(
            async { Ok(PathBuf::from("/nix/store/cached-nixos-system")) },
            Instant::now() + Duration::from_secs(60),
            |_| async { anyhow::bail!("must not build") },
        )
        .await
        .unwrap();
        assert_eq!(toplevel, PathBuf::from("/nix/store/cached-nixos-system"));
    }

    #[tokio::test]
    async fn builds_the_config_with_the_time_the_realise_left() {
        let total = Duration::from_secs(5);
        let mut given = Duration::ZERO;
        let toplevel = realise_or_build(
            async {
                tokio::time::sleep(Duration::from_millis(300)).await;
                anyhow::bail!("realise cached config failed")
            },
            Instant::now() + total,
            |left| {
                given = left;
                async { Ok(PathBuf::from("/nix/store/built-nixos-system")) }
            },
        )
        .await
        .unwrap();
        assert_eq!(toplevel, PathBuf::from("/nix/store/built-nixos-system"));
        assert!(given <= total - Duration::from_millis(300), "{given:?}");
        assert!(given > Duration::from_secs(3), "{given:?}");
    }

    #[tokio::test]
    async fn fails_without_building_when_the_realise_used_the_deadline() {
        let error = realise_or_build(
            async { anyhow::bail!("realise cached config failed") },
            Instant::now(),
            |_| async { anyhow::bail!("must not build") },
        )
        .await
        .unwrap_err();
        let error = format!("{error:#}");
        assert!(error.contains("no time left"), "{error}");
        assert!(error.contains("realise cached config failed"), "{error}");
    }
}