flowey_lib_hvlite/
artifact_openhcl_igvm_from_recipe_extras.rs

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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Artifact: An artifact containing various "extras" that are generated as part
//! of the OpenHCL IGVM build. e.g: debug symbols, constituent binaries, etc.

use crate::build_openhcl_igvm_from_recipe::OpenhclIgvmRecipe;
use flowey::node::prelude::*;

#[derive(Serialize, Deserialize)]
pub struct OpenhclIgvmExtras {
    pub recipe: OpenhclIgvmRecipe,
    pub openvmm_hcl_bin: crate::build_openvmm_hcl::OpenvmmHclOutput,
    pub openhcl_map: Option<PathBuf>,
    pub openhcl_boot: crate::build_openhcl_boot::OpenhclBootOutput,
    pub sidecar: Option<crate::build_sidecar::SidecarOutput>,
}

/// Publish the artifact.
pub mod publish {
    use super::OpenhclIgvmExtras;
    use crate::build_openhcl_boot::OpenhclBootOutput;
    use crate::build_openvmm_hcl::OpenvmmHclOutput;
    use crate::build_sidecar::SidecarOutput;
    use flowey::node::prelude::*;

    flowey_request! {
        pub struct Request {
            pub extras: Vec<ReadVar<OpenhclIgvmExtras>>,
            pub artifact_dir: ReadVar<PathBuf>,
            pub done: WriteVar<SideEffect>,
        }
    }

    new_simple_flow_node!(struct Node);

    impl SimpleFlowNode for Node {
        type Request = Request;

        fn imports(ctx: &mut ImportCtx<'_>) {
            ctx.import::<flowey_lib_common::copy_to_artifact_dir::Node>();
        }

        fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
            let Request {
                extras,
                artifact_dir,
                done,
            } = request;

            let files = ctx.emit_minor_rust_stepv("describe OpenHCL igvm extras artifact", |ctx| {
                let extras = extras.claim(ctx);
                |rt| {
                    let mut files = Vec::new();
                    for extra in extras {
                        let OpenhclIgvmExtras {
                            recipe,
                            openvmm_hcl_bin,
                            openhcl_map,
                            openhcl_boot,
                            sidecar,
                        } = rt.read(extra);

                        let folder_name =
                            crate::artifact_openhcl_igvm_from_recipe::recipe_to_filename(&recipe);

                        {
                            let OpenvmmHclOutput { bin, dbg } = openvmm_hcl_bin;
                            files.push((format!("{folder_name}/openvmm_hcl").into(), bin));
                            if let Some(dbg) = dbg {
                                files.push((format!("{folder_name}/openvmm_hcl.dbg").into(), dbg));
                            }
                        }

                        if let Some(map) = openhcl_map {
                            files.push((format!("{folder_name}/openhcl.bin.map").into(), map));
                        }

                        {
                            let OpenhclBootOutput { bin, dbg } = openhcl_boot;
                            files.push((format!("{folder_name}/openhcl_boot").into(), bin));
                            files.push((format!("{folder_name}/openhcl_boot.dbg").into(), dbg));
                        }

                        if let Some(SidecarOutput { bin, dbg }) = sidecar {
                            files.push((format!("{folder_name}/sidecar").into(), bin));
                            files.push((format!("{folder_name}/sidecar.dbg").into(), dbg));
                        }
                    }
                    files
                }
            });

            ctx.req(flowey_lib_common::copy_to_artifact_dir::Request {
                debug_label: "OpenHCL igvm extras".into(),
                files,
                artifact_dir,
                done,
            });

            Ok(())
        }
    }
}

/// Resolve the contents of an existing artifact.
pub mod resolve {
    use super::OpenhclIgvmExtras;
    use crate::build_openhcl_boot::OpenhclBootOutput;
    use crate::build_openvmm_hcl::OpenvmmHclOutput;
    use crate::build_sidecar::SidecarOutput;
    use flowey::node::prelude::*;

    flowey_request! {
        pub struct Request {
            pub artifact_dir: ReadVar<PathBuf>,
            pub extras: WriteVar<Vec<OpenhclIgvmExtras>>,
        }
    }

    new_simple_flow_node!(struct Node);

    impl SimpleFlowNode for Node {
        type Request = Request;

        fn imports(ctx: &mut ImportCtx<'_>) {
            ctx.import::<flowey_lib_common::copy_to_artifact_dir::Node>();
        }

        fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
            let Request {
                artifact_dir,
                extras,
            } = request;

            ctx.emit_rust_step("resolve OpenHCL igvm extras artifact", |ctx| {
                let artifact_dir = artifact_dir.claim(ctx);
                let extras = extras.claim(ctx);
                move |rt| {
                    let artifact_dir = rt.read(artifact_dir);

                    let mut files = Vec::new();
                    for entry in fs_err::read_dir(artifact_dir)? {
                        let entry = entry?;

                        if !entry.file_type()?.is_dir() {
                            anyhow::bail!("unexpected file in root: {:?}", entry.file_name());
                        }

                        // deny auto-ingest of custom igvm files.
                        //
                        // by defn: a "custom" igvm file is no longer "custom"
                        // when it gets auto-ingested by another job. You get
                        // into a world of matching filename string, and lose a
                        // bunch of type-safety...
                        let recipe = crate::artifact_openhcl_igvm_from_recipe::filename_to_recipe(
                            entry
                                .file_name()
                                .to_str()
                                .context("unexpected folder name")?,
                        )
                        .context("unexpected folder name")?;

                        let folder_name = entry.path();
                        files.push(OpenhclIgvmExtras {
                            recipe,
                            openvmm_hcl_bin: OpenvmmHclOutput {
                                bin: folder_name.join("openhcl"),
                                dbg: {
                                    let p = folder_name.join("openvmm_hcl.dbg");
                                    p.exists().then_some(p)
                                },
                            },
                            openhcl_map: {
                                let p = folder_name.join("openhcl.bin.map");
                                p.exists().then_some(p)
                            },
                            openhcl_boot: OpenhclBootOutput {
                                bin: folder_name.join("openhcl_boot"),
                                dbg: folder_name.join("openhcl_boot.dbg"),
                            },
                            sidecar: Some(SidecarOutput {
                                bin: folder_name.join("sidecar"),
                                dbg: folder_name.join("sidecar.dbg"),
                            }),
                        })
                    }

                    rt.write(extras, &files);

                    Ok(())
                }
            });

            Ok(())
        }
    }
}