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

more office wip, only wss proxy missing it seems

This commit is contained in:
Robin Appelman 2023-02-13 17:37:38 +01:00
commit 2a7f4de2b7
11 changed files with 186 additions and 110 deletions

View file

@ -78,10 +78,6 @@ impl CloudOptions {
app_packages: app_package, app_packages: app_package,
}) })
} }
pub fn with_php(self, php: PhpVersion) -> Self {
CloudOptions { php, ..self }
}
} }
#[test] #[test]
@ -392,13 +388,7 @@ impl Cloud {
} }
}); });
let clean_id = id.strip_prefix("haze-").unwrap_or(&id); let address = config.proxy.addr(&id, ip);
let address = match (&config.proxy.address, config.proxy.https) {
(public, true) if !public.is_empty() => format!("https://{clean_id}.{public}"),
(public, false) if !public.is_empty() => format!("http://{clean_id}.{public}"),
_ => format!("http://{ip}"),
};
Ok(Cloud { Ok(Cloud {
id, id,
@ -543,18 +533,10 @@ impl Cloud {
== 1; == 1;
let ip = network_info.ip_address.as_ref()?.parse().ok(); let ip = network_info.ip_address.as_ref()?.parse().ok();
let address = if let Some(ip) = ip {
let clean_id = id.strip_prefix("haze-").unwrap_or(&id); config.proxy.addr(&id, ip)
} else {
let address = match (&config.proxy.address, config.proxy.https, ip) { "Not running".into()
(public, true, Some(_)) if !public.is_empty() => {
format!("https://{clean_id}.{public}")
}
(public, false, Some(_)) if !public.is_empty() => {
format!("http://{clean_id}.{public}")
}
(_, _, Some(ip)) => format!("http://{ip}"),
_ => "Not running".into(),
}; };
service_ids.push(id.clone()); service_ids.push(id.clone());

View file

@ -5,6 +5,7 @@ use serde::Deserialize;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::env::var; use std::env::var;
use std::fs::{read, read_to_string}; use std::fs::{read, read_to_string};
use std::net::IpAddr;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(from = "RawHazeConfig")] #[serde(from = "RawHazeConfig")]
@ -160,6 +161,27 @@ pub struct ProxyConfig {
pub https: bool, pub https: bool,
} }
impl ProxyConfig {
/// Get a public address for a service, either with direct ip or through the proxy
pub fn addr(&self, id: &str, ip: IpAddr) -> String {
let clean_id = id.strip_prefix("haze-").unwrap_or(&id);
match (&self.address, self.https) {
(public, true) if !public.is_empty() => format!("https://{clean_id}.{public}"),
(public, false) if !public.is_empty() => format!("http://{clean_id}.{public}"),
_ => format!("http://{ip}"),
}
}
pub fn addr_with_port(&self, id: &str, ip: IpAddr, port: u16) -> String {
let clean_id = id.strip_prefix("haze-").unwrap_or(&id);
match (&self.address, self.https) {
(public, true) if !public.is_empty() => format!("https://{clean_id}.{public}"),
(public, false) if !public.is_empty() => format!("http://{clean_id}.{public}"),
_ => format!("http://{ip}:{port}"),
}
}
}
impl HazeConfig { impl HazeConfig {
pub fn load() -> Result<Self> { pub fn load() -> Result<Self> {
let dirs = ProjectDirs::from("nl", "icewind", "haze").unwrap(); let dirs = ProjectDirs::from("nl", "icewind", "haze").unwrap();

View file

@ -374,7 +374,7 @@ async fn setup(docker: &mut Docker, options: CloudOptions, config: &HazeConfig)
} }
} }
for service in &cloud.services { for service in &cloud.services {
for cmd in service.post_setup(&docker, &cloud.id).await? { for cmd in service.post_setup(&docker, &cloud.id, config).await? {
cloud cloud
.exec(docker, shell_words::split(&cmd).into_diagnostic()?, false) .exec(docker, shell_words::split(&cmd).into_diagnostic()?, false)
.await?; .await?;

View file

@ -1,3 +1,4 @@
use crate::service::ServiceTrait;
use crate::{Cloud, HazeConfig}; use crate::{Cloud, HazeConfig};
use crate::{Result, Service}; use crate::{Result, Service};
use bollard::Docker; use bollard::Docker;
@ -63,6 +64,20 @@ impl ActiveInstances {
.next()?; .next()?;
let ip = push.get_ip(&self.docker, &cloud.id).await.ok()?; let ip = push.get_ip(&self.docker, &cloud.id).await.ok()?;
SocketAddr::new(ip, 7867) SocketAddr::new(ip, 7867)
} else if let Some(name) = name.strip_suffix("-office") {
let cloud = Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)
.await
.ok()?;
let office = cloud
.services
.iter()
.filter_map(|service| match service {
Service::Office(office) => Some(office),
_ => None,
})
.next()?;
let ip = office.get_ip(&self.docker, &cloud.id).await.ok()?;
SocketAddr::new(ip, 9980)
} else { } else {
SocketAddr::new( SocketAddr::new(
Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config) Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)

View file

@ -19,7 +19,8 @@ use crate::service::smb::Smb;
use bollard::models::ContainerState; use bollard::models::ContainerState;
use bollard::Docker; use bollard::Docker;
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use miette::{IntoDiagnostic, Result, WrapErr}; use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::net::IpAddr;
use std::time::Duration; use std::time::Duration;
use tokio::time::{sleep, timeout}; use tokio::time::{sleep, timeout};
@ -64,9 +65,78 @@ pub trait ServiceTrait {
&[] &[]
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
_docker: &Docker,
_cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
Ok(Vec::new()) Ok(Vec::new())
} }
async fn is_running(&self, docker: &Docker, cloud_id: &str) -> Result<bool> {
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
Ok(matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
))
}
async fn wait_for_running(&self, docker: &Docker, cloud_id: &str) -> Result<()> {
timeout(Duration::from_secs(30), async {
while !self.is_running(docker, cloud_id).await? {
sleep(Duration::from_millis(100)).await
}
Ok(())
})
.await
.into_diagnostic()
.wrap_err("Timeout after 30 seconds")?
}
async fn get_ip(&self, docker: &Docker, cloud_id: &str) -> Result<IpAddr> {
docker
.start_container::<String>(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
self.wait_for_running(docker, cloud_id).await?;
sleep(Duration::from_millis(100)).await;
let info = docker
.inspect_container(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
if matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
) {
info.network_settings
.unwrap()
.networks
.unwrap()
.values()
.next()
.unwrap()
.ip_address
.clone()
.unwrap()
.parse()
.into_diagnostic()
.wrap_err("Invalid ip address")
} else {
Err(Report::msg("service not started"))
}
}
} }
#[enum_dispatch] #[enum_dispatch]

View file

@ -78,7 +78,12 @@ impl ServiceTrait for ClamIcap {
&["files_antivirus"] &["files_antivirus"]
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
_docker: &Docker,
_cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
Ok(vec![ Ok(vec![
"occ config:app:set files_antivirus av_mode --value=icap".into(), "occ config:app:set files_antivirus av_mode --value=icap".into(),
"occ config:app:set files_antivirus av_host --value=clamav-icap".into(), "occ config:app:set files_antivirus av_host --value=clamav-icap".into(),

View file

@ -91,7 +91,12 @@ impl ServiceTrait for Kaspersky {
&["files_antivirus"] &["files_antivirus"]
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
_docker: &Docker,
_cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
Ok(vec![ Ok(vec![
"occ config:app:set files_antivirus av_mode --value=kaspersky".into(), "occ config:app:set files_antivirus av_mode --value=kaspersky".into(),
"occ config:app:set files_antivirus av_host --value=kaspersky".into(), "occ config:app:set files_antivirus av_host --value=kaspersky".into(),
@ -186,7 +191,12 @@ impl ServiceTrait for KasperskyIcap {
&["files_antivirus"] &["files_antivirus"]
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
_docker: &Docker,
_cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
Ok(vec![ Ok(vec![
"occ config:app:set files_antivirus av_mode --value=icap".into(), "occ config:app:set files_antivirus av_mode --value=icap".into(),
"occ config:app:set files_antivirus av_host --value=kaspersky-icap".into(), "occ config:app:set files_antivirus av_host --value=kaspersky-icap".into(),

View file

@ -26,16 +26,34 @@ impl ServiceTrait for Office {
docker: &Docker, docker: &Docker,
cloud_id: &str, cloud_id: &str,
network: &str, network: &str,
_config: &HazeConfig, config: &HazeConfig,
) -> Result<String> { ) -> Result<String> {
let image = "collabora/code"; let image = "collabora/code";
pull_image(docker, image).await?; pull_image(docker, image).await?;
let container_id = self.container_name(cloud_id);
let options = Some(CreateContainerOptions { let options = Some(CreateContainerOptions {
name: self.container_name(cloud_id), name: container_id.clone(),
}); });
let mut env = vec!["extra_params=--o:ssl.enable=false --o:ssl.termination=true"];
let clean_id = container_id.strip_prefix("haze-").unwrap_or(&container_id);
let server_name_opt = match (&config.proxy.address, config.proxy.https) {
(public, true) if !public.is_empty() => {
format!("server_name={clean_id}.{public}")
}
(public, false) if !public.is_empty() => {
format!("server_name={clean_id}.{public}")
}
_ => "".to_string(),
};
if !server_name_opt.is_empty() {
env.push(&server_name_opt);
}
let config = Config { let config = Config {
image: Some(image), image: Some(image),
env: Some(vec!["extra_params=--o:ssl.enable=false"]), env: Some(env),
host_config: Some(HostConfig { host_config: Some(HostConfig {
network_mode: Some(network.to_string()), network_mode: Some(network.to_string()),
..Default::default() ..Default::default()
@ -74,9 +92,15 @@ impl ServiceTrait for Office {
&["richdocuments"] &["richdocuments"]
} }
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
docker: &Docker,
cloud_id: &str,
config: &HazeConfig,
) -> Result<Vec<String>> {
let container = &self.container_name(cloud_id);
let info = docker let info = docker
.inspect_container(&self.container_name(cloud_id), None) .inspect_container(container, None)
.await .await
.into_diagnostic()?; .into_diagnostic()?;
let ip = if matches!( let ip = if matches!(
@ -96,6 +120,8 @@ impl ServiceTrait for Office {
.ip_address .ip_address
.clone() .clone()
.unwrap() .unwrap()
.parse()
.into_diagnostic()?
} else { } else {
return Err(Report::msg("office not started")); return Err(Report::msg("office not started"));
}; };
@ -105,8 +131,8 @@ impl ServiceTrait for Office {
ip ip
), ),
format!( format!(
r#"occ config:app:set richdocuments public_wopi_url --value="http://{}:9980""#, r#"occ config:app:set richdocuments public_wopi_url --value="{}""#,
ip config.proxy.addr_with_port(container, ip, 9980)
), ),
format!( format!(
r#"occ config:app:set richdocuments wopi_root --value="http://{}""#, r#"occ config:app:set richdocuments wopi_root --value="http://{}""#,

View file

@ -73,7 +73,12 @@ impl ServiceTrait for OnlyOffice {
&["onlyoffice"] &["onlyoffice"]
} }
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
docker: &Docker,
cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
let info = docker let info = docker
.inspect_container(&self.container_name(cloud_id), None) .inspect_container(&self.container_name(cloud_id), None)
.await .await

View file

@ -2,13 +2,10 @@ use crate::config::HazeConfig;
use crate::image::pull_image; use crate::image::pull_image;
use crate::service::ServiceTrait; use crate::service::ServiceTrait;
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig}; use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
use bollard::models::{ContainerState, EndpointSettings, HostConfig}; use bollard::models::{EndpointSettings, HostConfig};
use bollard::Docker; use bollard::Docker;
use maplit::hashmap; use maplit::hashmap;
use miette::{IntoDiagnostic, Report, Result, WrapErr}; use miette::{IntoDiagnostic, Result};
use std::net::IpAddr;
use std::time::Duration;
use tokio::time::{sleep, timeout};
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct NotifyPush; pub struct NotifyPush;
@ -85,7 +82,12 @@ impl ServiceTrait for NotifyPush {
Ok(true) Ok(true)
} }
async fn post_setup(&self, docker: &Docker, cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
docker: &Docker,
cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
let ip = self.get_ip(docker, cloud_id).await?; let ip = self.get_ip(docker, cloud_id).await?;
Ok(vec![ Ok(vec![
format!("occ config:system:set trusted_proxies 1 --value {}", ip), format!("occ config:system:set trusted_proxies 1 --value {}", ip),
@ -93,69 +95,3 @@ impl ServiceTrait for NotifyPush {
]) ])
} }
} }
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
.into_diagnostic()?;
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
.into_diagnostic()
.wrap_err("Timeout after 30 seconds")?
}
pub async fn get_ip(&self, docker: &Docker, cloud_id: &str) -> Result<IpAddr> {
docker
.start_container::<String>(&self.container_name(cloud_id), None)
.await
.into_diagnostic()?;
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
.into_diagnostic()?;
if matches!(
info.state,
Some(ContainerState {
running: Some(true),
..
})
) {
info.network_settings
.unwrap()
.networks
.unwrap()
.values()
.next()
.unwrap()
.ip_address
.clone()
.unwrap()
.parse()
.into_diagnostic()
.wrap_err("Invalid ip address")
} else {
Err(Report::msg("notify_push not started"))
}
}
}

View file

@ -79,7 +79,12 @@ impl ServiceTrait for Smb {
Ok(true) Ok(true)
} }
async fn post_setup(&self, _docker: &Docker, _cloud_id: &str) -> Result<Vec<String>> { async fn post_setup(
&self,
_docker: &Docker,
_cloud_id: &str,
_config: &HazeConfig,
) -> Result<Vec<String>> {
Ok(vec![ Ok(vec![
"occ files_external:create smb smb password::password".into(), "occ files_external:create smb smb password::password".into(),
"occ files_external:config 1 host smb".into(), "occ files_external:config 1 host smb".into(),