Skip to content

Commit

Permalink
update to the latest Azure SDK for Rust (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmc-msft authored Nov 15, 2021
1 parent 8643a46 commit 2505d66
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 1,139 deletions.
1,464 changes: 498 additions & 966 deletions Cargo.lock

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,30 @@ categories = ["command-line-utilities"]
homepage = "https://github.com/microsoft/avml"
repository = "https://github.com/microsoft/avml"
readme = "README.md"
edition = "2018"
edition = "2021"

[features]
default = ["put", "blobstore"]
put = ["reqwest"]
blobstore = ["azure", "retry", "tokio-core", "url", "azure_sdk_core", "azure_sdk_storage_core"]
put = ["reqwest", "url", "tokio"]
blobstore = ["url", "azure_core", "azure_storage", "tokio", "tokio-util", "backoff"]

[dependencies]
bytes = "1.1"
elf = "0.0.10"
byteorder = "1.3.2"
argh = "0.1.6"
snap = "0.2.5"
url = { version = "2.1.0", optional = true }
tokio-core = { version = "0.1.17", optional = true }
retry = { version = "0.5.1", optional = true }
reqwest = { version = "0.9.19", default-features = false, features = ["rustls-tls"], optional = true }
azure = { version = "0.23.1", package = "azure_sdk_storage_blob", optional = true}
azure_sdk_core = { version = "0.20.3", optional = true }
azure_sdk_storage_core = { version = "0.20.4", optional = true }
byteorder = "1.3"
argh = "0.1"
snap = "1.0"
futures = "0.3"
url = { version = "2.2", optional = true }
tokio-util = { version = "0.6", features = ["codec"] , optional = true }
tokio = { version = "1.0", default-features = false, optional = true, features = ["fs"] }
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"], optional = true}
anyhow = "1.0"
md5 = "0.7"
backoff = { version = "0.3", features = ["tokio"], optional = true}

azure_storage = { git = "https://github.com/azure/azure-sdk-for-rust", default-features = false, features = ["enable_reqwest_rustls", "account", "blob"], optional = true }
azure_core= { git = "https://github.com/azure/azure-sdk-for-rust", default-features= false, features = ["enable_reqwest_rustls"], optional = true }

[profile.release]
opt-level="z"
Expand Down
16 changes: 9 additions & 7 deletions src/bin/avml-convert.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

use anyhow::{bail, Result};
use anyhow::{bail, Error, Result};
use argh::FromArgs;
use avml::ONE_MB;
use snap::Reader;
use std::{convert::TryFrom, fs::metadata, io::prelude::*, io::SeekFrom, path::PathBuf};
use snap::read::FrameDecoder;
use std::{
convert::TryFrom, fs::metadata, io::prelude::*, io::SeekFrom, path::PathBuf, str::FromStr,
};

fn convert(src: PathBuf, dst: PathBuf, compress: bool) -> Result<()> {
let src_len = metadata(&src)?.len();
Expand All @@ -26,7 +28,7 @@ fn convert(src: PathBuf, dst: PathBuf, compress: bool) -> Result<()> {
avml::image::copy_block(new_header, &mut image.src, &mut image.dst)?;
}
2 => {
let mut reader = Reader::new(&image.src);
let mut reader = FrameDecoder::new(&image.src);
avml::image::copy_block(new_header, &mut reader, &mut image.dst)?;
image.src.seek(SeekFrom::Current(8))?;
}
Expand Down Expand Up @@ -68,7 +70,7 @@ fn convert_to_raw(src: PathBuf, dst: PathBuf) -> Result<()> {
avml::image::copy(size, &mut image.src, &mut image.dst)?;
}
2 => {
let mut reader = Reader::new(&image.src);
let mut reader = FrameDecoder::new(&image.src);
avml::image::copy(size, &mut reader, &mut image.dst)?;
image.src.seek(SeekFrom::Current(8))?;
}
Expand Down Expand Up @@ -105,8 +107,8 @@ enum Format {
LimeCompressed,
}

impl ::std::str::FromStr for Format {
type Err = anyhow::Error;
impl FromStr for Format {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let x = match s {
"raw" => Self::Raw,
Expand Down
59 changes: 37 additions & 22 deletions src/bin/avml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use anyhow::{anyhow, bail, Context, Result};
use argh::FromArgs;
#[cfg(feature = "blobstore")]
use avml::ONE_MB;
#[cfg(any(feature = "blobstore", feature = "put"))]
use std::fs::remove_file;
use std::{
fs::metadata,
ops::Range,
path::{Path, PathBuf},
str::FromStr,
};
#[cfg(any(feature = "blobstore", feature = "put"))]
use tokio::{fs::remove_file, runtime::Runtime};
#[cfg(any(feature = "blobstore", feature = "put"))]
use url::Url;

#[derive(FromArgs)]
/// A portable volatile memory acquisition tool for Linux
Expand All @@ -27,7 +30,7 @@ struct Config {
/// upload via HTTP PUT upon acquisition
#[cfg(feature = "put")]
#[argh(option)]
url: Option<reqwest::Url>,
url: Option<Url>,

/// delete upon successful upload
#[cfg(any(feature = "blobstore", feature = "put"))]
Expand All @@ -37,7 +40,7 @@ struct Config {
/// upload via Azure Blob Store upon acquisition
#[cfg(feature = "blobstore")]
#[argh(option)]
sas_url: Option<url::Url>,
sas_url: Option<Url>,

/// specify maximum block size in MiB
#[cfg(feature = "blobstore")]
Expand All @@ -55,7 +58,7 @@ enum Source {
ProcKcore,
}

impl ::std::str::FromStr for Source {
impl FromStr for Source {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let x = match s {
Expand All @@ -68,7 +71,7 @@ impl ::std::str::FromStr for Source {
}
}

fn kcore(ranges: &[std::ops::Range<u64>], filename: &Path, version: u32) -> Result<()> {
fn kcore(ranges: &[Range<u64>], filename: &Path, version: u32) -> Result<()> {
if metadata("/proc/kcore")?.len() < 0x2000 {
bail!("locked down kcore");
}
Expand Down Expand Up @@ -102,7 +105,7 @@ fn kcore(ranges: &[std::ops::Range<u64>], filename: &Path, version: u32) -> Resu
Ok(())
}

fn phys(ranges: &[std::ops::Range<u64>], filename: &Path, mem: &Path, version: u32) -> Result<()> {
fn phys(ranges: &[Range<u64>], filename: &Path, mem: &Path, version: u32) -> Result<()> {
let mut image = avml::image::Image::new(version, mem, filename).with_context(|| {
format!(
"unable to create image. source:{} destination:{}",
Expand Down Expand Up @@ -164,20 +167,16 @@ fn get_mem(src: Option<&Source>, dst: &Path, version: u32) -> Result<()> {
bail!("unable to read physical memory")
}

fn main() -> Result<()> {
let config: Config = argh::from_env();

let version = if config.compress { 2 } else { 1 };
get_mem(config.source.as_ref(), &config.filename, version)
.context("unable to acquire memory")?;

#[cfg(any(feature = "blobstore", feature = "put"))]
#[cfg(any(feature = "blobstore", feature = "put"))]
async fn upload(config: &Config) -> Result<()> {
let mut delete = false;

#[cfg(feature = "put")]
{
if let Some(url) = config.url {
avml::upload::put(&config.filename, url).context("unable to upload via PUT")?;
if let Some(url) = &config.url {
avml::upload::put(&config.filename, url)
.await
.context("unable to upload via PUT")?;
delete = true;
}
}
Expand All @@ -186,18 +185,34 @@ fn main() -> Result<()> {
{
let sas_block_size = config.sas_block_size * ONE_MB;

if let Some(sas_url) = config.sas_url {
avml::blobstore::upload_sas(&config.filename, &sas_url, sas_block_size)
if let Some(sas_url) = &config.sas_url {
avml::blobstore::upload_sas(&config.filename, sas_url, sas_block_size)
.await
.context("upload via sas URL failed")?;
delete = true;
}
}

if delete && config.delete {
remove_file(&config.filename)
.await
.context("unable to remove file after PUT")?;
}

Ok(())
}

fn main() -> Result<()> {
let config: Config = argh::from_env();

let version = if config.compress { 2 } else { 1 };
get_mem(config.source.as_ref(), &config.filename, version)
.context("unable to acquire memory")?;

#[cfg(any(feature = "blobstore", feature = "put"))]
{
if delete && config.delete {
remove_file(&config.filename).context("unable to remove file after PUT")?;
}
let rt = Runtime::new()?;
rt.block_on(upload(&config))?;
}

Ok(())
Expand Down
Loading

0 comments on commit 2505d66

Please sign in to comment.