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::api::error::ReturnedError;
|
||||||
use crate::controller::Controller;
|
use crate::controller::Controller;
|
||||||
use crate::deploy::starter;
|
use crate::deploy::starter;
|
||||||
|
use crate::deploy::container_options::Options;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -9,6 +10,8 @@ struct ConainerParams {
|
|||||||
name: String,
|
name: String,
|
||||||
ip: String,
|
ip: String,
|
||||||
image: String,
|
image: String,
|
||||||
|
msg: Option<String>,
|
||||||
|
memory: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
@@ -19,7 +22,8 @@ struct ConainerStopParams {
|
|||||||
#[put("/container")]
|
#[put("/container")]
|
||||||
async fn create_container(controller: web::Data<Controller>,
|
async fn create_container(controller: web::Data<Controller>,
|
||||||
params: web::Query<ConainerParams>) -> impl Responder {
|
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")]
|
#[patch("/container/stop")]
|
||||||
@@ -55,3 +59,4 @@ pub async fn start() -> std::io::Result<()> {
|
|||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use crate::deploy;
|
|||||||
use crate::deploy::container::Container;
|
use crate::deploy::container::Container;
|
||||||
use crate::database::exposer::MemStorage;
|
use crate::database::exposer::MemStorage;
|
||||||
use crate::database::instance::Instance;
|
use crate::database::instance::Instance;
|
||||||
|
use crate::deploy::container_options::Options;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use log::error;
|
use log::error;
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ impl Controller {
|
|||||||
Ok(cont)
|
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());
|
let is_stored = self.storage.lock().unwrap().search_instance(domain.clone());
|
||||||
match is_stored {
|
match is_stored {
|
||||||
Some(c) => match c.docker_id {
|
Some(c) => match c.docker_id {
|
||||||
@@ -34,7 +35,7 @@ impl Controller {
|
|||||||
None => "Container without docker_id".to_string()
|
None => "Container without docker_id".to_string()
|
||||||
},
|
},
|
||||||
None => {
|
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) => {
|
Ok(c) => {
|
||||||
log::debug!("poisoned: {}",self.storage.is_poisoned());
|
log::debug!("poisoned: {}",self.storage.is_poisoned());
|
||||||
self.storage.try_lock().unwrap().new_instance(c.clone()).unwrap();
|
self.storage.try_lock().unwrap().new_instance(c.clone()).unwrap();
|
||||||
@@ -58,7 +59,8 @@ impl Controller {
|
|||||||
Some(instance.docker_id.clone()),
|
Some(instance.docker_id.clone()),
|
||||||
instance.domain.clone(),
|
instance.domain.clone(),
|
||||||
instance.ip.clone(),
|
instance.ip.clone(),
|
||||||
image).await {
|
image,
|
||||||
|
Options::new(None, None)).await {
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
self.storage.lock().unwrap().loaded_instance(instance, c);
|
self.storage.lock().unwrap().loaded_instance(instance, c);
|
||||||
},
|
},
|
||||||
@@ -69,7 +71,8 @@ impl Controller {
|
|||||||
async fn load_container(&self, docker_id: Option<String>,
|
async fn load_container(&self, docker_id: Option<String>,
|
||||||
domain: String,
|
domain: String,
|
||||||
ip: String,
|
ip: String,
|
||||||
image: String) -> Result<deploy::container::Container,
|
image: String,
|
||||||
|
ops: Options) -> Result<deploy::container::Container,
|
||||||
bollard::errors::Error>
|
bollard::errors::Error>
|
||||||
{
|
{
|
||||||
deploy::container::Container::new(self.driver.clone(),
|
deploy::container::Container::new(self.driver.clone(),
|
||||||
@@ -77,7 +80,8 @@ impl Controller {
|
|||||||
domain,
|
domain,
|
||||||
ip,
|
ip,
|
||||||
image,
|
image,
|
||||||
self.network.clone()).await
|
self.network.clone(),
|
||||||
|
ops).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn is_started(&self) -> bool {
|
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::io::ErrorKind::AlreadyExists;
|
||||||
use std::collections::hash_map::HashMap;
|
use std::collections::hash_map::HashMap;
|
||||||
use crate::deploy::bind;
|
use crate::deploy::bind;
|
||||||
|
use crate::deploy::container_options::Options;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Container {
|
pub struct Container {
|
||||||
@@ -19,7 +20,8 @@ pub struct Container {
|
|||||||
pub image: String,
|
pub image: String,
|
||||||
pub net: String,
|
pub net: String,
|
||||||
pub docker_id: Option<String>,
|
pub docker_id: Option<String>,
|
||||||
pub database_id: Option<i64>
|
pub database_id: Option<i64>,
|
||||||
|
ops: Options,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Container {
|
impl Container {
|
||||||
@@ -28,7 +30,8 @@ impl Container {
|
|||||||
name: String,
|
name: String,
|
||||||
ip: String,
|
ip: String,
|
||||||
image: String,
|
image: String,
|
||||||
net: String) -> Result<Container, Error> {
|
net: String,
|
||||||
|
ops: Options) -> Result<Container, Error> {
|
||||||
|
|
||||||
let mut container = Container {
|
let mut container = Container {
|
||||||
name: name,
|
name: name,
|
||||||
@@ -38,6 +41,7 @@ impl Container {
|
|||||||
start: false,
|
start: false,
|
||||||
docker_id: docker_id,
|
docker_id: docker_id,
|
||||||
database_id: None,
|
database_id: None,
|
||||||
|
ops: ops,
|
||||||
};
|
};
|
||||||
match container.start(docker).await {
|
match container.start(docker).await {
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
@@ -66,7 +70,8 @@ impl Container {
|
|||||||
create_config(self.name.clone(),
|
create_config(self.name.clone(),
|
||||||
self.ip.clone(),
|
self.ip.clone(),
|
||||||
self.net.clone(),
|
self.net.clone(),
|
||||||
self.image.clone()))
|
self.image.clone(),
|
||||||
|
self.ops.clone()))
|
||||||
.await?.id);
|
.await?.id);
|
||||||
false
|
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 {
|
let endpoint = EndpointSettings {
|
||||||
ipam_config: Some(EndpointIpamConfig{
|
ipam_config: Some(EndpointIpamConfig{
|
||||||
ipv4_address: Some(ip),
|
ipv4_address: Some(ip),
|
||||||
@@ -138,7 +143,8 @@ fn create_config(name: String, ip: String, net: String, image: String) -> Config
|
|||||||
let net = NetworkingConfig {
|
let net = NetworkingConfig {
|
||||||
endpoints_config: hash
|
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 {
|
Config {
|
||||||
image: Some(image),
|
image: Some(image),
|
||||||
env: Some(env),
|
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 container;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
pub mod starter;
|
pub mod starter;
|
||||||
|
pub mod container_options;
|
||||||
mod bind;
|
mod bind;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user