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
#[macro_use]
extern crate core;
extern crate bollard;
extern crate futures;

use bollard::container::{
    Config, CreateContainerOptions, HostConfig, LogOutput, LogsOptions, StartContainerOptions,
};
use bollard::Docker;
use core::plugins::runtime::*;
use futures::Future;
use tokio::prelude::*;
use tokio::runtime::*;

#[derive(Debug)]
pub struct ContainerdRuntimePlugin {
    docker: Docker,
    runner: tokio::runtime::Runtime,
}

impl ContainerdRuntimePlugin {
    pub fn new() -> Self {
        let mut doc = Docker::connect_with_local_defaults().unwrap();
        let mut rt = Runtime::new().unwrap();
        return ContainerdRuntimePlugin {
            runner: rt,
            docker: doc,
        };
    }
}

declare_plugin!(ContainerdRuntimePlugin, ContainerdRuntimePlugin::new);

impl RuntimePlugin for ContainerdRuntimePlugin {
    /// The name of the plugin used to identify it.
    fn name(&self) -> String {
        return "docker".to_string();
    }
    /// A callback fired immediately after the plugin is loaded. Usually used
    /// for initialization.
    fn on_plugin_load(&self) {}
    /// A callback fired immediately before the plugin is unloaded. Use this if
    /// you need to do any cleanup.
    fn on_plugin_unload(&self) {}

    fn get_features(&self) -> Vec<RuntimeFeatures> {
        return vec![RuntimeFeatures::WorkloadRunner, RuntimeFeatures::Container];
    }
    fn get_version(&self) -> i32 {
        0
    }

    //Required for feature WorkloadRunner
    fn create_workload(
        &self,
        id: String,
        config: &RuntimeConfig,
        options: &Option<SandboxConfig>,
    ) -> Result<String, RuntimeError> {
        return Err(RuntimeError::new(RuntimeErrorType::Unknown));
    }

    fn start_workload(&mut self, id: String) -> Option<RuntimeError> {
        let nginx_config = Config {
            image: Some("nginx"),
            env: Some(vec![]),
            ..Default::default()
        };
        let res = self.runner.block_on(
            self.docker
                .create_container(Some(CreateContainerOptions { name: "nginx" }), nginx_config),
        );
        let results = self.runner.block_on(
            self.docker
                .start_container("nginx", None::<StartContainerOptions<String>>),
        );
        match results {
            Ok(_) => return None,
            Err(_) => return Some(RuntimeError::new(RuntimeErrorType::Unknown)),
        }
        return Some(RuntimeError::new(RuntimeErrorType::Unknown));
    }

    fn stop_workload(&self, id: String, timeout: i32) -> Option<RuntimeError> {
        return None;
    }

    fn remove_workload(&self, id: String) -> Option<RuntimeError> {
        return None;
    }

    fn status_workload(&self, id: String) -> Result<WorkloadStatus, RuntimeError> {
        return Err(RuntimeError::new(RuntimeErrorType::Unknown));
    }

    //Required for feature container && vm
    fn update_workload_resources(
        &self,
        id: String,
        rez: WorkloadResources,
    ) -> Option<RuntimeError> {
        return None;
    }

    fn exec_sync(
        &self,
        id: String,
        cmd: &[String],
        timeout: i32,
    ) -> (&[u8], &[u8], Option<RuntimeError>) {
        return (&[0], &[0], None);
    }
}