1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-06-03 17:14:08 +02:00
This commit is contained in:
Robin Appelman 2021-03-18 21:10:44 +01:00
commit fdee3d7cfa
4 changed files with 176 additions and 5 deletions

View file

@ -3,12 +3,14 @@ use crate::database::Database;
use crate::exec::{exec, exec_tty}; use crate::exec::{exec, exec_tty};
use crate::mapping::default_mappings; use crate::mapping::default_mappings;
use crate::php::PhpVersion; use crate::php::PhpVersion;
use crate::service::Service;
use bollard::container::{ListContainersOptions, LogsOptions, RemoveContainerOptions}; use bollard::container::{ListContainersOptions, LogsOptions, RemoveContainerOptions};
use bollard::models::ContainerState; use bollard::models::ContainerState;
use bollard::network::CreateNetworkOptions; use bollard::network::CreateNetworkOptions;
use bollard::Docker; use bollard::Docker;
use camino::Utf8PathBuf; use camino::Utf8PathBuf;
use color_eyre::{eyre::WrapErr, Report, Result}; use color_eyre::{eyre::WrapErr, Report, Result};
use futures_util::future::try_join_all;
use futures_util::stream::StreamExt; use futures_util::stream::StreamExt;
use petname::petname; use petname::petname;
use std::collections::HashMap; use std::collections::HashMap;
@ -27,6 +29,7 @@ use tokio::time::sleep;
pub struct CloudOptions { pub struct CloudOptions {
db: Database, db: Database,
php: PhpVersion, php: PhpVersion,
services: Vec<Service>,
} }
impl CloudOptions { impl CloudOptions {
@ -37,6 +40,7 @@ impl CloudOptions {
{ {
let mut db = None; let mut db = None;
let mut php = None; let mut php = None;
let mut services = Vec::new();
while let Some(option) = args.peek() { while let Some(option) = args.peek() {
if let Ok(db_option) = Database::from_str(option.as_ref()) { if let Ok(db_option) = Database::from_str(option.as_ref()) {
@ -45,6 +49,9 @@ impl CloudOptions {
} else if let Ok(php_option) = PhpVersion::from_str(option.as_ref()) { } else if let Ok(php_option) = PhpVersion::from_str(option.as_ref()) {
php = Some(php_option); php = Some(php_option);
let _ = args.next(); let _ = args.next();
} else if let Some(service) = Service::from_type(option.as_ref()) {
services.push(service);
let _ = args.next();
} else { } else {
break; break;
} }
@ -54,10 +61,11 @@ impl CloudOptions {
} }
} }
Ok(CloudOptions { Ok(dbg!(CloudOptions {
db: db.unwrap_or_default(), db: db.unwrap_or_default(),
php: php.unwrap_or_default(), php: php.unwrap_or_default(),
}) services,
}))
} }
} }
@ -111,6 +119,7 @@ pub struct Cloud {
pub db: Database, pub db: Database,
pub ip: Option<IpAddr>, pub ip: Option<IpAddr>,
pub workdir: Utf8PathBuf, pub workdir: Utf8PathBuf,
pub services: Vec<Service>,
} }
impl Cloud { impl Cloud {
@ -167,6 +176,24 @@ impl Cloud {
env.push(format!("SQL={}", options.db.name())); env.push(format!("SQL={}", options.db.name()));
} }
let service_containers = try_join_all(
options
.services
.iter()
.map(|service| service.spawn(docker, &id, &network)),
)
.await?;
containers.extend_from_slice(&service_containers);
env.extend(
options
.services
.iter()
.flat_map(Service::env)
.copied()
.map(String::from),
);
let container = options let container = options
.php .php
.spawn(docker, &id, env, &options.db, &network, volumes) .spawn(docker, &id, env, &options.db, &network, volumes)
@ -214,6 +241,7 @@ impl Cloud {
db: options.db, db: options.db,
ip: Some(ip), ip: Some(ip),
workdir, workdir,
services: options.services,
}) })
} }
@ -311,6 +339,13 @@ impl Cloud {
let labels = cloud.labels?; let labels = cloud.labels?;
let db = labels.get("haze-db")?.parse().ok()?; let db = labels.get("haze-db")?.parse().ok()?;
let php = labels.get("haze-php")?.parse().ok()?; let php = labels.get("haze-php")?.parse().ok()?;
let found_services = services
.iter()
.flat_map(|container| &container.labels)
.flat_map(|labels| labels.get("haze-type"))
.map(String::as_str)
.flat_map(Service::from_type)
.collect();
let mut service_ids: Vec<String> = services let mut service_ids: Vec<String> = services
.iter() .iter()
.filter_map(|service| service.names.as_ref()?.first().map(String::clone)) .filter_map(|service| service.names.as_ref()?.first().map(String::clone))
@ -326,6 +361,7 @@ impl Cloud {
containers: service_ids, containers: service_ids,
ip: network_info.ip_address.as_ref()?.parse().ok(), ip: network_info.ip_address.as_ref()?.parse().ok(),
workdir, workdir,
services: found_services,
}, },
)) ))
}) })
@ -360,6 +396,13 @@ impl Cloud {
.wait_for_start(docker, &self.id) .wait_for_start(docker, &self.id)
.await .await
.wrap_err("Failed to wait for database container")?; .wrap_err("Failed to wait for database container")?;
try_join_all(
self.services
.iter()
.map(|service| service.wait_for_start(docker, &self.id)),
)
.await
.wrap_err("Failed to wait for service containers")?;
Ok(()) Ok(())
} }

View file

@ -8,7 +8,7 @@ use std::io::stdout;
use std::io::Write; use std::io::Write;
use termion::cursor; use termion::cursor;
pub async fn pull_image(docker: &mut Docker, image: &str) -> Result<()> { pub async fn pull_image(docker: &Docker, image: &str) -> Result<()> {
if let Err(_) = docker.inspect_image(image).await { if let Err(_) = docker.inspect_image(image).await {
let mut info_stream = docker.create_image( let mut info_stream = docker.create_image(
Some(CreateImageOptions { Some(CreateImageOptions {

View file

@ -1,6 +1,7 @@
use crate::args::HazeArgs; use crate::args::HazeArgs;
use crate::cloud::Cloud; use crate::cloud::Cloud;
use crate::config::HazeConfig; use crate::config::HazeConfig;
use crate::service::Service;
use bollard::Docker; use bollard::Docker;
use color_eyre::{eyre::WrapErr, Result}; use color_eyre::{eyre::WrapErr, Result};
@ -12,6 +13,7 @@ mod exec;
mod image; mod image;
mod mapping; mod mapping;
mod php; mod php;
mod service;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@ -33,19 +35,22 @@ async fn main() -> Result<()> {
HazeArgs::List { filter } => { HazeArgs::List { filter } => {
let list = Cloud::list(&mut docker, filter, &config).await?; let list = Cloud::list(&mut docker, filter, &config).await?;
for cloud in list { for cloud in list {
let mut services: Vec<_> = cloud.services.iter().map(Service::name).collect();
services.push(cloud.db.name());
let services = services.join(", ");
match cloud.ip { match cloud.ip {
Some(ip) => println!( Some(ip) => println!(
"Cloud {}, {}, {}, running on http://{}", "Cloud {}, {}, {}, running on http://{}",
cloud.id, cloud.id,
cloud.php.name(), cloud.php.name(),
cloud.db.name(), services,
ip ip
), ),
None => println!( None => println!(
"Cloud {}, {}, {}, not running", "Cloud {}, {}, {}, not running",
cloud.id, cloud.id,
cloud.php.name(), cloud.php.name(),
cloud.db.name() services
), ),
} }
} }

123
src/service.rs Normal file
View file

@ -0,0 +1,123 @@
use crate::image::pull_image;
use bollard::container::{Config, CreateContainerOptions, NetworkingConfig};
use bollard::models::{EndpointSettings, HostConfig};
use bollard::Docker;
use color_eyre::{eyre::WrapErr, Result};
use maplit::hashmap;
use std::time::Duration;
use tokio::time::{sleep, timeout};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Service {
ObjectStore(ObjectStore),
}
impl Service {
pub fn name(&self) -> &str {
match self {
Service::ObjectStore(store) => store.name(),
}
}
pub fn env(&self) -> &[&str] {
match self {
Service::ObjectStore(store) => store.env(),
}
}
pub async fn spawn(&self, docker: &Docker, cloud_id: &str, network: &str) -> Result<String> {
match self {
Service::ObjectStore(store) => store.spawn(docker, cloud_id, network).await,
}
}
async fn is_healthy(&self, docker: &Docker, cloud_id: &str) -> Result<bool> {
match self {
Service::ObjectStore(store) => store.is_healthy(docker, cloud_id).await,
}
}
pub fn from_type(ty: &str) -> Option<Self> {
match ty {
"s3" => Some(Service::ObjectStore(ObjectStore::S3)),
_ => None,
}
}
pub async fn wait_for_start(&self, docker: &Docker, cloud_id: &str) -> Result<()> {
timeout(Duration::from_secs(15), async {
while !self.is_healthy(docker, cloud_id).await? {
sleep(Duration::from_millis(100)).await
}
Ok(())
})
.await
.wrap_err("Timeout after 15 seconds")?
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ObjectStore {
S3,
}
impl ObjectStore {
fn image(&self) -> &str {
match self {
ObjectStore::S3 => "localstack/localstack:0.10.9",
}
}
fn name(&self) -> &str {
match self {
ObjectStore::S3 => "s3",
}
}
fn self_env(&self) -> Vec<&str> {
match self {
ObjectStore::S3 => vec!["DEBUG=1", "SERVICES=s3:4569"],
}
}
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> {
Ok(true)
}
}