Mohammed Naser | f0056fd | 2025-02-16 13:18:14 -0500 | [diff] [blame] | 1 | mod config; |
| 2 | |
| 3 | use clap::Parser; |
| 4 | use env_logger::Env; |
| 5 | use log::error; |
| 6 | use rtnetlink::Handle; |
| 7 | use std::{path::PathBuf, process}; |
| 8 | |
| 9 | #[derive(Parser, Debug)] |
| 10 | #[command(version, about, long_about = None)] |
| 11 | struct Cli { |
| 12 | #[arg(default_value = "/tmp/auto_bridge_add", help = "Path to the JSON file")] |
| 13 | config: PathBuf, |
| 14 | } |
| 15 | |
| 16 | #[tokio::main] |
| 17 | async fn main() { |
| 18 | let cli = Cli::parse(); |
| 19 | |
| 20 | let env = Env::default() |
| 21 | .filter_or("MY_LOG_LEVEL", "info") |
| 22 | .write_style_or("MY_LOG_STYLE", "always"); |
| 23 | env_logger::init_from_env(env); |
| 24 | |
| 25 | let network_config = match config::NetworkConfig::from_path(&cli.config) { |
| 26 | Ok(network_config) => network_config, |
| 27 | Err(e) => { |
| 28 | error!("Failed to load network config: {}", e); |
| 29 | |
| 30 | process::exit(1); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | let (connection, handle, _) = rtnetlink::new_connection().expect("Failed to create connection"); |
| 35 | tokio::spawn(connection); |
| 36 | |
| 37 | for (bridge_name, interface_name) in network_config.bridges_with_interfaces_iter() { |
| 38 | let interface = get_interface(&handle, interface_name).await; |
| 39 | let bridge = get_interface(&handle, bridge_name).await; |
| 40 | |
| 41 | if let Err(e) = bridge.migrate_from_interface(&handle, &interface).await { |
| 42 | error!( |
| 43 | "Failed to migrate from {} to {}: {}", |
| 44 | interface_name, bridge_name, e |
| 45 | ); |
| 46 | process::exit(1); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | async fn get_interface(handle: &Handle, name: &str) -> ovsinit::Interface { |
| 52 | match ovsinit::Interface::new(handle, name.to_string()).await { |
| 53 | Ok(interface) => interface, |
| 54 | Err(ovsinit::InterfaceError::NotFound(name)) => { |
| 55 | error!(interface = name.as_str(); "Interface not found."); |
| 56 | process::exit(1); |
| 57 | } |
| 58 | Err(e) => { |
| 59 | error!(error = e.to_string().as_str(); "Failed to lookup interface."); |
| 60 | process::exit(1); |
| 61 | } |
| 62 | } |
| 63 | } |