1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00

split services into seperate files

This commit is contained in:
Robin Appelman 2021-07-30 18:04:14 +02:00
commit 3cac4e4679
9 changed files with 434 additions and 463 deletions

160
src/service/ldap.rs Normal file
View file

@ -0,0 +1,160 @@
use crate::image::pull_image;
use crate::service::ServiceTrait;
use crate::Result;
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
use bollard::models::{ContainerState, EndpointSettings, HostConfig};
use bollard::Docker;
use color_eyre::Report;
use maplit::hashmap;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct LDAP;
#[async_trait::async_trait]
impl ServiceTrait for LDAP {
fn name(&self) -> &str {
"ldap"
}
fn env(&self) -> &[&str] {
&["LDAP=1"]
}
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> {
let image = "icewind1991/haze-ldap";
pull_image(docker, image).await?;
let options = Some(CreateContainerOptions {
name: self.container_name(cloud_id),
});
let config = Config {
image: Some(image),
env: Some(vec!["LDAP_ADMIN_PASSWORD=haze"]),
host_config: Some(HostConfig {
network_mode: Some(network.to_string()),
..Default::default()
}),
labels: Some(hashmap! {
"haze-type" => self.name(),
"haze-cloud-id" => cloud_id
}),
networking_config: Some(NetworkingConfig {
endpoints_config: hashmap! {
network => EndpointSettings {
aliases: Some(vec![self.name().to_string()]),
..Default::default()
}
},
}),
cmd: Some(vec!["--copy-service"]),
..Default::default()
};
let id = docker.create_container(options, config).await?.id;
docker.start_container::<String>(&id, None).await?;
Ok(id)
}
fn container_name(&self, cloud_id: &str) -> String {
format!("{}-ldap", cloud_id)
}
async fn start_message(&self, _docker: &Docker, _cloud_id: &str) -> Result<Option<String>> {
Ok(None)
}
fn apps(&self) -> &'static [&'static str] {
&["user_ldap"]
}
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct LDAPAdmin;
#[async_trait::async_trait]
impl ServiceTrait for LDAPAdmin {
fn name(&self) -> &str {
"ldap-admin"
}
fn env(&self) -> &[&str] {
&[]
}
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> {
let image = "osixia/phpldapadmin";
pull_image(docker, image).await?;
let options = Some(CreateContainerOptions {
name: self.container_name(cloud_id),
});
let config = Config {
image: Some(image),
env: Some(vec!["PHPLDAPADMIN_LDAP_HOSTS=ldap"]),
host_config: Some(HostConfig {
network_mode: Some(network.to_string()),
..Default::default()
}),
labels: Some(hashmap! {
"haze-type" => self.name(),
"haze-cloud-id" => cloud_id
}),
networking_config: Some(NetworkingConfig {
endpoints_config: hashmap! {
network => EndpointSettings {
aliases: Some(vec![self.name().to_string()]),
..Default::default()
}
},
}),
cmd: Some(vec!["--copy-service"]),
..Default::default()
};
let id = docker.create_container(options, config).await?.id;
docker.start_container::<String>(&id, None).await?;
Ok(id)
}
fn container_name(&self, cloud_id: &str) -> String {
format!("{}-ldap-admin", cloud_id)
}
async fn start_message(&self, docker: &Docker, cloud_id: &str) -> Result<Option<String>> {
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await?;
let ip = if matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
) {
info.network_settings
.unwrap()
.networks
.unwrap()
.values()
.next()
.unwrap()
.ip_address
.clone()
.unwrap()
} else {
return Err(Report::msg("ldap admin not started"));
};
Ok(Some(format!(
"Ldap admin running at: https://{} with 'cn=admin,dc=example,dc=org' and password 'haze'",
ip
)))
}
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
fn apps(&self) -> &'static [&'static str] {
&[]
}
}

104
src/service/objectstore.rs Normal file
View file

@ -0,0 +1,104 @@
use crate::exec::exec;
use crate::image::pull_image;
use crate::service::ServiceTrait;
use crate::Result;
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
use bollard::models::{EndpointSettings, HostConfig};
use bollard::Docker;
use maplit::hashmap;
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ObjectStore {
S3,
}
impl ObjectStore {
fn image(&self) -> &str {
match self {
ObjectStore::S3 => "localstack/localstack:0.12.7",
}
}
fn self_env(&self) -> Vec<&str> {
match self {
ObjectStore::S3 => vec!["DEBUG=1", "SERVICES=s3"],
}
}
}
#[async_trait::async_trait]
impl ServiceTrait for ObjectStore {
fn name(&self) -> &str {
match self {
ObjectStore::S3 => "s3",
}
}
fn env(&self) -> &[&str] {
match self {
ObjectStore::S3 => &["S3=1"],
}
}
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> {
pull_image(docker, self.image()).await?;
let options = Some(CreateContainerOptions {
name: format!("{}-object", cloud_id),
});
let config = Config {
image: Some(self.image()),
env: Some(self.self_env()),
host_config: Some(HostConfig {
network_mode: Some(network.to_string()),
..Default::default()
}),
labels: Some(hashmap! {
"haze-type" => self.name(),
"haze-cloud-id" => cloud_id
}),
networking_config: Some(NetworkingConfig {
endpoints_config: hashmap! {
network => EndpointSettings {
aliases: Some(vec![self.name().to_string()]),
..Default::default()
}
},
}),
..Default::default()
};
let id = docker.create_container(options, config).await?.id;
docker.start_container::<String>(&id, None).await?;
Ok(id)
}
async fn is_healthy(&self, docker: &Docker, cloud_id: &str) -> Result<bool> {
let mut output = Vec::new();
exec(
docker,
format!("{}-object", cloud_id),
"root",
vec!["curl", "localhost:4566/health"],
vec![],
Some(&mut output),
)
.await?;
let output = String::from_utf8(output)?;
Ok(output.contains(r#""s3": "running""#))
}
fn container_name(&self, cloud_id: &str) -> String {
format!("{}-object", cloud_id)
}
async fn start_message(&self, _docker: &Docker, _cloud_id: &str) -> Result<Option<String>> {
Ok(None)
}
fn apps(&self) -> &'static [&'static str] {
&["files_external"]
}
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
}

95
src/service/onlyoffice.rs Normal file
View file

@ -0,0 +1,95 @@
use crate::image::pull_image;
use crate::service::ServiceTrait;
use crate::Result;
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
use bollard::models::{ContainerState, EndpointSettings, HostConfig};
use bollard::Docker;
use color_eyre::Report;
use maplit::hashmap;
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct OnlyOffice;
#[async_trait::async_trait]
impl ServiceTrait for OnlyOffice {
fn name(&self) -> &str {
"onlyoffice"
}
fn env(&self) -> &[&str] {
&[]
}
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> {
let image = "onlyoffice/documentserver";
pull_image(docker, image).await?;
let options = Some(CreateContainerOptions {
name: self.container_name(cloud_id),
});
let config = Config {
image: Some(image),
host_config: Some(HostConfig {
network_mode: Some(network.to_string()),
..Default::default()
}),
labels: Some(hashmap! {
"haze-type" => self.name(),
"haze-cloud-id" => cloud_id
}),
networking_config: Some(NetworkingConfig {
endpoints_config: hashmap! {
network => EndpointSettings {
aliases: Some(vec![self.name().to_string()]),
..Default::default()
}
},
}),
..Default::default()
};
let id = docker.create_container(options, config).await?.id;
docker.start_container::<String>(&id, None).await?;
Ok(id)
}
fn container_name(&self, cloud_id: &str) -> String {
format!("{}-onlyoffice", cloud_id)
}
async fn start_message(&self, _docker: &Docker, _cloud_id: &str) -> Result<Option<String>> {
Ok(None)
}
fn apps(&self) -> &'static [&'static str] {
&["onlyoffice"]
}
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>> {
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await?;
let ip = if matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
) {
info.network_settings
.unwrap()
.networks
.unwrap()
.values()
.next()
.unwrap()
.ip_address
.clone()
.unwrap()
} else {
return Err(Report::msg("onlyoffice not started"));
};
Ok(vec![format!(
"occ config:app:set onlyoffice DocumentServerUrl --value http://{}/",
ip
)])
}
}