petri_tool/
main.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Tool for using petri functionality from the command line.
5
6use anyhow::Context as _;
7use clap::Parser;
8use petri::ArtifactResolver;
9use petri::TestArtifactRequirements;
10
11/// Command line interface for petri-related tasks.
12#[derive(Parser)]
13struct CliArgs {
14    #[clap(subcommand)]
15    command: Command,
16}
17
18#[derive(clap::Subcommand)]
19enum Command {
20    /// Builds a cloud-init disk image for a Linux VM image.
21    ///
22    /// This image creates a petri user with the password "petri".
23    CloudInit {
24        /// The architecture of the VM image.
25        #[clap(long)]
26        arch: MachineArch,
27
28        /// Path to the output disk image.
29        output: std::path::PathBuf,
30    },
31}
32
33#[derive(clap::ValueEnum, Clone, Copy, Debug)]
34enum MachineArch {
35    #[clap(name = "x86_64")]
36    X86_64,
37    Aarch64,
38}
39
40fn main() -> anyhow::Result<()> {
41    let args = CliArgs::parse();
42    match args.command {
43        Command::CloudInit { output, arch } => {
44            let image = build_with_artifacts(|resolver: ArtifactResolver<'_>| {
45                petri::disk_image::AgentImage::new(
46                    &resolver,
47                    match arch {
48                        MachineArch::X86_64 => petri_artifacts_common::tags::MachineArch::X86_64,
49                        MachineArch::Aarch64 => petri_artifacts_common::tags::MachineArch::Aarch64,
50                    },
51                    petri_artifacts_common::tags::OsFlavor::Linux,
52                )
53            })?;
54
55            let disk = image.build().context("failed to build disk image")?;
56            disk.persist(output)
57                .context("failed to persist disk image")?;
58
59            Ok(())
60        }
61    }
62}
63
64fn build_with_artifacts<R>(mut f: impl FnMut(ArtifactResolver<'_>) -> R) -> anyhow::Result<R> {
65    let resolver =
66        petri_artifact_resolver_openvmm_known_paths::OpenvmmKnownPathsTestArtifactResolver::new("");
67    let mut requirements = TestArtifactRequirements::new();
68    f(ArtifactResolver::collector(&mut requirements));
69    let artifacts = requirements.resolve(&resolver)?;
70    Ok(f(ArtifactResolver::resolver(&artifacts)))
71}