mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 17:14:08 +02:00
s3
This commit is contained in:
parent
a6b0e8fde1
commit
fdee3d7cfa
4 changed files with 176 additions and 5 deletions
47
src/cloud.rs
47
src/cloud.rs
|
|
@ -3,12 +3,14 @@ use crate::database::Database;
|
|||
use crate::exec::{exec, exec_tty};
|
||||
use crate::mapping::default_mappings;
|
||||
use crate::php::PhpVersion;
|
||||
use crate::service::Service;
|
||||
use bollard::container::{ListContainersOptions, LogsOptions, RemoveContainerOptions};
|
||||
use bollard::models::ContainerState;
|
||||
use bollard::network::CreateNetworkOptions;
|
||||
use bollard::Docker;
|
||||
use camino::Utf8PathBuf;
|
||||
use color_eyre::{eyre::WrapErr, Report, Result};
|
||||
use futures_util::future::try_join_all;
|
||||
use futures_util::stream::StreamExt;
|
||||
use petname::petname;
|
||||
use std::collections::HashMap;
|
||||
|
|
@ -27,6 +29,7 @@ use tokio::time::sleep;
|
|||
pub struct CloudOptions {
|
||||
db: Database,
|
||||
php: PhpVersion,
|
||||
services: Vec<Service>,
|
||||
}
|
||||
|
||||
impl CloudOptions {
|
||||
|
|
@ -37,6 +40,7 @@ impl CloudOptions {
|
|||
{
|
||||
let mut db = None;
|
||||
let mut php = None;
|
||||
let mut services = Vec::new();
|
||||
|
||||
while let Some(option) = args.peek() {
|
||||
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()) {
|
||||
php = Some(php_option);
|
||||
let _ = args.next();
|
||||
} else if let Some(service) = Service::from_type(option.as_ref()) {
|
||||
services.push(service);
|
||||
let _ = args.next();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
|
@ -54,10 +61,11 @@ impl CloudOptions {
|
|||
}
|
||||
}
|
||||
|
||||
Ok(CloudOptions {
|
||||
Ok(dbg!(CloudOptions {
|
||||
db: db.unwrap_or_default(),
|
||||
php: php.unwrap_or_default(),
|
||||
})
|
||||
services,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,6 +119,7 @@ pub struct Cloud {
|
|||
pub db: Database,
|
||||
pub ip: Option<IpAddr>,
|
||||
pub workdir: Utf8PathBuf,
|
||||
pub services: Vec<Service>,
|
||||
}
|
||||
|
||||
impl Cloud {
|
||||
|
|
@ -167,6 +176,24 @@ impl Cloud {
|
|||
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
|
||||
.php
|
||||
.spawn(docker, &id, env, &options.db, &network, volumes)
|
||||
|
|
@ -214,6 +241,7 @@ impl Cloud {
|
|||
db: options.db,
|
||||
ip: Some(ip),
|
||||
workdir,
|
||||
services: options.services,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -311,6 +339,13 @@ impl Cloud {
|
|||
let labels = cloud.labels?;
|
||||
let db = labels.get("haze-db")?.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
|
||||
.iter()
|
||||
.filter_map(|service| service.names.as_ref()?.first().map(String::clone))
|
||||
|
|
@ -326,6 +361,7 @@ impl Cloud {
|
|||
containers: service_ids,
|
||||
ip: network_info.ip_address.as_ref()?.parse().ok(),
|
||||
workdir,
|
||||
services: found_services,
|
||||
},
|
||||
))
|
||||
})
|
||||
|
|
@ -360,6 +396,13 @@ impl Cloud {
|
|||
.wait_for_start(docker, &self.id)
|
||||
.await
|
||||
.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(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use std::io::stdout;
|
|||
use std::io::Write;
|
||||
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 {
|
||||
let mut info_stream = docker.create_image(
|
||||
Some(CreateImageOptions {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use crate::args::HazeArgs;
|
||||
use crate::cloud::Cloud;
|
||||
use crate::config::HazeConfig;
|
||||
use crate::service::Service;
|
||||
use bollard::Docker;
|
||||
use color_eyre::{eyre::WrapErr, Result};
|
||||
|
||||
|
|
@ -12,6 +13,7 @@ mod exec;
|
|||
mod image;
|
||||
mod mapping;
|
||||
mod php;
|
||||
mod service;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
|
|
@ -33,19 +35,22 @@ async fn main() -> Result<()> {
|
|||
HazeArgs::List { filter } => {
|
||||
let list = Cloud::list(&mut docker, filter, &config).await?;
|
||||
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 {
|
||||
Some(ip) => println!(
|
||||
"Cloud {}, {}, {}, running on http://{}",
|
||||
cloud.id,
|
||||
cloud.php.name(),
|
||||
cloud.db.name(),
|
||||
services,
|
||||
ip
|
||||
),
|
||||
None => println!(
|
||||
"Cloud {}, {}, {}, not running",
|
||||
cloud.id,
|
||||
cloud.php.name(),
|
||||
cloud.db.name()
|
||||
services
|
||||
),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
123
src/service.rs
Normal file
123
src/service.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue