Skip to content

Commit

Permalink
Added possibility of output info to json file for easier integration …
Browse files Browse the repository at this point in the history
…with scripts
  • Loading branch information
scx1332 authored Jul 1, 2024
1 parent dfdb101 commit e44f269
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gvmkit-build"
version = "0.3.18"
version = "0.3.19"
description = "Tool used to build gvmi images used by Golem Network. Companion app to Golem Registry"
authors = ["Golem Factory <contact@golem.network>"]
edition = "2021"
Expand Down
44 changes: 33 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::image::{ImageBuilder, ImageName};
use clap::Parser;
use std::path::PathBuf;

use serde_json::json;
use std::env;

const COMPRESSION_POSSIBLE_VALUES: &[&str] = &["lzo", "gzip", "lz4", "zstd", "xz"];
Expand Down Expand Up @@ -76,6 +77,8 @@ struct CmdArgs {
/// Hide progress bars during operation
#[arg(help_heading = Some("Extra options"), long)]
hide_progress: bool,
#[arg(help_heading = Some("Write json info to info.json"), long)]
extra_json_info: bool,
}
use tokio::fs;

Expand Down Expand Up @@ -305,7 +308,7 @@ async fn main() -> anyhow::Result<()> {
}
};

if cmdargs.push || cmdargs.push_to.is_some() {
let repo_info = if cmdargs.push || cmdargs.push_to.is_some() {
println!(
"Uploading image to golem registry: {}",
REGISTRY_URL.as_str()
Expand All @@ -315,29 +318,48 @@ async fn main() -> anyhow::Result<()> {
if full_upload_needed {
if let Some(push_image_name) = &push_image_name {
//check if we can attach to the repo before uploading the file
attach_to_repo(
let _repo_info = attach_to_repo(
&descr.get_descr_hash_str(),
push_image_name,
&user_name,
&pat,
true,
)
.await?;
}
};
full_upload(&path, &descr, cmdargs.upload_workers).await?;
}

if let Some(push_image_name) = &push_image_name {
//attach to repo after upload
attach_to_repo(
&descr.get_descr_hash_str(),
push_image_name,
&user_name,
&pat,
false,
Some(
attach_to_repo(
&descr.get_descr_hash_str(),
push_image_name,
&user_name,
&pat,
false,
)
.await?,
)
.await?;
} else {
None
}
} else {
None
};
// write info to file
if cmdargs.extra_json_info {
println!(" * Writing info to info.json");
let repo_info_path = PathBuf::from("info.json");
let mut file = File::create(&repo_info_path).await?;
file.write_all(&serde_json::to_vec_pretty(&json!({
"repoInfo": &repo_info,
"imagePath": path.display().to_string(),
"imageDescrPath": descr_path.display().to_string(),
"hash": &descr.get_descr_hash_str(),
}))?)
.await?;
}

Ok(())
}
20 changes: 17 additions & 3 deletions src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::time::{Duration, Instant};

use once_cell::sync::Lazy;
use reqwest::{multipart, Body};
use serde::{Deserialize, Serialize};
use serde_json::json;
use sha2::{Digest, Sha256};
use tokio::io::AsyncReadExt;
Expand Down Expand Up @@ -86,13 +87,21 @@ pub async fn check_login(user_name: &str, pat: &str) -> anyhow::Result<bool> {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttachInfo {
pub url: String,
pub user: String,
pub repo: String,
pub tag: String,
}

pub async fn attach_to_repo(
descr_sha256: &str,
image_name: &ImageName,
login: &str,
pat: &str,
check: bool,
) -> anyhow::Result<()> {
) -> anyhow::Result<AttachInfo> {
let Some(image_user_name) = image_name.user.clone() else {
return Err(anyhow::anyhow!("Image name must contain user"));
};
Expand All @@ -101,7 +110,7 @@ pub async fn attach_to_repo(
let add_tag_endpoint = format!("{repo_url}/v1/image/descr/attach/{descr_sha256}");
let form = multipart::Form::new();
let form = form.text("tag", image_name.tag.clone());
let form = form.text("username", image_user_name);
let form = form.text("username", image_user_name.clone());
let form = form.text("repository", image_name.repository.clone());
let form = form.text("login", login.to_string());
let form = form.text("token", pat.to_string());
Expand Down Expand Up @@ -145,7 +154,12 @@ pub async fn attach_to_repo(
} else {
println!(" -- success: {}", text);
}
Ok(())
Ok(AttachInfo {
url: repo_url.to_string(),
repo: image_name.repository.clone(),
tag: image_name.tag.clone(),
user: image_user_name,
})
}

//returns if full upload is needed
Expand Down

0 comments on commit e44f269

Please sign in to comment.