Generate cloud init disk image
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				continuous-integration/drone/push Build is passing
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			This commit is contained in:
		@@ -109,6 +109,28 @@ pub async fn get_single_src_def(client: LibVirtReq, id: web::Path<SingleVMUUidRe
 | 
			
		||||
        .body(info))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Get the generated cloud init configuration disk of a vm
 | 
			
		||||
pub async fn get_cloud_init_disk(client: LibVirtReq, id: web::Path<SingleVMUUidReq>) -> HttpResult {
 | 
			
		||||
    let info = match client.get_single_domain(id.uid).await {
 | 
			
		||||
        Ok(i) => i,
 | 
			
		||||
        Err(e) => {
 | 
			
		||||
            log::error!("Failed to get domain information! {e}");
 | 
			
		||||
            return Ok(HttpResponse::InternalServerError().json(e.to_string()));
 | 
			
		||||
        }
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let vm = VMInfo::from_domain(info)?;
 | 
			
		||||
    let disk = vm.cloud_init.generate_nocloud_disk()?;
 | 
			
		||||
 | 
			
		||||
    Ok(HttpResponse::Ok()
 | 
			
		||||
        .content_type("application/x-iso9660-image")
 | 
			
		||||
        .insert_header((
 | 
			
		||||
            "Content-Disposition",
 | 
			
		||||
            format!("attachment; filename=\"cloud_init_{}.iso\"", vm.name),
 | 
			
		||||
        ))
 | 
			
		||||
        .body(disk))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Update a VM information
 | 
			
		||||
pub async fn update(
 | 
			
		||||
    client: LibVirtReq,
 | 
			
		||||
 
 | 
			
		||||
@@ -202,6 +202,10 @@ async fn main() -> std::io::Result<()> {
 | 
			
		||||
                "/api/vm/{uid}/src",
 | 
			
		||||
                web::get().to(vm_controller::get_single_src_def),
 | 
			
		||||
            )
 | 
			
		||||
            .route(
 | 
			
		||||
                "/api/vm/{uid}/cloud_init_disk",
 | 
			
		||||
                web::get().to(vm_controller::get_cloud_init_disk),
 | 
			
		||||
            )
 | 
			
		||||
            .route(
 | 
			
		||||
                "/api/vm/{uid}/autostart",
 | 
			
		||||
                web::get().to(vm_controller::get_autostart),
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,7 @@
 | 
			
		||||
use crate::app_config::AppConfig;
 | 
			
		||||
use crate::constants;
 | 
			
		||||
use std::process::Command;
 | 
			
		||||
 | 
			
		||||
/// VM Cloud Init configuration
 | 
			
		||||
///
 | 
			
		||||
/// RedHat documentation: https://docs.redhat.com/fr/documentation/red_hat_enterprise_linux/9/html/configuring_and_managing_cloud-init_for_rhel_9/configuring-cloud-init_cloud-content
 | 
			
		||||
@@ -17,3 +21,55 @@ pub struct CloudInitConfig {
 | 
			
		||||
    #[serde(skip_serializing_if = "Option::is_none")]
 | 
			
		||||
    network_configuration: Option<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl CloudInitConfig {
 | 
			
		||||
    /// Generate disk image for nocloud usage
 | 
			
		||||
    pub fn generate_nocloud_disk(&self) -> anyhow::Result<Vec<u8>> {
 | 
			
		||||
        let temp_path = tempfile::tempdir_in(&AppConfig::get().temp_dir)?;
 | 
			
		||||
 | 
			
		||||
        let mut cmd = Command::new(constants::PROGRAM_CLOUD_LOCALDS);
 | 
			
		||||
 | 
			
		||||
        // ISO destination path
 | 
			
		||||
        let temp_iso = temp_path.path().join("disk.iso");
 | 
			
		||||
        cmd.arg(&temp_iso);
 | 
			
		||||
 | 
			
		||||
        // Process network configuration
 | 
			
		||||
        if let Some(net_conf) = &self.network_configuration {
 | 
			
		||||
            let net_conf_path = temp_path.path().join("network");
 | 
			
		||||
            std::fs::write(&net_conf_path, net_conf)?;
 | 
			
		||||
            cmd.arg("--network-config").arg(&net_conf_path);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Process user data
 | 
			
		||||
        let user_data_path = temp_path.path().join("user-data");
 | 
			
		||||
        std::fs::write(&user_data_path, &self.user_data)?;
 | 
			
		||||
        cmd.arg(user_data_path);
 | 
			
		||||
 | 
			
		||||
        // Process metadata
 | 
			
		||||
        let mut metadatas = vec![];
 | 
			
		||||
        if let Some(inst_id) = &self.instance_id {
 | 
			
		||||
            metadatas.push(format!("instance-id: {}", inst_id));
 | 
			
		||||
        }
 | 
			
		||||
        if let Some(local_hostname) = &self.local_hostname {
 | 
			
		||||
            metadatas.push(format!("local-hostname: {}", local_hostname));
 | 
			
		||||
        }
 | 
			
		||||
        let meta_data_path = temp_path.path().join("meta-data");
 | 
			
		||||
        std::fs::write(&meta_data_path, metadatas.join("\n"))?;
 | 
			
		||||
        cmd.arg(meta_data_path);
 | 
			
		||||
 | 
			
		||||
        // Execute command
 | 
			
		||||
        let output = cmd.output()?;
 | 
			
		||||
        if !output.status.success() {
 | 
			
		||||
            anyhow::bail!(
 | 
			
		||||
                "{} exited with status {}!\nStdout: {}\nStderr: {}",
 | 
			
		||||
                constants::PROGRAM_CLOUD_LOCALDS,
 | 
			
		||||
                output.status,
 | 
			
		||||
                String::from_utf8_lossy(&output.stdout),
 | 
			
		||||
                String::from_utf8_lossy(&output.stderr)
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Read generated ISO file
 | 
			
		||||
        Ok(std::fs::read(temp_iso)?)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user