mirror of
https://codeberg.org/icewind/haze.git
synced 2026-06-03 09:04:12 +02:00
add haze env
This commit is contained in:
parent
e70764fbb5
commit
2a587c0c7e
4 changed files with 85 additions and 1 deletions
10
README.md
10
README.md
|
|
@ -147,6 +147,16 @@ Pinned instances will not be removed by `haze clean`.
|
||||||
haze [match] unpin
|
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
|
||||||
|
|
||||||
Configuration is loaded from `~/.config/haze/haze.toml` and has the following options
|
Configuration is loaded from `~/.config/haze/haze.toml` and has the following options
|
||||||
|
|
|
||||||
19
src/args.rs
19
src/args.rs
|
|
@ -64,6 +64,11 @@ pub enum HazeArgs {
|
||||||
Checkout {
|
Checkout {
|
||||||
branch: String,
|
branch: String,
|
||||||
},
|
},
|
||||||
|
Env {
|
||||||
|
filter: Option<String>,
|
||||||
|
command: String,
|
||||||
|
args: Vec<String>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
|
@ -222,6 +227,17 @@ impl HazeArgs {
|
||||||
.ok_or_else(|| Report::msg("No branch provided"))?;
|
.ok_or_else(|| Report::msg("No branch provided"))?;
|
||||||
Ok(HazeArgs::Checkout { branch })
|
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,
|
Unpin,
|
||||||
Proxy,
|
Proxy,
|
||||||
Checkout,
|
Checkout,
|
||||||
|
Env,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for HazeCommand {
|
impl FromStr for HazeCommand {
|
||||||
|
|
@ -270,6 +287,7 @@ impl FromStr for HazeCommand {
|
||||||
"unpin" => Ok(HazeCommand::Unpin),
|
"unpin" => Ok(HazeCommand::Unpin),
|
||||||
"proxy" => Ok(HazeCommand::Proxy),
|
"proxy" => Ok(HazeCommand::Proxy),
|
||||||
"checkout" => Ok(HazeCommand::Checkout),
|
"checkout" => Ok(HazeCommand::Checkout),
|
||||||
|
"env" => Ok(HazeCommand::Env),
|
||||||
_ => Err(Report::msg(format!("Unknown command: {}", s))),
|
_ => Err(Report::msg(format!("Unknown command: {}", s))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -295,6 +313,7 @@ impl HazeCommand {
|
||||||
HazeCommand::Unpin => true,
|
HazeCommand::Unpin => true,
|
||||||
HazeCommand::Proxy => false,
|
HazeCommand::Proxy => false,
|
||||||
HazeCommand::Checkout => false,
|
HazeCommand::Checkout => false,
|
||||||
|
HazeCommand::Env => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use bollard::Docker;
|
||||||
use maplit::hashmap;
|
use maplit::hashmap;
|
||||||
use miette::{IntoDiagnostic, Report, Result, WrapErr};
|
use miette::{IntoDiagnostic, Report, Result, WrapErr};
|
||||||
use std::io::{stdout, Stdout};
|
use std::io::{stdout, Stdout};
|
||||||
|
use std::net::IpAddr;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::{sleep, timeout};
|
use tokio::time::{sleep, timeout};
|
||||||
|
|
@ -294,6 +295,24 @@ impl Database {
|
||||||
.wrap_err("Timeout after 15 seconds")?
|
.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> {
|
async fn is_healthy(&self, docker: &mut Docker, cloud_id: &str) -> Result<bool> {
|
||||||
match self.family() {
|
match self.family() {
|
||||||
DatabaseFamily::Sqlite => Ok(true),
|
DatabaseFamily::Sqlite => Ok(true),
|
||||||
|
|
|
||||||
38
src/main.rs
38
src/main.rs
|
|
@ -3,6 +3,7 @@ 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;
|
||||||
|
use crate::database::DatabaseFamily;
|
||||||
use crate::exec::container_logs;
|
use crate::exec::container_logs;
|
||||||
use crate::git::checkout_all;
|
use crate::git::checkout_all;
|
||||||
use crate::network::clear_networks;
|
use crate::network::clear_networks;
|
||||||
|
|
@ -10,8 +11,10 @@ use crate::proxy::proxy;
|
||||||
use crate::service::Service;
|
use crate::service::Service;
|
||||||
use crate::service::ServiceTrait;
|
use crate::service::ServiceTrait;
|
||||||
use bollard::Docker;
|
use bollard::Docker;
|
||||||
use miette::{IntoDiagnostic, Result, WrapErr};
|
use miette::{IntoDiagnostic, Report, Result, WrapErr};
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
mod cloud;
|
mod cloud;
|
||||||
|
|
@ -292,6 +295,39 @@ async fn main() -> Result<()> {
|
||||||
HazeArgs::Checkout { branch } => {
|
HazeArgs::Checkout { branch } => {
|
||||||
checkout_all(&config.sources_root, &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(())
|
Ok(())
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue