gary_plugin_docker/
lib.rs

1#[macro_use]
2extern crate core;
3extern crate bollard;
4extern crate futures;
5
6use bollard::container::{
7    Config, CreateContainerOptions, HostConfig, LogOutput, LogsOptions, StartContainerOptions,
8};
9use bollard::Docker;
10use core::plugins::runtime::*;
11use futures::Future;
12use tokio::prelude::*;
13use tokio::runtime::*;
14
15#[derive(Debug)]
16pub struct ContainerdRuntimePlugin {
17    docker: Docker,
18    runner: tokio::runtime::Runtime,
19}
20
21impl ContainerdRuntimePlugin {
22    pub fn new() -> Self {
23        let mut doc = Docker::connect_with_local_defaults().unwrap();
24        let mut rt = Runtime::new().unwrap();
25        return ContainerdRuntimePlugin {
26            runner: rt,
27            docker: doc,
28        };
29    }
30}
31
32declare_plugin!(ContainerdRuntimePlugin, ContainerdRuntimePlugin::new);
33
34impl RuntimePlugin for ContainerdRuntimePlugin {
35    /// The name of the plugin used to identify it.
36    fn name(&self) -> String {
37        return "docker".to_string();
38    }
39    /// A callback fired immediately after the plugin is loaded. Usually used
40    /// for initialization.
41    fn on_plugin_load(&self) {}
42    /// A callback fired immediately before the plugin is unloaded. Use this if
43    /// you need to do any cleanup.
44    fn on_plugin_unload(&self) {}
45
46    fn get_features(&self) -> Vec<RuntimeFeatures> {
47        return vec![RuntimeFeatures::WorkloadRunner, RuntimeFeatures::Container];
48    }
49    fn get_version(&self) -> i32 {
50        0
51    }
52
53    //Required for feature WorkloadRunner
54    fn create_workload(
55        &self,
56        id: String,
57        config: &RuntimeConfig,
58        options: &Option<SandboxConfig>,
59    ) -> Result<String, RuntimeError> {
60        return Err(RuntimeError::new(RuntimeErrorType::Unknown));
61    }
62
63    fn start_workload(&mut self, id: String) -> Option<RuntimeError> {
64        let nginx_config = Config {
65            image: Some("nginx"),
66            env: Some(vec![]),
67            ..Default::default()
68        };
69        let res = self.runner.block_on(
70            self.docker
71                .create_container(Some(CreateContainerOptions { name: "nginx" }), nginx_config),
72        );
73        let results = self.runner.block_on(
74            self.docker
75                .start_container("nginx", None::<StartContainerOptions<String>>),
76        );
77        match results {
78            Ok(_) => return None,
79            Err(_) => return Some(RuntimeError::new(RuntimeErrorType::Unknown)),
80        }
81        return Some(RuntimeError::new(RuntimeErrorType::Unknown));
82    }
83
84    fn stop_workload(&self, id: String, timeout: i32) -> Option<RuntimeError> {
85        return None;
86    }
87
88    fn remove_workload(&self, id: String) -> Option<RuntimeError> {
89        return None;
90    }
91
92    fn status_workload(&self, id: String) -> Result<WorkloadStatus, RuntimeError> {
93        return Err(RuntimeError::new(RuntimeErrorType::Unknown));
94    }
95
96    //Required for feature container && vm
97    fn update_workload_resources(
98        &self,
99        id: String,
100        rez: WorkloadResources,
101    ) -> Option<RuntimeError> {
102        return None;
103    }
104
105    fn exec_sync(
106        &self,
107        id: String,
108        cmd: &[String],
109        timeout: i32,
110    ) -> (&[u8], &[u8], Option<RuntimeError>) {
111        return (&[0], &[0], None);
112    }
113}