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

add haze env

This commit is contained in:
Robin Appelman 2023-03-20 20:36:44 +01:00
commit 2a587c0c7e
4 changed files with 85 additions and 1 deletions

View file

@ -147,6 +147,16 @@ Pinned instances will not be removed by `haze clean`.
haze [match] unpin
```
#### Run a command with instance environment variables set
```bash
haze [match] env <cmd> [args]
```
Runs the provided command with `NEXTCLOUD_URL`, `DATABASE_URL` and `REDIS_URL` environment variables set for the matched instance.
This is indented to run a local [push daemon](https://github.com/nextcloud/notify_push) against an instance.
## Configuration
Configuration is loaded from `~/.config/haze/haze.toml` and has the following options

View file

@ -64,6 +64,11 @@ pub enum HazeArgs {
Checkout {
branch: String,
},
Env {
filter: Option<String>,
command: String,
args: Vec<String>,
},
}
#[derive(Debug, Clone, Eq, PartialEq)]
@ -222,6 +227,17 @@ impl HazeArgs {
.ok_or_else(|| Report::msg("No branch provided"))?;
Ok(HazeArgs::Checkout { branch })
}
HazeCommand::Env => {
let mut args = args.map(S::into);
let command = args
.next()
.ok_or_else(|| Report::msg("No command provided"))?;
Ok(HazeArgs::Env {
filter,
command,
args: args.collect(),
})
}
}
}
}
@ -245,6 +261,7 @@ pub enum HazeCommand {
Unpin,
Proxy,
Checkout,
Env,
}
impl FromStr for HazeCommand {
@ -270,6 +287,7 @@ impl FromStr for HazeCommand {
"unpin" => Ok(HazeCommand::Unpin),
"proxy" => Ok(HazeCommand::Proxy),
"checkout" => Ok(HazeCommand::Checkout),
"env" => Ok(HazeCommand::Env),
_ => Err(Report::msg(format!("Unknown command: {}", s))),
}
}
@ -295,6 +313,7 @@ impl HazeCommand {
HazeCommand::Unpin => true,
HazeCommand::Proxy => false,
HazeCommand::Checkout => false,
HazeCommand::Env => true,
}
}
}

View file

@ -6,6 +6,7 @@ use bollard::Docker;
use maplit::hashmap;
use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::io::{stdout, Stdout};
use std::net::IpAddr;
use std::str::FromStr;
use std::time::Duration;
use tokio::time::{sleep, timeout};
@ -294,6 +295,24 @@ impl Database {
.wrap_err("Timeout after 15 seconds")?
}
pub async fn ip(&self, docker: &mut Docker, cloud_id: &str) -> Option<IpAddr> {
match self.family() {
DatabaseFamily::Sqlite => return None,
_ => docker
.inspect_container(&format!("{}-db", cloud_id), None)
.await
.ok()?
.network_settings?
.networks?
.values()
.next()?
.ip_address
.clone()?
.parse()
.ok(),
}
}
async fn is_healthy(&self, docker: &mut Docker, cloud_id: &str) -> Result<bool> {
match self.family() {
DatabaseFamily::Sqlite => Ok(true),

View file

@ -3,6 +3,7 @@ extern crate core;
use crate::args::{ExecService, HazeArgs};
use crate::cloud::{Cloud, CloudOptions};
use crate::config::HazeConfig;
use crate::database::DatabaseFamily;
use crate::exec::container_logs;
use crate::git::checkout_all;
use crate::network::clear_networks;
@ -10,8 +11,10 @@ use crate::proxy::proxy;
use crate::service::Service;
use crate::service::ServiceTrait;
use bollard::Docker;
use miette::{IntoDiagnostic, Result, WrapErr};
use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::io::stdout;
use std::os::unix::process::CommandExt;
use std::process::Command;
mod args;
mod cloud;
@ -292,6 +295,39 @@ async fn main() -> Result<()> {
HazeArgs::Checkout { branch } => {
checkout_all(&config.sources_root, &branch)?;
}
HazeArgs::Env {
filter,
command,
args,
} => {
let cloud = Cloud::get_by_filter(&mut docker, filter, &config).await?;
let ip = cloud
.ip
.ok_or_else(|| Report::msg(format!("{} is not running", cloud.id)))?;
let db_type = match cloud.db.family() {
DatabaseFamily::Sqlite => {
return Err(Report::msg("sqlite is not supported with `haze env`"))
}
DatabaseFamily::Mysql | DatabaseFamily::MariaDB => "mysql",
DatabaseFamily::Postgres => "postgresql",
};
let db_ip = cloud
.db
.ip(&mut docker, &cloud.id)
.await
.ok_or_else(|| Report::msg(format!("{}-db is not running", cloud.id)))?;
let err = Command::new(command)
.args(args)
.env("REDIS_URL", format!("redis://{}", ip))
.env("NEXTCLOUD_URL", &cloud.address)
.env(
"DATABASE_URL",
format!("{}://haze:haze@{}/haze", db_type, db_ip),
)
.exec();
return Err(err).into_diagnostic();
}
};
Ok(())