From ade3ff2897e6d08753ebc7ae16cb986eec4a1c6c Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Wed, 4 Oct 2023 17:47:58 +0200 Subject: [PATCH 1/6] Separate migrate-wicked from agama-cli --- rust/Cargo.lock | 3 +- rust/agama-cli/Cargo.toml | 1 - rust/agama-cli/src/commands.rs | 4 -- rust/agama-cli/src/main.rs | 3 -- rust/agama-migrate-wicked/Cargo.toml | 6 +++ rust/agama-migrate-wicked/src/lib.rs | 3 -- rust/agama-migrate-wicked/src/main.rs | 68 +++++++++++++++++++++++++++ 7 files changed, 76 insertions(+), 12 deletions(-) delete mode 100644 rust/agama-migrate-wicked/src/lib.rs create mode 100644 rust/agama-migrate-wicked/src/main.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 6cf937638a..d6e2a8d8d6 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -13,7 +13,6 @@ name = "agama-cli" version = "1.0.0" dependencies = [ "agama-lib", - "agama-migrate-wicked", "agama-settings", "anyhow", "async-std", @@ -99,8 +98,10 @@ version = "0.1.0" dependencies = [ "agama-dbus-server", "agama-lib", + "anyhow", "async-std", "cidr", + "clap", "quick-xml", "regex", "serde", diff --git a/rust/agama-cli/Cargo.toml b/rust/agama-cli/Cargo.toml index 81628eb7e7..c7701ae7f4 100644 --- a/rust/agama-cli/Cargo.toml +++ b/rust/agama-cli/Cargo.toml @@ -9,7 +9,6 @@ edition = "2021" clap = { version = "4.1.4", features = ["derive", "wrap_help"] } agama-lib = { path="../agama-lib" } agama-settings = { path="../agama-settings" } -agama-migrate-wicked = { path="../agama-migrate-wicked" } serde = { version = "1.0.152" } serde_json = "1.0.91" serde_yaml = "0.9.17" diff --git a/rust/agama-cli/src/commands.rs b/rust/agama-cli/src/commands.rs index a095d59a09..99d51ece9f 100644 --- a/rust/agama-cli/src/commands.rs +++ b/rust/agama-cli/src/commands.rs @@ -2,7 +2,6 @@ use crate::config::ConfigCommands; use crate::logs::LogsCommands; use crate::profile::ProfileCommands; use crate::questions::QuestionsCommands; -use crate::wicked::WickedCommands; use clap::Subcommand; #[derive(Subcommand, Debug)] @@ -36,7 +35,4 @@ pub enum Commands { /// Collects logs #[command(subcommand)] Logs(LogsCommands), - /// Migrate wicked config - #[command(subcommand)] - Wicked(WickedCommands), } diff --git a/rust/agama-cli/src/main.rs b/rust/agama-cli/src/main.rs index c8d1cad8e7..b555884b6c 100644 --- a/rust/agama-cli/src/main.rs +++ b/rust/agama-cli/src/main.rs @@ -8,7 +8,6 @@ mod printers; mod profile; mod progress; mod questions; -mod wicked; use crate::error::CliError; use agama_lib::error::ServiceError; @@ -27,7 +26,6 @@ use std::{ thread::sleep, time::Duration, }; -use wicked::run as run_wicked_cmd; #[derive(Parser)] #[command(name = "agama", version, about, long_about = None)] @@ -136,7 +134,6 @@ async fn run_command(cli: Cli) -> anyhow::Result<()> { } Commands::Questions(subcommand) => block_on(run_questions_cmd(subcommand)), Commands::Logs(subcommand) => block_on(run_logs_cmd(subcommand)), - Commands::Wicked(subcommand) => block_on(run_wicked_cmd(subcommand, cli.format)), _ => unimplemented!(), } } diff --git a/rust/agama-migrate-wicked/Cargo.toml b/rust/agama-migrate-wicked/Cargo.toml index 41fc281afd..3767528a2e 100644 --- a/rust/agama-migrate-wicked/Cargo.toml +++ b/rust/agama-migrate-wicked/Cargo.toml @@ -13,3 +13,9 @@ regex = "1.9.5" agama-dbus-server = { path="../agama-dbus-server" } async-std = "1.12.0" cidr = { version = "0.2.2", features = ["serde"] } +clap = { version = "4.1.4", features = ["derive", "wrap_help"] } +anyhow = "1.0.71" + +[[bin]] +name = "migrate-wicked" +path = "src/main.rs" diff --git a/rust/agama-migrate-wicked/src/lib.rs b/rust/agama-migrate-wicked/src/lib.rs deleted file mode 100644 index a184bc4c4e..0000000000 --- a/rust/agama-migrate-wicked/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod interface; -pub mod migrate; -pub mod reader; diff --git a/rust/agama-migrate-wicked/src/main.rs b/rust/agama-migrate-wicked/src/main.rs new file mode 100644 index 0000000000..756384e276 --- /dev/null +++ b/rust/agama-migrate-wicked/src/main.rs @@ -0,0 +1,68 @@ +mod interface; +mod migrate; +mod reader; + +use clap::{Parser, Subcommand}; +use migrate::migrate; +use reader::read as wicked_read; +use std::process::{ExitCode, Termination}; + +#[derive(Parser)] +#[command(name = "migrate-wicked", version, about, long_about = None)] +struct Cli { + #[command(subcommand)] + pub command: Commands, +} + +#[derive(Subcommand, Debug)] +pub enum Commands { + /// Shows the current xml wicked configuration + Show { + /// Where wicked xml configs are located + path: String + }, + /// Migrate wicked state at path + Migrate { + /// Where wicked xml configs are located + path: String + }, +} + +async fn run_command(cli: Cli) -> anyhow::Result<()> { + match cli.command { + Commands::Show { path } => { + let interfaces = wicked_read(path.into()).await?; + println!("{:?}", interfaces); + Ok(()) + } + Commands::Migrate { path } => { + migrate(path).await.unwrap(); + Ok(()) + } + } +} + +/// Represents the result of execution. +pub enum CliResult { + /// Successful execution. + Ok = 0, + /// Something went wrong. + Error = 1, +} + +impl Termination for CliResult { + fn report(self) -> ExitCode { + ExitCode::from(self as u8) + } +} + +#[async_std::main] +async fn main() -> CliResult { + let cli = Cli::parse(); + + if let Err(error) = run_command(cli).await { + eprintln!("{:?}", error); + return CliResult::Error; + } + CliResult::Ok +} From 8f8453cc796e49486db759aadfeca03a6c82e1d9 Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Thu, 28 Sep 2023 18:02:19 +0200 Subject: [PATCH 2/6] Add logger to show logs when running commands --- rust/Cargo.lock | 2 ++ rust/agama-migrate-wicked/Cargo.toml | 2 ++ rust/agama-migrate-wicked/src/main.rs | 9 +++++++++ 3 files changed, 13 insertions(+) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index d6e2a8d8d6..9ec612dffc 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -102,9 +102,11 @@ dependencies = [ "async-std", "cidr", "clap", + "log", "quick-xml", "regex", "serde", + "simplelog", ] [[package]] diff --git a/rust/agama-migrate-wicked/Cargo.toml b/rust/agama-migrate-wicked/Cargo.toml index 3767528a2e..4b243e3c63 100644 --- a/rust/agama-migrate-wicked/Cargo.toml +++ b/rust/agama-migrate-wicked/Cargo.toml @@ -15,6 +15,8 @@ async-std = "1.12.0" cidr = { version = "0.2.2", features = ["serde"] } clap = { version = "4.1.4", features = ["derive", "wrap_help"] } anyhow = "1.0.71" +log = "0.4" +simplelog = "0.12.1" [[bin]] name = "migrate-wicked" diff --git a/rust/agama-migrate-wicked/src/main.rs b/rust/agama-migrate-wicked/src/main.rs index 756384e276..69ebb061ac 100644 --- a/rust/agama-migrate-wicked/src/main.rs +++ b/rust/agama-migrate-wicked/src/main.rs @@ -6,6 +6,7 @@ use clap::{Parser, Subcommand}; use migrate::migrate; use reader::read as wicked_read; use std::process::{ExitCode, Termination}; +use log::*; #[derive(Parser)] #[command(name = "migrate-wicked", version, about, long_about = None)] @@ -58,6 +59,14 @@ impl Termination for CliResult { #[async_std::main] async fn main() -> CliResult { + simplelog::TermLogger::init( + LevelFilter::Info, + simplelog::Config::default(), + simplelog::TerminalMode::Stderr, + simplelog::ColorChoice::Auto, + ) + .unwrap(); + let cli = Cli::parse(); if let Err(error) = run_command(cli).await { From 13d9492021f0ad10cb9cfd161b290bd99f76605e Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Wed, 4 Oct 2023 13:56:10 +0200 Subject: [PATCH 3/6] Add global option to define log level --- rust/agama-migrate-wicked/src/main.rs | 36 +++++++++++++++++---------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/rust/agama-migrate-wicked/src/main.rs b/rust/agama-migrate-wicked/src/main.rs index 69ebb061ac..e7dd5bdeb9 100644 --- a/rust/agama-migrate-wicked/src/main.rs +++ b/rust/agama-migrate-wicked/src/main.rs @@ -2,30 +2,40 @@ mod interface; mod migrate; mod reader; -use clap::{Parser, Subcommand}; +use clap::builder::TypedValueParser; +use clap::{Args, Parser, Subcommand}; +use log::*; use migrate::migrate; use reader::read as wicked_read; use std::process::{ExitCode, Termination}; -use log::*; #[derive(Parser)] #[command(name = "migrate-wicked", version, about, long_about = None)] struct Cli { + #[clap(flatten)] + global_opts: GlobalOpts, + #[command(subcommand)] pub command: Commands, } -#[derive(Subcommand, Debug)] +#[derive(Debug, Args)] +struct GlobalOpts { + #[arg(long, global = true, default_value_t = LevelFilter::Error, value_parser = clap::builder::PossibleValuesParser::new(["TRACE", "DEBUG", "INFO", "WARN", "ERROR"]).map(|s| s.parse::().unwrap()),)] + pub log_level: LevelFilter, +} + +#[derive(Subcommand)] pub enum Commands { /// Shows the current xml wicked configuration Show { /// Where wicked xml configs are located - path: String + path: String, }, /// Migrate wicked state at path Migrate { /// Where wicked xml configs are located - path: String + path: String, }, } @@ -59,16 +69,16 @@ impl Termination for CliResult { #[async_std::main] async fn main() -> CliResult { - simplelog::TermLogger::init( - LevelFilter::Info, - simplelog::Config::default(), - simplelog::TerminalMode::Stderr, - simplelog::ColorChoice::Auto, - ) - .unwrap(); - let cli = Cli::parse(); + simplelog::TermLogger::init( + cli.global_opts.log_level, + simplelog::Config::default(), + simplelog::TerminalMode::Stderr, + simplelog::ColorChoice::Auto, + ) + .unwrap(); + if let Err(error) = run_command(cli).await { eprintln!("{:?}", error); return CliResult::Error; From 03319df24199141462aec39c0557c5ec8954560a Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Wed, 4 Oct 2023 13:57:05 +0200 Subject: [PATCH 4/6] Add format options to show command --- rust/Cargo.lock | 2 ++ rust/agama-migrate-wicked/Cargo.toml | 2 ++ rust/agama-migrate-wicked/src/main.rs | 23 +++++++++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 9ec612dffc..5a72b8489f 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -106,6 +106,8 @@ dependencies = [ "quick-xml", "regex", "serde", + "serde_json", + "serde_yaml", "simplelog", ] diff --git a/rust/agama-migrate-wicked/Cargo.toml b/rust/agama-migrate-wicked/Cargo.toml index 4b243e3c63..3d54e1dcea 100644 --- a/rust/agama-migrate-wicked/Cargo.toml +++ b/rust/agama-migrate-wicked/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" [dependencies] serde = { version = "1.0.152", features = ["derive"] } +serde_json = "1.0.91" +serde_yaml = "0.9.17" quick-xml = { version = "0.28.2", features = ["serialize"] } agama-lib = { path="../agama-lib" } regex = "1.9.5" diff --git a/rust/agama-migrate-wicked/src/main.rs b/rust/agama-migrate-wicked/src/main.rs index e7dd5bdeb9..52a153ce19 100644 --- a/rust/agama-migrate-wicked/src/main.rs +++ b/rust/agama-migrate-wicked/src/main.rs @@ -29,6 +29,10 @@ struct GlobalOpts { pub enum Commands { /// Shows the current xml wicked configuration Show { + /// Format output + #[arg(value_enum, short, long, default_value_t = Format::Json)] + format: Format, + /// Where wicked xml configs are located path: String, }, @@ -39,11 +43,26 @@ pub enum Commands { }, } +/// Supported output formats +#[derive(clap::ValueEnum, Clone)] +pub enum Format { + Json, + PrettyJson, + Yaml, + Text, +} + async fn run_command(cli: Cli) -> anyhow::Result<()> { match cli.command { - Commands::Show { path } => { + Commands::Show { path, format } => { let interfaces = wicked_read(path.into()).await?; - println!("{:?}", interfaces); + let output: String = match format { + Format::Json => serde_json::to_string(&interfaces)?, + Format::PrettyJson => serde_json::to_string_pretty(&interfaces)?, + Format::Yaml => serde_yaml::to_string(&interfaces)?, + Format::Text => format!("{:?}", interfaces), + }; + println!("{}", output); Ok(()) } Commands::Migrate { path } => { From e4ba1f0950f25a15456a537b5ca00529a6865edf Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Wed, 4 Oct 2023 17:41:39 +0200 Subject: [PATCH 5/6] Create README.md for migrate-wicked --- rust/agama-migrate-wicked/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 rust/agama-migrate-wicked/README.md diff --git a/rust/agama-migrate-wicked/README.md b/rust/agama-migrate-wicked/README.md new file mode 100644 index 0000000000..193fc08218 --- /dev/null +++ b/rust/agama-migrate-wicked/README.md @@ -0,0 +1,11 @@ +# Migrate wicked +This project creates a `migrate-wicked` binary which is able to parse wicked xml configs and send them to a NetworkManager dbus service. +## Architecture +`migrate-wicked` uses agama as a library to communicate the parsed network state to NetworkManager +but the binary is completely independent of any agama services and can be run standalone +## Obtaining wicked xml config +A wicked config xml can be generated by running `wicked show-config > wicked.xml` which can then be passed to `migrate-wicked` +## Testing +Running the migration on a live system isn't currently recommended. To test it's recommended to use a container +(instructions how to set one up can be found [here](https://github.com/openSUSE/agama/blob/master/rust/agama-cli/doc/backend-for-testing.md)) +Example configurations can be found under `tests` From bddd7350e63886525abc82fa330f26e4f48fdac0 Mon Sep 17 00:00:00 2001 From: Jorik Cronenberg Date: Wed, 4 Oct 2023 18:25:58 +0200 Subject: [PATCH 6/6] Better description for path argument --- rust/agama-migrate-wicked/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/agama-migrate-wicked/src/main.rs b/rust/agama-migrate-wicked/src/main.rs index 52a153ce19..81eb00abf3 100644 --- a/rust/agama-migrate-wicked/src/main.rs +++ b/rust/agama-migrate-wicked/src/main.rs @@ -33,12 +33,12 @@ pub enum Commands { #[arg(value_enum, short, long, default_value_t = Format::Json)] format: Format, - /// Where wicked xml configs are located + /// Path to file or directory where the wicked xml config is located path: String, }, /// Migrate wicked state at path Migrate { - /// Where wicked xml configs are located + /// Path to file or directory where the wicked xml config is located path: String, }, }