Skip to content

Commit

Permalink
refactor: simplify image pulling to focus on layer extraction in prep…
Browse files Browse the repository at this point in the history
…aration for overlayfs (#140)

- Remove monofs layer creation and merging during image pulls
- Store extracted layers in a dedicated layers directory instead of download dir
- Remove head_cid columns from images and layers tables
- Rename layer/image "complete" checks to "exists" checks
- Remove monoimage module and registry implementation
- Update logging messages to use consistent lowercase style
  • Loading branch information
appcypher authored Feb 16, 2025
1 parent 66f49e8 commit a7e2ee7
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 459 deletions.
17 changes: 12 additions & 5 deletions monocore/bin/monocore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,31 @@ use monocore::{

#[tokio::main]
async fn main() -> MonocoreResult<()> {
tracing_subscriber::fmt::init();
tracing_subscriber::fmt()
.with_target(false)
.with_file(false)
.with_line_number(false)
.with_thread_ids(false)
.with_thread_names(false)
.with_level(true)
.init();

// Parse command line arguments
let args = MonocoreArgs::parse();
match args.subcommand {
Some(MonocoreSubcommand::Init { path }) => {
tracing::info!("Initializing monocore project: path={path:?}");
tracing::info!("initializing monocore project: path={path:?}");
management::init_menv(path).await?;
tracing::info!("Successfully initialized monocore project");
tracing::info!("successfully initialized monocore project");
}
Some(MonocoreSubcommand::Pull {
image,
image_group,
name,
}) => {
tracing::info!("Pulling image: name={name}, image={image}, image_group={image_group}");
tracing::info!("pulling image: name={name}, image={image}, image_group={image_group}");
management::pull_image(name, image, image_group).await?;
tracing::info!("Successfully pulled image");
tracing::info!("successfully pulled image");
}
Some(_) => (), // TODO: implement other subcommands
None => {
Expand Down
1 change: 0 additions & 1 deletion monocore/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ mod error;
pub mod cli;
pub mod config;
pub mod management;
pub mod monoimage;
pub mod oci;
pub mod runtime;
pub mod utils;
Expand Down
108 changes: 6 additions & 102 deletions monocore/lib/management/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,114 +388,18 @@ pub(crate) async fn save_or_update_layer(
}
}

/// Updates the head CID of a layer in the database.
///
/// ## Arguments
///
/// * `pool` - The database connection pool
/// * `digest` - The digest of the layer to update
/// * `head_cid` - The root CID of the monofs layer
pub(crate) async fn update_layer_head_cid(
pool: &Pool<Sqlite>,
digest: &str,
head_cid: &str,
) -> MonocoreResult<()> {
sqlx::query(
r#"
UPDATE layers
SET head_cid = ?, modified_at = CURRENT_TIMESTAMP
WHERE digest = ?
"#,
)
.bind(head_cid)
.bind(digest)
.execute(pool)
.await?;

Ok(())
}

/// Updates the head CID of an image in the database.
///
/// ## Arguments
///
/// * `pool` - The database connection pool
/// * `reference` - The reference of the image to update
/// * `head_cid` - The root CID of the merged monofs layers
pub(crate) async fn update_image_head_cid(
pool: &Pool<Sqlite>,
reference: &str,
head_cid: &str,
) -> MonocoreResult<()> {
sqlx::query(
r#"
UPDATE images
SET head_cid = ?, modified_at = CURRENT_TIMESTAMP
WHERE reference = ?
"#,
)
.bind(head_cid)
.bind(reference)
.execute(pool)
.await?;

Ok(())
}

/// Gets all layer CIDs for an image in base-to-top order.
///
/// This function returns a vector of tuples containing:
/// - The layer's digest
/// - The layer's head CID (if it exists)
///
/// The layers are returned in the order they should be applied,
/// from base layer to top layer.
///
/// ## Arguments
///
/// * `pool` - The database connection pool
/// * `reference` - The reference of the image to get layers for
pub(crate) async fn get_image_layer_cids(
pool: &Pool<Sqlite>,
reference: &str,
) -> MonocoreResult<Vec<(String, Option<String>)>> {
let records = sqlx::query(
r#"
SELECT l.digest, l.head_cid
FROM layers l
JOIN manifests m ON l.manifest_id = m.id
JOIN images i ON m.image_id = i.id
WHERE i.reference = ?
ORDER BY l.id ASC
"#,
)
.bind(reference)
.fetch_all(pool)
.await?;

Ok(records
.into_iter()
.map(|record| {
(
record.get::<String, _>("digest"),
record.get::<Option<String>, _>("head_cid"),
)
})
.collect())
}

/// Checks if a layer exists and is complete (has head_cid) in the database.
/// Checks if a layer exists in the database.
///
/// ## Arguments
///
/// * `pool` - The database connection pool
/// * `digest` - The digest string of the layer to check
pub(crate) async fn layer_complete(pool: &Pool<Sqlite>, digest: &str) -> MonocoreResult<bool> {
pub(crate) async fn layer_exists(pool: &Pool<Sqlite>, digest: &str) -> MonocoreResult<bool> {
let record = sqlx::query(
r#"
SELECT COUNT(*) as count
FROM layers
WHERE digest = ? AND head_cid IS NOT NULL
WHERE digest = ?
"#,
)
.bind(digest)
Expand All @@ -505,18 +409,18 @@ pub(crate) async fn layer_complete(pool: &Pool<Sqlite>, digest: &str) -> Monocor
Ok(record.get::<i64, _>("count") > 0)
}

/// Checks if an image exists and is complete (has head_cid) in the database.
/// Checks if an image exists in the database.
///
/// ## Arguments
///
/// * `pool` - The database connection pool
/// * `reference` - The reference string of the image to check
pub(crate) async fn image_complete(pool: &Pool<Sqlite>, reference: &str) -> MonocoreResult<bool> {
pub(crate) async fn image_exists(pool: &Pool<Sqlite>, reference: &str) -> MonocoreResult<bool> {
let record = sqlx::query(
r#"
SELECT COUNT(*) as count
FROM images
WHERE reference = ? AND head_cid IS NOT NULL
WHERE reference = ?
"#,
)
.bind(reference)
Expand Down
Loading

0 comments on commit a7e2ee7

Please sign in to comment.