Skip to content

Commit

Permalink
support switch EL clients between reth and geth
Browse files Browse the repository at this point in the history
  • Loading branch information
haxjump committed Oct 9, 2024
1 parent 10b5373 commit c715b0a
Show file tree
Hide file tree
Showing 3 changed files with 210 additions and 9 deletions.
44 changes: 43 additions & 1 deletion src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ pub enum DevOp {
#[clap(short = 'N', long)]
node_id: Option<NodeID>,
},
#[clap(
name = "switch-EL-to-geth",
about = "Switch the EL client to `geth`, NOTE: the node will be left stopped, you need to start it manually"
)]
SwitchELToGeth {
#[clap(short = 'e', long)]
env_name: Option<String>,
#[clap(short = 'N', long)]
node_id: NodeID,
},
#[clap(
name = "switch-EL-to-reth",
about = "Switch the EL client to `reth`, NOTE: the node will be left stopped, you need to start it manually"
)]
SwitchELToReth {
#[clap(short = 'e', long)]
env_name: Option<String>,
#[clap(short = 'N', long)]
node_id: NodeID,
},
#[clap(about = "Default operation, show the information of an existing ENV")]
Show {
#[clap(short = 'e', long)]
Expand Down Expand Up @@ -263,7 +283,9 @@ pub enum DDevOp {
#[clap(long, help = "To get a archive node, set true")]
is_archive: bool,
},
#[clap(about = "Migrate an existing node to another host")]
#[clap(
about = "Migrate an existing node to another host, NOTE: the node will be left stopped, you need to start it manually"
)]
MigrateNode {
#[clap(short = 'e', long)]
env_name: Option<String>,
Expand All @@ -279,6 +301,26 @@ pub enum DDevOp {
#[clap(short = 'N', long)]
node_id: Option<NodeID>,
},
#[clap(
name = "switch-EL-to-geth",
about = "Switch the EL client to `geth`, NOTE: the node will be left stopped, you need to start it manually"
)]
SwitchELToGeth {
#[clap(short = 'e', long)]
env_name: Option<String>,
#[clap(short = 'N', long)]
node_id: NodeID,
},
#[clap(
name = "switch-EL-to-reth",
about = "Switch the EL client to `reth`, NOTE: the node will be left stopped, you need to start it manually"
)]
SwitchELToReth {
#[clap(short = 'e', long)]
env_name: Option<String>,
#[clap(short = 'N', long)]
node_id: NodeID,
},
#[clap(about = "Add a new host to the cluster")]
PushHost {
#[clap(short = 'e', long)]
Expand Down
83 changes: 79 additions & 4 deletions src/ddev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use chaindev::{
collect_files_from_nodes as env_collect_files,
collect_tgz_from_nodes as env_collect_tgz, Remote,
},
Env, EnvCfg as SysCfg, EnvMeta, EnvOpts as SysOpts, HostAddr, Hosts, Node,
NodeCmdGenerator, NodeKind, Op, NODE_HOME_GENESIS_DST, NODE_HOME_VCDATA_DST,
Env as SysEnv, EnvCfg as SysCfg, EnvMeta, EnvOpts as SysOpts, HostAddr, Hosts,
Node, NodeCmdGenerator, NodeKind, Op, NODE_HOME_GENESIS_DST,
NODE_HOME_VCDATA_DST,
},
CustomOps, EnvName,
CustomOps, EnvName, NodeID,
};
use ruc::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -258,6 +259,18 @@ impl From<DDevCfg> for EnvCfg {
}
Op::Custom(ExtraOp::DumpVcData(local_base_dir))
}
DDevOp::SwitchELToGeth { env_name, node_id } => {
if let Some(n) = env_name {
en = n.into();
}
Op::Custom(ExtraOp::SwitchELToGeth(node_id))
}
DDevOp::SwitchELToReth { env_name, node_id } => {
if let Some(n) = env_name {
en = n.into();
}
Op::Custom(ExtraOp::SwitchELToReth(node_id))
}
};

Self {
Expand Down Expand Up @@ -639,11 +652,13 @@ fn env_hosts() -> Option<Hosts> {
enum ExtraOp {
GetLogs(Option<String>),
DumpVcData(Option<String>),
SwitchELToGeth(NodeID),
SwitchELToReth(NodeID),
}

impl CustomOps for ExtraOp {
fn exec(&self, en: &EnvName) -> Result<()> {
let env = Env::<CustomInfo, Ports, CmdGenerator>::load_env_by_name(en)
let mut env = SysEnv::<CustomInfo, Ports, CmdGenerator>::load_env_by_name(en)
.c(d!())?
.c(d!("ENV does not exist!"))?;

Expand All @@ -661,6 +676,66 @@ impl CustomOps for ExtraOp {
Self::DumpVcData(ldir) => {
env_collect_tgz(&env, &["{CL_VC_DIR}"], ldir.as_deref()).c(d!())
}
Self::SwitchELToGeth(id) => {
let n = env
.meta
.nodes
.get_mut(id)
.or_else(|| env.meta.bootstraps.get_mut(id))
.c(d!())?;

SysCfg {
name: en.clone(),
op: Op::<CustomInfo, Ports, ExtraOp>::Stop((Some(*id), false)),
}
.exec(CmdGenerator)
.c(d!())?;

sleep_ms!(3000); // wait for the graceful exiting process

n.mark = Some(GETH_MARK);

let remote = Remote::from(&n.host);

// Just remove $EL_DIR.
// When starting up, if $EL_DIR is detected to not exist,
// the new client will re-create it, and sync data from the CL.
remote
.exec_cmd(&format!("rm -rf {}/{EL_DIR}", n.home))
.c(d!())?;

env.write_cfg().c(d!())
}
Self::SwitchELToReth(id) => {
let n = env
.meta
.nodes
.get_mut(id)
.or_else(|| env.meta.bootstraps.get_mut(id))
.c(d!())?;

SysCfg {
name: en.clone(),
op: Op::<CustomInfo, Ports, ExtraOp>::Stop((Some(*id), false)),
}
.exec(CmdGenerator)
.c(d!())?;

sleep_ms!(3000); // wait for the graceful exiting process

n.mark = Some(RETH_MARK);

let remote = Remote::from(&n.host);

// Just remove $EL_DIR.
// When starting up, if $EL_DIR is detected to not exist,
// the new client will re-create it, and sync data from the CL.
remote
.exec_cmd(&format!("rm -rf {}/{EL_DIR}", n.home))
.c(d!())?;

env.write_cfg().c(d!())
}
}
}
}
92 changes: 88 additions & 4 deletions src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ use crate::{
};
use chaindev::{
beacon_dev::{
EnvCfg as SysCfg, EnvMeta, EnvOpts as SysOpts, Node, NodeCmdGenerator, NodeKind,
Op, NODE_HOME_GENESIS_DST, NODE_HOME_VCDATA_DST,
Env as SysEnv, EnvCfg as SysCfg, EnvMeta, EnvOpts as SysOpts, Node,
NodeCmdGenerator, NodeKind, Op, NODE_HOME_GENESIS_DST, NODE_HOME_VCDATA_DST,
},
EnvName,
CustomOps, EnvName, NodeID,
};
use ruc::*;
use serde::{Deserialize, Serialize};
use std::fs;

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EnvCfg {
sys_cfg: SysCfg<CustomInfo, Ports, ()>,
sys_cfg: SysCfg<CustomInfo, Ports, ExtraOp>,
}

impl From<DevCfg> for EnvCfg {
Expand Down Expand Up @@ -133,6 +133,18 @@ impl From<DevCfg> for EnvCfg {
}
Op::KickNode(node_id)
}
DevOp::SwitchELToGeth { env_name, node_id } => {
if let Some(n) = env_name {
en = n.into();
}
Op::Custom(ExtraOp::SwitchELToGeth(node_id))
}
DevOp::SwitchELToReth { env_name, node_id } => {
if let Some(n) = env_name {
en = n.into();
}
Op::Custom(ExtraOp::SwitchELToReth(node_id))
}
DevOp::Show { env_name } => {
if let Some(n) = env_name {
en = n.into();
Expand Down Expand Up @@ -476,3 +488,75 @@ fi "#
)
}
}

//////////////////////////////////////////////////
//////////////////////////////////////////////////

#[derive(Clone, Debug, Serialize, Deserialize)]
enum ExtraOp {
SwitchELToGeth(NodeID),
SwitchELToReth(NodeID),
}

impl CustomOps for ExtraOp {
fn exec(&self, en: &EnvName) -> Result<()> {
let mut env = SysEnv::<CustomInfo, Ports, CmdGenerator>::load_env_by_name(en)
.c(d!())?
.c(d!("ENV does not exist!"))?;

match self {
Self::SwitchELToGeth(id) => {
let n = env
.meta
.nodes
.get_mut(id)
.or_else(|| env.meta.bootstraps.get_mut(id))
.c(d!())?;

SysCfg {
name: en.clone(),
op: Op::<CustomInfo, Ports, ExtraOp>::Stop((Some(*id), false)),
}
.exec(CmdGenerator)
.c(d!())?;

sleep_ms!(3000); // wait for the graceful exiting process

n.mark = Some(GETH_MARK);

// Just remove $EL_DIR.
// When starting up, if $EL_DIR is detected to not exist,
// the new client will re-create it, and sync data from the CL.
fs::remove_dir_all(format!("{}/{EL_DIR}", n.home)).c(d!())?;

env.write_cfg().c(d!())
}
Self::SwitchELToReth(id) => {
let n = env
.meta
.nodes
.get_mut(id)
.or_else(|| env.meta.bootstraps.get_mut(id))
.c(d!())?;

SysCfg {
name: en.clone(),
op: Op::<CustomInfo, Ports, ExtraOp>::Stop((Some(*id), false)),
}
.exec(CmdGenerator)
.c(d!())?;

sleep_ms!(3000); // wait for the graceful exiting process

n.mark = Some(RETH_MARK);

// Just remove $EL_DIR.
// When starting up, if $EL_DIR is detected to not exist,
// the new client will re-create it, and sync data from the CL.
fs::remove_dir_all(format!("{}/{EL_DIR}", n.home)).c(d!())?;

env.write_cfg().c(d!())
}
}
}
}

0 comments on commit c715b0a

Please sign in to comment.