Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ description = "A reference implementation for provisioning Linux VMs on Azure."
exitcode = "1.1.2"
anyhow = "1.0.81"
tokio = { version = "1", features = ["full"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing = "0.1.40"

[dependencies.libazureinit]
path = "libazureinit"
Expand Down
10 changes: 10 additions & 0 deletions libazureinit/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde::Deserialize;
use serde_xml_rs::from_str;

use tracing;
use tracing::instrument;

use crate::error::Error;

Expand Down Expand Up @@ -76,6 +77,7 @@ pub const PATH_MOUNT_POINT: &str = "/run/azure-init/media/";
const CDROM_VALID_FS: &[&str] = &["iso9660", "udf"];

// Get a mounted device with any filesystem for CDROM
#[instrument]
pub fn get_mount_device() -> Result<Vec<String>, Error> {
let mut list_devices: Vec<String> = Vec::new();

Expand All @@ -90,9 +92,12 @@ pub fn get_mount_device() -> Result<Vec<String>, Error> {
}

// Some zero-sized structs that just provide states for our state machine
#[derive(Debug)]
pub struct Mounted;
#[derive(Debug)]
pub struct Unmounted;

#[derive(Debug)]
pub struct Media<State = Unmounted> {
device_path: PathBuf,
mount_path: PathBuf,
Expand All @@ -108,6 +113,7 @@ impl Media<Unmounted> {
}
}

#[instrument]
pub fn mount(self) -> Result<Media<Mounted>, Error> {
create_dir_all(&self.mount_path)?;

Expand Down Expand Up @@ -140,6 +146,7 @@ impl Media<Unmounted> {
}

impl Media<Mounted> {
#[instrument]
pub fn unmount(self) -> Result<(), Error> {
let umount_status =
Command::new("umount").arg(self.mount_path).status()?;
Expand All @@ -162,6 +169,7 @@ impl Media<Mounted> {
}
}

#[instrument]
pub fn read_ovf_env_to_string(&self) -> Result<String, Error> {
let mut file_path = self.mount_path.clone();
file_path.push("ovf-env.xml");
Expand All @@ -174,6 +182,7 @@ impl Media<Mounted> {
}
}

#[instrument(skip_all)]
pub fn parse_ovf_env(ovf_body: &str) -> Result<Environment, Error> {
let environment: Environment = from_str(ovf_body)?;

Expand All @@ -190,6 +199,7 @@ pub fn parse_ovf_env(ovf_body: &str) -> Result<Environment, Error> {
}

// Mount the given device, get OVF environment data, return it.
#[instrument(skip_all)]
pub fn mount_parse_ovf_env(dev: String) -> Result<Environment, Error> {
let mount_media =
Media::new(PathBuf::from(dev), PathBuf::from(PATH_MOUNT_POINT));
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ use libazureinit::{
reqwest::{header, Client},
HostnameProvisioner, PasswordProvisioner, Provision, UserProvisioner,
};
use tracing::instrument;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::EnvFilter;

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[instrument]
fn get_environment() -> Result<Environment, anyhow::Error> {
let ovf_devices = media::get_mount_device()?;
let mut environment: Option<Environment> = None;
Expand All @@ -33,6 +38,7 @@ fn get_environment() -> Result<Environment, anyhow::Error> {
.ok_or_else(|| anyhow::anyhow!("Unable to get list of block devices"))
}

#[instrument(skip_all)]
fn get_username(
instance_metadata: Option<&InstanceMetadata>,
environment: Option<&Environment>,
Expand Down Expand Up @@ -60,6 +66,17 @@ fn get_username(

#[tokio::main]
async fn main() -> ExitCode {
let stderr = tracing_subscriber::fmt::layer()
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.with_writer(std::io::stderr);
let registry = tracing_subscriber::registry()
.with(stderr)
.with(EnvFilter::from_env("AZURE_INIT_LOG"));
tracing::subscriber::set_global_default(registry).expect(
"Only an application should set the global default; \
a library is mis-using the tracing API.",
);

match provision().await {
Ok(_) => ExitCode::SUCCESS,
Err(e) => {
Expand All @@ -78,6 +95,7 @@ async fn main() -> ExitCode {
}
}

#[instrument]
async fn provision() -> Result<(), anyhow::Error> {
let mut default_headers = header::HeaderMap::new();
let user_agent = header::HeaderValue::from_str(
Expand Down