Skip to content

Commit

Permalink
refactoring ECR handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mwu-tow committed Oct 13, 2022
1 parent a5a7c29 commit d25df55
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 68 deletions.
4 changes: 2 additions & 2 deletions build/build/src/aws/ecr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ide_ci::programs::docker;
pub mod runtime;



/// Lookup the repository by name.
#[instrument(skip(client))]
pub async fn resolve_repository(
client: &aws_sdk_ecr::Client,
Expand All @@ -23,7 +23,7 @@ pub async fn resolve_repository(
.repositories
.context("Missing repositories information.")?
.pop()
.context(format!("Cannot find repository {repository_name} in the registry."))
.with_context(|| format!("Cannot find repository {repository_name} in the registry."))
}

#[instrument(skip(client))]
Expand Down
80 changes: 14 additions & 66 deletions build/build/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ pub async fn publish_release(context: &BuildContext) -> Result {
Ok(())
}

/// Download the Enso Engine distribution from the GitHub release.
pub async fn get_engine_package(
octocrab: &Octocrab,
repo: impl IsRepo + Send + Sync + 'static,
repo: &(impl IsRepo + Send + Sync + 'static),
output: impl AsRef<Path>,
triple: &TargetTriple,
) -> Result<generated::EnginePackage> {
Expand All @@ -100,40 +101,23 @@ pub async fn get_engine_package(
Ok(engine_package)
}

/// Download the Enso Engine distribution from the GitHub release and build RUntime Docker image
/// from it.
pub async fn generate_runtime_image(
context: &BuildContext,
tag: impl Into<String>,
) -> Result<ide_ci::programs::docker::ImageId> {
let octocrab = &context.octocrab;
let release_id = crate::env::ENSO_RELEASE_ID.get()?;

// Our runtime images always target Linux.
let linux_triple = TargetTriple { os: OS::Linux, ..context.triple.clone() };
let package_name =
generated::RepoRootBuiltDistribution::new_root(".", linux_triple.to_string())
.enso_engine_triple
.file_name()
.context("Failed to get Engine Package name.")?
.as_str()
.to_string();

let release = context.remote_repo.find_release_by_id(octocrab, release_id).await?;
let asset = github::find_asset_by_text(&release, &package_name)?;


let temp_for_archive = tempdir()?;
let downloaded_asset = context
.remote_repo
.download_asset_to(octocrab, asset, temp_for_archive.path().to_owned())
.await?;

let temp_for_extraction = tempdir()?;
ide_ci::archive::extract_to(&downloaded_asset, &temp_for_extraction).await?;

let engine_package = generated::EnginePackage::new_under(
&temp_for_extraction,
context.triple.versions.version.to_string(),
);

let engine_package = get_engine_package(
octocrab,
&context.remote_repo,
temp_for_extraction.path(),
&linux_triple,
)
.await?;
crate::aws::ecr::runtime::build_runtime_image(
context.repo_root.tools.ci.docker.clone(),
engine_package,
Expand All @@ -143,48 +127,12 @@ pub async fn generate_runtime_image(
}

pub async fn deploy_to_ecr(context: &BuildContext, repository: String) -> Result {
let octocrab = &context.octocrab;
let release_id = crate::env::ENSO_RELEASE_ID.get()?;

let linux_triple = TargetTriple { os: OS::Linux, ..context.triple.clone() };
let package_name =
generated::RepoRootBuiltDistribution::new_root(".", linux_triple.to_string())
.enso_engine_triple
.file_name()
.context("Failed to get Engine Package name.")?
.as_str()
.to_string();

let release = context.remote_repo.find_release_by_id(octocrab, release_id).await?;
let asset = github::find_asset_by_text(&release, &package_name)?;


let temp_for_archive = tempdir()?;
let downloaded_asset = context
.remote_repo
.download_asset_to(octocrab, asset, temp_for_archive.path().to_owned())
.await?;

let temp_for_extraction = tempdir()?;
ide_ci::archive::extract_to(&downloaded_asset, &temp_for_extraction).await?;

let engine_package = generated::EnginePackage::new_under(
&temp_for_extraction,
context.triple.versions.version.to_string(),
);


let config = &aws_config::load_from_env().await;
let client = aws_sdk_ecr::Client::new(config);
let repository_uri = crate::aws::ecr::get_repository_uri(&client, &repository).await?;
let tag = format!("{}:{}", repository_uri, context.triple.versions.version);
let _image = crate::aws::ecr::runtime::build_runtime_image(
context.repo_root.tools.ci.docker.clone(),
engine_package,
tag.clone(),
)
.await?;

// We don't care about the image ID, we will refer to it by the tag.
let _image_id = generate_runtime_image(context, &repository).await?;
let credentials = crate::aws::ecr::get_credentials(&client).await?;
Docker.while_logged_in(credentials, || async move { Docker.push(&tag).await }).await?;
Ok(())
Expand Down

0 comments on commit d25df55

Please sign in to comment.