From da1440fe2e21029439ec9621733db06ae0aaf9a5 Mon Sep 17 00:00:00 2001 From: Jk Jensen Date: Tue, 23 Jan 2024 11:38:05 -0700 Subject: [PATCH 1/2] [mysten-service] 6/n update the sui-services dockerfile with new service --- crates/suiop-cli/src/cli/service/init.rs | 66 ++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/crates/suiop-cli/src/cli/service/init.rs b/crates/suiop-cli/src/cli/service/init.rs index ec83570cc2d91..001e1126513a3 100644 --- a/crates/suiop-cli/src/cli/service/init.rs +++ b/crates/suiop-cli/src/cli/service/init.rs @@ -1,6 +1,7 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 +use anyhow::Context; use anyhow::Result; use clap::Parser; use clap::ValueEnum; @@ -9,6 +10,7 @@ use std::fs::create_dir_all; use std::fs::File; use std::io::prelude::*; use std::path::Path; +use tracing::debug; use tracing::info; // include the boilerplate code in this binary @@ -27,12 +29,57 @@ pub fn bootstrap_service(lang: &ServiceLanguage, path: &Path) -> Result<()> { } } +/// Add the new service to the sui-services dockerfile in the sui repository +fn add_to_sui_dockerfile(path: &Path) -> Result<()> { + let path = path.canonicalize().context("canonicalizing service path")?; + let crates_dir = path.parent().unwrap(); + if !crates_dir.ends_with("sui/crates") { + panic!("directory wasn't in the sui repo"); + } + let sui_services_dockerfile_path = &crates_dir.join("../docker/sui-services/Dockerfile"); + // read the dockerfile + let dockerfile = std::fs::read_to_string(sui_services_dockerfile_path) + .context("reading sui-services dockerfile")?; + + // find the line with the build cmd + let build_line = dockerfile + .lines() + .enumerate() + .find(|(_, line)| line.contains("RUN cargo build --release \\")) + .expect("couldn't find build line in sui-services dockerfile") + .0; + // update with the new service + let mut final_dockerfile = dockerfile.lines().collect::>(); + let bin_str = format!( + " --bin {} \\", + path.file_name() + .expect("getting the project name from the given path") + .to_str() + .unwrap() + ); + final_dockerfile.insert(build_line + 1, &bin_str); + // write the file back + std::fs::write(sui_services_dockerfile_path, final_dockerfile.join("\n")) + .context("writing sui-services dockerfile after modifying it")?; + + Ok(()) +} + fn create_rust_service(path: &Path) -> Result<()> { info!("creating rust service in {}", path.to_string_lossy()); - let cargo_toml_path = if path.to_string_lossy().contains("sui/crates") { - "Cargo-sui.toml" - } else { + // create the dir to ensure we can canonicalize any relative paths + create_dir_all(path)?; + let is_sui_service = path + // expand relative paths and symlinks + .canonicalize() + .context("canonicalizing service path")? + .to_string_lossy() + .contains("sui/crates"); + debug!("sui service: {:?}", is_sui_service); + let cargo_toml_path = if is_sui_service { "Cargo.toml" + } else { + "Cargo-external.toml" }; let cargo_toml = PROJECT_DIR.get_file(cargo_toml_path).unwrap(); let main_rs = PROJECT_DIR.get_file("src/main.rs").unwrap(); @@ -43,5 +90,18 @@ fn create_rust_service(path: &Path) -> Result<()> { main_file.write_all(main_body)?; let mut cargo_file = File::create(path.join("Cargo.toml"))?; cargo_file.write_all(cargo_body)?; + + // add the project as a member of the cargo workspace + if is_sui_service { + // TODO: add_member_to_workspace(path)?; + } + // now that the source directory works, let's update/add a dockerfile + if is_sui_service { + // update sui-services dockerfile + add_to_sui_dockerfile(path)?; + } else { + // TODO: create a new dockerfile where the user designates + } + Ok(()) } From de439da9a1517e01af6fad561874bb74afcc52a3 Mon Sep 17 00:00:00 2001 From: jk jensen Date: Wed, 24 Jan 2024 16:36:21 -0700 Subject: [PATCH 2/2] [mysten-service] 7/n add new service as member of sui workspace (#15876) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description Describe the changes or additions included in this PR. ## Test Plan ``` ยป RUST_LOG=debug cargo r -- s i --path ../my-dumb-service Compiling suiop-cli v0.1.5 (/Users/jkjensen/mysten/sui/crates/suiop-cli) Finished dev [unoptimized + debuginfo] target(s) in 2.62s Running `/Users/jkjensen/mysten/sui/target/debug/suiop s i --path ../my-dumb-service` 2024-01-23T20:39:35.910330Z INFO suioplib::cli::service::init: creating rust service in ../my-dumb-service 2024-01-23T20:39:35.910729Z DEBUG suioplib::cli::service::init: sui service: true ``` ``` diff --git a/Cargo.toml b/Cargo.toml index afa17929a1..b194896523 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -187,7 +187,7 @@ members = [ "sui-execution/v0/sui-verifier", "sui-execution/v1/sui-adapter", "sui-execution/v1/sui-move-natives", - "sui-execution/v1/sui-verifier", + "sui-execution/v1/sui-verifier", "my-dumb-service", ] ``` --- If your changes are not user-facing and do not break anything, you can skip the following section. Otherwise, please briefly describe what has changed under the Release Notes section. ### Type of Change (Check all that apply) - [ ] protocol change - [ ] user-visible impact - [ ] breaking change for a client SDKs - [ ] breaking change for FNs (FN binary must upgrade) - [ ] breaking change for validators or node operators (must upgrade binaries) - [ ] breaking change for on-chain data layout - [ ] necessitate either a data wipe or data migration ### Release notes --- crates/suiop-cli/src/cli/service/init.rs | 33 +++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/suiop-cli/src/cli/service/init.rs b/crates/suiop-cli/src/cli/service/init.rs index 001e1126513a3..f340041099a92 100644 --- a/crates/suiop-cli/src/cli/service/init.rs +++ b/crates/suiop-cli/src/cli/service/init.rs @@ -6,6 +6,7 @@ use anyhow::Result; use clap::Parser; use clap::ValueEnum; use include_dir::{include_dir, Dir}; +use std::fs; use std::fs::create_dir_all; use std::fs::File; use std::io::prelude::*; @@ -38,7 +39,7 @@ fn add_to_sui_dockerfile(path: &Path) -> Result<()> { } let sui_services_dockerfile_path = &crates_dir.join("../docker/sui-services/Dockerfile"); // read the dockerfile - let dockerfile = std::fs::read_to_string(sui_services_dockerfile_path) + let dockerfile = fs::read_to_string(sui_services_dockerfile_path) .context("reading sui-services dockerfile")?; // find the line with the build cmd @@ -59,12 +60,38 @@ fn add_to_sui_dockerfile(path: &Path) -> Result<()> { ); final_dockerfile.insert(build_line + 1, &bin_str); // write the file back - std::fs::write(sui_services_dockerfile_path, final_dockerfile.join("\n")) + fs::write(sui_services_dockerfile_path, final_dockerfile.join("\n")) .context("writing sui-services dockerfile after modifying it")?; Ok(()) } +fn add_member_to_workspace(path: &Path) -> Result<()> { + // test + let path = path.canonicalize().context("canonicalizing service path")?; + let crates_dir = path.parent().unwrap(); + if !crates_dir.ends_with("sui/crates") { + panic!("directory wasn't in the sui repo"); + } + let workspace_toml_path = &crates_dir.join("../Cargo.toml"); + // read the workspace toml + let toml_content = fs::read_to_string(workspace_toml_path)?; + let mut toml = toml_content.parse::()?; + toml["workspace"]["members"] + .as_array_mut() + .unwrap() + .push_formatted(toml_edit::Value::String(toml_edit::Formatted::new( + path.file_name() + .expect("getting the project name from the given path") + .to_str() + .unwrap() + .to_string(), + ))); + fs::write(workspace_toml_path, toml.to_string()) + .context("failed to write workspace Cargo.toml back after update")?; + Ok(()) +} + fn create_rust_service(path: &Path) -> Result<()> { info!("creating rust service in {}", path.to_string_lossy()); // create the dir to ensure we can canonicalize any relative paths @@ -93,7 +120,7 @@ fn create_rust_service(path: &Path) -> Result<()> { // add the project as a member of the cargo workspace if is_sui_service { - // TODO: add_member_to_workspace(path)?; + add_member_to_workspace(path)?; } // now that the source directory works, let's update/add a dockerfile if is_sui_service {