use crate::cloud::CloudOptions; use crate::config::HazeConfig; use crate::exec::exec; use crate::image::pull_image; use crate::service::ServiceTrait; use crate::Result; use bollard::models::{ ContainerCreateBody, ContainerState, EndpointSettings, HostConfig, NetworkingConfig, }; use bollard::query_parameters::CreateContainerOptions; use bollard::Docker; use maplit::hashmap; use miette::{IntoDiagnostic, Report}; use std::net::Ipv4Addr; #[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, _config: &HazeConfig, _options: &CloudOptions, ) -> Result> { let image = "onlyoffice/documentserver"; 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!("{}-onlyoffice", cloud_id)) } fn apps(&self) -> &'static [&'static str] { &["onlyoffice"] } async fn post_setup( &self, docker: &Docker, cloud_id: &str, config: &HazeConfig, ) -> Result> { let info = docker .inspect_container(&self.container_name(cloud_id).unwrap(), None) .await .into_diagnostic()?; 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")); }; let mut secret = Vec::new(); let exit = exec( docker, self.container_name(cloud_id).unwrap(), "root", vec![ "/var/www/onlyoffice/documentserver/npm/json", "-f", "/etc/onlyoffice/documentserver/local.json", "services.CoAuthoring.secret.session.string", ], Vec::::default(), Some(&mut secret), ) .await?; if !exit.is_ok() { dbg!(String::from_utf8_lossy(&secret)); return Err(Report::msg("Failed to get onlyoffice secret")); } let secret = String::from_utf8_lossy(&secret); let secret = secret.trim(); if config.proxy.https && !config.proxy.address.is_empty() { let addr = config.proxy.addr( &self.container_name(cloud_id).unwrap(), Ipv4Addr::UNSPECIFIED.into(), ); Ok(vec![ format!("occ config:app:set onlyoffice DocumentServerUrl --value {addr}/"), format!("occ config:app:set onlyoffice jwt_secret --value {secret}"), "occ onlyoffice:documentserver --check".into(), ]) } else { Ok(vec![ format!("occ config:app:set onlyoffice DocumentServerUrl --value https://{ip}/"), "occ config:app:set onlyoffice verify_peer_off --value true".into(), format!("occ config:app:set onlyoffice jwt_secret --value {secret}"), "occ onlyoffice:documentserver --check".into(), ]) } } }