-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: add
system-reinstall-bootc
binary
# Background The current usage instructions for bootc involve a long podman invocation. # Issue It's hard to remember and type the long podman invocation, making the usage of bootc difficult for users. See https://issues.redhat.com/browse/BIFROST-610 and https://issues.redhat.com/browse/BIFROST-611 (Epic https://issues.redhat.com/browse/BIFROST-594) # Solution We want to make the usage of bootc easier by providing a new Fedora/RHEL subpackage that includes a new binary `system-reinstall-bootc`. This binary will simplify the usage of bootc by providing a simple command line interface (configured either through CLI flags or a configuration file) with an interactive prompt that allows users to reinstall the current system using bootc. The commandline will handle helping the user choose SSH keys / users, warn the user about the destructive nature of the operation, and eventually report issues they might run into in the various clouds (e.g. missing cloud agent on the target image) # Implementation Added new system-reinstall-bootc crate that outputs the new system-reinstall-bootc binary. This new crate depends on the existing utils crate. Refactored the tracing initialization from the bootc binary into the utils crate so that it can be reused by the new crate. The new CLI can either be configured through commandline flags or through a configuration file in a path set by the environment variable `BOOTC_REINSTALL_CONFIG`. The configuration file is a YAML file. # Limitations Only root SSH keys are supported. The multi user selection TUI is implemented, but if you choose anything other than root you will get an error. # TODO Missing docs, missing functionality. Everything is in alpha stage. User choice / SSH keys / prompt disabling should also eventually be supported to be configured through commandline arguments or the configuration file. Signed-off-by: Omer Tuchfeld <[email protected]>
- Loading branch information
Showing
11 changed files
with
443 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
[workspace] | ||
members = [ | ||
"cli", | ||
"system-reinstall-bootc", | ||
"lib", | ||
"ostree-ext", | ||
"utils", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "system-reinstall-bootc" | ||
version = "0.1.9" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/containers/bootc" | ||
readme = "README.md" | ||
publish = false | ||
# For now don't bump this above what is currently shipped in RHEL9. | ||
rust-version = "1.75.0" | ||
|
||
# See https://github.com/coreos/cargo-vendor-filterer | ||
[package.metadata.vendor-filter] | ||
# For now we only care about tier 1+2 Linux. (In practice, it's unlikely there is a tier3-only Linux dependency) | ||
platforms = ["*-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
bootc-utils = { path = "../utils" } | ||
clap = { workspace = true, features = ["derive"] } | ||
dialoguer = "0.11.0" | ||
log = "0.4.21" | ||
rustix = { workspace = true } | ||
serde = { workspace = true, features = ["derive"] } | ||
serde_json = { workspace = true } | ||
serde_yaml = "0.9.22" | ||
tracing = { workspace = true } | ||
uzers = "0.12.1" | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# The bootc container image to install | ||
bootc_image: quay.io/fedora/fedora-bootc:41 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use clap::Parser; | ||
|
||
#[derive(Parser)] | ||
pub(crate) struct Cli { | ||
/// The bootc container image to install, e.g. quay.io/fedora/fedora-bootc:41 | ||
pub(crate) bootc_image: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use anyhow::{ensure, Context, Result}; | ||
use clap::Parser; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
mod cli; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub(crate) struct ReinstallConfig { | ||
/// The bootc image to install on the system. | ||
pub(crate) bootc_image: String, | ||
|
||
/// The raw CLI arguments that were used to invoke the program. None if the config was loaded | ||
/// from a file. | ||
#[serde(skip_deserializing)] | ||
cli_flags: Option<Vec<String>>, | ||
} | ||
|
||
impl ReinstallConfig { | ||
pub fn parse_from_cli(cli: cli::Cli) -> Self { | ||
Self { | ||
bootc_image: cli.bootc_image, | ||
cli_flags: Some(std::env::args().collect::<Vec<String>>()), | ||
} | ||
} | ||
|
||
pub fn load() -> Result<Self> { | ||
Ok(match std::env::var("BOOTC_REINSTALL_CONFIG") { | ||
Ok(config_path) => { | ||
ensure_no_cli_args()?; | ||
|
||
serde_yaml::from_slice( | ||
&std::fs::read(&config_path) | ||
.context("reading BOOTC_REINSTALL_CONFIG file {config_path}")?, | ||
) | ||
.context("parsing BOOTC_REINSTALL_CONFIG file {config_path}")? | ||
} | ||
Err(_) => ReinstallConfig::parse_from_cli(cli::Cli::parse()), | ||
}) | ||
} | ||
} | ||
|
||
fn ensure_no_cli_args() -> Result<()> { | ||
let num_args = std::env::args().len(); | ||
|
||
ensure!( | ||
num_args == 1, | ||
"BOOTC_REINSTALL_CONFIG is set, but there are {num_args} CLI arguments. BOOTC_REINSTALL_CONFIG is meant to be used with no arguments." | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//! The main entrypoint for the bootc system reinstallation CLI | ||
use anyhow::{ensure, Context, Result}; | ||
use bootc_utils::CommandRunExt; | ||
use rustix::process::getuid; | ||
|
||
mod config; | ||
mod podman; | ||
mod prompt; | ||
pub(crate) mod users; | ||
|
||
const ROOT_KEY_MOUNT_POINT: &str = "/bootc_authorized_ssh_keys/root"; | ||
|
||
fn run() -> Result<()> { | ||
bootc_utils::initialize_tracing(); | ||
tracing::trace!("starting {}", env!("CARGO_PKG_NAME")); | ||
|
||
// Rootless podman is not supported by bootc | ||
ensure!(getuid().is_root(), "Must run as the root user"); | ||
|
||
let config = config::ReinstallConfig::load().context("loading config")?; | ||
|
||
let mut reinstall_podman_command = | ||
podman::command(&config.bootc_image, &prompt::get_root_key()?); | ||
|
||
println!(); | ||
|
||
println!("Going to run command {:?}", reinstall_podman_command); | ||
|
||
prompt::temporary_developer_protection_prompt()?; | ||
|
||
reinstall_podman_command | ||
.run_with_cmd_context() | ||
.context("running reinstall command")?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn main() { | ||
// In order to print the error in a custom format (with :#) our | ||
// main simply invokes a run() where all the work is done. | ||
// This code just captures any errors. | ||
if let Err(e) = run() { | ||
tracing::error!("{:#}", e); | ||
std::process::exit(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::process::Command; | ||
|
||
use super::ROOT_KEY_MOUNT_POINT; | ||
use crate::users::UserKeys; | ||
|
||
pub(crate) fn command(image: &str, root_key: &Option<UserKeys>) -> Command { | ||
let mut podman_command_and_args = [ | ||
// We use podman to run the bootc container. This might change in the future to remove the | ||
// podman dependency. | ||
"podman", | ||
"run", | ||
// The container needs to be privileged, as it heavily modifies the host | ||
"--privileged", | ||
// The container needs to access the host's PID namespace to mount host directories | ||
"--pid=host", | ||
// Since https://github.com/containers/bootc/pull/919 this mount should not be needed, but | ||
// some reason with e.g. quay.io/fedora/fedora-bootc:41 it is still needed. | ||
"-v", | ||
"/var/lib/containers:/var/lib/containers", | ||
] | ||
.map(String::from) | ||
.to_vec(); | ||
|
||
let mut bootc_command_and_args = [ | ||
"bootc", | ||
"install", | ||
// We're replacing the current root | ||
"to-existing-root", | ||
// The user already knows they're reinstalling their machine, that's the entire purpose of | ||
// this binary. Since this is no longer an "arcane" bootc command, we can safely avoid this | ||
// timed warning prompt. TODO: Discuss in https://github.com/containers/bootc/discussions/1060 | ||
"--acknowledge-destructive", | ||
] | ||
.map(String::from) | ||
.to_vec(); | ||
|
||
if let Some(root_key) = root_key.as_ref() { | ||
let root_authorized_keys_path = root_key.authorized_keys_path.clone(); | ||
|
||
podman_command_and_args.push("-v".to_string()); | ||
podman_command_and_args.push(format!( | ||
"{root_authorized_keys_path}:{ROOT_KEY_MOUNT_POINT}" | ||
)); | ||
|
||
bootc_command_and_args.push("--root-ssh-authorized-keys".to_string()); | ||
bootc_command_and_args.push(ROOT_KEY_MOUNT_POINT.to_string()); | ||
} | ||
|
||
let all_args = [ | ||
podman_command_and_args, | ||
vec![image.to_string()], | ||
bootc_command_and_args, | ||
] | ||
.concat(); | ||
|
||
let mut command = Command::new(&all_args[0]); | ||
command.args(&all_args[1..]); | ||
|
||
command | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use crate::users::{get_all_users_keys, UserKeys}; | ||
use anyhow::{ensure, Context, Result}; | ||
|
||
fn prompt_single_user(user: &crate::users::UserKeys) -> Result<Vec<&crate::users::UserKeys>> { | ||
let prompt = format!( | ||
"Found only one user ({}) with {} SSH authorized keys. Would you like to install this user in the system?", | ||
user.user, | ||
user.num_keys(), | ||
); | ||
let answer = ask_yes_no(&prompt, true)?; | ||
Ok(if answer { vec![&user] } else { vec![] }) | ||
} | ||
|
||
fn prompt_user_selection( | ||
all_users: &[crate::users::UserKeys], | ||
) -> Result<Vec<&crate::users::UserKeys>> { | ||
let keys: Vec<String> = all_users.iter().map(|x| x.user.clone()).collect(); | ||
|
||
// TODO: Handle https://github.com/console-rs/dialoguer/issues/77 | ||
let selected_user_indices: Vec<usize> = dialoguer::MultiSelect::new() | ||
.with_prompt("Select the users you want to install in the system (along with their authorized SSH keys)") | ||
.items(&keys) | ||
.interact()?; | ||
|
||
Ok(selected_user_indices | ||
.iter() | ||
// Safe unwrap because we know the index is valid | ||
.map(|x| all_users.get(*x).unwrap()) | ||
.collect()) | ||
} | ||
|
||
/// Temporary safety mechanism to stop devs from running it on their dev machine. TODO: Discuss | ||
/// final prompting UX in https://github.com/containers/bootc/discussions/1060 | ||
pub(crate) fn temporary_developer_protection_prompt() -> Result<()> { | ||
// Print an empty line so that the warning stands out from the rest of the output | ||
println!(); | ||
|
||
let prompt = "THIS WILL REINSTALL YOUR SYSTEM! Are you sure you want to continue?"; | ||
let answer = ask_yes_no(prompt, false)?; | ||
|
||
if !answer { | ||
println!("Exiting without reinstalling the system."); | ||
std::process::exit(0); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn ask_yes_no(prompt: &str, default: bool) -> Result<bool> { | ||
dialoguer::Confirm::new() | ||
.with_prompt(prompt) | ||
.default(default) | ||
.wait_for_newline(true) | ||
.interact() | ||
.context("prompting") | ||
} | ||
|
||
/// For now we only support the root user. This function returns the root user's SSH | ||
/// authorized_keys. In the future, when bootc supports multiple users, this function will need to | ||
/// be updated to return the SSH authorized_keys for all the users selected by the user. | ||
pub(crate) fn get_root_key() -> Result<Option<UserKeys>> { | ||
let users = get_all_users_keys()?; | ||
if users.is_empty() { | ||
return Ok(None); | ||
} | ||
|
||
let selected_users = if users.len() == 1 { | ||
prompt_single_user(&users[0])? | ||
} else { | ||
prompt_user_selection(&users)? | ||
}; | ||
|
||
ensure!( | ||
selected_users.iter().all(|x| x.user == "root"), | ||
"Only importing the root user keys is supported for now" | ||
); | ||
|
||
let root_key = selected_users.into_iter().find(|x| x.user == "root"); | ||
|
||
Ok(root_key.cloned()) | ||
} |
Oops, something went wrong.