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

[Graphite MQ] Draft PR GROUP:89205c (PRs 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599) #600

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,091 changes: 1,059 additions & 32 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "2"
members = [
"packages/cli",
"packages/deno-embed",
"packages/js-utils-embed",
"packages/js-utils-embed", "packages/kv-str",
"packages/toolchain",
]

Expand Down
5 changes: 5 additions & 0 deletions examples/js-kv/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"@rivet-gg/actors-core": "jsr:@rivet-gg/actors-core@0.0.1-rc.4"
}
}
16 changes: 16 additions & 0 deletions examples/js-kv/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/js-kv/index.js → examples/js-kv/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Rivet from "@rivet-gg/actors-core";

await Rivet.kv.put("count", 0);

console.log('Started', Deno.env.toObject());
Expand Down
4 changes: 2 additions & 2 deletions examples/js-kv/rivet.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{
"tags": { "name": "kv" },
"runtime": "javascript",
"bundler": "none",
"script": "index.js"
"bundler": "deno",
"script": "index.ts"
}
]
}
3 changes: 3 additions & 0 deletions examples/types-test/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
6 changes: 6 additions & 0 deletions examples/types-test/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": {
"@rivet-gg/actor-types": "jsr:@rivet-gg/actor-types@^0.0.9",
"@std/assert": "jsr:@std/assert@1"
}
}
32 changes: 32 additions & 0 deletions examples/types-test/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/types-test/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Rivet from "@rivet-gg/actor-types";
15 changes: 14 additions & 1 deletion packages/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ repository = "https://github.com/rivet-gg/cli"
name = "rivet"
path = "src/main.rs"

[features]
default = ["sentry"]
sentry = []

[dependencies]
clap = { version = "4.5.9", features = ["derive"] }
toolchain = { version = "0.1.0", path = "../toolchain", package = "rivet-toolchain" }
Expand All @@ -18,9 +22,18 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.120"
anyhow = "1.0"
uuid = { version = "1.11.0", features = ["v4"] }
envy = "0.4.2"
url = { version = "2.5.3", features = ["serde"] }
base64 = "0.22.1"
kv-str = { version = "0.1.0", path = "../kv-str" }
inquire = "0.7.5"
webbrowser = "1.0.2"
sentry = { version = "0.34.0", features = ["anyhow"] }
sysinfo = "0.32.0"
ctrlc = "3.4.5"

[dependencies.async-posthog]
git = "https://github.com/rivet-gg/posthog-rs"
rev = "ef4e80e"

[build-dependencies]
anyhow = "1.0"
Expand Down
199 changes: 144 additions & 55 deletions packages/cli/src/commands/actor/create.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use anyhow::*;
use clap::{Parser, ValueEnum};
use serde::Deserialize;
use std::{collections::HashMap, process::ExitCode};
use toolchain::rivet_api::{apis, models};
use std::collections::HashMap;
use toolchain::{
build,
rivet_api::{apis, models},
};
use uuid::Uuid;

use crate::util::kv_str;

#[derive(ValueEnum, Clone)]
enum NetworkMode {
Bridge,
Expand All @@ -27,25 +28,28 @@ struct Port {

#[derive(Parser)]
pub struct Opts {
#[clap(index = 1)]
environment: String,
#[clap(long, alias = "env", short = 'e')]
environment: Option<String>,

#[clap(long, short = 'r')]
region: String,
region: Option<String>,

/// Tags to use for both the actor & build tags. This allows for creating actors quickly since
/// the tags are often identical between the two.
#[clap(long = "tags", short = 't')]
universal_tags: Option<String>,

#[clap(long, short = 't')]
tags: Option<String>,
#[clap(long, short = 'a')]
actor_tags: Option<String>,

/// Build ID.
#[clap(long)]
build: Option<String>,

#[clap(long, short = 'b')]
build_tags: Option<String>,

#[clap(long = "arg")]
arguments: Option<Vec<String>>,

#[clap(long = "env")]
#[clap(long = "env-var")]
env_vars: Option<Vec<String>>,

#[clap(long, value_enum)]
Expand All @@ -65,36 +69,53 @@ pub struct Opts {

#[clap(long)]
durable: bool,

/// If included, the `current` tag will not be automatically inserted to the build tag.
#[clap(long)]
no_build_current_tag: bool,

#[clap(long)]
logs: bool,

#[clap(long)]
log_stream: Option<crate::util::actor::logs::LogStream>,

#[clap(long)]
deploy: bool,
}

impl Opts {
pub async fn execute(&self) -> ExitCode {
match self.execute_inner().await {
Result::Ok(code) => code,
Err(err) => {
eprintln!("{err}");
ExitCode::FAILURE
}
}
}

pub async fn execute_inner(&self) -> Result<ExitCode> {
pub async fn execute(&self) -> Result<()> {
let ctx = toolchain::toolchain_ctx::load().await?;

let env = crate::util::env::get_or_select(&ctx, self.environment.as_ref()).await?;

// Parse tags
let tags = self
.tags
let actor_tags = if let Some(t) = &self.actor_tags {
kv_str::from_str::<HashMap<String, String>>(t)?
} else if let Some(t) = &self.universal_tags {
kv_str::from_str::<HashMap<String, String>>(t)?
} else {
// No tags
HashMap::new()
};

// Parse build ID
let mut build_id = self
.build
.as_ref()
.map(|tags_str| kv_str::from_str::<HashMap<String, String>>(tags_str))
.transpose()?
.unwrap_or_else(|| HashMap::new());
.map(|b| Uuid::parse_str(&b))
.transpose()
.context("invalid build uuid")?;

// Parse build tags
let build_tags = self
.build_tags
.as_ref()
.map(|tags_str| kv_str::from_str::<HashMap<String, String>>(tags_str))
.transpose()?;
let mut build_tags = if let Some(t) = &self.build_tags {
Some(kv_str::from_str::<HashMap<String, String>>(t)?)
} else if let Some(t) = &self.universal_tags {
Some(kv_str::from_str::<HashMap<String, String>>(t)?)
} else {
None
};

// Parse ports
let ports = self
Expand Down Expand Up @@ -141,18 +162,75 @@ impl Opts {
})
.transpose()?;

// Auto-deploy
if self.deploy {
// Remove build tags, since we'll be using the build ID
let build_tags = build_tags
.take()
.context("must define build tags when using deploy flag")?;

// Deploys erver
let deploy_build_ids = crate::util::deploy::deploy(crate::util::deploy::DeployOpts {
environment: &env,
build_tags: Some(build_tags),
})
.await?;

if deploy_build_ids.len() > 1 {
println!("Warning: Multiple build IDs match tags, proceeding with first");
}

let deploy_build_id = deploy_build_ids
.first()
.context("No builds matched build tags")?;
build_id = Some(*deploy_build_id);
}

// Automatically add `current` tag to make querying easier
//
// Do this AFTER the deploy since this will mess up the build filter.
if !self.no_build_current_tag {
if let Some(build_tags) = build_tags.as_mut() {
if !build_tags.contains_key(build::tags::VERSION) {
build_tags.insert(build::tags::CURRENT.into(), "true".into());
}
}
}

// Auto-select region if needed
let region = if let Some(region) = &self.region {
region.clone()
} else {
let regions = apis::actor_regions_api::actor_regions_list(
&ctx.openapi_config_cloud,
Some(&ctx.project.name_id.to_string()),
Some(&env),
)
.await?;

// TODO(RVT-4207): Improve automatic region selection logic
// Choose a region
let auto_region = if let Some(ideal_region) = regions
.regions
.iter()
.filter(|r| r.id == "lax" || r.id == "local")
.next()
{
ideal_region.id.clone()
} else {
regions.regions.first().context("no regions")?.id.clone()
};
println!("Automatically selected region: {auto_region}");

auto_region
};

let request = models::ActorCreateActorRequest {
region: self.region.clone(),
tags: Some(serde_json::json!(tags)),
build: self
.build
.as_ref()
.map(|b| Uuid::parse_str(&b))
.transpose()
.context("invalid build uuid")?,
region,
tags: Some(serde_json::json!(actor_tags)),
build: build_id,
build_tags: build_tags.map(|bt| Some(serde_json::json!(bt))),
runtime: Box::new(models::ActorCreateActorRuntimeRequest {
arguments: self.arguments.clone(),
environment: env_vars,
}),
network: Some(Box::new(models::ActorCreateActorNetworkRequest {
Expand All @@ -172,22 +250,33 @@ impl Opts {
})),
};

match apis::actor_api::actor_create(
let response = apis::actor_api::actor_create(
&ctx.openapi_config_cloud,
request,
Some(&ctx.project.name_id),
Some(&self.environment),
Some(&env),
)
.await
{
Result::Ok(response) => {
println!("Created actor:\n{:#?}", response.actor);
Ok(ExitCode::SUCCESS)
}
Err(e) => {
eprintln!("Failed to create actor: {}", e);
Ok(ExitCode::FAILURE)
}
.await?;
println!("Created actor:\n{:#?}", response.actor);

// Tail logs
if self.logs {
crate::util::actor::logs::tail(
&ctx,
crate::util::actor::logs::TailOpts {
environment: &env,
actor_id: response.actor.id,
stream: self
.log_stream
.clone()
.unwrap_or(crate::util::actor::logs::LogStream::All),
follow: true,
timestamps: true,
},
)
.await?;
}

Ok(())
}
}
Loading
Loading