Show guidelines on UI on how to setup network hook
This commit is contained in:
		@@ -86,3 +86,6 @@ pub const STORAGE_NAT_DIR: &str = "nat";
 | 
			
		||||
 | 
			
		||||
/// Environment variable that is set to run VirtWeb in NAT configuration mode
 | 
			
		||||
pub const NAT_MODE_ENV_VAR_NAME: &str = "NAT_MODE";
 | 
			
		||||
 | 
			
		||||
/// Nat hook file path
 | 
			
		||||
pub const NAT_HOOK_PATH: &str = "/etc/libvirt/hooks/network";
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ use crate::constants::{DISK_NAME_MAX_LEN, DISK_NAME_MIN_LEN, DISK_SIZE_MAX, DISK
 | 
			
		||||
use crate::controllers::{HttpResult, LibVirtReq};
 | 
			
		||||
use crate::extractors::local_auth_extractor::LocalAuthEnabled;
 | 
			
		||||
use crate::libvirt_rest_structures::hypervisor::HypervisorInfo;
 | 
			
		||||
use crate::nat::nat_hook;
 | 
			
		||||
use crate::utils::net_utils;
 | 
			
		||||
use actix_web::{HttpResponse, Responder};
 | 
			
		||||
use sysinfo::{System, SystemExt};
 | 
			
		||||
@@ -120,6 +121,21 @@ pub async fn server_info(client: LibVirtReq) -> HttpResult {
 | 
			
		||||
    }))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(serde::Serialize)]
 | 
			
		||||
struct NetworkHookStatus {
 | 
			
		||||
    installed: bool,
 | 
			
		||||
    content: String,
 | 
			
		||||
    path: &'static str,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn network_hook_status() -> HttpResult {
 | 
			
		||||
    Ok(HttpResponse::Ok().json(NetworkHookStatus {
 | 
			
		||||
        installed: nat_hook::is_installed()?,
 | 
			
		||||
        content: nat_hook::hook_content()?,
 | 
			
		||||
        path: constants::NAT_HOOK_PATH,
 | 
			
		||||
    }))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn number_vcpus() -> HttpResult {
 | 
			
		||||
    let mut system = System::new();
 | 
			
		||||
    system.refresh_cpu();
 | 
			
		||||
 
 | 
			
		||||
@@ -118,6 +118,10 @@ async fn main() -> std::io::Result<()> {
 | 
			
		||||
                "/api/server/info",
 | 
			
		||||
                web::get().to(server_controller::server_info),
 | 
			
		||||
            )
 | 
			
		||||
            .route(
 | 
			
		||||
                "/api/server/network_hook_status",
 | 
			
		||||
                web::get().to(server_controller::network_hook_status),
 | 
			
		||||
            )
 | 
			
		||||
            .route(
 | 
			
		||||
                "/api/server/number_vcpus",
 | 
			
		||||
                web::get().to(server_controller::number_vcpus),
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,4 @@
 | 
			
		||||
pub mod nat_conf_mode;
 | 
			
		||||
pub mod nat_definition;
 | 
			
		||||
pub mod nat_hook;
 | 
			
		||||
pub mod nat_lib;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										29
									
								
								virtweb_backend/src/nat/nat_hook.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								virtweb_backend/src/nat/nat_hook.rs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
use crate::app_config::AppConfig;
 | 
			
		||||
use crate::constants;
 | 
			
		||||
use std::path::Path;
 | 
			
		||||
 | 
			
		||||
/// Check out whether NAT hook has been installed or not
 | 
			
		||||
pub fn is_installed() -> anyhow::Result<bool> {
 | 
			
		||||
    let hook_file = Path::new(constants::NAT_HOOK_PATH);
 | 
			
		||||
 | 
			
		||||
    if !hook_file.exists() {
 | 
			
		||||
        return Ok(false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let exe = std::env::current_exe()?;
 | 
			
		||||
    let hook_content = std::fs::read_to_string(hook_file)?;
 | 
			
		||||
 | 
			
		||||
    Ok(hook_content.contains(exe.to_string_lossy().as_ref()))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get nat hook expected content
 | 
			
		||||
pub fn hook_content() -> anyhow::Result<String> {
 | 
			
		||||
    let exe = std::env::current_exe()?;
 | 
			
		||||
 | 
			
		||||
    Ok(format!(
 | 
			
		||||
        "#!/bin/bash\n\
 | 
			
		||||
    {} --storage {} --network-name \"$1\" --operation \"$2\" --sub-operation \"$3\"",
 | 
			
		||||
        exe.to_string_lossy(),
 | 
			
		||||
        AppConfig::get().storage
 | 
			
		||||
    ))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user