use crate::cloud::CloudOptions; use crate::config::HazeConfig; use crate::exec::exec; use crate::image::{image_exists, pull_image}; use crate::service::{split_cmnd, ServiceTrait}; use crate::Result; use bollard::models::{ContainerCreateBody, EndpointSettings, HostConfig, NetworkingConfig}; use bollard::query_parameters::CreateContainerOptions; use bollard::Docker; use maplit::hashmap; use miette::{bail, IntoDiagnostic}; use std::io::Stdout; #[derive(Debug, Clone, Eq, PartialEq)] pub struct Kaspersky; #[async_trait::async_trait] impl ServiceTrait for Kaspersky { fn name(&self) -> &str { "kaspersky" } fn env(&self) -> &[&str] { &["KASPERSKY_HOST=kaspersky", "KASPERSKY_PORT=80"] } async fn spawn( &self, docker: &Docker, cloud_id: &str, network: &str, _config: &HazeConfig, _options: &CloudOptions, ) -> Result> { let image = "kaspersky"; if !image_exists(docker, image).await { bail!("You need to manually create the 'kaspersky' image"); } pull_image(docker, image).await?; let options = Some(CreateContainerOptions { name: self.container_name(cloud_id), ..CreateContainerOptions::default() }); let config = ContainerCreateBody { image: Some(image.into()), host_config: Some(HostConfig { network_mode: Some(network.to_string()), ..Default::default() }), labels: Some(hashmap! { "haze-type".into() => self.name().into(), "haze-cloud-id".into() => cloud_id.into() }), networking_config: Some(NetworkingConfig { endpoints_config: Some(hashmap! { network.into() => EndpointSettings { aliases: Some(vec![self.name().to_string()]), ..Default::default() } }), }), ..Default::default() }; let id = docker .create_container(options, config) .await .into_diagnostic()? .id; docker.start_container(&id, None).await.into_diagnostic()?; Ok(vec![id]) } async fn is_healthy( &self, docker: &Docker, cloud_id: &str, _options: &CloudOptions, ) -> Result { let exit = exec( docker, self.container_name(cloud_id).unwrap(), "root", vec!["curl", "localhost/licenseinfo"], Vec::::default(), Option::::None, ) .await?; Ok(exit.to_result().is_ok()) } fn container_name(&self, cloud_id: &str) -> Option { Some(format!("{}-kaspersky", cloud_id)) } fn apps(&self) -> &'static [&'static str] { &["files_antivirus"] } async fn post_setup( &self, _docker: &Docker, _cloud_id: &str, _config: &HazeConfig, ) -> Result>> { Ok(vec![ split_cmnd("occ config:app:set files_antivirus av_mode --value=kaspersky"), split_cmnd("occ config:app:set files_antivirus av_host --value=kaspersky"), split_cmnd("occ config:app:set files_antivirus av_port --value=80"), ]) } } #[derive(Debug, Clone, Eq, PartialEq)] pub struct KasperskyIcap; #[async_trait::async_trait] impl ServiceTrait for KasperskyIcap { fn name(&self) -> &str { "kaspersky-icap" } fn env(&self) -> &[&str] { &[ "ICAP_HOST=kaspersky-icap", "ICAP_PORT=1344", "ICAP_REQUEST=req", "ICAP_HEADER=X-Virus-ID", "ICAP_MODE=reqmod", ] } async fn spawn( &self, docker: &Docker, cloud_id: &str, network: &str, _config: &HazeConfig, _options: &CloudOptions, ) -> Result> { let image = "kaspersky-icap"; if !image_exists(docker, image).await { bail!("You need to manually create the 'kaspersky-icap' image"); } pull_image(docker, image).await?; let options = Some(CreateContainerOptions { name: self.container_name(cloud_id), ..CreateContainerOptions::default() }); let config = ContainerCreateBody { image: Some(image.into()), host_config: Some(HostConfig { network_mode: Some(network.to_string()), ..Default::default() }), labels: Some(hashmap! { "haze-type".into() => self.name().into(), "haze-cloud-id".into() => cloud_id.into(), }), networking_config: Some(NetworkingConfig { endpoints_config: Some(hashmap! { network.into() => EndpointSettings { aliases: Some(vec![self.name().to_string()]), ..Default::default() } }), }), ..Default::default() }; let id = docker .create_container(options, config) .await .into_diagnostic()? .id; docker.start_container(&id, None).await.into_diagnostic()?; Ok(vec![id]) } fn container_name(&self, cloud_id: &str) -> Option { Some(format!("{}-kaspersky-icap", cloud_id)) } fn apps(&self) -> &'static [&'static str] { &["files_antivirus"] } async fn post_setup( &self, _docker: &Docker, _cloud_id: &str, _config: &HazeConfig, ) -> Result>> { Ok(vec![ split_cmnd("occ config:app:set files_antivirus av_mode --value=icap"), split_cmnd("occ config:app:set files_antivirus av_host --value=kaspersky-icap"), split_cmnd("occ config:app:set files_antivirus av_port --value=1344"), split_cmnd("occ config:app:set files_antivirus av_icap_request_service --value=req"), split_cmnd( "occ config:app:set files_antivirus av_icap_response_header --value=X-Virus-ID", ), ]) } }