summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@noreply.codeberg.org>2024-05-11 16:18:41 +0000
committerLars Wirzenius <liw@noreply.codeberg.org>2024-05-11 16:18:41 +0000
commitaf95dd76cc041259eaa43134b41b18b47381e1e8 (patch)
treec95ead7fd5d9f66bd183618f2af07743b72eab55
parentd8469a0287255236a0c21a54329aef13d9788789 (diff)
parentcc0990e7937fbd93f4867a2ba507d340f45243fc (diff)
downloadambient-driver-main.tar.gz
Merge pull request 'refactor: move running of steps into a helper function' (#108) from refactor-run into mainHEADmain
Reviewed-on: https://codeberg.org/ambient/ambient-driver/pulls/108
-rw-r--r--src/run.rs148
1 files changed, 95 insertions, 53 deletions
diff --git a/src/run.rs b/src/run.rs
index e226983..84ca848 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -65,59 +65,18 @@ fn cmd_run(config: &EffectiveConfig, run: &RunCommand) -> Result<(), RunError> {
action.execute(project, &state, run)?;
}
- debug!("Executing CI run");
- let tmp = tempdir_in(config.stored_config().tmpdir()).map_err(RunError::TempDir)?;
- let artifact = tmp.path().join("artifact.tar");
-
- let run_ci_dir = mkdir_child(tmp.path(), "run-ci")?;
- assert!(run.run_ci().is_some());
- let run_ci_bin = &run.run_ci().unwrap();
- let run_ci_exe = run_ci_dir.join("run-ci");
-
- debug!(
- "copying {} to {}",
- run_ci_bin.display(),
- run_ci_exe.display()
- );
- std::fs::copy(run_ci_bin, &run_ci_exe)
- .map_err(|e| RunError::Copy(run_ci_bin.into(), run_ci_exe, e))?;
-
- let mut plan = Plan::default();
- prelude(&mut plan);
- for action in project.plan().iter() {
- plan.push(action.clone());
- }
- epilog(&mut plan);
- debug!("plan: {:#?}", plan);
- let plan_filename = run_ci_dir.join("plan.yaml");
- plan.to_file(&plan_filename).map_err(RunError::Plan)?;
-
- let qemu = Qemu::new(
- project.image(),
- &config.stored_config().tmpdir(),
- config.cpus(),
- config.memory(),
- )
- .with_run_ci(&run_ci_dir)
- .with_source(project.source())
- .with_artifact(&Some(artifact.clone()))
- .with_artifact_max_size(
- project
- .artifact_max_size()
- .unwrap_or(config.artfifacts_max_size()),
- )
- .with_dependencies(&Some(dependencies.clone()))
- .with_cache(&Some(cachedir.clone()))
- .with_cache_max_size(project.cache_max_size().unwrap_or(config.cache_max_size()))
- .with_log(log);
- let exit = qemu.run()?;
- debug!("QEMU exit code {}", exit);
- if exit != 0 {
- return Err(RunError::QemuFailed(exit));
- }
-
- let vdrive = VirtualDriveBuilder::default().filename(&artifact).open()?;
- vdrive.extract_to(&artifactsdir)?;
+ let plan = construct_unsafe_plan(project)?;
+
+ run_in_qemu(
+ config,
+ run,
+ &plan,
+ project,
+ &dependencies,
+ &cachedir,
+ &artifactsdir,
+ log,
+ )?;
debug!("Executing post-plan steps");
for action in project.post_plan() {
@@ -139,6 +98,80 @@ fn cmd_run(config: &EffectiveConfig, run: &RunCommand) -> Result<(), RunError> {
Ok(())
}
+#[allow(clippy::too_many_arguments)]
+fn run_in_qemu(
+ config: &EffectiveConfig,
+ run: &RunCommand,
+ plan: &str,
+ project: &Project,
+ dependencies: &Path,
+ cachedir: &Path,
+ artifactsdir: &Path,
+ log: &Path,
+) -> Result<(), RunError> {
+ debug!("Executing CI run");
+ let tmp = tempdir_in(config.stored_config().tmpdir()).map_err(RunError::TempDir)?;
+ let artifact = tmp.path().join("artifact.tar");
+
+ let run_ci_dir = mkdir_child(tmp.path(), "run-ci")?;
+ assert!(run.run_ci().is_some());
+ let run_ci_bin = &run.run_ci().unwrap();
+ let run_ci_exe = run_ci_dir.join("run-ci");
+
+ debug!(
+ "copying {} to {}",
+ run_ci_bin.display(),
+ run_ci_exe.display()
+ );
+ std::fs::copy(run_ci_bin, &run_ci_exe)
+ .map_err(|e| RunError::Copy(run_ci_bin.into(), run_ci_exe, e))?;
+
+ let plan_filename = run_ci_dir.join("plan.yaml");
+ std::fs::write(&plan_filename, plan.as_bytes())
+ .map_err(|e| RunError::PlanWrite(plan_filename.clone(), e))?;
+
+ let qemu = Qemu::new(
+ project.image(),
+ &config.stored_config().tmpdir(),
+ config.cpus(),
+ config.memory(),
+ )
+ .with_run_ci(&run_ci_dir)
+ .with_source(project.source())
+ .with_artifact(&Some(artifact.clone()))
+ .with_artifact_max_size(
+ project
+ .artifact_max_size()
+ .unwrap_or(config.artfifacts_max_size()),
+ )
+ .with_dependencies(&Some(dependencies.into()))
+ .with_cache(&Some(cachedir.into()))
+ .with_cache_max_size(project.cache_max_size().unwrap_or(config.cache_max_size()))
+ .with_log(log);
+ let exit = qemu.run()?;
+ debug!("QEMU exit code {}", exit);
+ if exit != 0 {
+ return Err(RunError::QemuFailed(exit));
+ }
+
+ let vdrive = VirtualDriveBuilder::default().filename(&artifact).open()?;
+ vdrive.extract_to(artifactsdir)?;
+
+ Ok(())
+}
+
+fn construct_unsafe_plan(project: &Project) -> Result<String, RunError> {
+ let mut plan = Plan::default();
+ prelude(&mut plan);
+ for action in project.plan().iter() {
+ plan.push(action.clone());
+ }
+ epilog(&mut plan);
+ debug!("plan: {:#?}", plan);
+
+ serde_yaml::to_string(&plan).map_err(RunError::PlanSerialize)
+}
+
fn should_run(
run: &RunCommand,
statedir: &Path,
@@ -354,6 +387,15 @@ pub enum RunError {
#[error("no state directory specified")]
NoState,
+
+ #[error("failed to parse CI plan file as YAML: {0}")]
+ PlanParse(PathBuf, #[source] serde_yaml::Error),
+
+ #[error("failed to serialize CI plan as YAML")]
+ PlanSerialize(#[source] serde_yaml::Error),
+
+ #[error("failed to write CI plan file: {0}")]
+ PlanWrite(PathBuf, #[source] std::io::Error),
}
#[derive(Debug, Default)]