-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset_nodelist.rs
77 lines (68 loc) · 2.67 KB
/
set_nodelist.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Rivtower Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::config::chain_config::ConfigStage;
use crate::config::chain_config::NodeNetworkAddressBuilder;
use crate::constant::CHAIN_CONFIG_FILE;
use crate::error::Error;
use crate::util::{rand_string, read_chain_config, write_toml};
use clap::Parser;
/// A subcommand for run
#[derive(Parser, Debug, Clone)]
pub struct SetNodeListOpts {
/// set chain name
#[clap(long = "chain-name", default_value = "test-chain")]
pub(crate) chain_name: String,
/// set config file directory, default means current directory
#[clap(long = "config-dir", default_value = ".")]
pub(crate) config_dir: String,
/// node list looks like localhost:40000:node0:k8s_cluster1,localhost:40001:node1:k8s_cluster2
/// last slice is optional, none means not k8s env.
#[clap(long = "nodelist")]
pub(crate) node_list: String,
}
/// execute set node list
pub fn execute_set_nodelist(opts: SetNodeListOpts) -> Result<(), Error> {
// load chain_config
let file_name = format!(
"{}/{}/{}",
&opts.config_dir, &opts.chain_name, CHAIN_CONFIG_FILE
);
let mut chain_config = read_chain_config(&file_name).unwrap();
// public and finalize is ok
if chain_config.stage == ConfigStage::Init {
return Err(Error::InvalidStage);
}
let node_list_str: Vec<&str> = opts.node_list.split(',').collect();
let node_list = node_list_str
.iter()
.map(|node| {
let node_network_info: Vec<&str> = node.split(':').collect();
let cluster = if node_network_info.len() == 3 {
rand_string()
} else {
node_network_info[3].to_string()
};
NodeNetworkAddressBuilder::default()
.host(node_network_info[0].to_string())
.port(node_network_info[1].parse::<u16>().unwrap())
.domain(node_network_info[2].to_string())
.cluster(cluster)
.build()
})
.collect();
chain_config.set_node_network_address_list(node_list);
// store chain_config
write_toml(&chain_config, file_name);
Ok(())
}