Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
feat: allow configuring opengb config path
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Jul 22, 2024
1 parent 1ad47f6 commit de1cf11
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
9 changes: 7 additions & 2 deletions rivet-toolchain/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rivet_api::{apis, models};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, io::Write, path::PathBuf};
use tempfile::NamedTempFile;
use tokio::{process::Command};
use tokio::process::Command;

use crate::{
config,
Expand All @@ -15,6 +15,7 @@ use crate::{

const DEFAULT_OPENGB_DOCKER_TAG: &'static str = "ghcr.io/rivet-gg/opengb/v0.1.2";
pub struct OpenGbCommandOpts {
pub config_path: String,
pub args: Vec<String>,
pub env: HashMap<String, String>,
pub cwd: PathBuf,
Expand Down Expand Up @@ -45,6 +46,7 @@ pub async fn build_opengb_command(opts: OpenGbCommandOpts) -> GlobalResult<Comma
match runtime {
OpenGbRuntime::Native => {
let mut cmd = shell_cmd("opengb");
cmd.arg("--path").arg(opts.config_path);
cmd.args(opts.args);
cmd.envs(opts.env);
cmd.current_dir(opts.cwd);
Expand All @@ -71,6 +73,9 @@ pub async fn build_opengb_command(opts: OpenGbCommandOpts) -> GlobalResult<Comma
cmd.arg(format!("--volume={}:/backend", opts.cwd.display()));
cmd.arg("--workdir=/backend");
cmd.arg(image_tag);
cmd.arg("--");
cmd.arg("--path");
cmd.arg(opts.config_path);
cmd.args(&opts.args);
Ok(cmd)
}
Expand All @@ -96,7 +101,7 @@ pub async fn get_or_create_project(ctx: &Ctx) -> GlobalResult<Box<models::EeBack
)
.await?;

// TOOD: Add get or create project
// TODO: Add get or create project
let project = unwrap!(
project_res.project,
"No OpenGB project linked to the current game. Create one on the hub: https://hub.rivet.gg/"
Expand Down
18 changes: 18 additions & 0 deletions rivet-toolchain/src/config/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,31 @@ pub struct BackendDevConfig {
/// Env vars to pass to the deploy OpenGB commands.
#[serde(default)]
pub command_environment: HashMap<String, String>,
/// Backend ocnfig to use when running backend config.
#[serde(default = "BackendDevConfig::config_path")]
pub config_path: String,
}

impl BackendDevConfig {
fn config_path() -> String {
"backend.dev.json".to_string()
}
}

#[derive(Default, Clone, Serialize, Deserialize)]
pub struct BackendDeployConfig {
/// Env vars to pass to the deploy OpenGB commands.
#[serde(default)]
pub command_environment: HashMap<String, String>,
/// Backend ocnfig to use when running backend config.
#[serde(default = "BackendDeployConfig::config_path")]
pub config_path: String,
}

impl BackendDeployConfig {
fn config_path() -> String {
"backend.json".into()
}
}

#[derive(Default, Clone, Serialize, Deserialize)]
Expand Down
5 changes: 3 additions & 2 deletions rivet-toolchain/src/tasks/backend_dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ impl super::Task for Task {
}

async fn run(task: TaskCtx, input: Self::Input) -> GlobalResult<Self::Output> {
let mut cmd_env = config::settings::try_read(|settings| {
let (mut cmd_env, config_path) = config::settings::try_read(|settings| {
let mut env = settings.backend.command_environment.clone();
env.extend(settings.backend.dev.command_environment.clone());
Ok(env)
Ok((env, settings.backend.dev.config_path.clone()))
})
.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(),
backend::OpenGbCommandOpts {
config_path,
args: vec!["dev".into(), "--force-deploy-migrations".into()],
env: cmd_env,
cwd: input.cwd.into(),
Expand Down
9 changes: 7 additions & 2 deletions rivet-toolchain/src/tasks/backend_sdk_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ impl super::Task for Task {
}

async fn run(task: TaskCtx, input: Input) -> GlobalResult<Output> {
let (mut cmd_env, sdk_settings) = config::settings::try_read(|settings| {
let (mut cmd_env, sdk_settings, config_path) = config::settings::try_read(|settings| {
let mut env = settings.backend.command_environment.clone();
env.extend(settings.backend.sdk.command_environment.clone());
Ok((env, settings.backend.sdk.clone()))
Ok((
env,
settings.backend.sdk.clone(),
settings.backend.deploy.config_path.clone(),
))
})
.await?;

Expand All @@ -44,6 +48,7 @@ impl super::Task for Task {
let exit_code = backend::run_opengb_command(
task.clone(),
backend::OpenGbCommandOpts {
config_path,
args: vec![
"sdk".into(),
"generate".into(),
Expand Down
6 changes: 4 additions & 2 deletions rivet-toolchain/src/tasks/deploy/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,16 @@ pub async fn deploy(ctx: &Ctx, task: TaskCtx, opts: DeployOpts) -> GlobalResult<
task.log_stdout(format!("[Building Project] {}", project_path.display()));

// Build
let cmd_env = config::settings::try_read(|settings| {
let (cmd_env, config_path) = config::settings::try_read(|settings| {
let mut env = settings.backend.command_environment.clone();
env.extend(settings.backend.deploy.command_environment.clone());
Ok(env)
Ok((env, settings.backend.deploy.config_path.clone()))
})
.await?;
let cmd = backend::run_opengb_command(
task.clone(),
backend::OpenGbCommandOpts {
config_path: config_path.clone(),
args: vec![
"build".into(),
"--db-driver".into(),
Expand Down Expand Up @@ -170,6 +171,7 @@ pub async fn deploy(ctx: &Ctx, task: TaskCtx, opts: DeployOpts) -> GlobalResult<
let migrate_cmd = backend::run_opengb_command(
task.clone(),
backend::OpenGbCommandOpts {
config_path,
args: vec!["db".into(), "deploy".into()],
env: migrate_env,
cwd: project_path.clone(),
Expand Down

0 comments on commit de1cf11

Please sign in to comment.