diff --git a/Cargo.lock b/Cargo.lock index ce57333e..00949a4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1520,6 +1520,15 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" +[[package]] +name = "portpicker" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be97d76faf1bfab666e1375477b23fde79eccf0276e9b63b92a39d676a889ba9" +dependencies = [ + "rand", +] + [[package]] name = "posthog-core" version = "0.1.0" @@ -1816,6 +1825,7 @@ dependencies = [ "nix", "open", "openssl", + "portpicker", "rand", "regex", "reqwest", diff --git a/rivet-toolchain/Cargo.toml b/rivet-toolchain/Cargo.toml index 2de1c5ee..3f354e28 100644 --- a/rivet-toolchain/Cargo.toml +++ b/rivet-toolchain/Cargo.toml @@ -38,6 +38,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" strum = { version = "0.24", features = ["derive"] } sysinfo = "0.30.0" +portpicker = "0.1" tabled = "0.8.0" tar = "0.4.40" tempfile = "3.2" diff --git a/rivet-toolchain/src/tasks/backend_choose_local_port.rs b/rivet-toolchain/src/tasks/backend_choose_local_port.rs new file mode 100644 index 00000000..be230688 --- /dev/null +++ b/rivet-toolchain/src/tasks/backend_choose_local_port.rs @@ -0,0 +1,28 @@ +use global_error::prelude::*; +use serde::{Deserialize, Serialize}; + +use crate::util::task::TaskCtx; + +#[derive(Deserialize)] +pub struct Input {} + +#[derive(Serialize)] +pub struct Output { + pub port: u16, +} + +pub struct Task; + +impl super::Task for Task { + type Input = Input; + type Output = Output; + + fn name() -> &'static str { + "backend_choose_local_port" + } + + async fn run(_task: TaskCtx, _input: Self::Input) -> GlobalResult { + let port = unwrap!(portpicker::pick_unused_port(), "no free ports"); + Ok(Output { port }) + } +} diff --git a/rivet-toolchain/src/tasks/backend_dev.rs b/rivet-toolchain/src/tasks/backend_dev.rs index 214f9a38..f5911a4a 100644 --- a/rivet-toolchain/src/tasks/backend_dev.rs +++ b/rivet-toolchain/src/tasks/backend_dev.rs @@ -5,6 +5,7 @@ use crate::{backend, config, util::task::TaskCtx}; #[derive(Deserialize)] pub struct Input { + pub port: u16, pub cwd: String, } @@ -30,6 +31,7 @@ impl super::Task for Task { Ok(env) }) .await?; + cmd_env.insert("OPENGB_PORT".into(), input.port.to_string()); cmd_env.insert("OPENGB_TERM_COLOR".into(), "never".into()); let exit_code = backend::run_opengb_command( task.clone(), diff --git a/rivet-toolchain/src/tasks/mod.rs b/rivet-toolchain/src/tasks/mod.rs index 63a76987..a2c4df1e 100644 --- a/rivet-toolchain/src/tasks/mod.rs +++ b/rivet-toolchain/src/tasks/mod.rs @@ -1,3 +1,4 @@ +pub mod backend_choose_local_port; pub mod backend_dev; pub mod backend_sdk_gen; pub mod check_login_state; @@ -72,6 +73,7 @@ macro_rules! gen_run_task { } gen_run_task!( + backend_choose_local_port::Task, backend_dev::Task, backend_sdk_gen::Task, check_login_state::Task,