Skip to content

Commit 248422a

Browse files
committed
support: change file on target nodes
1 parent ba4d7fe commit 248422a

File tree

3 files changed

+189
-5
lines changed

3 files changed

+189
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chaindev"
3-
version = "0.52.8"
3+
version = "0.52.9-alpha.0"
44
edition = "2021"
55
authors = ["hui.fan@mail.ru"]
66
description = "Powerful development and testing utils for blockchain developers."

src/beacon_based/ddev/remote.rs

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,95 @@ use ruc::*;
1111
use std::collections::BTreeMap;
1212
use std::thread;
1313

14+
pub fn chg_file_on_nodes<C, P, S>(
15+
env: &Env<C, P, S>,
16+
ids: Option<&[NodeID]>,
17+
local_file: &str, // local absolute path on the control host
18+
remote_file: &str, // remote file path relative to the node home
19+
) -> Result<()>
20+
where
21+
C: CustomData,
22+
P: NodePorts,
23+
S: NodeCmdGenerator<Node<P>, EnvMeta<C, Node<P>>>,
24+
{
25+
if let Some(ids) = ids {
26+
for id in ids.iter() {
27+
if env
28+
.meta
29+
.nodes
30+
.get(id)
31+
.or_else(|| env.meta.fuhrers.get(id))
32+
.is_none()
33+
{
34+
return Err(eg!("The node(id: {}) does not exist!", id));
35+
}
36+
}
37+
}
38+
39+
let mut errlist = vec![];
40+
41+
// Use chunks to avoid resource overload
42+
for (idx, nodes) in env
43+
.meta
44+
.fuhrers
45+
.values()
46+
.chain(env.meta.nodes.values())
47+
.filter(|n| ids.map(|ids| ids.contains(&n.id)).unwrap_or(true))
48+
.collect::<Vec<_>>()
49+
.chunks(12)
50+
.enumerate()
51+
{
52+
let mut path_map = BTreeMap::new();
53+
54+
thread::scope(|s| {
55+
nodes
56+
.iter()
57+
.map(|n| {
58+
let host = n.host.clone();
59+
let remote_file = format!("{}/{remote_file}", n.home);
60+
s.spawn(move || {
61+
let remote = Remote::from(&host);
62+
remote.put_file(local_file, &remote_file).c(d!()).map(|_| {
63+
(
64+
local_file,
65+
format!(
66+
"[{},N{},{}] {}",
67+
host.addr.connection_addr(),
68+
n.id,
69+
n.kind,
70+
remote_file
71+
),
72+
)
73+
})
74+
})
75+
})
76+
.collect::<Vec<_>>()
77+
.into_iter()
78+
.flat_map(|h| h.join())
79+
.for_each(|t| match t {
80+
Ok((f, lp)) => {
81+
path_map.entry(f).or_insert_with(Vec::new).push(lp);
82+
}
83+
Err(e) => {
84+
errlist.push(e);
85+
}
86+
});
87+
});
88+
89+
// Print good resp at first,
90+
path_map.into_iter().for_each(|(f, mut paths)| {
91+
println!("[Chunk {idx}] The '{}' have been put at:", f);
92+
paths.sort();
93+
paths.iter().for_each(|p| {
94+
println!("\t- {}", p);
95+
});
96+
});
97+
}
98+
99+
// Then pop err msg
100+
check_errlist!(errlist)
101+
}
102+
14103
pub fn collect_files_from_nodes<C, P, S>(
15104
env: &Env<C, P, S>,
16105
ids: Option<&[NodeID]>,
@@ -96,7 +185,7 @@ where
96185

97186
// Print good resp at first,
98187
path_map.into_iter().for_each(|(f, mut paths)| {
99-
println!("[Chunk {idx}] Files of the '{}' are stored at:", f);
188+
println!("[Chunk {idx}] Files of the '{}' have been put at:", f);
100189
paths.sort();
101190
paths.iter().for_each(|p| {
102191
println!("\t- {}", p);
@@ -204,7 +293,10 @@ where
204293

205294
// Print good resp at first,
206295
path_map.into_iter().for_each(|(f, mut paths)| {
207-
println!("[Chunk {idx}] Tar packages of the '{}' are stored at:", f);
296+
println!(
297+
"[Chunk {idx}] Tar packages of the '{}' have been put at:",
298+
f
299+
);
208300
paths.sort();
209301
paths.iter().for_each(|p| {
210302
println!("\t- {}", p);

src/tendermint_based/ddev/remote.rs

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,95 @@ use serde::{Deserialize, Serialize};
99
use std::collections::BTreeMap;
1010
use std::{fmt, thread};
1111

12+
pub fn chg_file_on_nodes<C, P, S>(
13+
env: &Env<C, P, S>,
14+
ids: Option<&[NodeID]>,
15+
local_file: &str, // local absolute path on the control host
16+
remote_file: &str, // remote file path relative to the node home
17+
) -> Result<()>
18+
where
19+
C: Send + Sync + fmt::Debug + Clone + Serialize + for<'a> Deserialize<'a>,
20+
P: NodePorts,
21+
S: NodeCmdGenerator<Node<P>, EnvMeta<C, Node<P>>>,
22+
{
23+
if let Some(ids) = ids {
24+
for id in ids.iter() {
25+
if env
26+
.meta
27+
.nodes
28+
.get(id)
29+
.or_else(|| env.meta.fuhrers.get(id))
30+
.is_none()
31+
{
32+
return Err(eg!("The node(id: {}) does not exist!", id));
33+
}
34+
}
35+
}
36+
37+
let mut errlist = vec![];
38+
39+
// Use chunks to avoid resource overload
40+
for (idx, nodes) in env
41+
.meta
42+
.fuhrers
43+
.values()
44+
.chain(env.meta.nodes.values())
45+
.filter(|n| ids.map(|ids| ids.contains(&n.id)).unwrap_or(true))
46+
.collect::<Vec<_>>()
47+
.chunks(12)
48+
.enumerate()
49+
{
50+
let mut path_map = BTreeMap::new();
51+
52+
thread::scope(|s| {
53+
nodes
54+
.iter()
55+
.map(|n| {
56+
let host = n.host.clone();
57+
let remote_file = format!("{}/{remote_file}", n.home);
58+
s.spawn(move || {
59+
let remote = Remote::from(&host);
60+
remote.put_file(local_file, &remote_file).c(d!()).map(|_| {
61+
(
62+
local_file,
63+
format!(
64+
"[{},N{},{}] {}",
65+
host.addr.connection_addr(),
66+
n.id,
67+
n.kind,
68+
remote_file
69+
),
70+
)
71+
})
72+
})
73+
})
74+
.collect::<Vec<_>>()
75+
.into_iter()
76+
.flat_map(|h| h.join())
77+
.for_each(|t| match t {
78+
Ok((f, lp)) => {
79+
path_map.entry(f).or_insert_with(Vec::new).push(lp);
80+
}
81+
Err(e) => {
82+
errlist.push(e);
83+
}
84+
});
85+
});
86+
87+
// Print good resp at first,
88+
path_map.into_iter().for_each(|(f, mut paths)| {
89+
println!("[Chunk {idx}] The '{}' have been put at:", f);
90+
paths.sort();
91+
paths.iter().for_each(|p| {
92+
println!("\t- {}", p);
93+
});
94+
});
95+
}
96+
97+
// Then pop err msg
98+
check_errlist!(errlist)
99+
}
100+
12101
pub fn collect_files_from_nodes<C, P, S>(
13102
env: &Env<C, P, S>,
14103
ids: Option<&[NodeID]>,
@@ -94,7 +183,7 @@ where
94183

95184
// Print good resp at first,
96185
path_map.into_iter().for_each(|(f, mut paths)| {
97-
println!("[Chunk {idx}] Files of the '{}' are stored at:", f);
186+
println!("[Chunk {idx}] Files of the '{}' have been put at:", f);
98187
paths.sort();
99188
paths.iter().for_each(|p| {
100189
println!("\t- {}", p);
@@ -202,7 +291,10 @@ where
202291

203292
// Print good resp at first,
204293
path_map.into_iter().for_each(|(f, mut paths)| {
205-
println!("[Chunk {idx}] Tar packages of the '{}' are stored at:", f);
294+
println!(
295+
"[Chunk {idx}] Tar packages of the '{}' have been put at:",
296+
f
297+
);
206298
paths.sort();
207299
paths.iter().for_each(|p| {
208300
println!("\t- {}", p);

0 commit comments

Comments
 (0)