diff --git a/libazureinit/Cargo.toml b/libazureinit/Cargo.toml index bc3a28fb..3bab5eac 100644 --- a/libazureinit/Cargo.toml +++ b/libazureinit/Cargo.toml @@ -2,6 +2,7 @@ name = "libazureinit" version = "0.1.1" edition = "2021" +rust-version = "1.76" repository = "https://github.com/Azure/azure-init/" homepage = "https://github.com/Azure/azure-init/" license = "MIT" @@ -20,6 +21,7 @@ serde_json = "1.0.96" nix = {version = "0.28.0", features = ["fs", "user"]} libc = "0.2.146" block-utils = "0.11.1" +tracing = "0.1.40" [dev-dependencies] tempfile = "3" diff --git a/libazureinit/src/media.rs b/libazureinit/src/media.rs index ff725ad1..76b6aabe 100644 --- a/libazureinit/src/media.rs +++ b/libazureinit/src/media.rs @@ -12,6 +12,8 @@ use std::process::Command; use serde::Deserialize; use serde_xml_rs::from_str; +use tracing; + use crate::error::Error; #[derive(Debug, Default, Deserialize, PartialEq, Clone)] @@ -187,6 +189,24 @@ pub fn parse_ovf_env(ovf_body: &str) -> Result { } } +// Mount the given device, get OVF environment data, return it. +pub fn mount_parse_ovf_env(dev: String) -> Result { + let mount_media = + Media::new(PathBuf::from(dev), PathBuf::from(PATH_MOUNT_POINT)); + let mounted = mount_media.mount().inspect_err( + |e| tracing::error!(error = ?e, "Failed to mount media."), + )?; + + let ovf_body = mounted.read_ovf_env_to_string()?; + let environment = parse_ovf_env(ovf_body.as_str())?; + + mounted.unmount().inspect_err( + |e| tracing::error!(error = ?e, "Failed to remove media."), + )?; + + Ok(environment) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index 40ab4014..daf985be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -use std::path::PathBuf; use std::process::ExitCode; use anyhow::Context; @@ -11,33 +10,32 @@ use libazureinit::imds::InstanceMetadata; use libazureinit::{ error::Error as LibError, goalstate, imds, media, - media::{Environment, Media}, + media::Environment, reqwest::{header, Client}, user, }; const VERSION: &str = env!("CARGO_PKG_VERSION"); -// Mount the given device, get OVF environment data, return it. -fn mount_parse_ovf_env(dev: String) -> Result { - let mount_media = - Media::new(PathBuf::from(dev), PathBuf::from(media::PATH_MOUNT_POINT)); - let mounted = mount_media - .mount() - .with_context(|| "Failed to mount media.")?; +fn get_environment() -> Result { + let ovf_devices = media::get_mount_device()?; + let mut environment: Option = None; - let ovf_body = mounted.read_ovf_env_to_string()?; - let environment = media::parse_ovf_env(ovf_body.as_str())?; - - mounted - .unmount() - .with_context(|| "Failed to remove media.")?; + // loop until it finds a correct device. + for dev in ovf_devices { + environment = match media::mount_parse_ovf_env(dev) { + Ok(env) => Some(env), + Err(_) => continue, + } + } - Ok(environment) + environment + .ok_or_else(|| anyhow::anyhow!("Unable to get list of block devices")) } fn get_username( instance_metadata: &InstanceMetadata, + environment: &Environment, ) -> Result { if instance_metadata .compute @@ -49,22 +47,8 @@ fn get_username( } else { // password authentication is enabled - // list of CDROM devices that is available with possible filesystems. - let ovf_devices = media::get_mount_device()?; - let mut environment: Option = None; - - // loop until it finds a correct device. - for dev in ovf_devices { - environment = match mount_parse_ovf_env(dev) { - Ok(env) => Some(env), - Err(_) => continue, - } - } - Ok(environment - .ok_or_else(|| { - anyhow::anyhow!("Unable to get list of block devices") - })? + .clone() .provisioning_section .linux_prov_conf_set .username) @@ -103,7 +87,7 @@ async fn provision() -> Result<(), anyhow::Error> { .build()?; let instance_metadata = imds::query(&client).await?; - let username = get_username(&instance_metadata)?; + let username = get_username(&instance_metadata, &get_environment()?)?; let mut file_path = "/home/".to_string(); file_path.push_str(username.as_str());