summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2021-01-30 21:27:34 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2021-01-30 21:27:34 +0100
commit6eb0851420b358132dd8a72312b25a1f7efd02de (patch)
tree6cae08b9d96c6d38969e7025b400c6a21459ba63
parentb55edb96ae0f45d9dce64d6f2e78a9b8d3ff4ef9 (diff)
downloadrebel-6eb0851420b358132dd8a72312b25a1f7efd02de.tar
rebel-6eb0851420b358132dd8a72312b25a1f7efd02de.zip
runner: run commands in buildah containers
-rw-r--r--Cargo.lock7
-rw-r--r--Cargo.toml1
-rw-r--r--src/executor.rs2
-rw-r--r--src/runner/buildah.rs50
4 files changed, 54 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5b12c9a..cd97c8e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -34,6 +34,7 @@ dependencies = [
name = "rebel"
version = "0.1.0"
dependencies = [
+ "scopeguard",
"serde",
"serde_yaml",
"walkdir",
@@ -49,6 +50,12 @@ dependencies = [
]
[[package]]
+name = "scopeguard"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
+
+[[package]]
name = "serde"
version = "1.0.121"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index d6eea9f..820a5fa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+scopeguard = "1.1.0"
serde = { version = "1", features = ["derive"] }
serde_yaml = "0.8"
walkdir = "2"
diff --git a/src/executor.rs b/src/executor.rs
index e07b4e3..2523162 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -70,7 +70,7 @@ impl<'a> Executor<'a> {
}
pub fn run(&mut self) -> runner::Result<()> {
- let runner = runner::buildah::RunnerBuildah::new(self.tasks);
+ let runner = runner::buildah::BuildahRunner::new(self.tasks);
while !self.tasks_runnable.is_empty() {
self.run_one(&runner)?;
}
diff --git a/src/runner/buildah.rs b/src/runner/buildah.rs
index a4f0d3b..72a2e5f 100644
--- a/src/runner/buildah.rs
+++ b/src/runner/buildah.rs
@@ -1,21 +1,61 @@
+use scopeguard::defer;
+use std::{
+ io::{Error, ErrorKind},
+ process::{Command, Stdio},
+};
+
use super::*;
use crate::types::*;
-pub struct RunnerBuildah<'a> {
+static IMAGE: &str = "rebel:latest";
+
+pub struct BuildahRunner<'a> {
tasks: &'a TaskMap,
}
-impl<'a> RunnerBuildah<'a> {
+impl<'a> BuildahRunner<'a> {
pub fn new(tasks: &'a TaskMap) -> Self {
- RunnerBuildah { tasks }
+ BuildahRunner { tasks }
}
}
-impl<'a> Runner for RunnerBuildah<'a> {
+fn check_status(output: &std::process::Output, message: &str) -> Result<()> {
+ if output.status.success() {
+ Ok(())
+ } else {
+ Err(Error::new(ErrorKind::Other, message))
+ }
+}
+
+impl<'a> Runner for BuildahRunner<'a> {
fn run(&self, task: &TaskRef) -> Result<()> {
let task_def = self.tasks.get(task).expect("Invalid TaskRef");
- println!("{}: {:?}", task, task_def.run);
+ let buildah_from = Command::new("buildah")
+ .arg("from")
+ .arg("--name")
+ .arg(task)
+ .arg(IMAGE)
+ .output()?;
+ check_status(&buildah_from, "unable to create container")?;
+
+ defer! {
+ let _ = Command::new("buildah")
+ .arg("rm")
+ .arg(task)
+ .stdout(Stdio::null())
+ .output();
+ }
+
+ let buildah_run = Command::new("buildah")
+ .arg("run")
+ .arg(task)
+ .arg("sh")
+ .arg("-c")
+ .arg(&task_def.run)
+ .output()?;
+
+ println!("{}:\n\t{:?}\n\t{:?}", task, task_def, buildah_run);
Ok(())
}