Implement image configuration in bollard layer
This commit is contained in:
@@ -2,6 +2,7 @@ use actix_web::{patch, put, delete, web, App, HttpServer, Responder};
|
||||
use crate::api::error::ReturnedError;
|
||||
use crate::controller::Controller;
|
||||
use crate::deploy::starter;
|
||||
use crate::deploy::container_options::Options;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -9,6 +10,8 @@ struct ConainerParams {
|
||||
name: String,
|
||||
ip: String,
|
||||
image: String,
|
||||
msg: Option<String>,
|
||||
memory: Option<String>
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -19,7 +22,8 @@ struct ConainerStopParams {
|
||||
#[put("/container")]
|
||||
async fn create_container(controller: web::Data<Controller>,
|
||||
params: web::Query<ConainerParams>) -> impl Responder {
|
||||
controller.create_container(params.name.clone(), params.ip.clone(), params.image.clone()).await
|
||||
let ops = Options::new(params.memory.clone(), params.msg.clone());
|
||||
controller.create_container(params.name.clone(), params.ip.clone(), params.image.clone(), ops).await
|
||||
}
|
||||
|
||||
#[patch("/container/stop")]
|
||||
@@ -55,3 +59,4 @@ pub async fn start() -> std::io::Result<()> {
|
||||
.run()
|
||||
.await
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::deploy;
|
||||
use crate::deploy::container::Container;
|
||||
use crate::database::exposer::MemStorage;
|
||||
use crate::database::instance::Instance;
|
||||
use crate::deploy::container_options::Options;
|
||||
use std::sync::Mutex;
|
||||
use log::error;
|
||||
|
||||
@@ -26,7 +27,7 @@ impl Controller {
|
||||
Ok(cont)
|
||||
}
|
||||
|
||||
pub async fn create_container(&self, domain: String, ip: String, image: String) -> String {
|
||||
pub async fn create_container(&self, domain: String, ip: String, image: String, ops: Options) -> String {
|
||||
let is_stored = self.storage.lock().unwrap().search_instance(domain.clone());
|
||||
match is_stored {
|
||||
Some(c) => match c.docker_id {
|
||||
@@ -34,7 +35,7 @@ impl Controller {
|
||||
None => "Container without docker_id".to_string()
|
||||
},
|
||||
None => {
|
||||
match self.load_container(None,domain.clone(),ip.clone(),image.clone()).await {
|
||||
match self.load_container(None,domain.clone(),ip.clone(),image.clone(),ops).await {
|
||||
Ok(c) => {
|
||||
log::debug!("poisoned: {}",self.storage.is_poisoned());
|
||||
self.storage.try_lock().unwrap().new_instance(c.clone()).unwrap();
|
||||
@@ -58,7 +59,8 @@ impl Controller {
|
||||
Some(instance.docker_id.clone()),
|
||||
instance.domain.clone(),
|
||||
instance.ip.clone(),
|
||||
image).await {
|
||||
image,
|
||||
Options::new(None, None)).await {
|
||||
Ok(c) => {
|
||||
self.storage.lock().unwrap().loaded_instance(instance, c);
|
||||
},
|
||||
@@ -69,7 +71,8 @@ impl Controller {
|
||||
async fn load_container(&self, docker_id: Option<String>,
|
||||
domain: String,
|
||||
ip: String,
|
||||
image: String) -> Result<deploy::container::Container,
|
||||
image: String,
|
||||
ops: Options) -> Result<deploy::container::Container,
|
||||
bollard::errors::Error>
|
||||
{
|
||||
deploy::container::Container::new(self.driver.clone(),
|
||||
@@ -77,7 +80,8 @@ impl Controller {
|
||||
domain,
|
||||
ip,
|
||||
image,
|
||||
self.network.clone()).await
|
||||
self.network.clone(),
|
||||
ops).await
|
||||
}
|
||||
|
||||
pub async fn is_started(&self) -> bool {
|
||||
@@ -141,3 +145,4 @@ impl Controller {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use bollard::errors::{Error, Error::IOError};
|
||||
use std::io::ErrorKind::AlreadyExists;
|
||||
use std::collections::hash_map::HashMap;
|
||||
use crate::deploy::bind;
|
||||
use crate::deploy::container_options::Options;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Container {
|
||||
@@ -19,7 +20,8 @@ pub struct Container {
|
||||
pub image: String,
|
||||
pub net: String,
|
||||
pub docker_id: Option<String>,
|
||||
pub database_id: Option<i64>
|
||||
pub database_id: Option<i64>,
|
||||
ops: Options,
|
||||
}
|
||||
|
||||
impl Container {
|
||||
@@ -28,7 +30,8 @@ impl Container {
|
||||
name: String,
|
||||
ip: String,
|
||||
image: String,
|
||||
net: String) -> Result<Container, Error> {
|
||||
net: String,
|
||||
ops: Options) -> Result<Container, Error> {
|
||||
|
||||
let mut container = Container {
|
||||
name: name,
|
||||
@@ -38,6 +41,7 @@ impl Container {
|
||||
start: false,
|
||||
docker_id: docker_id,
|
||||
database_id: None,
|
||||
ops: ops,
|
||||
};
|
||||
match container.start(docker).await {
|
||||
Ok(c) => {
|
||||
@@ -66,7 +70,8 @@ impl Container {
|
||||
create_config(self.name.clone(),
|
||||
self.ip.clone(),
|
||||
self.net.clone(),
|
||||
self.image.clone()))
|
||||
self.image.clone(),
|
||||
self.ops.clone()))
|
||||
.await?.id);
|
||||
false
|
||||
},
|
||||
@@ -125,7 +130,7 @@ fn create_search_container(name: String) -> ListContainersOptions<String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_config(name: String, ip: String, net: String, image: String) -> Config<String> {
|
||||
fn create_config(name: String, ip: String, net: String, image: String, ops: Options) -> Config<String> {
|
||||
let endpoint = EndpointSettings {
|
||||
ipam_config: Some(EndpointIpamConfig{
|
||||
ipv4_address: Some(ip),
|
||||
@@ -138,7 +143,8 @@ fn create_config(name: String, ip: String, net: String, image: String) -> Config
|
||||
let net = NetworkingConfig {
|
||||
endpoints_config: hash
|
||||
};
|
||||
let env = vec!["EULA=TRUE".to_string()];
|
||||
let mut env = vec!["EULA=TRUE".to_string()];
|
||||
env.append(&mut ops.generate_config());
|
||||
Config {
|
||||
image: Some(image),
|
||||
env: Some(env),
|
||||
|
||||
37
src/deploy/container_options.rs
Normal file
37
src/deploy/container_options.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
#[derive(Clone)]
|
||||
pub struct Options {
|
||||
memory: Option<String>,
|
||||
rolling_logs: bool,
|
||||
msg: Option<String>
|
||||
}
|
||||
|
||||
impl Options {
|
||||
pub fn new(memory: Option<String>, msg: Option<String>) -> Self {
|
||||
Self {
|
||||
memory: memory,
|
||||
rolling_logs: true,
|
||||
msg: msg
|
||||
}
|
||||
}
|
||||
|
||||
pub fn generate_config(&self) -> Vec<String> {
|
||||
let mut ret = Vec::new();
|
||||
if self.rolling_logs {
|
||||
ret.push("ENABLE_ROLLING_LOGS=true".to_string());
|
||||
}
|
||||
Self::insert_to_vec("MEMORY=", self.memory.clone(), &mut ret);
|
||||
Self::insert_to_vec("MOTD=", self.msg.clone(), &mut ret);
|
||||
ret
|
||||
}
|
||||
|
||||
fn insert_to_vec(variable: &str, value: Option<String>, data: &mut Vec<String>) {
|
||||
match value {
|
||||
Some(mut val) => {
|
||||
val.insert_str(0, variable);
|
||||
data.push(val);
|
||||
},
|
||||
None => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
pub mod container;
|
||||
pub mod network;
|
||||
pub mod starter;
|
||||
pub mod container_options;
|
||||
mod bind;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user