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

add notify_push service

This commit is contained in:
Robin Appelman 2021-07-30 19:12:58 +02:00
commit 93c20bc530
10 changed files with 224 additions and 45 deletions

View file

@ -4,5 +4,6 @@
'memcache.distributed' => '\\OC\\Memcache\\APCu', 'memcache.distributed' => '\\OC\\Memcache\\APCu',
'memcache.locking' => '\\OC\\Memcache\\APCu', 'memcache.locking' => '\\OC\\Memcache\\APCu',
'allow_local_remote_servers' => true, 'allow_local_remote_servers' => true,
'trusted_domains' => ['cloud'],
//PLACEHOLDER //PLACEHOLDER
]; ];

View file

@ -61,5 +61,5 @@ then
fi fi
/usr/sbin/cron -f & /usr/sbin/cron -f &
/usr/bin/redis-server & /usr/bin/redis-server --protected-mode no &
/usr/local/bin/bootstrap-nginx.sh /usr/local/bin/bootstrap-nginx.sh

View file

@ -259,7 +259,7 @@ impl Cloud {
options options
.services .services
.iter() .iter()
.map(|service| service.spawn(docker, &id, &network)), .map(|service| service.spawn(docker, &id, &network, config)),
) )
.await?; .await?;
containers.extend_from_slice(&service_containers); containers.extend_from_slice(&service_containers);

View file

@ -79,15 +79,31 @@ async fn main() -> Result<()> {
) )
.await?; .await?;
cloud cloud
.exec( .exec_with_output(
&mut docker, &mut docker,
vec![ vec![
"sed", "occ",
"-i", "config:system:set",
&format!("s/0 => 'localhost'/'{}'/", cloud.ip.unwrap()), "trusted_domains",
"config/config.php", "1",
"--value",
&format!("{}", cloud.ip.unwrap()),
], ],
false, Option::<&mut Vec<u8>>::None,
)
.await?;
cloud
.exec_with_output(
&mut docker,
vec![
"occ",
"config:system:set",
"trusted_domains",
"2",
"--value",
"cloud",
],
Option::<&mut Vec<u8>>::None,
) )
.await?; .await?;
for service in &cloud.services { for service in &cloud.services {

View file

@ -75,7 +75,6 @@ impl PhpVersion {
env: Some(env), env: Some(env),
host_config: Some(HostConfig { host_config: Some(HostConfig {
network_mode: Some(network.to_string()), network_mode: Some(network.to_string()),
// links: Some(links),
binds: Some(volumes), binds: Some(volumes),
extra_hosts: Some(vec![format!("hazehost:{}", host)]), extra_hosts: Some(vec![format!("hazehost:{}", host)]),
..Default::default() ..Default::default()

View file

@ -1,10 +1,13 @@
mod ldap; mod ldap;
mod objectstore; mod objectstore;
mod onlyoffice; mod onlyoffice;
mod push;
use crate::config::HazeConfig;
use crate::service::ldap::{LDAPAdmin, LDAP}; use crate::service::ldap::{LDAPAdmin, LDAP};
use crate::service::objectstore::ObjectStore; use crate::service::objectstore::ObjectStore;
use crate::service::onlyoffice::OnlyOffice; use crate::service::onlyoffice::OnlyOffice;
use crate::service::push::NotifyPush;
use bollard::models::ContainerState; use bollard::models::ContainerState;
use bollard::Docker; use bollard::Docker;
use color_eyre::{eyre::WrapErr, Result}; use color_eyre::{eyre::WrapErr, Result};
@ -19,7 +22,13 @@ pub trait ServiceTrait {
fn env(&self) -> &[&str]; fn env(&self) -> &[&str];
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String>; async fn spawn(
&self,
docker: &Docker,
cloud_id: &str,
network: &str,
_config: &HazeConfig,
) -> Result<String>;
async fn is_healthy(&self, docker: &Docker, cloud_id: &str) -> Result<bool> { async fn is_healthy(&self, docker: &Docker, cloud_id: &str) -> Result<bool> {
let info = docker let info = docker
@ -36,11 +45,17 @@ pub trait ServiceTrait {
fn container_name(&self, cloud_id: &str) -> String; fn container_name(&self, cloud_id: &str) -> String;
async fn start_message(&self, docker: &Docker, cloud_id: &str) -> Result<Option<String>>; async fn start_message(&self, _docker: &Docker, _cloud_id: &str) -> Result<Option<String>> {
Ok(None)
}
fn apps(&self) -> &'static [&'static str]; fn apps(&self) -> &'static [&'static str] {
&[]
}
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>>; async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
} }
#[enum_dispatch] #[enum_dispatch]
@ -50,6 +65,7 @@ pub enum Service {
LDAP(LDAP), LDAP(LDAP),
LDAPAdmin(LDAPAdmin), LDAPAdmin(LDAPAdmin),
OnlyOffice(OnlyOffice), OnlyOffice(OnlyOffice),
Push(NotifyPush),
} }
impl Service { impl Service {
@ -58,6 +74,7 @@ impl Service {
"s3" => Some(&[Service::ObjectStore(ObjectStore::S3)]), "s3" => Some(&[Service::ObjectStore(ObjectStore::S3)]),
"ldap" => Some(&[Service::LDAP(LDAP), Service::LDAPAdmin(LDAPAdmin)]), "ldap" => Some(&[Service::LDAP(LDAP), Service::LDAPAdmin(LDAPAdmin)]),
"onlyoffice" => Some(&[Service::OnlyOffice(OnlyOffice)]), "onlyoffice" => Some(&[Service::OnlyOffice(OnlyOffice)]),
"push" => Some(&[Service::Push(NotifyPush)]),
_ => None, _ => None,
} }
} }

View file

@ -1,3 +1,4 @@
use crate::config::HazeConfig;
use crate::image::pull_image; use crate::image::pull_image;
use crate::service::ServiceTrait; use crate::service::ServiceTrait;
use crate::Result; use crate::Result;
@ -20,7 +21,13 @@ impl ServiceTrait for LDAP {
&["LDAP=1"] &["LDAP=1"]
} }
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> { async fn spawn(
&self,
docker: &Docker,
cloud_id: &str,
network: &str,
_config: &HazeConfig,
) -> Result<String> {
let image = "icewind1991/haze-ldap"; let image = "icewind1991/haze-ldap";
pull_image(docker, image).await?; pull_image(docker, image).await?;
let options = Some(CreateContainerOptions { let options = Some(CreateContainerOptions {
@ -57,17 +64,9 @@ impl ServiceTrait for LDAP {
format!("{}-ldap", cloud_id) 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] { fn apps(&self) -> &'static [&'static str] {
&["user_ldap"] &["user_ldap"]
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
} }
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
@ -83,7 +82,13 @@ impl ServiceTrait for LDAPAdmin {
&[] &[]
} }
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> { async fn spawn(
&self,
docker: &Docker,
cloud_id: &str,
network: &str,
_config: &HazeConfig,
) -> Result<String> {
let image = "osixia/phpldapadmin"; let image = "osixia/phpldapadmin";
pull_image(docker, image).await?; pull_image(docker, image).await?;
let options = Some(CreateContainerOptions { let options = Some(CreateContainerOptions {
@ -149,12 +154,4 @@ impl ServiceTrait for LDAPAdmin {
ip ip
))) )))
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
fn apps(&self) -> &'static [&'static str] {
&[]
}
} }

View file

@ -1,3 +1,4 @@
use crate::config::HazeConfig;
use crate::exec::exec; use crate::exec::exec;
use crate::image::pull_image; use crate::image::pull_image;
use crate::service::ServiceTrait; use crate::service::ServiceTrait;
@ -40,7 +41,13 @@ impl ServiceTrait for ObjectStore {
} }
} }
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> { async fn spawn(
&self,
docker: &Docker,
cloud_id: &str,
network: &str,
_config: &HazeConfig,
) -> Result<String> {
pull_image(docker, self.image()).await?; pull_image(docker, self.image()).await?;
let options = Some(CreateContainerOptions { let options = Some(CreateContainerOptions {
name: format!("{}-object", cloud_id), name: format!("{}-object", cloud_id),
@ -90,15 +97,7 @@ impl ServiceTrait for ObjectStore {
format!("{}-object", cloud_id) 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] { fn apps(&self) -> &'static [&'static str] {
&["files_external"] &["files_external"]
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> {
Ok(Vec::new())
}
} }

View file

@ -1,3 +1,4 @@
use crate::config::HazeConfig;
use crate::image::pull_image; use crate::image::pull_image;
use crate::service::ServiceTrait; use crate::service::ServiceTrait;
use crate::Result; use crate::Result;
@ -20,7 +21,13 @@ impl ServiceTrait for OnlyOffice {
&[] &[]
} }
async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> { async fn spawn(
&self,
docker: &Docker,
cloud_id: &str,
network: &str,
_config: &HazeConfig,
) -> Result<String> {
let image = "onlyoffice/documentserver"; let image = "onlyoffice/documentserver";
pull_image(docker, image).await?; pull_image(docker, image).await?;
let options = Some(CreateContainerOptions { let options = Some(CreateContainerOptions {
@ -55,10 +62,6 @@ impl ServiceTrait for OnlyOffice {
format!("{}-onlyoffice", cloud_id) 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] { fn apps(&self) -> &'static [&'static str] {
&["onlyoffice"] &["onlyoffice"]
} }

147
src/service/push.rs Normal file
View file

@ -0,0 +1,147 @@
use crate::config::HazeConfig;
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::eyre::WrapErr;
use color_eyre::Report;
use maplit::hashmap;
use std::time::Duration;
use tokio::time::{sleep, timeout};
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct NotifyPush;
#[async_trait::async_trait]
impl ServiceTrait for NotifyPush {
fn name(&self) -> &str {
"push"
}
fn env(&self) -> &[&str] {
&[]
}
async fn spawn(
&self,
docker: &Docker,
cloud_id: &str,
network: &str,
config: &HazeConfig,
) -> Result<String> {
let image = "icewind1991/notify_push";
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()),
binds: Some(vec![
format!("{}/config:/config:ro", config.work_dir.join(cloud_id)),
format!("{}/data:/var/www/html/data", config.work_dir.join(cloud_id)),
]),
..Default::default()
}),
env: Some(vec![
"NEXTCLOUD_URL=http://cloud/",
"LOG=debug",
"REDIS_URL=redis://cloud/",
]),
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!["/notify_push", "/config/config.php"]),
..Default::default()
};
let id = docker.create_container(options, config).await?.id;
Ok(id)
}
fn container_name(&self, cloud_id: &str) -> String {
format!("{}-push", cloud_id)
}
fn apps(&self) -> &'static [&'static str] {
&["notify_push"]
}
async fn is_healthy(&self, _docker: &Docker, _cloud_id: &str) -> Result<bool> {
Ok(true)
}
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>> {
docker
.start_container::<String>(&self.container_name(cloud_id), None)
.await?;
self.wait_for_push(docker, cloud_id).await?;
sleep(Duration::from_millis(100)).await;
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("notify_push not started"));
};
Ok(vec![
format!("occ config:system:set trusted_proxies 1 --value {}", ip),
format!("occ notify_push:setup http://{}:7867", ip),
])
}
}
impl NotifyPush {
async fn is_push_running(&self, docker: &Docker, cloud_id: &str) -> Result<bool> {
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await?;
Ok(matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
))
}
async fn wait_for_push(&self, docker: &Docker, cloud_id: &str) -> Result<()> {
timeout(Duration::from_secs(30), async {
while !self.is_push_running(docker, cloud_id).await? {
sleep(Duration::from_millis(100)).await
}
Ok(())
})
.await
.wrap_err("Timeout after 30 seconds")?
}
}