-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add a feature to create automatically a random temporary directory for base path & remove Clone
#6221
Add a feature to create automatically a random temporary directory for base path & remove Clone
#6221
Changes from 7 commits
7fbed1b
aa518e4
5338633
baf6ad2
179dda3
2aea88f
b6358e8
f81522d
55ed555
cfc6fa4
5a5c546
bf1da7e
68dfe89
898dcd6
d26da87
9c6f71a
9e989de
c811cb6
65cb074
fdb3f20
39bc022
cb140b6
ca42803
468f16a
49cfe00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
#![cfg(unix)] | ||
|
||
use assert_cmd::cargo::cargo_bin; | ||
use nix::sys::signal::{kill, Signal::SIGINT}; | ||
use nix::unistd::Pid; | ||
use regex::Regex; | ||
use std::convert::TryInto; | ||
use std::io::Read; | ||
use std::path::PathBuf; | ||
use std::process::{Command, Stdio}; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
pub mod common; | ||
|
||
#[test] | ||
fn temp_base_path_works() { | ||
let mut cmd = Command::new(cargo_bin("substrate")); | ||
|
||
let mut cmd = cmd | ||
.args(&["--dev", "--tmp"]) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::piped()) | ||
.spawn() | ||
.unwrap(); | ||
|
||
// Let it produce some blocks. | ||
thread::sleep(Duration::from_secs(30)); | ||
assert!( | ||
cmd.try_wait().unwrap().is_none(), | ||
"the process should still be running" | ||
); | ||
|
||
// Stop the process | ||
kill(Pid::from_raw(cmd.id().try_into().unwrap()), SIGINT).unwrap(); | ||
assert!(common::wait_for(&mut cmd, 40) | ||
.map(|x| x.success()) | ||
.unwrap_or_default()); | ||
|
||
// Ensure the database has been deleted | ||
let mut stderr = String::new(); | ||
cmd.stderr.unwrap().read_to_string(&mut stderr).unwrap(); | ||
let re = Regex::new(r"Database: .+ at (\S+)").unwrap(); | ||
let db_path = PathBuf::from( | ||
re.captures(stderr.as_str()) | ||
.unwrap() | ||
.get(1) | ||
.unwrap() | ||
.as_str() | ||
.to_string(), | ||
); | ||
|
||
assert!(!db_path.exists()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,21 +21,26 @@ use crate::error::{Error, Result}; | |
use crate::params::ImportParams; | ||
use crate::params::KeystoreParams; | ||
use crate::params::NetworkParams; | ||
use crate::params::OffchainWorkerParams; | ||
use crate::params::SharedParams; | ||
use crate::params::TransactionPoolParams; | ||
use crate::params::OffchainWorkerParams; | ||
use crate::CliConfiguration; | ||
use regex::Regex; | ||
use sc_service::{ | ||
config::{MultiaddrWithPeerId, PrometheusConfig, TransactionPoolOptions}, | ||
ChainSpec, Role, | ||
}; | ||
use sc_telemetry::TelemetryEndpoints; | ||
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; | ||
use std::{ | ||
cell::RefCell, | ||
net::{IpAddr, Ipv4Addr, SocketAddr}, | ||
path::PathBuf, | ||
}; | ||
use structopt::StructOpt; | ||
use tempfile::TempDir; | ||
|
||
/// The `run` command used to run a node. | ||
#[derive(Debug, StructOpt, Clone)] | ||
#[derive(Debug, StructOpt)] | ||
pub struct RunCmd { | ||
/// Enable validator mode. | ||
/// | ||
|
@@ -250,6 +255,18 @@ pub struct RunCmd { | |
conflicts_with_all = &[ "sentry", "public-addr" ] | ||
)] | ||
pub sentry_nodes: Vec<MultiaddrWithPeerId>, | ||
|
||
/// Run a temporary node. | ||
/// | ||
/// A random temporary directory will be created to store the configuration and will be deleted | ||
/// at the end of the process. | ||
/// | ||
/// Note: the directory is random per process execution. | ||
#[structopt(long)] | ||
pub tmp: bool, | ||
|
||
#[structopt(skip)] | ||
temp_base_path: RefCell<Option<TempDir>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. This is a really bad hack. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What part you don't like exactly? (so I can find another way) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not having the temp base path as parameter here. We could create some custom type that we return in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok done! Now the TempDir is hold in the configuration object instead of the SubstrateCli object, then it's move to the service and dropped with it. Also the core of the feature itself has moved from sc-cli to sc-service. |
||
} | ||
|
||
impl RunCmd { | ||
|
@@ -446,6 +463,18 @@ impl CliConfiguration for RunCmd { | |
fn max_runtime_instances(&self) -> Result<Option<usize>> { | ||
Ok(self.max_runtime_instances.map(|x| x.min(256))) | ||
} | ||
|
||
fn base_path(&self) -> Result<Option<PathBuf>> { | ||
Ok(if self.tmp { | ||
let tmp = tempfile::Builder::new().prefix("substrate").tempdir()?; | ||
let path = tmp.path().into(); | ||
*self.temp_base_path.borrow_mut() = Some(tmp); | ||
|
||
Some(path) | ||
} else { | ||
self.shared_params().base_path() | ||
}) | ||
} | ||
} | ||
|
||
/// Check whether a node name is considered as valid. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"random temporary" => "temporary"
Please also make the user aware that this is the "base-path" and what will be part of this path. Database, node key etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in 55ed555