Skip to main content

flowey_lib_hvlite/_jobs/
local_check_cca_emu_prereq.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! To install CCA emulation environment, we need a few tools. This job checks
5//! their existence.
6use flowey::node::prelude::*;
7use std::fs;
8
9flowey_request! {
10    pub struct Params {
11        pub done: WriteVar<SideEffect>,
12    }
13}
14
15new_simple_flow_node!(struct Node);
16
17impl SimpleFlowNode for Node {
18    type Request = Params;
19
20    fn imports(ctx: &mut ImportCtx<'_>) {
21        ctx.import::<crate::run_cargo_build::Node>();
22        ctx.import::<flowey_lib_common::install_dist_pkg::Node>();
23    }
24
25    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
26        let Params { done } = request;
27
28        let required_packages_installed = ctx.reqv(|v| {
29            flowey_lib_common::install_dist_pkg::Request::Install {
30                package_names: vec![
31                    "netcat-openbsd".into(),
32                    "python3".into(),
33                    "python3-pip".into(),
34                    "telnet".into(),
35                    "docker.io".into(),
36                    "gcc-aarch64-linux-gnu".into(),
37                    // flex and bison are needed when building linux kernel kconfig parser
38                    "flex".into(),
39                    "bison".into(),
40                    "libssl-dev".into(),
41                    "python3-venv".into(),
42                ],
43                done: v,
44            }
45        });
46
47        ctx.emit_rust_step("check prerequisite of arm64 emulation environment", |ctx| {
48            done.claim(ctx);
49            required_packages_installed.claim(ctx);
50            move |rt| {
51                // Check if docker is setup
52                let group_name = "docker";
53                let group_file = fs::read_to_string("/etc/group").expect("Failed to read /etc/group");
54                let docker_group = group_file
55                    .lines()
56                    .find(|line| line.starts_with(&format!("{group_name}:")));
57
58                if docker_group.is_none() {
59                    anyhow::bail!("Group '{group_name}' does not exist, please add it using 'sudo groupadd docker'");
60                }
61
62                // Check if current user is in the group
63                let output = flowey::shell_cmd!(rt, "id -nG").output()?;
64                let output = String::from_utf8(output.stdout)?;
65                let is_member = output.split_whitespace().any(|g| g == group_name);
66                if !is_member {
67                    anyhow::bail!("Current user does NOT belong to the '{group_name}' group, please add it using 'sudo usermod -aG docker $USER', and restart the shell!");
68                }
69
70                Ok(())
71            }
72        });
73
74        Ok(())
75    }
76}