From f2ea49db5830b19aa117a0f07e3a399588deaeb0 Mon Sep 17 00:00:00 2001 From: Greg Cusack Date: Thu, 23 May 2024 10:55:18 -0500 Subject: [PATCH] switch docker tag to commit/release-version (#42) * rm --tag and replace with commit/release version, use build-type skip for skip-docker-build * address comments --- README.md | 1 - src/docker.rs | 2 +- src/lib.rs | 2 +- src/main.rs | 30 ++++------------- src/release.rs | 75 ++++++++++++++++++++---------------------- src/startup_scripts.rs | 2 +- 6 files changed, 46 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 86cdfc0..76d4f12 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,6 @@ cargo run --bin cluster -- --slots-per-epoch # docker config --registry # e.g. gregcusack - --tag # e.g. v1 --base-image # e.g. ubuntu:20.04 --image-name # e.g. cluster-image # validator config diff --git a/src/docker.rs b/src/docker.rs index 0b0df72..5c3d6dc 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -18,7 +18,7 @@ pub struct DockerImage { registry: String, validator_type: ValidatorType, image_name: String, - tag: String, + tag: String, // commit (`abcd1234`) or version (`v1.18.12`) } impl DockerImage { diff --git a/src/lib.rs b/src/lib.rs index f0a695d..1d1ffdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -135,7 +135,7 @@ pub fn cat_file(path: &PathBuf) -> std::io::Result<()> { let mut file = File::open(path)?; let mut contents = String::new(); file.read_to_string(&mut contents)?; - info!("{:?}:\n{contents}", path.file_name()); + debug!("{:?}:\n{contents}", path.file_name()); Ok(()) } diff --git a/src/main.rs b/src/main.rs index 2c57946..5a6bed3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,8 @@ fn parse_matches() -> clap::ArgMatches { .takes_value(true) .possible_values(BuildType::VARIANTS) .default_value(BuildType::Release.into()) - .help("Specifies the build type: skip, debug, or release."), + .help("Specifies the build type: skip, debug, or release. + Skip -> Will not build release or local repo and will not push to container registry"), ) .arg( Arg::with_name("release_channel") @@ -145,11 +146,6 @@ fn parse_matches() -> clap::ArgMatches { .help("Genesis config. bootstrap validator stake sol"), ) //Docker config - .arg( - Arg::with_name("skip_docker_build") - .long("skip-docker-build") - .help("Skips build Docker images"), - ) .arg( Arg::with_name("registry_name") .long("registry") @@ -161,7 +157,7 @@ fn parse_matches() -> clap::ArgMatches { Arg::with_name("image_name") .long("image-name") .takes_value(true) - .default_value("k8s-cluster-image") + .default_value("k8s-image") .help("Docker image name. Will be prepended with validator_type (bootstrap or validator)"), ) .arg( @@ -171,13 +167,6 @@ fn parse_matches() -> clap::ArgMatches { .default_value("ubuntu:20.04") .help("Docker base image"), ) - .arg( - Arg::with_name("image_tag") - .long("tag") - .takes_value(true) - .default_value("latest") - .help("Docker image tag."), - ) // Bootstrap/Validator Config .arg( Arg::with_name("limit_ledger_size") @@ -429,9 +418,8 @@ async fn main() -> Result<(), Box> { let build_config = BuildConfig::new( deploy_method.clone(), - build_type, + build_type.clone(), solana_root.get_root_path(), - !matches.is_present("skip_docker_build"), ); let genesis_flags = GenesisFlags { @@ -538,8 +526,8 @@ async fn main() -> Result<(), Box> { .into()); } - build_config.prepare().await?; - info!("Setup Validator Environment"); + let image_tag = build_config.prepare().await?; + info!("Setup Validator Environment. Image tag: {image_tag}"); let config_directory = solana_root.get_root_path().join("config-k8s"); let mut genesis = Genesis::new(config_directory.clone(), genesis_flags); @@ -586,10 +574,6 @@ async fn main() -> Result<(), Box> { let registry_name = matches.value_of("registry_name").unwrap().to_string(); let image_name = matches.value_of("image_name").unwrap().to_string(); - let image_tag = matches - .value_of("image_tag") - .unwrap_or_default() - .to_string(); let mut cluster_images = ClusterImages::default(); @@ -631,7 +615,7 @@ async fn main() -> Result<(), Box> { cluster_images.set_item(client, ValidatorType::Client(client_index)); } - if build_config.docker_build() { + if build_type != BuildType::Skip { for v in cluster_images.get_all() { docker.build_image(solana_root.get_root_path(), v.image())?; info!("Built {} image", v.validator_type()); diff --git a/src/release.rs b/src/release.rs index 7e19fe2..6770ea9 100644 --- a/src/release.rs +++ b/src/release.rs @@ -17,7 +17,7 @@ pub enum DeployMethod { ReleaseChannel(String), } -#[derive(PartialEq, EnumString, IntoStaticStr, VariantNames)] +#[derive(PartialEq, EnumString, IntoStaticStr, VariantNames, Clone)] #[strum(serialize_all = "lowercase")] pub enum BuildType { Skip, // use Agave build from the previous run @@ -29,7 +29,6 @@ pub struct BuildConfig { deploy_method: DeployMethod, build_type: BuildType, solana_root_path: PathBuf, - docker_build: bool, } impl BuildConfig { @@ -37,43 +36,35 @@ impl BuildConfig { deploy_method: DeployMethod, build_type: BuildType, solana_root_path: &Path, - docker_build: bool, ) -> Self { BuildConfig { deploy_method, build_type, solana_root_path: solana_root_path.to_path_buf(), - docker_build, } } - pub fn docker_build(&self) -> bool { - self.docker_build - } - - pub async fn prepare(&self) -> Result<(), Box> { - if self.build_type == BuildType::Skip { - info!("skipping build"); - return Ok(()); - } + pub async fn prepare(&self) -> Result> { match &self.deploy_method { DeployMethod::ReleaseChannel(channel) => { + if self.build_type == BuildType::Skip { + return Ok(channel.clone()); + } let tar_directory = self.setup_tar_deploy(channel).await?; info!("Successfully setup tar file"); cat_file(&tar_directory.join("version.yml"))?; + Ok(channel.clone()) } DeployMethod::Local(_) => { - self.build()?; + let image_tag = self.build()?; + Ok(image_tag) } } - info!("Completed Prepare Deploy"); - Ok(()) } async fn setup_tar_deploy(&self, release_channel: &String) -> Result> { let file_name = "solana-release"; let tar_filename = format!("{file_name}.tar.bz2"); - info!("tar file: {tar_filename}"); self.download_release_from_channel(&tar_filename, release_channel) .await?; @@ -90,30 +81,32 @@ impl BuildConfig { Ok(release_dir) } - fn build(&self) -> Result<(), Box> { + fn build(&self) -> Result> { let start_time = Instant::now(); - let build_variant = if self.build_type == BuildType::Debug { - "--debug" - } else { - "" - }; - - let install_directory = self.solana_root_path.join("farf"); - let install_script = self.solana_root_path.join("scripts/cargo-install-all.sh"); - match std::process::Command::new(install_script) - .arg(install_directory) - .arg(build_variant) - .arg("--validator-only") - .status() - { - Ok(result) => { - if result.success() { - info!("Successfully built validator") - } else { - return Err("Failed to build validator".into()); + if self.build_type != BuildType::Skip { + let build_variant = if self.build_type == BuildType::Debug { + "--debug" + } else { + "" + }; + + let install_directory = self.solana_root_path.join("farf"); + let install_script = self.solana_root_path.join("scripts/cargo-install-all.sh"); + match std::process::Command::new(install_script) + .arg(install_directory) + .arg(build_variant) + .arg("--validator-only") + .status() + { + Ok(result) => { + if result.success() { + info!("Successfully built validator") + } else { + return Err("Failed to build validator".into()); + } } + Err(err) => return Err(Box::new(err)), } - Err(err) => return Err(Box::new(err)), } let solana_repo = Repository::open(self.solana_root_path.as_path())?; @@ -126,12 +119,14 @@ impl BuildConfig { // Check if current commit is associated with a tag let mut note = branch; + let mut commit_tag = None; for tag in (&solana_repo.tag_names(None)?).into_iter().flatten() { // Get the target object of the tag let tag_object = solana_repo.revparse_single(tag)?.id(); // Check if the commit associated with the tag is the same as the current commit if tag_object == commit { info!("The current commit is associated with tag: {tag}"); + commit_tag = Some(tag.to_string()); note = tag_object.to_string(); break; } @@ -142,8 +137,10 @@ impl BuildConfig { std::fs::write(self.solana_root_path.join("farf/version.yml"), content) .expect("Failed to write version.yml"); + let label = commit_tag.unwrap_or_else(|| commit.to_string()[..8].to_string()); + info!("Build took {:.3?} seconds", start_time.elapsed()); - Ok(()) + Ok(label) } async fn download_release_from_channel( diff --git a/src/startup_scripts.rs b/src/startup_scripts.rs index 781f9a5..e9158ac 100644 --- a/src/startup_scripts.rs +++ b/src/startup_scripts.rs @@ -592,7 +592,7 @@ run_delegate_stake() { echo "delegated stake" fi - solana --url $LOAD_BALANCER_RPC_URL --keypair $IDENTITY_FILE stakes validator-accounts/stake.json + solana --url $LOAD_BALANCER_RPC_URL --keypair $IDENTITY_FILE stake-account validator-accounts/stake.json } echo "get airdrop and create vote account"