Skip to content

Commit

Permalink
build: support ephermal instance config
Browse files Browse the repository at this point in the history
  • Loading branch information
xtexx committed Dec 15, 2024
1 parent 3d5734b commit 2d85df1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/actions/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
common::*,
config::{self, InstanceConfig, WorkspaceConfig},
error, info,
machine::{self, get_container_ns_name, inspect_instance, spawn_container},
machine::{self, get_container_ns_name, inspect_instance, spawn_container, CielInstance},
network::download_file_progress,
overlayfs, warn,
};
Expand Down Expand Up @@ -401,3 +401,8 @@ fn apt_update_os(instance: &str) -> Result<()> {

Ok(())
}

pub fn inspect_container(instance: &str) -> Result<CielInstance> {
let ns_name = get_instance_ns_name(instance)?;
inspect_instance(instance, &ns_name)
}
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ pub fn build_cli() -> Command {
.arg(Arg::new("CONTINUE").conflicts_with("SELECT").short('c').long("resume").alias("continue").num_args(1).help("Continue from a Ciel checkpoint"))
.arg(Arg::new("SELECT").num_args(0..=1).long("stage-select").help("Select the starting point for a build"))
.arg(Arg::new("PACKAGES").conflicts_with("CONTINUE").num_args(1..))
.args(instance_configs.iter().cloned())
.about("Build the packages using the specified instance"),
)
.subcommand(
Expand Down
14 changes: 12 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ use anyhow::{Context, Result};
use console::user_attended;
use dialoguer::{theme::ColorfulTheme, Confirm, Editor, Input};
use serde::{Deserialize, Serialize};
use std::cell::OnceCell;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, OnceLock, RwLock, RwLockReadGuard};
use std::sync::{Arc, Mutex, OnceLock, RwLock};
use std::{ffi::OsString, path::Path};

const DEFAULT_CONFIG_LOCATION: &str = ".ciel/data/config.toml";
Expand Down Expand Up @@ -264,6 +263,17 @@ impl InstanceConfig {
fs::write(path, self.to_toml()?)?;
Ok(())
}

pub fn load_mounted<S: AsRef<str>>(instance: S) -> Result<Self> {
let path = Path::new(instance.as_ref()).join(".ciel.toml");
if path.exists() {
let content = fs::read_to_string(&path)
.with_context(|| format!("load instance config from {}", path.display()))?;
Self::from_toml(content)
} else {
Self::load(instance)
}
}
}

static INSTANCE_CONFIGS: OnceLock<Mutex<HashMap<String, Arc<RwLock<InstanceConfig>>>>> =
Expand Down
24 changes: 23 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ mod network;
mod overlayfs;
mod repo;

use actions::{inspect_container, patch_instance_config};
use anyhow::{anyhow, bail, Context, Result};
use clap::ArgMatches;
use config::WorkspaceConfig;
use config::{InstanceConfig, WorkspaceConfig};
use console::{style, user_attended};
use dotenvy::dotenv;
use libc::exit;

Check warning on line 20 in src/main.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `libc::exit`

Check warning on line 20 in src/main.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `libc::exit`
use std::process;
use std::{path::Path, process::Command};

Expand Down Expand Up @@ -301,12 +303,27 @@ fn main() -> Result<()> {
}
("build", args) => {
let instance = get_instance_option(args)?;
let config_ref = InstanceConfig::get(&instance)?;
patch_instance_config(&instance, args, &mut *config_ref.write().unwrap())?;

let container = inspect_container(&instance)?;
let need_rollback = container.mounted
&& *config_ref.read().unwrap() != InstanceConfig::load_mounted(&instance)?;

let settings = BuildSettings {
offline: args.get_flag("OFFLINE"),
stage2: args.get_flag("STAGE2"),
};
let mut state = None;
if let Some(cont) = args.get_one::<String>("CONTINUE") {
if need_rollback {
error!("The current instance configuration differs from the mounted instance. Cannot continue without rolling-back.");
process::exit(1);
}
if container.started {
error!("The current instance has not been started. Cannot continue.");
process::exit(1);
}
state = Some(actions::load_build_checkpoint(cont)?);
let empty: Vec<&str> = Vec::new();
let status = actions::package_build(&instance, empty.into_iter(), state, settings)?;
Expand All @@ -319,6 +336,11 @@ fn main() -> Result<()> {
process::exit(1);
}
let packages = packages.unwrap();

if need_rollback {
actions::rollback_container(&instance)?;
}

if args.contains_id("SELECT") {
let start_package = args.get_one::<String>("SELECT");
let status =
Expand Down

0 comments on commit 2d85df1

Please sign in to comment.