From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id AE6BE1FF348 for ; Wed, 17 Apr 2024 14:34:27 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 594E86317; Wed, 17 Apr 2024 14:32:14 +0200 (CEST) From: Aaron Lauterer To: pve-devel@lists.proxmox.com Date: Wed, 17 Apr 2024 14:30:46 +0200 Message-Id: <20240417123108.212720-15-a.lauterer@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417123108.212720-1-a.lauterer@proxmox.com> References: <20240417123108.212720-1-a.lauterer@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.047 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH installer v6 14/36] auto-installer: add auto-installer binary X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" It expects the contents of an answer file via stdin. It will then be parsed and the JSON for the low level installer is generated. It then calls the low level installer directly. The output of the installaton progress is kept rather simple for now. If configured in the answer file, commands will be run pre and post the low level installer. It also logs everything to the logfile, currently '/tmp/auto_installer.log'. Tested-by: Christoph Heiss Reviewed-by: Christoph Heiss Signed-off-by: Aaron Lauterer --- Makefile | 9 +- .../src/bin/proxmox-auto-installer.rs | 195 ++++++++++++++++++ 2 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 proxmox-auto-installer/src/bin/proxmox-auto-installer.rs diff --git a/Makefile b/Makefile index 4f140ca..3ac5769 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,9 @@ INSTALLER_SOURCES=$(shell git ls-files) country.dat PREFIX = /usr BINDIR = $(PREFIX)/bin -USR_BIN := proxmox-tui-installer +USR_BIN := \ + proxmox-tui-installer\ + proxmox-auto-installer COMPILED_BINS := \ $(addprefix $(CARGO_COMPILEDIR)/,$(USR_BIN)) @@ -99,7 +101,7 @@ VARLIBDIR=$(DESTDIR)/var/lib/proxmox-installer HTMLDIR=$(VARLIBDIR)/html/common .PHONY: install -install: $(INSTALLER_SOURCES) $(CARGO_COMPILEDIR)/proxmox-tui-installer +install: $(INSTALLER_SOURCES) $(COMPILED_BINS) $(MAKE) -C banner install $(MAKE) -C Proxmox install install -D -m 644 interfaces $(DESTDIR)/etc/network/interfaces @@ -118,7 +120,8 @@ install: $(INSTALLER_SOURCES) $(CARGO_COMPILEDIR)/proxmox-tui-installer $(COMPILED_BINS): cargo-build .PHONY: cargo-build cargo-build: - $(CARGO) build --package proxmox-tui-installer --bin proxmox-tui-installer $(CARGO_BUILD_ARGS) + $(CARGO) build --package proxmox-tui-installer --bin proxmox-tui-installer \ + --package proxmox-auto-installer --bin proxmox-auto-installer $(CARGO_BUILD_ARGS) %-banner.png: %-banner.svg rsvg-convert -o $@ $< diff --git a/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs b/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs new file mode 100644 index 0000000..f43b12f --- /dev/null +++ b/proxmox-auto-installer/src/bin/proxmox-auto-installer.rs @@ -0,0 +1,195 @@ +use anyhow::{anyhow, bail, Error, Result}; +use log::{error, info, LevelFilter}; +use std::{ + env, + io::{BufRead, BufReader, Write}, + path::PathBuf, + process::ExitCode, +}; + +use proxmox_installer_common::setup::{ + installer_setup, read_json, spawn_low_level_installer, LocaleInfo, RuntimeInfo, SetupInfo, +}; + +use proxmox_auto_installer::{ + answer::Answer, + log::AutoInstLogger, + udevinfo::UdevInfo, + utils, + utils::{parse_answer, LowLevelMessage}, +}; + +static LOGGER: AutoInstLogger = AutoInstLogger; + +pub fn init_log() -> Result<()> { + AutoInstLogger::init("/tmp/auto_installer.log")?; + log::set_logger(&LOGGER) + .map(|()| log::set_max_level(LevelFilter::Info)) + .map_err(|err| anyhow!(err)) +} + +fn auto_installer_setup(in_test_mode: bool) -> Result<(Answer, UdevInfo)> { + let base_path = if in_test_mode { "./testdir" } else { "/" }; + let mut path = PathBuf::from(base_path); + + path.push("run"); + path.push("proxmox-installer"); + + let udev_info: UdevInfo = { + let mut path = path.clone(); + path.push("run-env-udev.json"); + + read_json(&path).map_err(|err| anyhow!("Failed to retrieve udev info details: {err}"))? + }; + + let mut buffer = String::new(); + let lines = std::io::stdin().lock().lines(); + for line in lines { + buffer.push_str(&line.unwrap()); + buffer.push('\n'); + } + + let answer: Answer = + toml::from_str(&buffer).map_err(|err| anyhow!("Failed parsing answer file: {err}"))?; + + Ok((answer, udev_info)) +} + +fn main() -> ExitCode { + if let Err(err) = init_log() { + panic!("could not initilize logging: {}", err); + } + + let in_test_mode = match env::args().nth(1).as_deref() { + Some("-t") => true, + // Always force the test directory in debug builds + _ => cfg!(debug_assertions), + }; + info!("Starting auto installer"); + + let (setup_info, locales, runtime_info) = match installer_setup(in_test_mode) { + Ok(result) => result, + Err(err) => { + error!("Installer setup error: {err}"); + return ExitCode::FAILURE; + } + }; + + let (answer, udevadm_info) = match auto_installer_setup(in_test_mode) { + Ok(result) => result, + Err(err) => { + error!("Autoinstaller setup error: {err}"); + return ExitCode::FAILURE; + } + }; + + match utils::run_cmds("Pre", &answer.global.pre_commands) { + Ok(_) => (), + Err(err) => { + error!("Error when running Pre-Commands: {}", err); + return exit_failure(answer.global.reboot_on_error); + } + }; + match run_installation(&answer, &locales, &runtime_info, &udevadm_info, &setup_info) { + Ok(_) => info!("Installation done."), + Err(err) => { + error!("Installation failed: {err}"); + return exit_failure(answer.global.reboot_on_error); + } + } + match utils::run_cmds("Post", &answer.global.post_commands) { + Ok(_) => (), + Err(err) => { + error!("Error when running Post-Commands: {}", err); + return exit_failure(answer.global.reboot_on_error); + } + }; + ExitCode::SUCCESS +} + +/// When we exit with a failure, the installer will not automatically reboot. +/// Default value for reboot_on_error is false +fn exit_failure(reboot_on_error: bool) -> ExitCode { + if reboot_on_error { + ExitCode::SUCCESS + } else { + ExitCode::FAILURE + } +} + +fn run_installation( + answer: &Answer, + locales: &LocaleInfo, + runtime_info: &RuntimeInfo, + udevadm_info: &UdevInfo, + setup_info: &SetupInfo, +) -> Result<()> { + let config = parse_answer(answer, udevadm_info, runtime_info, locales, setup_info)?; + info!("Calling low-level installer"); + + let mut child = match spawn_low_level_installer(false) { + Ok(child) => child, + Err(err) => { + bail!("Low level installer could not be started: {}", err); + } + }; + + let mut cur_counter = 111; + let mut inner = || -> Result<()> { + let reader = child + .stdout + .take() + .map(BufReader::new) + .ok_or(anyhow!("failed to get stdout reader"))?; + let mut writer = child + .stdin + .take() + .ok_or(anyhow!("failed to get stdin writer"))?; + + serde_json::to_writer(&mut writer, &config) + .map_err(|err| anyhow!("failed to serialize install config: {err}"))?; + writeln!(writer).map_err(|err| anyhow!("failed to write install config: {err}"))?; + + for line in reader.lines() { + let line = match line { + Ok(line) => line, + Err(_) => break, + }; + let msg = match serde_json::from_str::(&line) { + Ok(msg) => msg, + Err(_) => { + // Not a fatal error, so don't abort the installation by returning + continue; + } + }; + + match msg.clone() { + LowLevelMessage::Info { message } => info!("{message}"), + LowLevelMessage::Error { message } => error!("{message}"), + LowLevelMessage::Prompt { query } => { + bail!("Got interactive prompt I cannot answer: {query}") + } + LowLevelMessage::Progress { ratio, text: _ } => { + let counter = (ratio * 100.).floor() as usize; + if counter != cur_counter { + cur_counter = counter; + info!("Progress: {counter:>3}%"); + } + } + LowLevelMessage::Finished { state, message } => { + if state == "err" { + bail!("{message}"); + } + info!("Finished: '{state}' {message}"); + } + }; + } + Ok(()) + }; + match inner() { + Err(err) => Err(Error::msg(format!( + "low level installer returned early: {err}" + ))), + _ => Ok(()), + } +} -- 2.39.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel