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

Re architecture #70

Merged
merged 5 commits into from
Feb 24, 2024
Merged
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
8 changes: 6 additions & 2 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ clap = { version = "4.4.12", features = ["derive"] }
clap-verbosity-flag = "2.1.1"
derive_more = "0.99.17"
futures-util = "0.3.30"
golem-client = "0.0.60"
golem-client = "0.0.62"
golem-examples = "0.1.11"
http = "1.0.0"
indoc = "2.0.4"
Expand All @@ -43,6 +43,10 @@ url = "2.5.0"
uuid = "1.6.1"
dirs = "5.0.1"
tracing-subscriber = "0.3.18"
h2 = "0.3.24"
hyper = "0.14.28"
tower = "0.4.13"
testcontainers-modules = { version = "0.3.2", features = ["postgres", "redis"] }

[dev-dependencies]
env_logger = "0.11.1"
Expand Down
17 changes: 11 additions & 6 deletions src/clients/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use std::io::Read;
use async_trait::async_trait;
use golem_client::model::{
Export, ExportFunction, ExportInstance, FunctionParameter, FunctionResult, NameOptionTypePair,
NameTypePair, Template, Type, TypeEnum, TypeFlags, TypeRecord, TypeTuple, TypeVariant,
NameTypePair, ResourceMode, Template, Type, TypeEnum, TypeFlags, TypeRecord, TypeTuple,
TypeVariant,
};
use serde::{Deserialize, Serialize};
use tokio::fs::File;
Expand Down Expand Up @@ -151,6 +152,10 @@ fn render_type(typ: &Type) -> String {
Type::U8 { .. } => "u8".to_string(),
Type::S8 { .. } => "s8".to_string(),
Type::Bool { .. } => "bool".to_string(),
Type::Handle(handle) => match handle.mode {
ResourceMode::Borrowed => format!("&handle<{}>", handle.resource_id),
ResourceMode::Owned => format!("handle<{}>", handle.resource_id),
},
}
}

Expand Down Expand Up @@ -187,7 +192,7 @@ impl<C: golem_client::api::TemplateClient + Sync + Send> TemplateClient for Temp

let name = name.map(|n| n.0);

let templates: Vec<Template> = self.client.get(name.as_deref()).await?;
let templates: Vec<Template> = self.client.get_all_templates(name.as_deref()).await?;
let views = templates.iter().map(|c| c.into()).collect();
Ok(views)
}
Expand All @@ -205,7 +210,7 @@ impl<C: golem_client::api::TemplateClient + Sync + Send> TemplateClient for Temp
.await
.map_err(|e| GolemError(format!("Can't open template file: {e}")))?;

self.client.post(&name.0, file).await?
self.client.upload_template(&name.0, file).await?
}
PathBufOrStdin::Stdin => {
let mut bytes = Vec::new();
Expand All @@ -214,7 +219,7 @@ impl<C: golem_client::api::TemplateClient + Sync + Send> TemplateClient for Temp
.read_to_end(&mut bytes) // TODO: steaming request from stdin
.map_err(|e| GolemError(format!("Failed to read stdin: {e:?}")))?;

self.client.post(&name.0, bytes).await?
self.client.upload_template(&name.0, bytes).await?
}
};

Expand All @@ -234,7 +239,7 @@ impl<C: golem_client::api::TemplateClient + Sync + Send> TemplateClient for Temp
.await
.map_err(|e| GolemError(format!("Can't open template file: {e}")))?;

self.client.template_id_upload_put(&id.0, file).await?
self.client.update_template(&id.0, file).await?
}
PathBufOrStdin::Stdin => {
let mut bytes = Vec::new();
Expand All @@ -243,7 +248,7 @@ impl<C: golem_client::api::TemplateClient + Sync + Send> TemplateClient for Temp
.read_to_end(&mut bytes) // TODO: steaming request from stdin
.map_err(|e| GolemError(format!("Failed to read stdin: {e:?}")))?;

self.client.template_id_upload_put(&id.0, bytes).await?
self.client.update_template(&id.0, bytes).await?
}
};

Expand Down
24 changes: 8 additions & 16 deletions src/clients/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

Ok(self
.client
.template_id_workers_post(
.launch_new_worker(
&template_id.0,
&WorkerCreationRequest {
name: name.0,
Expand All @@ -124,7 +124,7 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

let key = self
.client
.template_id_workers_worker_name_key_post(&template_id.0, &name.0)
.get_invocation_key(&template_id.0, &name.0)
.await?;

Ok(key_api_to_cli(key))
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

Ok(self
.client
.template_id_workers_worker_name_invoke_and_await_post(
.invoke_and_await_function(
&template_id.0,
&name.0,
&invocation_key.0,
Expand All @@ -174,12 +174,7 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

let _ = self
.client
.template_id_workers_worker_name_invoke_post(
&template_id.0,
&name.0,
&function,
&parameters,
)
.invoke_function(&template_id.0, &name.0, &function, &parameters)
.await?;
Ok(())
}
Expand All @@ -193,7 +188,7 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

let _ = self
.client
.template_id_workers_worker_name_interrupt_post(&template_id.0, &name.0, Some(false))
.interrupt_worker(&template_id.0, &name.0, Some(false))
.await?;
Ok(())
}
Expand All @@ -207,18 +202,15 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

let _ = self
.client
.template_id_workers_worker_name_interrupt_post(&template_id.0, &name.0, Some(true))
.interrupt_worker(&template_id.0, &name.0, Some(true))
.await?;
Ok(())
}

async fn delete(&self, name: WorkerName, template_id: RawTemplateId) -> Result<(), GolemError> {
info!("Deleting worker {}/{}", template_id.0, name.0);

let _ = self
.client
.template_id_workers_worker_name_delete(&template_id.0, &name.0)
.await?;
let _ = self.client.delete_worker(&template_id.0, &name.0).await?;
Ok(())
}

Expand All @@ -231,7 +223,7 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl

Ok(self
.client
.template_id_workers_worker_name_get(&template_id.0, &name.0)
.get_worker_metadata(&template_id.0, &name.0)
.await?)
}

Expand Down
28 changes: 22 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ struct GolemCommand {

#[arg(short = 'u', long)]
/// Golem base url. Default: GOLEM_BASE_URL environment variable or http://localhost:9881.
///
/// You can also specify different URLs for different services
/// via GOLEM_TEMPLATE_BASE_URL and GOLEM_WORKER_BASE_URL
/// environment variables.
golem_url: Option<String>,

#[command(subcommand)]
Expand Down Expand Up @@ -117,7 +121,14 @@ async fn async_main(cmd: GolemCommand) -> Result<(), Box<dyn std::error::Error>>
.golem_url
.or_else(|| std::env::var("GOLEM_BASE_URL").ok())
.unwrap_or("http://localhost:9881".to_string());
let url = Url::parse(&url_str).unwrap();
let template_url_str = std::env::var("GOLEM_TEMPLATE_BASE_URL")
.ok()
.unwrap_or(url_str.to_string());
let worker_url_str = std::env::var("GOLEM_WORKER_BASE_URL")
.ok()
.unwrap_or(url_str);
let template_url = Url::parse(&template_url_str).unwrap();
let worker_url = Url::parse(&worker_url_str).unwrap();
let allow_insecure_str = std::env::var("GOLEM_ALLOW_INSECURE").unwrap_or("false".to_string());
let allow_insecure = allow_insecure_str != "false";

Expand All @@ -127,24 +138,29 @@ async fn async_main(cmd: GolemCommand) -> Result<(), Box<dyn std::error::Error>>
}
let client = builder.connection_verbose(true).build()?;

let context = Context {
base_url: url.clone(),
let template_context = Context {
base_url: template_url.clone(),
client: client.clone(),
};

let worker_context = Context {
base_url: worker_url.clone(),
client: client.clone(),
};

let template_client = TemplateClientLive {
client: golem_client::api::TemplateClientLive {
context: context.clone(),
context: template_context,
},
};
let template_srv = TemplateHandlerLive {
client: template_client,
};
let worker_client = WorkerClientLive {
client: golem_client::api::WorkerClientLive {
context: context.clone(),
context: worker_context.clone(),
},
context: context.clone(),
context: worker_context.clone(),
allow_insecure,
};
let worker_srv = WorkerHandlerLive {
Expand Down
Binary file modified test-templates/auction.wasm
Binary file not shown.
Binary file modified test-templates/auction_registry_composed.wasm
Binary file not shown.
Binary file modified test-templates/environment-service.wasm
Binary file not shown.
Binary file modified test-templates/interruption.wasm
Binary file not shown.
Binary file modified test-templates/write-stdout.wasm
Binary file not shown.
38 changes: 28 additions & 10 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::context::golem_service::GolemServiceInfo;
use crate::context::ContextInfo;
use libtest_mimic::Failed;
use serde::de::DeserializeOwned;
use serde_json::Value;
Expand Down Expand Up @@ -36,34 +36,45 @@ pub trait Cli {
#[derive(Debug, Clone)]
pub struct CliLive {
pub config: CliConfig,
golem_port: u16,
golem_template_port: u16,
golem_worker_port: u16,
golem_cli_path: PathBuf,
}

impl CliLive {
pub fn with_short_args(&self) -> Self {
CliLive {
config: CliConfig { short_args: true },
golem_port: self.golem_port,
golem_template_port: self.golem_template_port,
golem_worker_port: self.golem_worker_port,
golem_cli_path: self.golem_cli_path.clone(),
}
}

pub fn with_long_args(&self) -> Self {
CliLive {
config: CliConfig { short_args: false },
golem_port: self.golem_port,
golem_template_port: self.golem_template_port,
golem_worker_port: self.golem_worker_port,
golem_cli_path: self.golem_cli_path.clone(),
}
}

pub fn make(golem: &GolemServiceInfo) -> Result<CliLive, Failed> {
// TODO; Use NginxInfo
pub fn make(context: &ContextInfo) -> Result<CliLive, Failed> {
let golem_cli_path = PathBuf::from("./target/debug/golem-cli");

println!(
"CLI with template port {} and worker port {}",
context.golem_template_service.local_http_port,
context.golem_worker_service.local_http_port
);

if golem_cli_path.exists() {
Ok(CliLive {
config: CliConfig { short_args: false },
golem_port: golem.local_http_port,
golem_template_port: context.golem_template_service.local_http_port,
golem_worker_port: context.golem_worker_service.local_http_port,
golem_cli_path,
})
} else {
Expand All @@ -75,8 +86,12 @@ impl CliLive {
}
}

fn base_url(&self) -> String {
format!("http://localhost:{}", self.golem_port)
fn template_base_url(&self) -> String {
format!("http://localhost:{}", self.golem_template_port)
}

fn worker_base_url(&self) -> String {
format!("http://localhost:{}", self.golem_worker_port)
}

fn run_inner<S: AsRef<OsStr> + Debug>(&self, args: &[S]) -> Result<String, Failed> {
Expand All @@ -86,9 +101,11 @@ impl CliLive {
);

let output = Command::new(&self.golem_cli_path)
.env("GOLEM_BASE_URL", self.base_url())
.env("GOLEM_TEMPLATE_BASE_URL", self.template_base_url())
.env("GOLEM_WORKER_BASE_URL", self.worker_base_url())
.arg(self.config.arg('F', "format"))
.arg("json")
.arg("-v")
.args(args)
.output()?;

Expand Down Expand Up @@ -138,7 +155,8 @@ impl Cli for CliLive {
);

let mut child = Command::new(&self.golem_cli_path)
.env("GOLEM_BASE_URL", self.base_url())
.env("GOLEM_TEMPLATE_BASE_URL", self.template_base_url())
.env("GOLEM_WORKER_BASE_URL", self.worker_base_url())
.arg(self.config.arg('F', "format"))
.arg("json")
.args(args)
Expand Down
Loading
Loading