lanzaboote/rust/tool/systemd/src/cli.rs

108 lines
2.4 KiB
Rust
Raw Normal View History

2022-11-26 08:55:15 -05:00
use std::path::PathBuf;
2022-11-21 19:29:16 -05:00
2022-11-25 17:48:20 -05:00
use anyhow::{Context, Result};
2022-11-21 19:29:16 -05:00
use clap::{Parser, Subcommand};
use crate::esp::Architecture;
use crate::install;
use lanzaboote_tool::signature::KeyPair;
2022-11-21 19:29:16 -05:00
/// The default log level.
///
/// 2 corresponds to the level INFO.
const DEFAULT_LOG_LEVEL: usize = 2;
2022-11-21 19:29:16 -05:00
#[derive(Parser)]
pub struct Cli {
/// Silence all output
#[arg(short, long)]
quiet: bool,
/// Verbose mode (-v, -vv, etc.)
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
2022-11-21 19:29:16 -05:00
#[clap(subcommand)]
2022-11-26 08:55:15 -05:00
commands: Commands,
2022-11-21 19:29:16 -05:00
}
#[derive(Subcommand)]
2022-11-26 08:55:15 -05:00
enum Commands {
Install(InstallCommand),
}
#[derive(Parser)]
struct InstallCommand {
/// Target system
#[arg(long)]
target_system: String,
/// Systemd path
#[arg(long)]
systemd: PathBuf,
/// Systemd-boot loader config
#[arg(long)]
systemd_boot_loader_config: PathBuf,
2022-11-26 18:12:00 -05:00
/// sbsign Public Key
2022-11-26 08:55:15 -05:00
#[arg(long)]
public_key: PathBuf,
2022-11-24 08:12:00 -05:00
2022-11-26 18:12:00 -05:00
/// sbsign Private Key
2022-11-26 08:55:15 -05:00
#[arg(long)]
private_key: PathBuf,
2022-11-24 08:12:00 -05:00
/// Configuration limit
#[arg(long, default_value_t = 1)]
configuration_limit: usize,
2022-11-26 18:12:00 -05:00
/// EFI system partition mountpoint (e.g. efiSysMountPoint)
esp: PathBuf,
/// List of generation links (e.g. /nix/var/nix/profiles/system-*-link)
2022-11-26 08:55:15 -05:00
generations: Vec<PathBuf>,
2022-11-21 19:29:16 -05:00
}
impl Cli {
pub fn call(self, module: &str) {
stderrlog::new()
.module(module)
.show_level(false)
.quiet(self.quiet)
.verbosity(DEFAULT_LOG_LEVEL + usize::from(self.verbose))
.init()
.expect("Failed to setup logger.");
if let Err(e) = self.commands.call() {
log::error!("{e:#}");
std::process::exit(1);
};
2022-11-21 19:29:16 -05:00
}
}
impl Commands {
pub fn call(self) -> Result<()> {
match self {
2022-11-26 08:55:15 -05:00
Commands::Install(args) => install(args),
2022-11-21 19:29:16 -05:00
}
}
}
2022-11-26 08:55:15 -05:00
fn install(args: InstallCommand) -> Result<()> {
2022-11-25 17:48:20 -05:00
let lanzaboote_stub =
std::env::var("LANZABOOTE_STUB").context("Failed to read LANZABOOTE_STUB env variable")?;
2022-11-24 08:12:00 -05:00
2022-11-26 17:19:08 -05:00
let key_pair = KeyPair::new(&args.public_key, &args.private_key);
2022-11-26 08:55:15 -05:00
install::Installer::new(
PathBuf::from(lanzaboote_stub),
Architecture::from_nixos_system(&args.target_system)?,
args.systemd,
args.systemd_boot_loader_config,
2022-11-26 17:19:08 -05:00
key_pair,
args.configuration_limit,
args.esp,
2022-11-26 08:55:15 -05:00
args.generations,
2022-11-23 14:40:01 -05:00
)
2022-11-26 08:55:15 -05:00
.install()
2022-11-23 09:26:26 -05:00
}