Skip to content

Commit ec1a8f6

Browse files
committed
move mount_parse_ovf_env to libazureinit/media
As mount_parse_ovf_env is a helper for media module, it makes sense to move the function to libazureinit/media.
1 parent d631c59 commit ec1a8f6

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

libazureinit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ serde_json = "1.0.96"
2020
nix = {version = "0.28.0", features = ["fs", "user"]}
2121
libc = "0.2.146"
2222
block-utils = "0.11.1"
23+
anyhow = "1.0.86"
2324

2425
[dev-dependencies]
2526
tempfile = "3"

libazureinit/src/media.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use std::os::unix::fs::PermissionsExt;
99
use std::path::PathBuf;
1010
use std::process::Command;
1111

12+
use anyhow::Context;
13+
1214
use serde::Deserialize;
1315
use serde_xml_rs::from_str;
1416

@@ -187,6 +189,24 @@ pub fn parse_ovf_env(ovf_body: &str) -> Result<Environment, Error> {
187189
}
188190
}
189191

192+
// Mount the given device, get OVF environment data, return it.
193+
pub fn mount_parse_ovf_env(dev: String) -> Result<Environment, anyhow::Error> {
194+
let mount_media =
195+
Media::new(PathBuf::from(dev), PathBuf::from(PATH_MOUNT_POINT));
196+
let mounted = mount_media
197+
.mount()
198+
.with_context(|| "Failed to mount media.")?;
199+
200+
let ovf_body = mounted.read_ovf_env_to_string()?;
201+
let environment = parse_ovf_env(ovf_body.as_str())?;
202+
203+
mounted
204+
.unmount()
205+
.with_context(|| "Failed to remove media.")?;
206+
207+
Ok(environment)
208+
}
209+
190210
#[cfg(test)]
191211
mod tests {
192212
use super::*;

src/main.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use std::path::PathBuf;
54
use std::process::ExitCode;
65

76
use anyhow::Context;
@@ -11,38 +10,20 @@ use libazureinit::imds::InstanceMetadata;
1110
use libazureinit::{
1211
error::Error as LibError,
1312
goalstate, imds, media,
14-
media::{Environment, Media},
13+
media::Environment,
1514
reqwest::{header, Client},
1615
user,
1716
};
1817

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

21-
// Mount the given device, get OVF environment data, return it.
22-
fn mount_parse_ovf_env(dev: String) -> Result<Environment, anyhow::Error> {
23-
let mount_media =
24-
Media::new(PathBuf::from(dev), PathBuf::from(media::PATH_MOUNT_POINT));
25-
let mounted = mount_media
26-
.mount()
27-
.with_context(|| "Failed to mount media.")?;
28-
29-
let ovf_body = mounted.read_ovf_env_to_string()?;
30-
let environment = media::parse_ovf_env(ovf_body.as_str())?;
31-
32-
mounted
33-
.unmount()
34-
.with_context(|| "Failed to remove media.")?;
35-
36-
Ok(environment)
37-
}
38-
3920
fn get_environment() -> Result<Environment, anyhow::Error> {
4021
let ovf_devices = media::get_mount_device()?;
4122
let mut environment: Option<Environment> = None;
4223

4324
// loop until it finds a correct device.
4425
for dev in ovf_devices {
45-
environment = match mount_parse_ovf_env(dev) {
26+
environment = match media::mount_parse_ovf_env(dev) {
4627
Ok(env) => Some(env),
4728
Err(_) => continue,
4829
}

0 commit comments

Comments
 (0)