-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.rs
More file actions
169 lines (138 loc) · 4.52 KB
/
main.rs
File metadata and controls
169 lines (138 loc) · 4.52 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use {
anyhow::Context,
clap::{Args, Parser, Subcommand},
derive_more::AsRef,
std::io::{self, Write as _},
wcn_cluster::NodeOperator,
};
mod deploy;
mod maintenance;
mod migration;
mod operator;
mod settings;
mod view;
type Cluster = wcn_cluster::Cluster<ClusterConfig>;
/// WCN Admin CLI.
#[derive(Debug, Parser)]
#[command(name = "wcn_admin", version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// Deploy a new Cluster
Deploy(deploy::Args),
/// Get overview of the Cluster
View(ClusterArgs),
/// Operator management
#[command(subcommand)]
Operator(operator::Command),
/// Migration management
#[command(subcommand)]
Migration(migration::Command),
/// Maintenance scheduling
#[command(subcommand)]
Maintenance(maintenance::Command),
/// Cluster settings management
#[command(subcommand)]
Settings(settings::Command),
}
#[derive(Debug, Args)]
struct ClusterArgs {
/// Private key of the WCN Cluster Smart-Contract owner
#[arg(
id = "PRIVATE_KEY",
long = "private-key",
env = "WCN_CLUSTER_SMART_CONTRACT_OWNER_PRIVATE_KEY"
)]
signer: wcn_cluster::smart_contract::evm::Signer,
/// WCN Cluster Smart-Contract encryption key
#[arg(
long = "encryption-key",
env = "WCN_CLUSTER_SMART_CONTRACT_ENCRYPTION_KEY"
)]
encryption_key: wcn_cluster::EncryptionKey,
/// WCN Cluster Smart-Contract address
#[arg(long = "contract-address", env = "WCN_CLUSTER_SMART_CONTRACT_ADDRESS")]
contract_address: wcn_cluster::smart_contract::Address,
/// Optimism RPC provider URL
///
/// Only ws:// and wss:// are supported
#[arg(long = "rpc-provider-url", env = "OPTIMISM_RPC_PROVIDER_URL")]
rpc_provider_url: wcn_cluster::smart_contract::evm::RpcUrl,
}
impl ClusterArgs {
async fn connect(self) -> anyhow::Result<Cluster> {
let cfg = ClusterConfig {
encryption_key: self.encryption_key,
};
let connector =
wcn_cluster::smart_contract::evm::RpcProvider::new(self.rpc_provider_url, self.signer)
.await
.context("RpcProvider::new")?;
Cluster::connect(cfg, &connector, self.contract_address)
.await
.context("Cluster::connect")
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Command::Deploy(args) => deploy::execute(args).await,
Command::View(args) => view::execute(args).await,
Command::Operator(cmd) => operator::execute(cmd).await,
Command::Migration(cmd) => migration::execute(cmd).await,
Command::Maintenance(cmd) => maintenance::execute(cmd).await,
Command::Settings(cmd) => settings::execute(cmd).await,
}
}
#[derive(AsRef, Clone, Copy)]
pub(crate) struct ClusterConfig {
#[as_ref]
encryption_key: wcn_cluster::EncryptionKey,
}
impl wcn_cluster::Config for ClusterConfig {
type SmartContract = wcn_cluster::smart_contract::evm::SmartContract;
type KeyspaceShards = ();
type Node = wcn_cluster::Node;
fn new_node(
&self,
_operator_id: wcn_cluster::node_operator::Id,
node: wcn_cluster::Node,
) -> Self::Node {
node
}
fn update_settings(&self, _settings: &wcn_cluster::Settings) {}
}
fn ask_approval() -> anyhow::Result<bool> {
print!("Proceed? (y/yes):");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
Ok(["y", "yes"].contains(&input.trim()))
}
fn print_node_operator(idx: Option<u8>, operator: &NodeOperator) {
let idx = idx.map(|idx| idx.to_string()).unwrap_or("?".to_string());
println!("operator[{idx}]: {} {}", operator.name, operator.id);
for (idx, node) in operator.nodes().iter().enumerate() {
let id = node.peer_id;
let addr = node.ipv4_addr;
let priv_addr = node
.private_ipv4_addr
.map(|addr| addr.to_string())
.unwrap_or_else(|| "None".to_string());
let port0 = node.primary_port;
let port1 = node.secondary_port;
println!("\tnode[{idx}]: {id} {addr} {priv_addr} {port0} {port1}");
}
if !operator.clients.is_empty() {
for (idx, client) in operator.clients.iter().enumerate() {
let id = client.peer_id;
let namespaces = &client.authorized_namespaces;
println!("\tclient[{idx}]: {id} {namespaces:?}");
}
}
println!()
}