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

push proxy wip

This commit is contained in:
Robin Appelman 2022-10-25 14:10:54 +02:00
commit 58918942fe
4 changed files with 84 additions and 58 deletions

20
flake.lock generated
View file

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"flake-utils": { "flake-utils": {
"locked": { "locked": {
"lastModified": 1656928814, "lastModified": 1659877975,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=", "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249", "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -20,11 +20,11 @@
"nixpkgs": "nixpkgs" "nixpkgs": "nixpkgs"
}, },
"locked": { "locked": {
"lastModified": 1655042882, "lastModified": 1662220400,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=", "narHash": "sha256-9o2OGQqu4xyLZP9K6kNe1pTHnyPz0Wr3raGYnr9AIgY=",
"owner": "nix-community", "owner": "nix-community",
"repo": "naersk", "repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f", "rev": "6944160c19cb591eb85bbf9b2f2768a935623ed3",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -36,8 +36,8 @@
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 0, "lastModified": 0,
"narHash": "sha256-DWMAHXaKn3Nb2n/f7XP059sYkW+Qq9D+YjyX15wrb98=", "narHash": "sha256-h+4F8intTl5QFb+Yv9e4xH1p5FSbHlmbCavxnuoFFSE=",
"path": "/nix/store/v1skd43lw11gyb85cpzx5ns1hafapsyx-source", "path": "/nix/store/k0ag5ji9grl4v14xsj1hyvbdzghyr578-source",
"type": "path" "type": "path"
}, },
"original": { "original": {
@ -48,8 +48,8 @@
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 0, "lastModified": 0,
"narHash": "sha256-DWMAHXaKn3Nb2n/f7XP059sYkW+Qq9D+YjyX15wrb98=", "narHash": "sha256-h+4F8intTl5QFb+Yv9e4xH1p5FSbHlmbCavxnuoFFSE=",
"path": "/nix/store/v1skd43lw11gyb85cpzx5ns1hafapsyx-source", "path": "/nix/store/k0ag5ji9grl4v14xsj1hyvbdzghyr578-source",
"type": "path" "type": "path"
}, },
"original": { "original": {

View file

@ -1,3 +1,5 @@
extern crate core;
use crate::args::{ExecService, HazeArgs}; use crate::args::{ExecService, HazeArgs};
use crate::cloud::{Cloud, CloudOptions}; use crate::cloud::{Cloud, CloudOptions};
use crate::config::HazeConfig; use crate::config::HazeConfig;

View file

@ -1,12 +1,12 @@
use crate::Result;
use crate::{Cloud, HazeConfig}; use crate::{Cloud, HazeConfig};
use crate::{Result, Service};
use bollard::Docker; use bollard::Docker;
use futures_util::future::Either; use futures_util::future::Either;
use futures_util::FutureExt; use futures_util::FutureExt;
use miette::{miette, Context, IntoDiagnostic}; use miette::{miette, Context, IntoDiagnostic};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{create_dir_all, remove_file, set_permissions}; use std::fs::{create_dir_all, remove_file, set_permissions};
use std::net::{IpAddr, SocketAddr}; use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr; use std::str::FromStr;
@ -24,7 +24,7 @@ use warp_reverse_proxy::{
}; };
struct ActiveInstances { struct ActiveInstances {
known: Mutex<HashMap<String, IpAddr>>, known: Mutex<HashMap<String, SocketAddr>>,
docker: Docker, docker: Docker,
config: HazeConfig, config: HazeConfig,
} }
@ -38,23 +38,39 @@ impl ActiveInstances {
} }
} }
pub async fn get(&self, name: &str) -> Option<IpAddr> { pub async fn get(&self, name: &str) -> Option<SocketAddr> {
if let Some(ip) = self.known.lock().unwrap().get(name).cloned() { if let Some(ip) = self.known.lock().unwrap().get(name).cloned() {
return Some(ip); return Some(ip);
} }
let addr = if let Some(name) = name.strip_suffix("-push") {
let cloud = Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config) let cloud = Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)
.await .await
.ok()?; .ok()?;
let push = cloud
.services
.iter()
.filter_map(|service| match service {
Service::Push(push) => Some(push),
_ => None,
})
.next()?;
let ip = push.get_ip(&self.docker, &cloud.id).await.ok()?;
SocketAddr::new(ip, 7867)
} else {
SocketAddr::new(
Cloud::get_by_filter(&self.docker, Some(name.into()), &self.config)
.await
.ok()?
.ip?,
80,
)
};
if let Some(ip) = cloud.ip { println!("{name} => {addr}");
println!("{name} => {ip}");
self.known.lock().unwrap().insert(name.into(), ip); self.known.lock().unwrap().insert(name.into(), addr);
return Some(ip); Some(addr)
}
None
} }
} }

View file

@ -6,6 +6,7 @@ use bollard::models::{ContainerState, EndpointSettings, HostConfig};
use bollard::Docker; use bollard::Docker;
use maplit::hashmap; use maplit::hashmap;
use miette::{IntoDiagnostic, Report, 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};
@ -85,38 +86,7 @@ impl ServiceTrait for NotifyPush {
} }
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>> {
docker let ip = self.get_ip(docker, cloud_id).await?;
.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()?;
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![ Ok(vec![
format!("occ config:system:set trusted_proxies 1 --value {}", ip), format!("occ config:system:set trusted_proxies 1 --value {}", ip),
format!("occ notify_push:setup http://{}:7867", ip), format!("occ notify_push:setup http://{}:7867", ip),
@ -150,4 +120,42 @@ impl NotifyPush {
.into_diagnostic() .into_diagnostic()
.wrap_err("Timeout after 30 seconds")? .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"))
}
}
} }