Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtask: redirect stdout and stderr into log files #231

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/demo/sovereign/target
/demo/sovereign/demo-rollup/demo_data
/target
/test_log
31 changes: 16 additions & 15 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{cli::test::BuildParams, run_maybe_quiet};
use crate::{cli::test::BuildParams, logging::create_with_logs};
use duct::cmd;
use tracing::info;

// TODO: https://github.com/thrumdev/blobs/issues/225

Expand All @@ -9,28 +8,30 @@ pub fn build(params: BuildParams) -> anyhow::Result<()> {
return Ok(());
}

tracing::info!("Building logs redirected {}", params.log_path);
let with_logs = create_with_logs(params.log_path);

// `it is advisable to use CARGO environmental variable to get the right cargo`
// quoted by xtask readme
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());

info!("Start building sugondat-node");
run_maybe_quiet(
with_logs(
"Building sugondat-node",
cmd!(&cargo, "build", "-p", "sugondat-node", "--release"),
params.quiet,
)?;
)
.run()?;

info!("Start building sugondat-node");
run_maybe_quiet(
with_logs(
"Building sugondat-shim",
cmd!(&cargo, "build", "-p", "sugondat-shim", "--release"),
params.quiet,
)?;

info!("Start building sovereign demo-rollup");
)
.run()?;

run_maybe_quiet(
with_logs(
"Building sovereign demo-rollup",
cmd!(&cargo, "build", "--release").dir("demo/sovereign/demo-rollup"),
params.quiet,
)?;
)
.run()?;

Ok(())
}
42 changes: 29 additions & 13 deletions xtask/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,48 @@ pub mod test {
/// Skip building required binaries
/// (sugondat-node, sugondat-shim, sov-demo-rollup and sov-cli)
#[clap(default_value = "false")]
#[arg(long = "build-skip", value_name = "skip", id = "build.skip")]
#[arg(long = "skip-build", value_name = "skip", id = "build.skip")]
pub skip: bool,

/// Don't print to stdout during the build process
#[arg(long = "build-quiet", value_name = "quiet", id = "build.quiet")]
pub quiet: bool,
/// Build process stdout and stderr are redirected into this file
#[arg(
long = "build-log-path",
value_name = "log-path",
id = "build.log-path"
)]
#[clap(default_value = "test_log/build.log")]
pub log_path: String,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that these are all flattened into the same Params, how do you differentiate them?

Which one does --log-path X trigger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick with value_name and id makes the items look like <prefix>-log-path, where the prefix could be zombienet, shim, or sovereign to specify the path where you want to output the logs of the prefix process. This is a workaround, though, and the discussion on how the flags should look like can be found here: #224

}

#[derive(clap::Args, Debug, Clone)]
pub struct ShimParams {
/// Don't print shim process stdout
#[arg(long = "shim-quiet", value_name = "quiet", id = "shim.quiet")]
pub quiet: bool,
/// Shim process stdout and stderr are redirected into this file
#[arg(long = "shim-log-path", value_name = "log-path", id = "shim.log-path")]
#[clap(default_value = "test_log/shim.log")]
pub log_path: String,
}

#[derive(clap::Args, Debug, Clone)]
pub struct ZombienetParams {
/// Don't print zombienet process stdout
#[arg(long = "zombienet-quiet", value_name = "quiet", id = "zombienet.quiet")]
pub quiet: bool,
/// Zombienet process stdout and stderr are redirected into this file
#[arg(
long = "zombienet-log-path",
value_name = "log-path",
id = "zombienet.log-path"
)]
#[clap(default_value = "test_log/zombienet.log")]
pub log_path: String,
}

#[derive(clap::Args, Debug, Clone)]
pub struct SovereignParams {
/// Don't print sovereing rollup processes stdout
#[arg(long = "sovereign-quiet", value_name = "quiet", id = "sovereign.quiet")]
pub quiet: bool,
/// Sovereign rollup process stdout and stderr are redirected into this file
#[arg(
long = "sovereign-log-path",
value_name = "log-path",
id = "sovereign.log-path"
)]
#[clap(default_value = "test_log/sovereign.log")]
pub log_path: String,
}
}
51 changes: 51 additions & 0 deletions xtask/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use std::io::Write;
use std::path::Path;
use tracing::{info, warn};

fn create_log_file(log_path: &String) -> std::io::Result<()> {
if let Some(prefix) = Path::new(&log_path).parent() {
std::fs::create_dir_all(prefix)?;
}
std::fs::File::create(&log_path)?;
Ok(())
}

// If the log file cannot be created due to any reasons,
// such as lack of permission to create files or new folders in the path,
// things will be printed to stdout instead of being redirected to the logs file
//
// The returned closure will accept a description of the command and the command itself as a duct::Expression.
// The description will be printed to both stdout and the log file, if possible, while
// to the expression will be added the redirection of the logs, if possible.
pub fn create_with_logs(
log_path: String,
) -> Box<dyn Fn(&str, duct::Expression) -> duct::Expression> {
let without_logs = |description: &str, cmd: duct::Expression| -> duct::Expression {
info!("{description}");
cmd
};

if let Err(e) = create_log_file(&log_path) {
warn!("Impossible redirect to {log_path}, using stdout instead. Error: {e}");
return Box::new(without_logs);
}

let with_logs = move |description: &str, cmd: duct::Expression| -> duct::Expression {
// The file has just been created
let mut log_file = std::fs::File::options()
.append(true)
.open(&log_path)
.unwrap();

info!("{description}");
let _ = log_file
.write(format!("{}/n", description).as_bytes())
.map_err(|e| warn!("Error writing into {log_path}, error: {e}"));
let _ = log_file
.flush()
.map_err(|e| warn!("Error writing into {log_path}, error: {e}"));
cmd.stderr_to_stdout().stdout_file(log_file)
};

Box::new(with_logs)
}
15 changes: 1 addition & 14 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod build;
mod cli;
mod logging;
mod shim;
mod sovereign;
mod zombienet;
Expand Down Expand Up @@ -50,17 +51,3 @@ fn init_logging() -> anyhow::Result<()> {
.try_init()?;
Ok(())
}

fn run_maybe_quiet(cmd: duct::Expression, quiet: bool) -> std::io::Result<std::process::Output> {
match quiet {
true => cmd.stdout_null().run(),
false => cmd.run(),
}
}

fn start_maybe_quiet(cmd: duct::Expression, quiet: bool) -> std::io::Result<duct::Handle> {
match quiet {
true => cmd.stdout_null().start(),
false => cmd.start(),
}
}
21 changes: 12 additions & 9 deletions xtask/src/shim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cli::test::ShimParams, run_maybe_quiet, start_maybe_quiet};
use crate::{cli::test::ShimParams, logging::create_with_logs};
use duct::cmd;
use tracing::info;

Expand All @@ -7,18 +7,21 @@ pub struct Shim(duct::Handle);
impl Shim {
// Try launching the shim, it requires an up an running sugondat-node
pub fn try_new(params: ShimParams) -> anyhow::Result<Self> {
tracing::info!("Shim logs redirected to {}", params.log_path);
let with_logs = create_with_logs(params.log_path);

// Wait for the shim to be connected, which indicates that the network is ready
info!("Wait for the network to be ready");
run_maybe_quiet(
with_logs(
"Wait for the network to be ready",
cmd!("sugondat-shim", "query", "block", "--wait", "1",).dir("target/release/"),
params.quiet,
)?;
)
.run()?;

info!("Launching Shim");
let shim_handle = start_maybe_quiet(
let shim_handle = with_logs(
"Launching Shim",
cmd!("sugondat-shim", "serve", "--submit-dev-alice").dir("target/release/"),
params.quiet,
)?;
)
.start()?;

Ok(Self(shim_handle))
}
Expand Down
94 changes: 55 additions & 39 deletions xtask/src/sovereign.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
use crate::{cli::test::SovereignParams, start_maybe_quiet};
use crate::{cli::test::SovereignParams, logging::create_with_logs};
use anyhow::bail;
use duct::cmd;
use tracing::info;

pub struct Sovereign(duct::Handle);
pub struct Sovereign {
process: duct::Handle,
with_logs: Box<dyn Fn(&str, duct::Expression) -> duct::Expression>,
}

impl Sovereign {
// Try launching the sovereing rollup using zombienet
pub fn try_new(params: SovereignParams) -> anyhow::Result<Self> {
info!("Deleting rollup db if it already exists");
cmd!("rm", "-r", "demo/sovereign/demo-rollup/demo_data")
.unchecked()
.stderr_null()
.stdout_null()
.run()?;

info!("Sovereign logs redirected to {}", params.log_path);
let with_logs = create_with_logs(params.log_path.clone());

//TODO: https://github.com/thrumdev/blobs/issues/227
info!("Launching sovereign rollup");
#[rustfmt::skip]
let sovereign_handle = start_maybe_quiet(
let sovereign_handle = with_logs(
"Launching sovereign rollup",
cmd!(
"sh", "-c",
"cd demo/sovereign/demo-rollup && ./../target/release/sov-demo-rollup"
),
params.quiet,
)?;
).start()?;

Ok(Self(sovereign_handle))
Ok(Self {
process: sovereign_handle,
with_logs,
})
}

// All the networks must be up (relaychain and sugondat-node), including the sovereign rollup."
Expand All @@ -34,37 +44,43 @@ impl Sovereign {
//TODO: https://github.com/thrumdev/blobs/issues/227
let cli = "../target/release/sov-cli";
let test_data_path = "../test-data/";
let run_cli_cmd = |args: &str| {
let args = [
"-c",
&format!("cd demo/sovereign/demo-rollup/ && ./{} {}", cli, args),
];

duct::cmd("sh", args).run()
};

info!("setup rpc endpoint");
run_cli_cmd("rpc set-url http://127.0.0.1:12345")?;

info!("import keys");
run_cli_cmd(&format!(
"keys import --nickname token_deployer --path {}keys/token_deployer_private_key.json",
test_data_path
))?;

info!("create and mint a new token");
run_cli_cmd(&format!(
"transactions import from-file bank --path {}requests/create_token.json",
test_data_path
))?;

run_cli_cmd(&format!(
"transactions import from-file bank --path {}requests/mint.json",
test_data_path
))?;

info!("submit batch with two transactions");
run_cli_cmd("rpc submit-batch by-nickname token_deployer")?;
let run_cli_cmd =
|description: &str, args: &str| -> std::io::Result<std::process::Output> {
let args = [
"-c",
&format!("cd demo/sovereign/demo-rollup/ && ./{} {}", cli, args),
];

(self.with_logs)(description, duct::cmd("sh", args)).run()
};

run_cli_cmd("setup rpc endpoint", "rpc set-url http://127.0.0.1:12345")?;

run_cli_cmd(
"import keys",
&format!("keys import --nickname token_deployer --path {}keys/token_deployer_private_key.json", test_data_path),
)?;

run_cli_cmd(
"create a new token",
&format!(
"transactions import from-file bank --path {}requests/create_token.json",
test_data_path
),
)?;

run_cli_cmd(
"mint just created token",
&format!(
"transactions import from-file bank --path {}requests/mint.json",
test_data_path
),
)?;

run_cli_cmd(
"submit batch with two transactions",
"rpc submit-batch by-nickname token_deployer",
)?;

// TODO: https://github.com/thrumdev/blobs/issues/226
info!("waiting for the rollup to process the transactions");
Expand All @@ -90,6 +106,6 @@ impl Drop for Sovereign {
// duct::Handle does not implement kill on drop
fn drop(&mut self) {
info!("Sovereign rollup process is going to be killed");
let _ = self.0.kill();
let _ = self.process.kill();
}
}
12 changes: 7 additions & 5 deletions xtask/src/zombienet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cli::test::ZombienetParams, start_maybe_quiet};
use crate::{cli::test::ZombienetParams, logging::create_with_logs};
use duct::cmd;
use tracing::info;

Expand Down Expand Up @@ -43,12 +43,14 @@ impl Zombienet {
cd to 'sugondat/chain' and run 'cargo build --release' and add the result into your PATH."
)?;

info!("Launching zombienet");
tracing::info!("Zombienet logs redirected to {}", params.log_path);
let with_logs = create_with_logs(params.log_path);

#[rustfmt::skip]
let zombienet_handle = start_maybe_quiet(
let zombienet_handle = with_logs(
"Launching zombienet",
cmd!("zombienet", "spawn", "-p", "native", "--dir", "zombienet", "testnet.toml"),
params.quiet,
)?;
).start()?;

Ok(Self(zombienet_handle))
}
Expand Down