Skip to content

Commit

Permalink
Merge pull request #363 from cbgbt/protect-lock
Browse files Browse the repository at this point in the history
twoliter: refactor lock & project interfaces
  • Loading branch information
cbgbt authored Sep 11, 2024
2 parents 4bb087a + 10af17f commit bf28d26
Show file tree
Hide file tree
Showing 20 changed files with 616 additions and 350 deletions.
2 changes: 2 additions & 0 deletions tests/projects/local-kit/Twoliter.override
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bottlerocket.bottlerocket-sdk]
registry = "public.ecr.aws/bottlerocket"
4 changes: 3 additions & 1 deletion tests/projects/local-kit/Twoliter.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ vendor = "bottlerocket"
version = "0.41.0"

[vendor.bottlerocket]
registry = "public.ecr.aws/bottlerocket"
# This test will fail if Twoliter overrides aren't working
# See `Twoliter.override`
registry = "this-definitely-wont-resolve/bottlerocket"
2 changes: 1 addition & 1 deletion tools/oci-cli-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl ImageTool {
}

#[async_trait]
pub trait ImageToolImpl: std::fmt::Debug {
pub trait ImageToolImpl: std::fmt::Debug + Send + Sync + 'static {
/// Pull an image archive to disk
async fn pull_oci_image(&self, path: &Path, uri: &str) -> Result<()>;
/// Fetch the image config
Expand Down
11 changes: 5 additions & 6 deletions twoliter/src/cmd/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use super::build_clean::BuildClean;
use crate::cargo_make::CargoMake;
use crate::common::fs;
use crate::lock::Lock;
use crate::project;
use crate::project::{self, Locked};
use crate::tools::install_tools;
use anyhow::{Context, Result};
use clap::Parser;
Expand Down Expand Up @@ -53,7 +52,7 @@ pub(crate) struct BuildKit {
impl BuildKit {
pub(super) async fn run(&self) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let lock = Lock::load(&project).await?;
let project = project.load_lock::<Locked>().await?;
let toolsdir = project.project_dir().join("build/tools");
install_tools(&toolsdir).await?;
let makefile_path = toolsdir.join("Makefile.toml");
Expand All @@ -64,7 +63,7 @@ impl BuildKit {
optional_envs.push(("BUILDSYS_LOOKASIDE_CACHE", lookaside_cache))
}

CargoMake::new(&lock.sdk.source)?
CargoMake::new(&project.sdk_image().project_image_uri().to_string())?
.env("TWOLITER_TOOLS_DIR", toolsdir.display().to_string())
.env("BUILDSYS_ARCH", &self.arch)
.env("BUILDSYS_KIT", &self.kit)
Expand Down Expand Up @@ -113,7 +112,7 @@ pub(crate) struct BuildVariant {
impl BuildVariant {
pub(super) async fn run(&self) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let lock = Lock::load(&project).await?;
let project = project.load_lock::<Locked>().await?;
let toolsdir = project.project_dir().join("build/tools");
install_tools(&toolsdir).await?;
let makefile_path = toolsdir.join("Makefile.toml");
Expand All @@ -136,7 +135,7 @@ impl BuildVariant {
))
}

CargoMake::new(&lock.sdk.source)?
CargoMake::new(&project.sdk_image().project_image_uri().to_string())?
.env("TWOLITER_TOOLS_DIR", toolsdir.display().to_string())
.env("BUILDSYS_ARCH", &self.arch)
.env("BUILDSYS_VARIANT", &self.variant)
Expand Down
7 changes: 3 additions & 4 deletions twoliter/src/cmd/build_clean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cargo_make::CargoMake;
use crate::lock::Lock;
use crate::project;
use crate::project::{self, Locked};
use crate::tools;
use anyhow::Result;
use clap::Parser;
Expand All @@ -16,12 +15,12 @@ pub(crate) struct BuildClean {
impl BuildClean {
pub(super) async fn run(&self) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let lock = Lock::load(&project).await?;
let project = project.load_lock::<Locked>().await?;
let toolsdir = project.project_dir().join("build/tools");
tools::install_tools(&toolsdir).await?;
let makefile_path = toolsdir.join("Makefile.toml");

CargoMake::new(&lock.sdk.source)?
CargoMake::new(&project.sdk_image().project_image_uri().to_string())?
.env("TWOLITER_TOOLS_DIR", toolsdir.display().to_string())
.makefile(makefile_path)
.project_dir(project.project_dir())
Expand Down
7 changes: 3 additions & 4 deletions twoliter/src/cmd/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::lock::Lock;
use crate::project;
use crate::project::{self, Locked};
use anyhow::Result;
use clap::Parser;
use std::path::PathBuf;
Expand All @@ -18,8 +17,8 @@ pub(crate) struct Fetch {
impl Fetch {
pub(super) async fn run(&self) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let lock_file = Lock::load(&project).await?;
lock_file.fetch(&project, self.arch.as_str()).await?;
let project = project.load_lock::<Locked>().await?;
project.fetch(self.arch.as_str()).await?;
Ok(())
}
}
27 changes: 13 additions & 14 deletions twoliter/src/cmd/make.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cargo_make::CargoMake;
use crate::lock::{Lock, LockedSDK};
use crate::project::{self};
use crate::project::{self, Locked, SDKLocked, Unlocked};
use crate::tools::install_tools;
use anyhow::Result;
use clap::Parser;
Expand Down Expand Up @@ -69,24 +68,23 @@ impl Make {
.await
}

fn can_skip_kit_verification(&self, project: &project::Project) -> bool {
fn can_skip_kit_verification(&self, project: &project::Project<Unlocked>) -> bool {
let target_allows_kit_verification_skip =
!MUST_VALIDATE_KITS_TARGETS.contains(&self.makefile_task.as_str());
let project_has_explicit_sdk_dep = project.sdk_image().is_some();
let project_has_explicit_sdk_dep = project.direct_sdk_image_dep().is_some();

target_allows_kit_verification_skip && project_has_explicit_sdk_dep
}

/// Returns the locked SDK image for the project.
async fn locked_sdk(&self, project: &project::Project) -> Result<String> {
let sdk_source = if self.can_skip_kit_verification(project) {
let lock = LockedSDK::load(project).await?;
lock.0.source
async fn locked_sdk(&self, project: &project::Project<Unlocked>) -> Result<String> {
Ok(if self.can_skip_kit_verification(project) {
project.load_lock::<SDKLocked>().await?.sdk_image()
} else {
let lock = Lock::load(project).await?;
lock.sdk.source
};
Ok(sdk_source)
project.load_lock::<Locked>().await?.sdk_image()
}
.project_image_uri()
.to_string())
}
}

Expand All @@ -95,7 +93,7 @@ mod test {
use std::path::Path;

use crate::cmd::update::Update;
use crate::lock::VerificationTagger;
use crate::project::VerificationTagger;

use super::*;

Expand Down Expand Up @@ -215,7 +213,8 @@ mod test {
let project = project::load_or_find_project(Some(project_path))
.await
.unwrap();
let sdk_source = LockedSDK::load(&project).await.unwrap().0.source;
let project = project.load_lock::<SDKLocked>().await.unwrap();
let sdk_source = project.sdk_image().project_image_uri().to_string();

if delete_verifier_tags {
// Clean up tags so that the build fails
Expand Down
7 changes: 3 additions & 4 deletions twoliter/src/cmd/publish_kit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::cargo_make::CargoMake;
use crate::lock::Lock;
use crate::project;
use crate::project::{self, Locked};
use crate::tools::install_tools;
use anyhow::Result;
use clap::Parser;
Expand Down Expand Up @@ -37,12 +36,12 @@ pub(crate) struct PublishKit {
impl PublishKit {
pub(super) async fn run(&self) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
let lock = Lock::load(&project).await?;
let project = project.load_lock::<Locked>().await?;
let toolsdir = project.project_dir().join("build/tools");
install_tools(&toolsdir).await?;
let makefile_path = toolsdir.join("Makefile.toml");

CargoMake::new(&lock.sdk.source)?
CargoMake::new(project.sdk_image().project_image_uri().to_string().as_str())?
.env("TWOLITER_TOOLS_DIR", toolsdir.display().to_string())
.env("BUILDSYS_KIT", &self.kit_name)
.env("BUILDSYS_VERSION_IMAGE", project.release_version())
Expand Down
3 changes: 1 addition & 2 deletions twoliter/src/cmd/update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::lock::Lock;
use crate::project;
use anyhow::Result;
use clap::Parser;
Expand All @@ -14,7 +13,7 @@ pub(crate) struct Update {
impl Update {
pub(super) async fn run(&self) -> Result<()> {
let project = project::load_or_find_project(self.project_path.clone()).await?;
Lock::create(&project).await?;
project.create_lock().await?;
Ok(())
}
}
1 change: 0 additions & 1 deletion twoliter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod cargo_make;
mod cmd;
mod common;
mod docker;
mod lock;
mod project;
mod schema_version;
/// Test code that should only be compiled when running tests.
Expand Down
File renamed without changes.
Loading

0 comments on commit bf28d26

Please sign in to comment.