]> git.proxmox.com Git - pve-installer.git/commitdiff
fetch answer: allow to override fetch-from mode through CLI arguments
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 23 Apr 2024 19:02:47 +0000 (21:02 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 23 Apr 2024 19:02:49 +0000 (21:02 +0200)
Allow the user to pass a fetch-from mode also through CLI arguments.

This can be useful for debugging or if the GRUB boot entry from the
advanced menu is used with a ISO that did not (yet) got prepared for
automatic installation.

This is done in a quite crudely way, probably still beats most C
programs though. Only real bigger ugliness the user will notice is
that it exits with a failure code when the user asking for help
through passing `-h` or `--help`.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
proxmox-fetch-answer/src/main.rs

index cfc267741cf4637ef4cc3c23205d1d5967b8e3ba..660dc5157483be634322d3acb74640246325ec35 100644 (file)
@@ -6,7 +6,7 @@ use log::{error, info, LevelFilter};
 
 use proxmox_auto_installer::{
     log::AutoInstLogger,
-    utils::{AutoInstSettings, FetchAnswerFrom},
+    utils::{AutoInstSettings, FetchAnswerFrom, HttpOptions},
 };
 
 use fetch_plugins::{http::FetchFromHTTP, partition::FetchFromPartition};
@@ -45,16 +45,49 @@ fn fetch_answer(install_settings: &AutoInstSettings) -> Result<String> {
     bail!("Could not find any answer file!");
 }
 
+fn settings_from_cli_args(args: &[String]) -> Result<AutoInstSettings> {
+    // TODO: this was done in a bit of a hurry, needs tidying up
+    let mode = match args[1].to_lowercase().as_str() {
+        "iso" => FetchAnswerFrom::Iso,
+        "http" => FetchAnswerFrom::Http,
+        "partition" => FetchAnswerFrom::Partition,
+        "-h" | "--help" => bail!(
+            "usage: {} <http|iso|partition> [<http-url>] [<tls-cert-fingerprint>]",
+            args[0]
+        ),
+        _ => bail!("failed to parse fetch-from argument, not one of 'http', 'iso', or 'partition'"),
+    };
+    if args.len() > 4 {
+    } else if args.len() > 2 && mode != FetchAnswerFrom::Http {
+        bail!("only 'http' fetch-from mode supports additional url and cert-fingerprint mode");
+    }
+    Ok(AutoInstSettings {
+        mode,
+        http: HttpOptions {
+            url: args.get(2).cloned(),
+            cert_fingerprint: args.get(3).cloned(),
+        },
+    })
+}
+
 fn do_main() -> Result<()> {
     if let Err(err) = init_log() {
         bail!("could not initialize logging: {err}");
     }
 
-    let raw_install_settings = fs::read_to_string(AUTOINST_MODE_FILE).map_err(|err| {
-        format_err!("Could not find needed file '{AUTOINST_MODE_FILE}' in live environment: {err}")
-    })?;
-    let install_settings: AutoInstSettings = toml::from_str(raw_install_settings.as_str())
-        .map_err(|err| format_err!("Failed to parse '{AUTOINST_MODE_FILE}': {err}"))?;
+    let args: Vec<String> = std::env::args().collect();
+
+    let install_settings: AutoInstSettings = if args.len() > 1 {
+        settings_from_cli_args(&args)?
+    } else {
+        let raw_install_settings = fs::read_to_string(AUTOINST_MODE_FILE).map_err(|err| {
+            format_err!(
+                "Could not find needed file '{AUTOINST_MODE_FILE}' in live environment: {err}"
+            )
+        })?;
+        toml::from_str(raw_install_settings.as_str())
+            .map_err(|err| format_err!("Failed to parse '{AUTOINST_MODE_FILE}': {err}"))?
+    };
 
     let answer = fetch_answer(&install_settings).map_err(|err| format_err!("Aborting: {err}"))?;
     info!("queried answer file for automatic installation successfully");