gary_core/plugins/
runtime.rs

1use std::any::Any;
2use std::fmt;
3/*
4 * Experimental
5 * consider the use of this atfilename: Pfilename: P your own risk, eventually this will
6 * be versioned and safe to use, currently it may change at whim.
7 * Use of this api now may mean a lot of work keeping up with it.
8*/
9
10/// A trait for plugins that run workloads
11/// Any unimplemented function should return RuntimeErrors::Unimplemented
12/// as this is used internally to ensure functionality of the plugin.
13pub trait RuntimePlugin: Any + Send + Sync {
14    //Required for all plugins
15
16    /// The name of the plugin used to identify it.
17    fn name(&self) -> String;
18    /// A callback fired immediately after the plugin is loaded. Usually used
19    /// for initialization.
20    fn on_plugin_load(&self) {}
21    /// A callback fired immediately before the plugin is unloaded. Use this if
22    /// you need to do any cleanup.
23    fn on_plugin_unload(&self) {}
24    fn get_features(&self) -> Vec<RuntimeFeatures>;
25    fn get_version(&self) -> i32;
26
27    //Required for feature WorkloadRunner
28    fn create_workload(
29        &self,
30        id: String,
31        config: &RuntimeConfig,
32        options: &Option<SandboxConfig>,
33    ) -> Result<String, RuntimeError>;
34
35    fn start_workload(&mut self, id: String) -> Option<RuntimeError>;
36    fn stop_workload(&self, id: String, timeout: i32) -> Option<RuntimeError>;
37    fn remove_workload(&self, id: String) -> Option<RuntimeError>;
38    fn status_workload(&self, id: String) -> Result<WorkloadStatus, RuntimeError>;
39
40    //Required for feature container && vm
41    fn update_workload_resources(&self, id: String, rez: WorkloadResources)
42        -> Option<RuntimeError>;
43    fn exec_sync(
44        &self,
45        id: String,
46        cmd: &[String],
47        timeout: i32,
48    ) -> (&[u8], &[u8], Option<RuntimeError>);
49
50    //Required for feature vm
51
52    //Required for feature function
53}
54
55pub struct WorkloadResources {}
56
57pub struct WorkloadStatus {}
58
59#[derive(Debug, PartialEq)]
60pub enum RuntimeFeatures {
61    WorkloadRunner,
62    Container,
63    VM,
64    Function,
65}
66
67pub struct RuntimeConfig {}
68
69pub struct SandboxConfig {}
70
71#[derive(Debug)]
72pub enum RuntimeErrorType {
73    Unimplemented,
74    Timeout,
75    Unknown,
76}
77
78#[derive(Debug)]
79pub struct RuntimeError {
80    error_type: RuntimeErrorType,
81}
82
83impl std::error::Error for RuntimeError {
84    fn description(&self) -> &str {
85        "This action has failed."
86    }
87}
88
89impl fmt::Display for RuntimeError {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        write!(f, "RuntimeError has arrived.")
92    }
93}
94
95impl RuntimeError {
96    pub fn new(kind: RuntimeErrorType) -> Self {
97        return RuntimeError { error_type: kind };
98    }
99}