Skip to main content

hoprd_localcluster/
blokli_helper.rs

1use std::{
2    fs::{self, File},
3    path::Path,
4    process::{Child, Command, Stdio},
5};
6
7use anyhow::{Context, Result};
8
9pub struct ChainHandle {
10    name: String,
11    child: Child,
12}
13
14impl ChainHandle {
15    pub fn start(chain_image: &str, log_dir: &Path) -> Result<Self> {
16        fs::create_dir_all(log_dir).context("failed to create log directory")?;
17        let log_file = log_dir.join("chain.log");
18        let log_file = File::create(&log_file).context("failed to create blokli log file")?;
19        let log_err = log_file.try_clone().context("failed to clone blokli log file handle")?;
20        let name = "hopr-chain";
21
22        let mut cmd = Command::new("docker");
23        cmd.arg("run")
24            .arg("--rm")
25            .arg("--name")
26            .arg(name)
27            .arg("-p")
28            .arg("8080:8080")
29            .arg(chain_image)
30            .stdout(Stdio::from(log_file))
31            .stderr(Stdio::from(log_err));
32
33        let child = cmd.spawn().context("failed to start blokli container")?;
34
35        Ok(Self {
36            name: name.to_string(),
37            child,
38        })
39    }
40
41    pub fn stop(&mut self) {
42        let _ = self.child.kill();
43        let _ = Command::new("docker").arg("rm").arg("-f").arg(&self.name).status();
44    }
45}