From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 77A29B8BC9 for ; Wed, 6 Dec 2023 12:35:14 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 60F8F6DC for ; Wed, 6 Dec 2023 12:35:14 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 6 Dec 2023 12:35:13 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 3490741042 for ; Wed, 6 Dec 2023 12:35:13 +0100 (CET) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Wed, 6 Dec 2023 12:34:52 +0100 Message-ID: <20231206113456.411898-4-c.heiss@proxmox.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231206113456.411898-1-c.heiss@proxmox.com> References: <20231206113456.411898-1-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.003 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [PATCH installer 3/6] tui, ui: switch over to JSON-based protocol 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: , X-List-Received-Date: Wed, 06 Dec 2023 11:35:14 -0000 Signed-off-by: Christoph Heiss --- Proxmox/UI/StdIO.pm | 41 ++++-- .../src/views/install_progress.rs | 117 ++++++++---------- 2 files changed, 83 insertions(+), 75 deletions(-) diff --git a/Proxmox/UI/StdIO.pm b/Proxmox/UI/StdIO.pm index a97245c..25d1c82 100644 --- a/Proxmox/UI/StdIO.pm +++ b/Proxmox/UI/StdIO.pm @@ -3,10 +3,26 @@ package Proxmox::UI::StdIO; use strict; use warnings; +use JSON qw(from_json to_json); + use base qw(Proxmox::UI::Base); use Proxmox::Log; +my sub send_msg : prototype($$) { + my ($type, %values) = @_; + + my $json = to_json({ type => $type, %values }, { utf8 => 1, canonical => 1 }); + print STDOUT "$json\n"; +} + +my sub recv_msg : prototype() { + my $response = // ''; # FIXME: error handling? + chomp($response); + + return eval { from_json($response, { utf8 => 1 }) }; +} + sub init { my ($self) = @_; @@ -16,34 +32,33 @@ sub init { sub message { my ($self, $msg) = @_; - print STDOUT "message: $msg\n"; + &send_msg('message', message => $msg); } sub error { my ($self, $msg) = @_; - log_err("error: $msg\n"); - print STDOUT "error: $msg\n"; + + log_error("error: $msg"); + &send_msg('error', message => $msg); } sub finished { my ($self, $success, $msg) = @_; my $state = $success ? 'ok' : 'err'; - log_info("finished: $state, $msg\n"); - print STDOUT "finished: $state, $msg\n"; + log_info("finished: $state, $msg"); + &send_msg('finished', state => $state, message => $msg); } sub prompt { my ($self, $query) = @_; - $query =~ s/\n/ /g; # FIXME: use a better serialisation (e.g., JSON) - print STDOUT "prompt: $query\n"; - - my $response = // ''; # FIXME: error handling? - - chomp($response); + &send_msg('prompt', query => $query); + my $response = &recv_msg(); - return lc($response) eq 'ok'; + if (defined($response) && $response->{type} eq 'prompt-answer') { + return lc($response->{answer}) eq 'ok'; + } } sub display_html { @@ -57,7 +72,7 @@ sub progress { $text = '' if !defined($text); - print STDOUT "progress: $ratio $text\n"; + &send_msg('progress', ratio => $ratio, text => $text); } sub process_events { diff --git a/proxmox-tui-installer/src/views/install_progress.rs b/proxmox-tui-installer/src/views/install_progress.rs index 01c9941..741529f 100644 --- a/proxmox-tui-installer/src/views/install_progress.rs +++ b/proxmox-tui-installer/src/views/install_progress.rs @@ -1,17 +1,16 @@ -use std::{ - io::{BufRead, BufReader, Write}, - str::FromStr, - sync::{Arc, Mutex}, - thread, - time::Duration, -}; - use cursive::{ utils::Counter, view::{Nameable, Resizable, ViewWrapper}, views::{Dialog, DummyView, LinearLayout, PaddedView, ProgressBar, TextContent, TextView}, CbSink, Cursive, }; +use serde::Deserialize; +use std::{ + io::{BufRead, BufReader, Write}, + sync::{Arc, Mutex}, + thread, + time::Duration, +}; use crate::{abort_install_button, prompt_dialog, setup::InstallConfig, InstallerState}; use proxmox_installer_common::setup::spawn_low_level_installer; @@ -95,7 +94,7 @@ impl InstallProgressView { Err(err) => return Err(format!("low-level installer exited early: {err}")), }; - let msg = match line.parse::() { + let msg = match serde_json::from_str::(&line) { Ok(msg) => msg, Err(stray) => { // Not a fatal error, so don't abort the installation by returning @@ -105,26 +104,26 @@ impl InstallProgressView { }; let result = match msg.clone() { - UiMessage::Info(s) => cb_sink.send(Box::new(|siv| { - siv.add_layer(Dialog::info(s).title("Information")); + UiMessage::Info { message } => cb_sink.send(Box::new(|siv| { + siv.add_layer(Dialog::info(message).title("Information")); })), - UiMessage::Error(s) => cb_sink.send(Box::new(|siv| { - siv.add_layer(Dialog::info(s).title("Error")); + UiMessage::Error { message } => cb_sink.send(Box::new(|siv| { + siv.add_layer(Dialog::info(message).title("Error")); })), - UiMessage::Prompt(s) => cb_sink.send({ + UiMessage::Prompt { query } => cb_sink.send({ let writer = writer.clone(); - Box::new(move |siv| Self::show_prompt(siv, &s, writer)) + Box::new(move |siv| Self::show_prompt(siv, &query, writer)) }), - UiMessage::Progress(ratio, s) => { - counter.set(ratio); - progress_text.set_content(s); + UiMessage::Progress { ratio, text } => { + counter.set((ratio * 100.).floor() as usize); + progress_text.set_content(text); Ok(()) } - UiMessage::Finished(success, msg) => { + UiMessage::Finished { state, message } => { counter.set(100); - progress_text.set_content(msg.to_owned()); + progress_text.set_content(message.to_owned()); cb_sink.send(Box::new(move |siv| { - Self::prepare_for_reboot(siv, success, &msg) + Self::prepare_for_reboot(siv, state == "ok", &message); })) } }; @@ -189,6 +188,19 @@ impl InstallProgressView { } fn show_prompt(siv: &mut Cursive, text: &str, writer: Arc>) { + let send_answer = |writer: Arc>, answer| { + if let Ok(mut writer) = writer.lock() { + let _ = writeln!( + writer, + "{}", + serde_json::json!({ + "type" : "prompt-answer", + "answer" : answer, + }) + ); + } + }; + prompt_dialog( siv, "Prompt", @@ -197,16 +209,12 @@ impl InstallProgressView { Box::new({ let writer = writer.clone(); move |_| { - if let Ok(mut writer) = writer.lock() { - let _ = writeln!(writer, "ok"); - } + send_answer(writer.clone(), "ok"); } }), "Cancel", Box::new(move |_| { - if let Ok(mut writer) = writer.lock() { - let _ = writeln!(writer); - } + send_answer(writer.clone(), "cancel"); }), ); } @@ -216,40 +224,25 @@ impl ViewWrapper for InstallProgressView { cursive::wrap_impl!(self.view: PaddedView); } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Deserialize, PartialEq)] +#[serde(tag = "type", rename_all = "lowercase")] enum UiMessage { - Info(String), - Error(String), - Prompt(String), - Finished(bool, String), - Progress(usize, String), -} - -impl FromStr for UiMessage { - type Err = String; - - fn from_str(s: &str) -> Result { - let (ty, rest) = s.split_once(": ").ok_or("invalid message: no type")?; - - match ty { - "message" => Ok(UiMessage::Info(rest.to_owned())), - "error" => Ok(UiMessage::Error(rest.to_owned())), - "prompt" => Ok(UiMessage::Prompt(rest.to_owned())), - "finished" => { - let (state, rest) = rest.split_once(", ").ok_or("invalid message: no state")?; - Ok(UiMessage::Finished(state == "ok", rest.to_owned())) - } - "progress" => { - let (percent, rest) = rest.split_once(' ').ok_or("invalid progress message")?; - Ok(UiMessage::Progress( - percent - .parse::() - .map(|v| (v * 100.).floor() as usize) - .map_err(|err| err.to_string())?, - rest.to_owned(), - )) - } - unknown => Err(format!("invalid message type {unknown}, rest: {rest}")), - } - } + #[serde(rename = "message")] + Info { + message: String, + }, + Error { + message: String, + }, + Prompt { + query: String, + }, + Finished { + state: String, + message: String, + }, + Progress { + ratio: f32, + text: String, + }, } -- 2.42.0