Skip to content

Commit

Permalink
chore: ezkl self update (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto authored Jun 7, 2024
1 parent 685487c commit f5f8ef5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ num = "0.4.1"
portable-atomic = "1.6.0"
tosubcommand = { git = "https://github.com/zkonduit/enum_to_subcommand", package = "tosubcommand" }
metal = { git = "https://github.com/gfx-rs/metal-rs", optional = true }
semver = "1.0.22"

# evm related deps
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
4 changes: 4 additions & 0 deletions install_ezkl_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ fi
echo "Removing old ezkl binary if it exists"
[ -e file ] && rm file

# echo platform and architecture
echo "Platform: $PLATFORM"
echo "Architecture: $ARCHITECTURE"

# download the release and unpack the right tarball
if [ "$PLATFORM" == "windows-msvc" ]; then
JSON_RESPONSE=$(curl -s "$RELEASE_URL")
Expand Down
13 changes: 6 additions & 7 deletions src/bin/ezkl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ use rand::prelude::SliceRandom;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(feature = "icicle")]
use std::env;
#[cfg(not(target_arch = "wasm32"))]
use std::error::Error;

#[tokio::main(flavor = "current_thread")]
#[cfg(not(target_arch = "wasm32"))]
pub async fn main() -> Result<(), Box<dyn Error>> {
pub async fn main() {
let args = Cli::parse();

if let Some(generator) = args.generator {
ezkl::commands::print_completions(generator, &mut Cli::command());
Ok(())
} else if let Some(command) = args.command {
init_logger();
#[cfg(not(any(target_arch = "wasm32", feature = "no-banner")))]
Expand All @@ -38,15 +35,17 @@ pub async fn main() -> Result<(), Box<dyn Error>> {
} else {
info!("Running with CPU");
}
info!("command: \n {}", &command.as_json().to_colored_json_auto()?);
info!(
"command: \n {}",
&command.as_json().to_colored_json_auto().unwrap()
);
let res = run(command).await;
match &res {
Ok(_) => info!("succeeded"),
Err(e) => error!("failed: {}", e),
};
res.map(|_| ())
} else {
Err("No command provided".into())
error!("no command provided");
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,12 @@ pub enum Commands {
#[arg(long, value_hint = clap::ValueHint::Other)]
addr_vk: Option<H160Flag>,
},
/// Updates ezkl binary to version specified (or latest if not specified)
Update {
/// The version to update to
#[arg(value_hint = clap::ValueHint::Other, short='v', long)]
version: Option<String>,
},
}


Expand Down
46 changes: 46 additions & 0 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,52 @@ pub async fn run(command: Commands) -> Result<String, Box<dyn Error>> {
)
.await
}
Commands::Update { version } => update_ezkl_binary(&version).map(|e| e.to_string()),
}
}

/// Assert that the version is valid
fn assert_version_is_valid(version: &str) -> Result<(), Box<dyn Error>> {
let err_string = "Invalid version string. Must be in the format v0.0.0";
if version.is_empty() {
return Err(err_string.into());
}
// safe to unwrap since we know the length is not 0
if version.chars().nth(0).unwrap() != 'v' {
return Err(err_string.into());
}

semver::Version::parse(&version[1..])
.map_err(|_| "Invalid version string. Must be in the format v0.0.0")?;

Ok(())
}

const INSTALL_BYTES: &[u8] = include_bytes!("../install_ezkl_cli.sh");

fn update_ezkl_binary(version: &Option<String>) -> Result<String, Box<dyn Error>> {
// run the install script with the version
let install_script = std::str::from_utf8(INSTALL_BYTES)?;
// now run as sh script with the version as an argument
let mut command = std::process::Command::new("sh");
let mut command = command.arg("-c").arg(install_script);

if let Some(version) = version {
assert_version_is_valid(version)?;
command = command.arg(version)
};
let output = command.output()?;

if output.status.success() {
info!("updated binary");
Ok("".to_string())
} else {
Err(format!(
"failed to update binary: {}, {}",
std::str::from_utf8(&output.stdout)?,
std::str::from_utf8(&output.stderr)?
)
.into())
}
}

Expand Down

0 comments on commit f5f8ef5

Please sign in to comment.