1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::cell::RefCell;
use std::rc::Rc;
use futures::channel::oneshot;
use futures::future::LocalBoxFuture;
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use crate::cli::CommandCli;
use crate::context::Context;
use crate::error::Error;
use crate::runtime_api::server::*;
use ya_runtime_api::deploy::ContainerEndpoint;
pub type ProcessId = u64;
pub type EmptyResponse<'a> = LocalBoxFuture<'a, Result<(), Error>>;
pub type OutputResponse<'a> = LocalBoxFuture<'a, Result<Option<serde_json::Value>, Error>>;
pub type EndpointResponse<'a> = LocalBoxFuture<'a, Result<ContainerEndpoint, Error>>;
pub type ProcessIdResponse<'a> = LocalBoxFuture<'a, Result<ProcessId, Error>>;
pub trait Runtime: RuntimeDef {
const MODE: RuntimeMode = RuntimeMode::Server;
fn deploy<'a>(&mut self, ctx: &mut Context<Self>) -> OutputResponse<'a>;
fn start<'a>(&mut self, ctx: &mut Context<Self>) -> OutputResponse<'a>;
fn stop<'a>(&mut self, _ctx: &mut Context<Self>) -> EmptyResponse<'a> {
async move { Ok(()) }.boxed_local()
}
fn run_command<'a>(
&mut self,
command: RunProcess,
mode: RuntimeMode,
ctx: &mut Context<Self>,
) -> ProcessIdResponse<'a>;
fn kill_command<'a>(
&mut self,
_kill: KillProcess,
_ctx: &mut Context<Self>,
) -> EmptyResponse<'a> {
async move { Err(Error::from_string("Not supported")) }.boxed_local()
}
fn offer<'a>(&mut self, _ctx: &mut Context<Self>) -> OutputResponse<'a> {
async move {
Ok(Some(crate::serialize::json::json!({
"constraints": "",
"properties": {}
})))
}
.boxed_local()
}
fn test<'a>(&mut self, _ctx: &mut Context<Self>) -> EmptyResponse<'a> {
async move { Ok(()) }.boxed_local()
}
fn join_network<'a>(
&mut self,
_network: CreateNetwork,
_ctx: &mut Context<Self>,
) -> EndpointResponse<'a> {
async move { Err(Error::from_string("Not supported")) }.boxed_local()
}
}
pub trait RuntimeDef {
const NAME: &'static str;
const VERSION: &'static str;
type Cli: CommandCli;
type Conf: Default + Serialize + for<'de> Deserialize<'de>;
}
#[derive(Clone, Copy, Debug)]
pub enum RuntimeMode {
Server,
Command,
}
#[derive(Clone, Default)]
pub struct RuntimeControl {
pub(crate) shutdown_tx: Rc<RefCell<Option<oneshot::Sender<()>>>>,
}
impl RuntimeControl {
pub fn shutdown(&mut self) {
if let Some(tx) = self.shutdown_tx.borrow_mut().take() {
let _ = tx.send(());
}
}
}