ambient-ci 0.14.0

A continuous integration engine
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Run QEMU.

use std::{
    ffi::{OsStr, OsString},
    path::{Path, PathBuf},
    process::Command,
};

use bytesize::MIB;
use clingwrap::runner::{CommandError, CommandRunner};
use tempfile::{tempdir_in, TempDir};

use crate::{
    cloud_init::LocalDataStore,
    config::Config,
    qemu_utils::create_cow_image,
    runlog::{RunLog, RunLogError, RunLogSource},
    util::{copy_file_rw, create_file},
    vdrive::{VirtualDrive, VirtualDriveError},
};

/// Path in VM to executor drive.
pub const EXECUTOR_DRIVE: &str = "/dev/vdb";
/// Path in VM to source drive.
pub const SOURCE_DRIVE: &str = "/dev/vdc";
/// Path in VM to artifact drive.
pub const ARTIFACT_DRIVE: &str = "/dev/vdd";
/// Path in VM to cache drive.
pub const CACHE_DRIVE: &str = "/dev/vde";
/// Path in VM to dependencies drive.
pub const DEPS_DRIVE: &str = "/dev/vdf";

/// Path in VM to workspace root directory.
pub const WORKSPACE_DIR: &str = "/ci";
/// Path in VM to source directory.
pub const SOURCE_DIR: &str = "/ci/src";
/// Path in VM to dependencies directory.
pub const DEPS_DIR: &str = "/ci/deps";
/// Path in VM to cache directory.
pub const CACHE_DIR: &str = "/ci/cache";
/// Path in VM to artifactsdirectory.
pub const ARTIFACTS_DIR: &str = "/ci/artifacts";

/// Run QEMU.
#[derive(Default)]
pub struct QemuRunner<'a> {
    config: Option<&'a Config>,
    base_image: Option<PathBuf>,
    cow_image: Option<PathBuf>,
    cloud_init: Option<LocalDataStore>,
    executor: Option<&'a VirtualDrive>,
    source: Option<&'a VirtualDrive>,
    dependencies: Option<&'a VirtualDrive>,
    cache: Option<&'a VirtualDrive>,
    artifacts: Option<&'a VirtualDrive>,
    console_log: Option<PathBuf>,
    raw_log: Option<PathBuf>,
    network: bool,
    uefi: bool,
}

impl<'a> QemuRunner<'a> {
    /// Set configuration.
    pub fn config(mut self, value: &'a Config) -> Self {
        self.config = Some(value);
        self
    }

    /// Set base image.
    pub fn base_image(mut self, filename: &Path) -> Self {
        self.base_image = Some(filename.into());
        self
    }

    /// Set copy-on-write image.
    pub fn cow_image(mut self, filename: &Path) -> Self {
        self.cow_image = Some(filename.into());
        self
    }

    /// Set `cloud-init` data store.
    pub fn cloud_init(mut self, ds: &LocalDataStore) -> Self {
        self.cloud_init = Some(ds.clone());
        self
    }

    /// Set executor drive.
    pub fn executor(mut self, value: &'a VirtualDrive) -> Self {
        self.executor = Some(value);
        self
    }

    /// Set source drive.
    pub fn source(mut self, value: &'a VirtualDrive) -> Self {
        self.source = Some(value);
        self
    }

    /// Set dependencies drive.
    pub fn dependencies(mut self, value: &'a VirtualDrive) -> Self {
        self.dependencies = Some(value);
        self
    }

    /// Set cache drive.
    pub fn cache(mut self, value: &'a VirtualDrive) -> Self {
        self.cache = Some(value);
        self
    }

    /// Set artifacts drive.
    pub fn artifacts(mut self, value: &'a VirtualDrive) -> Self {
        self.artifacts = Some(value);
        self
    }

    /// Set console log file.
    pub fn console_log(mut self, value: &'a Path) -> Self {
        self.console_log = Some(value.into());
        self
    }

    /// Set run log file.
    pub fn raw_log(mut self, value: &'a Path) -> Self {
        self.raw_log = Some(value.into());
        self
    }

    /// Allow network?
    pub fn network(mut self, network: bool) -> Self {
        self.network = network;
        self
    }

    /// Use UEFI?
    pub fn uefi(mut self, uefi: bool) -> Self {
        self.uefi = uefi;
        self
    }

    /// Run QEMU.
    pub fn run(&self, source: RunLogSource, runlog: &mut RunLog) -> Result<i32, QemuError> {
        let config = self.config.ok_or(QemuError::Missing("config"))?;
        let image = self.base_image.clone().ok_or(QemuError::Missing("image"))?;
        let cloud_init = self
            .cloud_init
            .clone()
            .ok_or(QemuError::Missing("cloud_init"))?;
        let executor_drive = self.executor.ok_or(QemuError::Missing("executor_drive"))?;

        let raw_log = self.raw_log.clone().ok_or(QemuError::Missing("raw_log"))?;
        let console_log = self
            .console_log
            .clone()
            .ok_or(QemuError::Missing("console_log"))?;

        let qemu = Qemu {
            kvm_binary: config.kvm_binary(),
            ovmf_code: config.ovmf_code_file(),
            ovmf_vars: config.ovmf_vars_file(),
            image,
            cow_image: self.cow_image.clone(),
            tmpdir: tempdir_in(config.tmpdir()).map_err(QemuError::TempDir)?,
            console_log,
            raw_log: raw_log.clone(),
            cloud_init,
            executor: executor_drive.clone(),
            source: self.source,
            artifact: self.artifacts,
            dependencies: self.dependencies,
            cache: self.cache,
            cpus: config.cpus(),
            memory: config.memory().as_u64(),
            network: self.network,
        };
        let result = qemu.run(source, runlog);
        if result.is_err() {
            if let Ok(data) = std::fs::read(&raw_log) {
                eprintln!(
                    "========================= raw log:\n{}=========================\n",
                    String::from_utf8_lossy(&data)
                );
            }
        }
        result
    }
}

/// A QEMU runner.
#[derive(Debug)]
struct Qemu<'a> {
    kvm_binary: PathBuf,
    ovmf_code: PathBuf,
    ovmf_vars: PathBuf,
    image: PathBuf,
    cow_image: Option<PathBuf>,
    cloud_init: LocalDataStore,
    tmpdir: TempDir,
    console_log: PathBuf,
    raw_log: PathBuf,
    executor: VirtualDrive,
    source: Option<&'a VirtualDrive>,
    artifact: Option<&'a VirtualDrive>,
    dependencies: Option<&'a VirtualDrive>,
    cache: Option<&'a VirtualDrive>,
    cpus: usize,
    memory: u64,
    network: bool,
}

impl Qemu<'_> {
    /// Run QEMU in the specified way.
    #[allow(clippy::unwrap_used)]
    fn run(&self, source: RunLogSource, runlog: &mut RunLog) -> Result<i32, QemuError> {
        let tmp = tempdir_in(&self.tmpdir).map_err(QemuError::TempDir)?;

        let cow_image = self
            .cow_image
            .clone()
            .unwrap_or_else(|| tmp.path().join("vm.qcow2"));
        let iso = tmp.path().join("cloud_init.iso");
        let vars = tmp.path().join("vars.fd");

        create_cow_image(&self.image, &cow_image).map_err(QemuError::COW)?;

        copy_file_rw(&self.ovmf_vars, &vars)
            .map_err(|e| QemuError::Copy(self.ovmf_vars.to_path_buf(), Box::new(e)))?;
        assert!(!std::fs::metadata(&vars).unwrap().permissions().readonly());

        self.cloud_init
            .iso(&iso)
            .map_err(|err| QemuError::Iso(iso.clone(), err))?;

        create_file(&self.console_log).map_err(QemuError::CreateFile)?;

        let raw_log_filename = create_file(&self.raw_log).map_err(QemuError::CreateFile)?;

        let cpus = format!("cpus={}", self.cpus);
        let memory = format!("{}", self.memory / MIB);
        let mut args = QemuArgs::default()
            .with_valued_arg("-m", &memory)
            .with_valued_arg("-smp", &cpus)
            .with_valued_arg("-cpu", "kvm64")
            .with_valued_arg("-machine", "type=q35,accel=kvm,usb=off")
            .with_valued_arg("-uuid", "a85c9de7-edc0-4e54-bead-112e5733582c")
            .with_valued_arg("-boot", "strict=on")
            .with_valued_arg("-name", "ambient-ci-vm")
            .with_valued_arg("-rtc", "base=utc,driftfix=slew")
            .with_valued_arg("-display", "none")
            .with_valued_arg("-device", "virtio-rng-pci")
            .with_valued_arg("-serial", &format!("file:{}", self.console_log.display())) // ttyS0
            .with_valued_arg("-serial", &format!("file:{}", raw_log_filename.display())) // ttyS1
            .with_qcow2(&cow_image.to_string_lossy())
            .with_raw(self.executor.filename(), true)
            .with_valued_arg("-cdrom", &iso.display().to_string());

        args = args.with_ipflash(0, &self.ovmf_code.to_string_lossy(), true);
        args = args.with_ipflash(1, &vars.to_string_lossy(), false);

        if let Some(drive) = self.source {
            args = args.with_raw(drive.filename(), true);
        }
        if let Some(drive) = self.artifact {
            args = args.with_raw(drive.filename(), false);
        }
        if let Some(drive) = self.cache {
            args = args.with_raw(drive.filename(), false);
        }
        if let Some(drive) = self.dependencies {
            args = args.with_raw(drive.filename(), true);
        }
        if self.network {
            args = args.with_valued_arg("-nic", "user,model=virtio");
        }
        args = args.with_arg("-nodefaults").with_arg("-no-user-config");

        let mut cmd = Command::new(&self.kvm_binary);
        cmd.args(args.iter());

        runlog.start_qemu(source, &cmd);
        let runner = CommandRunner::new(cmd);
        let result = runner.execute();
        let (vm_runlog, exit) = Self::parse_raw_log(&raw_log_filename)?;
        for msg in vm_runlog.msgs() {
            runlog.write(msg);
        }
        match &result {
            Ok(output) => {
                runlog.qemu_succeeded(source, output);
                Ok(exit)
            }
            Err(CommandError::KilledBySignal { .. }) => {
                let err = result.unwrap_err();
                runlog.qemu_failed(source, &err);
                Err(QemuError::Qemu(err))
            }
            Err(CommandError::CommandFailed { exit_code, .. }) => {
                let exit_code = *exit_code;
                let err = result.unwrap_err();
                runlog.qemu_failed(source, &err);
                Ok(exit_code)
            }
            Err(_) => {
                let err = result.unwrap_err();
                runlog.qemu_failed(source, &err);
                Err(QemuError::Qemu(err))
            }
        }
    }

    fn parse_raw_log(filename: &Path) -> Result<(RunLog, i32), QemuError> {
        const BEGIN: &str = "====================== BEGIN ======================";
        const EXIT: &str = "\nEXIT CODE: ";

        let raw = std::fs::read(filename).map_err(|e| QemuError::ReadLog(filename.into(), e))?;
        let runlog = RunLog::from_raw(raw.clone()).map_err(QemuError::ParseRaw)?;

        let log = String::from_utf8_lossy(&raw);
        if let Some((_, log)) = log.split_once(BEGIN) {
            if let Some((_, rest)) = log.split_once(EXIT) {
                if let Some((exit, _)) = rest.split_once('\n') {
                    let exit = exit.trim();
                    let exit = exit
                        .parse::<i32>()
                        .or(Err(QemuError::ParseExit(exit.to_string())))?;
                    Ok((runlog, exit))
                } else {
                    Err(QemuError::BadExitCode)
                }
            } else {
                Err(QemuError::NoBeginMarker)
            }
        } else {
            Err(QemuError::NoExit)
        }
    }
}

#[derive(Debug, Default)]
struct QemuArgs {
    args: Vec<OsString>,
}

impl QemuArgs {
    fn with_arg(mut self, arg: &str) -> Self {
        self.args.push(arg.into());
        self
    }

    fn with_valued_arg(mut self, arg: &str, value: &str) -> Self {
        self.args.push(arg.into());
        self.args.push(value.into());
        self
    }

    fn with_ipflash(mut self, unit: usize, path: &str, readonly: bool) -> Self {
        self.args.push("-drive".into());
        self.args.push(
            format!(
                "if=pflash,format=raw,unit={},file={}{}",
                unit,
                path,
                if readonly { ",readonly=on" } else { "" },
            )
            .into(),
        );
        self
    }

    fn with_qcow2(mut self, path: &str) -> Self {
        self.args.push("-drive".into());
        self.args
            .push(format!("format=qcow2,if=virtio,file={path}").into());
        self
    }

    fn with_raw(mut self, path: &Path, readonly: bool) -> Self {
        self.args.push("-drive".into());
        self.args.push(
            format!(
                "format=raw,if=virtio,file={}{}",
                path.display(),
                if readonly { ",readonly=on" } else { "" },
            )
            .into(),
        );
        self
    }

    fn iter(&self) -> impl Iterator<Item = &OsStr> {
        self.args.iter().map(|s| s.as_os_str())
    }
}

/// Possible errors from running Qemu.
#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
pub enum QemuError {
    #[error("missing field in QemuRunner: {0}")]
    Missing(&'static str),

    #[error("failed to create a temporary directory")]
    TempDir(#[source] std::io::Error),

    #[error("failed to copy to temporary directory: {0}")]
    Copy(PathBuf, #[source] Box<crate::util::UtilError>),

    #[error("failed to read log file {0}")]
    ReadLog(PathBuf, #[source] std::io::Error),

    #[error("failed to parse raw log for JSON Lines")]
    ParseRaw(#[source] RunLogError),

    #[error("failed to create a tar archive from {0}")]
    Tar(PathBuf, #[source] Box<VirtualDriveError>),

    #[error("failed to extract cache drive to {0}")]
    ExtractCache(PathBuf, #[source] Box<VirtualDriveError>),

    #[error("failed to read temporary file for logging")]
    TemporaryLog(#[source] std::io::Error),

    #[error("run log lacks exit code of run")]
    NoExit,

    #[error("failed to get length of file {0}")]
    Metadata(PathBuf, #[source] std::io::Error),

    #[error("failed to set length of file to {0}: {1}")]
    SetLen(u64, PathBuf, #[source] std::io::Error),

    #[error("failed to run actions in QEMU")]
    QemuFailed(i32),

    #[error("failed to create cloud-init ISO file {0}")]
    Iso(PathBuf, #[source] crate::cloud_init::CloudInitError),

    #[error(transparent)]
    COW(#[from] crate::qemu_utils::QemuUtilError),

    #[error("failed to run QEMU")]
    Qemu(#[source] CommandError),

    #[error(transparent)]
    CreateFile(#[from] crate::util::UtilError),

    #[error("failed to parse exist code {0:?}")]
    ParseExit(String),

    #[error("run log from QEMU does not have a BEGIN marker")]
    NoBeginMarker,

    #[error("run log from QEMU has malformed exit code marker")]
    BadExitCode,
}