Skip to content

Commit

Permalink
Default test and offer-template implementations (#90)
Browse files Browse the repository at this point in the history
* Default test and offer-template implementations.

* Dummy runtime with empty implementation overrides.

* Cmd offer-template with pretty print of output
  • Loading branch information
pwalski authored Apr 4, 2024
1 parent 7ae55db commit f1d1c0f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 30 deletions.
10 changes: 4 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,12 @@ async fn run<RUNTIME: process::Runtime + Clone + Unpin + 'static>(
args,
),
Command::OfferTemplate => {
let template = offer_template::template(&runtime_config)?;
io::stdout().write_all(template.as_ref())?;
return Ok(());
}
Command::Test => {
offer_template::gpu_detection(&runtime_config)?;
let offer_template = RUNTIME::offer_template(&runtime_config)?;
let offer_template = serde_json::to_string_pretty(&offer_template)?;
io::stdout().write_all(offer_template.as_bytes())?;
return Ok(());
}
Command::Test => return RUNTIME::test(&runtime_config),
};

let runtime_config = Box::pin(runtime_config);
Expand Down
27 changes: 5 additions & 22 deletions src/offer_template.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
use crate::process::RuntimeConfig;
use gpu_detection::model::Gpu;

use gpu_detection::model::Gpu;
use gpu_detection::GpuDetectionError;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::collections::BTreeMap;
use ya_agreement_utils::OfferTemplate;

#[derive(Deserialize, Serialize)]
struct OfferTemplate {
properties: BTreeMap<String, serde_json::Value>,
constraints: String,
}
pub(crate) fn gpu_detection<CONFIG: RuntimeConfig>(config: &CONFIG) -> anyhow::Result<Option<Gpu>> {
match gpu_detection::GpuDetection::init() {
Ok(gpu_detection) => {
Expand All @@ -22,18 +15,8 @@ pub(crate) fn gpu_detection<CONFIG: RuntimeConfig>(config: &CONFIG) -> anyhow::R
}
}

pub(crate) fn template<CONFIG: RuntimeConfig>(
config: &CONFIG,
) -> anyhow::Result<Cow<'static, [u8]>> {
pub(crate) fn template<CONFIG: RuntimeConfig>(_config: &CONFIG) -> anyhow::Result<OfferTemplate> {
let offer_template = include_bytes!("offer-template.json");
let mut template: OfferTemplate = serde_json::from_slice(offer_template.as_ref())?;

if let Some(gpu) = gpu_detection(config)? {
let gpu = serde_json::value::to_value(gpu)?;
template
.properties
.insert("golem.!exp.gap-35.v1.inf.gpu".into(), gpu);
}

Ok(Cow::Owned(serde_json::to_vec_pretty(&template)?))
let template: OfferTemplate = serde_json::from_slice(offer_template.as_ref())?;
Ok(template)
}
23 changes: 21 additions & 2 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Context;
use async_trait::async_trait;

use futures::TryFutureExt;
use serde::de::DeserializeOwned;

use serde_json::Value;

use std::cell::RefCell;
use std::env::current_exe;
use std::fmt::Debug;
Expand All @@ -15,6 +14,10 @@ use std::process::ExitStatus;
use std::rc::Rc;
use std::task::Poll;

use ya_agreement_utils::OfferTemplate;

use crate::offer_template::{self, gpu_detection};

pub mod automatic;
pub mod dummy;
pub mod win;
Expand All @@ -40,6 +43,22 @@ pub(crate) trait Runtime: Sized {
async fn stop(&mut self) -> anyhow::Result<()>;

async fn wait(&mut self) -> std::io::Result<ExitStatus>;

fn test(config: &Self::CONFIG) -> anyhow::Result<()> {
gpu_detection(config).context("Testing runtime failed. Unable to detect GPU.")?;
Ok(())
}

fn offer_template(config: &Self::CONFIG) -> anyhow::Result<OfferTemplate> {
let mut template = offer_template::template(config)?;
if let Some(gpu) = gpu_detection(config)
.context("Generating offer template failed. Unable to detect GPU.")?
{
let gpu = serde_json::value::to_value(gpu)?;
template.set_property("golem.!exp.gap-35.v1.inf.gpu", gpu);
}
Ok(template)
}
}

pub(crate) trait RuntimeConfig: DeserializeOwned + Default + Debug + Clone {
Expand Down
12 changes: 12 additions & 0 deletions src/process/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::{Child, Command};
use tokio::sync::Mutex;

use ya_agreement_utils::OfferTemplate;

use crate::offer_template;

use super::{Runtime, RuntimeConfig};

#[derive(Clone)]
Expand Down Expand Up @@ -81,4 +85,12 @@ impl Runtime for Dummy {
let mut child = self.child.lock().await;
child.wait().await
}

fn test(_config: &Self::CONFIG) -> anyhow::Result<()> {
Ok(())
}

fn offer_template(config: &Self::CONFIG) -> anyhow::Result<OfferTemplate> {
offer_template::template(config)
}
}

0 comments on commit f1d1c0f

Please sign in to comment.