Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2aeb9ee132 | |||
| a003043c93 | |||
| 891c601466 | |||
| 8f94cccd41 | |||
| 4d4ce9068e | |||
| fdf6085a35 | |||
| 7fb751c2ef | |||
| ae6c62d5de | |||
| 8246e237bf | |||
| 34a77eb436 | |||
| 97c25f6b20 | |||
| 31bf15d37d | |||
| 35a2609f29 | |||
| 2dfd67ec0d | |||
| 71f4faa8cb | |||
| 5414b1d529 | |||
| dca467e21d | |||
| 8d36882fbd | |||
| b1bee39653 | |||
| 2e87e4d92f | |||
| b58c9fbeb5 | |||
| 15fe8c9ce6 |
Generated
+250
-555
File diff suppressed because it is too large
Load Diff
+5
-4
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dkl"
|
name = "dkl"
|
||||||
version = "1.1.0"
|
version = "1.2.3"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
@@ -23,13 +23,14 @@ eyre = "0.6.12"
|
|||||||
fastrand = "2.3.0"
|
fastrand = "2.3.0"
|
||||||
futures = "0.3.31"
|
futures = "0.3.31"
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
|
getrandom = "0.4.2"
|
||||||
glob = "0.3.2"
|
glob = "0.3.2"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
human-units = "0.5.3"
|
human-units = "0.5.3"
|
||||||
log = "0.4.27"
|
log = "0.4.27"
|
||||||
lz4 = "1.28.1"
|
lz4 = "1.28.1"
|
||||||
memchr = "2.8.0"
|
memchr = "2.8.0"
|
||||||
nix = { version = "0.31.2", features = ["process", "signal", "user"] }
|
nix = { version = "0.31.2", features = ["fs", "process", "signal", "user"] }
|
||||||
openssl = "0.10.73"
|
openssl = "0.10.73"
|
||||||
page_size = "0.6.0"
|
page_size = "0.6.0"
|
||||||
reqwest = { version = "0.13.1", features = ["json", "stream", "native-tls", "socks"], default-features = false }
|
reqwest = { version = "0.13.1", features = ["json", "stream", "native-tls", "socks"], default-features = false }
|
||||||
@@ -37,9 +38,9 @@ rpassword = "7.4.0"
|
|||||||
rust-argon2 = "3.0.0"
|
rust-argon2 = "3.0.0"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
serde_yaml = "0.9.34"
|
serde_yaml = { version = "0.10.0", package = "serde_yaml_ng" }
|
||||||
signal-hook = "0.4.4"
|
signal-hook = "0.4.4"
|
||||||
tabled = "0.20.0"
|
tabled = "0.20.0"
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tokio = { version = "1.45.1", features = ["fs", "io-std", "macros", "process", "rt"] }
|
tokio = { version = "1.45.1", features = ["fs", "io-std", "macros", "process", "rt", "signal"] }
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
from mcluseau/rust:1.94.0 as build
|
from mcluseau/rust:1.97.0 as build
|
||||||
|
|
||||||
workdir /app
|
workdir /app
|
||||||
copy . .
|
copy . .
|
||||||
@@ -10,6 +10,6 @@ run \
|
|||||||
&& find target/release -maxdepth 1 -type f -executable -exec cp -v {} /dist/ +
|
&& find target/release -maxdepth 1 -type f -executable -exec cp -v {} /dist/ +
|
||||||
|
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
from alpine:3.23
|
from alpine:3.24.0
|
||||||
copy --from=build /dist/ /bin/
|
copy --from=build /dist/ /bin/
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
cargo install --path . --root dist
|
cargo install --locked --path . --root dist
|
||||||
|
|||||||
+6
-4
@@ -2,15 +2,17 @@ set -ex
|
|||||||
tag=$(git describe --always)
|
tag=$(git describe --always)
|
||||||
repo=novit.tech/direktil/dkl:$tag
|
repo=novit.tech/direktil/dkl:$tag
|
||||||
|
|
||||||
docker build --push --platform=linux/amd64,linux/arm64 . -t $repo
|
|
||||||
|
|
||||||
publish() {
|
publish() {
|
||||||
arch=$1
|
arch=$1
|
||||||
pf=$2
|
pf=$2
|
||||||
|
|
||||||
|
docker build --push --platform=$pf . -t $repo
|
||||||
|
|
||||||
|
for bin in dkl dls; do
|
||||||
curl --user $(jq '.auths["novit.tech"].auth' ~/.docker/config.json -r |base64 -d) \
|
curl --user $(jq '.auths["novit.tech"].auth' ~/.docker/config.json -r |base64 -d) \
|
||||||
--upload-file <(docker run --rm --platform $pf $repo cat /bin/dkl) \
|
--upload-file <(docker run --rm --platform $pf $repo cat /bin/$bin) \
|
||||||
https://novit.tech/api/packages/direktil/generic/dkl/$tag/dkl.$arch
|
https://novit.tech/api/packages/direktil/generic/dkl/$tag/$bin.$arch
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
publish x86_64 linux/amd64
|
publish x86_64 linux/amd64
|
||||||
|
|||||||
+31
-34
@@ -1,12 +1,20 @@
|
|||||||
use eyre::Result;
|
use eyre::{Result, format_err, eyre};
|
||||||
use log::info;
|
use log::info;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
||||||
use crate::base64_decode;
|
use crate::File;
|
||||||
|
|
||||||
pub async fn files(files: &[crate::File], root: &str, dry_run: bool) -> Result<()> {
|
pub async fn files(files: &[File], root: &str, dry_run: bool) -> Result<()> {
|
||||||
for file in files {
|
for f in files {
|
||||||
|
if let Err(e) = file(f, root, dry_run).await {
|
||||||
|
return Err(format_err!("{}: {e}", f.path));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn file(file: &File, root: &str, dry_run: bool) -> Result<()> {
|
||||||
let path = chroot(root, &file.path);
|
let path = chroot(root, &file.path);
|
||||||
let path = Path::new(&path);
|
let path = Path::new(&path);
|
||||||
|
|
||||||
@@ -14,60 +22,49 @@ pub async fn files(files: &[crate::File], root: &str, dry_run: bool) -> Result<(
|
|||||||
fs::create_dir_all(parent).await?;
|
fs::create_dir_all(parent).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let kind = file.kind();
|
||||||
|
let content = kind.content()?;
|
||||||
|
|
||||||
use crate::FileKind as K;
|
use crate::FileKind as K;
|
||||||
match &file.kind {
|
match kind.as_ref() {
|
||||||
K::Content(content) => {
|
K::Skip => {
|
||||||
|
info!("{}: kind is skip", file.path);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
K::Content(_) | K::Content64(_) | K::Parts(_) => {
|
||||||
|
let content = content.expect("this file kind should have content");
|
||||||
if dry_run {
|
if dry_run {
|
||||||
info!(
|
info!("would create {} ({} bytes)", file.path, content.len());
|
||||||
"would create {} ({} bytes from content)",
|
|
||||||
file.path,
|
|
||||||
content.len()
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
fs::write(path, content.as_bytes()).await?;
|
fs::write(path, &content).await.map_err(|e| eyre!("write {}: {e}", path.display()))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
K::Content64(content) => {
|
K::Dir => {
|
||||||
let content = base64_decode(content)?;
|
|
||||||
if dry_run {
|
|
||||||
info!(
|
|
||||||
"would create {} ({} bytes from content64)",
|
|
||||||
file.path,
|
|
||||||
content.len()
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
fs::write(path, content).await?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
K::Dir(true) => {
|
|
||||||
if dry_run {
|
if dry_run {
|
||||||
info!("would create {} (directory)", file.path);
|
info!("would create {} (directory)", file.path);
|
||||||
} else {
|
} else {
|
||||||
fs::create_dir(path).await?;
|
fs::create_dir(path).await.map_err(|e| eyre!("create dir {}: {e}", path.display()))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
K::Dir(false) => {} // shouldn't happen, but semantic is to ignore
|
|
||||||
K::Symlink(tgt) => {
|
K::Symlink(tgt) => {
|
||||||
if dry_run {
|
if dry_run {
|
||||||
info!("would create {} (symlink to {})", file.path, tgt);
|
info!("would create {} (symlink to {})", file.path, tgt);
|
||||||
} else {
|
} else {
|
||||||
fs::symlink(tgt, path).await?;
|
let _ = fs::remove_file(path).await; // we're ln --force
|
||||||
|
fs::symlink(tgt, path).await.map_err(|e| eyre!("symlink {} -> {tgt}: {e}", path.display()))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if dry_run {
|
if dry_run {
|
||||||
continue;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
match file.kind {
|
if !file.is_symlink() {
|
||||||
K::Symlink(_) => {}
|
set_perms(path, file.mode).await?;
|
||||||
_ => set_perms(path, file.mode).await?,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("created {}", file.path);
|
info!("created {}", file.path);
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+100
-20
@@ -1,5 +1,5 @@
|
|||||||
use clap::{CommandFactory, Parser, Subcommand};
|
use clap::{CommandFactory, Parser, Subcommand};
|
||||||
use eyre::{format_err, Result};
|
use eyre::{Result, format_err};
|
||||||
use human_units::Duration;
|
use human_units::Duration;
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@@ -9,6 +9,8 @@ use tokio::fs;
|
|||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command()]
|
#[command()]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
|
#[arg(long)]
|
||||||
|
log_to: Option<PathBuf>,
|
||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
command: Command,
|
command: Command,
|
||||||
}
|
}
|
||||||
@@ -32,17 +34,17 @@ enum Command {
|
|||||||
Logger {
|
Logger {
|
||||||
/// Path where the logs are stored
|
/// Path where the logs are stored
|
||||||
#[arg(long, short = 'p', default_value = "/var/log", env = "DKL_LOG_PATH")]
|
#[arg(long, short = 'p', default_value = "/var/log", env = "DKL_LOG_PATH")]
|
||||||
log_path: String,
|
log_path: PathBuf,
|
||||||
/// Name of the log instead of the command's basename
|
/// Name of the log instead of the command's basename
|
||||||
#[arg(long, short = 'n')]
|
#[arg(long, short = 'n')]
|
||||||
log_name: Option<String>,
|
log_name: Option<PathBuf>,
|
||||||
/// prefix log lines with time & stream
|
/// prefix log lines with time & stream
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
with_prefix: bool,
|
with_prefix: bool,
|
||||||
/// exec command in this cgroup
|
/// exec command in this cgroup
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
cgroup: Option<String>,
|
cgroup: Option<String>,
|
||||||
command: String,
|
command: PathBuf,
|
||||||
args: Vec<String>,
|
args: Vec<String>,
|
||||||
},
|
},
|
||||||
Log {
|
Log {
|
||||||
@@ -90,6 +92,11 @@ enum Command {
|
|||||||
#[command(subcommand)]
|
#[command(subcommand)]
|
||||||
cmd: CgCmd,
|
cmd: CgCmd,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
Rc {
|
||||||
|
#[command(subcommand)]
|
||||||
|
cmd: RcCmd,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
@@ -104,16 +111,56 @@ enum CgCmd {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum RcCmd {
|
||||||
|
Run,
|
||||||
|
Ls,
|
||||||
|
Status,
|
||||||
|
ReloadConfig,
|
||||||
|
Start {
|
||||||
|
#[arg(add = completions(dkl::rc::complete))]
|
||||||
|
key: String,
|
||||||
|
},
|
||||||
|
Stop {
|
||||||
|
#[arg(add = completions(dkl::rc::complete))]
|
||||||
|
key: String,
|
||||||
|
},
|
||||||
|
Reload {
|
||||||
|
#[arg(add = completions(dkl::rc::complete))]
|
||||||
|
key: String,
|
||||||
|
},
|
||||||
|
Sig {
|
||||||
|
#[arg(add = completions(dkl::rc::complete))]
|
||||||
|
key: String,
|
||||||
|
signal: u32,
|
||||||
|
},
|
||||||
|
Ctl {
|
||||||
|
args: Vec<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
clap_complete::CompleteEnv::with_factory(Cli::command).complete();
|
clap_complete::CompleteEnv::with_factory(Cli::command).complete();
|
||||||
|
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
|
|
||||||
env_logger::builder()
|
{
|
||||||
.parse_filters("info")
|
let mut builder = env_logger::builder();
|
||||||
.parse_default_env()
|
builder.parse_filters("info").parse_default_env();
|
||||||
.init();
|
|
||||||
|
if let Some(log_to) = cli.log_to {
|
||||||
|
builder.target(env_logger::Target::Pipe(Box::new(
|
||||||
|
std::fs::OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.append(true)
|
||||||
|
.open(log_to)
|
||||||
|
.unwrap(),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.init();
|
||||||
|
}
|
||||||
|
|
||||||
use Command as C;
|
use Command as C;
|
||||||
match cli.command {
|
match cli.command {
|
||||||
@@ -127,23 +174,24 @@ async fn main() -> Result<()> {
|
|||||||
apply_config(&config, &filters, &prefix, dry_run).await
|
apply_config(&config, &filters, &prefix, dry_run).await
|
||||||
}
|
}
|
||||||
C::Logger {
|
C::Logger {
|
||||||
ref log_path,
|
log_path,
|
||||||
ref log_name,
|
log_name,
|
||||||
with_prefix,
|
with_prefix,
|
||||||
cgroup,
|
cgroup,
|
||||||
command,
|
command,
|
||||||
args,
|
args,
|
||||||
} => {
|
} => {
|
||||||
let command = command.as_str();
|
let log_name = log_name.unwrap_or_else(|| command.file_prefix().unwrap().into());
|
||||||
let log_name = log_name.as_deref().unwrap_or_else(|| basename(command));
|
|
||||||
|
|
||||||
dkl::logger::Logger {
|
let logger = dkl::logger::Logger {
|
||||||
log_path,
|
log_path,
|
||||||
log_name,
|
log_name,
|
||||||
with_prefix,
|
with_prefix,
|
||||||
}
|
cgroup,
|
||||||
.run(cgroup, command, &args)
|
};
|
||||||
.await
|
|
||||||
|
let cmd = logger.setup(command, &args).await?;
|
||||||
|
logger.exec(cmd).await
|
||||||
}
|
}
|
||||||
C::Log {
|
C::Log {
|
||||||
log_path,
|
log_path,
|
||||||
@@ -187,6 +235,20 @@ async fn main() -> Result<()> {
|
|||||||
cols,
|
cols,
|
||||||
} => Ok(dkl::cgroup::ls(root, &exclude, cols.as_deref()).await?),
|
} => Ok(dkl::cgroup::ls(root, &exclude, cols.as_deref()).await?),
|
||||||
},
|
},
|
||||||
|
|
||||||
|
C::Rc { cmd } => match cmd {
|
||||||
|
RcCmd::Run => Ok(dkl::rc::run().await?),
|
||||||
|
RcCmd::Ls => Ok(dkl::rc::ctl(["ls"]).await?),
|
||||||
|
RcCmd::Status => Ok(dkl::rc::ctl(["status"]).await?),
|
||||||
|
RcCmd::ReloadConfig => Ok(dkl::rc::ctl(["reload-config"]).await?),
|
||||||
|
RcCmd::Start { key } => Ok(dkl::rc::ctl(["start", &key]).await?),
|
||||||
|
RcCmd::Stop { key } => Ok(dkl::rc::ctl(["stop", &key]).await?),
|
||||||
|
RcCmd::Reload { key } => Ok(dkl::rc::ctl(["reload", &key]).await?),
|
||||||
|
RcCmd::Sig { key, signal } => {
|
||||||
|
Ok(dkl::rc::ctl(["sig", &key, &signal.to_string()]).await?)
|
||||||
|
}
|
||||||
|
RcCmd::Ctl { args } => Ok(dkl::rc::ctl(&args).await?),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,10 +360,6 @@ fn parse_ts_arg(ts: Option<String>) -> Result<Option<dkl::logger::Timestamp>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn basename(path: &str) -> &str {
|
|
||||||
path.rsplit_once('/').map_or(path, |split| split.1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_globs(filters: &[String]) -> Result<Vec<glob::Pattern>> {
|
fn parse_globs(filters: &[String]) -> Result<Vec<glob::Pattern>> {
|
||||||
let mut errors = false;
|
let mut errors = false;
|
||||||
let filters = (filters.iter())
|
let filters = (filters.iter())
|
||||||
@@ -321,3 +379,25 @@ fn parse_globs(filters: &[String]) -> Result<Vec<glob::Pattern>> {
|
|||||||
|
|
||||||
Ok(filters)
|
Ok(filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use clap_complete::{ArgValueCandidates, CompletionCandidate};
|
||||||
|
|
||||||
|
fn completions(f: impl AsyncFn() -> Vec<String> + Send + Sync + 'static) -> ArgValueCandidates {
|
||||||
|
let f = std::sync::Arc::new(f);
|
||||||
|
|
||||||
|
ArgValueCandidates::new(move || {
|
||||||
|
let f = f.clone();
|
||||||
|
std::thread::spawn(move || {
|
||||||
|
tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
.block_on(async move { f().await })
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
.map(CompletionCandidate::new)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
+62
-8
@@ -3,6 +3,7 @@ use clap::{CommandFactory, Parser, Subcommand};
|
|||||||
use eyre::format_err;
|
use eyre::format_err;
|
||||||
use futures_util::Stream;
|
use futures_util::Stream;
|
||||||
use futures_util::StreamExt;
|
use futures_util::StreamExt;
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
||||||
@@ -21,6 +22,8 @@ struct Cli {
|
|||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Command {
|
enum Command {
|
||||||
|
#[command(subcommand)]
|
||||||
|
Config(Config),
|
||||||
Clusters,
|
Clusters,
|
||||||
Cluster {
|
Cluster {
|
||||||
cluster: String,
|
cluster: String,
|
||||||
@@ -40,6 +43,16 @@ enum Command {
|
|||||||
Hash {
|
Hash {
|
||||||
salt: String,
|
salt: String,
|
||||||
},
|
},
|
||||||
|
Store {
|
||||||
|
store_path: PathBuf,
|
||||||
|
#[command(subcommand)]
|
||||||
|
op: StoreOp,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Config {
|
||||||
|
Upload { config_path: PathBuf },
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
@@ -71,6 +84,10 @@ enum ClusterCommand {
|
|||||||
#[arg(default_value = "cluster")]
|
#[arg(default_value = "cluster")]
|
||||||
name: String,
|
name: String,
|
||||||
},
|
},
|
||||||
|
SetCaCert {
|
||||||
|
ca: String,
|
||||||
|
cert_path: PathBuf,
|
||||||
|
},
|
||||||
Token {
|
Token {
|
||||||
#[arg(default_value = "admin")]
|
#[arg(default_value = "admin")]
|
||||||
name: String,
|
name: String,
|
||||||
@@ -96,6 +113,12 @@ enum ClusterCommand {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum StoreOp {
|
||||||
|
Get { data_path: PathBuf },
|
||||||
|
Set { data_path: PathBuf, value: String },
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
async fn main() -> eyre::Result<()> {
|
async fn main() -> eyre::Result<()> {
|
||||||
clap_complete::CompleteEnv::with_factory(Cli::command).complete();
|
clap_complete::CompleteEnv::with_factory(Cli::command).complete();
|
||||||
@@ -119,18 +142,26 @@ async fn main() -> eyre::Result<()> {
|
|||||||
let dls = dls();
|
let dls = dls();
|
||||||
let cluster = dls.cluster(cluster);
|
let cluster = dls.cluster(cluster);
|
||||||
|
|
||||||
|
let Some(command) = command else {
|
||||||
|
write_json(&cluster.config().await?);
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
use ClusterCommand as CC;
|
use ClusterCommand as CC;
|
||||||
match command {
|
match command {
|
||||||
None => write_json(&cluster.config().await?),
|
CC::CaCert { name } => write_raw(&cluster.ca_cert(&name).await?),
|
||||||
Some(CC::CaCert { name }) => write_raw(&cluster.ca_cert(&name).await?),
|
CC::SetCaCert { ca, cert_path } => {
|
||||||
Some(CC::Token { name }) => println!("{}", &cluster.token(&name).await?),
|
let cert = tokio::fs::read(cert_path).await?;
|
||||||
Some(CC::Addons) => write_raw(&cluster.addons().await?),
|
cluster.set_ca_cert(&ca, cert).await?;
|
||||||
Some(CC::SshSign {
|
}
|
||||||
|
CC::Token { name } => println!("{}", &cluster.token(&name).await?),
|
||||||
|
CC::Addons => write_raw(&cluster.addons().await?),
|
||||||
|
CC::SshSign {
|
||||||
user_public_key,
|
user_public_key,
|
||||||
principal,
|
principal,
|
||||||
validity,
|
validity,
|
||||||
options,
|
options,
|
||||||
}) => {
|
} => {
|
||||||
let pub_key = tokio::fs::read_to_string(user_public_key).await?;
|
let pub_key = tokio::fs::read_to_string(user_public_key).await?;
|
||||||
let cert = cluster
|
let cert = cluster
|
||||||
.ssh_userca_sign(&dls::SshSignReq {
|
.ssh_userca_sign(&dls::SshSignReq {
|
||||||
@@ -142,12 +173,12 @@ async fn main() -> eyre::Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
write_raw(&cert);
|
write_raw(&cert);
|
||||||
}
|
}
|
||||||
Some(CC::KubeSign {
|
CC::KubeSign {
|
||||||
csr,
|
csr,
|
||||||
user,
|
user,
|
||||||
group,
|
group,
|
||||||
validity,
|
validity,
|
||||||
}) => {
|
} => {
|
||||||
let csr = tokio::fs::read_to_string(csr).await?;
|
let csr = tokio::fs::read_to_string(csr).await?;
|
||||||
let cert = cluster
|
let cert = cluster
|
||||||
.kube_sign(&dls::KubeSignReq {
|
.kube_sign(&dls::KubeSignReq {
|
||||||
@@ -231,6 +262,29 @@ async fn main() -> eyre::Result<()> {
|
|||||||
println!("hash (hex): {}", hex::encode(&hash));
|
println!("hash (hex): {}", hex::encode(&hash));
|
||||||
println!("hash (base64): {}", dkl::base64_encode(&hash));
|
println!("hash (base64): {}", dkl::base64_encode(&hash));
|
||||||
}
|
}
|
||||||
|
C::Store { store_path, op } => {
|
||||||
|
let mut s = dls::store::Store::new(store_path);
|
||||||
|
s.unlock(&std::env::var("DLS_STORE_PW").unwrap()).await?;
|
||||||
|
|
||||||
|
match op {
|
||||||
|
StoreOp::Get { data_path } => {
|
||||||
|
let mut data = std::io::Cursor::new(s.read(data_path).await?);
|
||||||
|
tokio::io::copy(&mut data, &mut tokio::io::stdout()).await?;
|
||||||
|
}
|
||||||
|
StoreOp::Set { data_path, value } => s.write(data_path, value.as_bytes()).await?,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
C::Config(cmd) => match cmd {
|
||||||
|
Config::Upload { config_path } => {
|
||||||
|
let cfg = fs::read(&config_path).await?;
|
||||||
|
let cfg: dls::Config = if config_path.ends_with(".yaml") {
|
||||||
|
serde_yaml::from_slice(&cfg)?
|
||||||
|
} else {
|
||||||
|
serde_json::from_slice(&cfg)?
|
||||||
|
};
|
||||||
|
dls().upload_config(&cfg).await?;
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -250,4 +250,10 @@ pub struct Bootstrap {
|
|||||||
pub dev: String,
|
pub dev: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub seed: Option<String>,
|
pub seed: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub seed_ca: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub seed_servername: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub seed_proxy: Option<String>,
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -64,8 +64,8 @@ pub async fn ls(
|
|||||||
}
|
}
|
||||||
|
|
||||||
use tabled::settings::{
|
use tabled::settings::{
|
||||||
object::{Column, Row},
|
|
||||||
Alignment, Modify,
|
Alignment, Modify,
|
||||||
|
object::{Column, Row},
|
||||||
};
|
};
|
||||||
let mut table = table.build();
|
let mut table = table.build();
|
||||||
table.with(tabled::settings::Style::psql());
|
table.with(tabled::settings::Style::psql());
|
||||||
|
|||||||
+70
-13
@@ -23,6 +23,18 @@ impl Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_with_http_client(
|
||||||
|
base_url: String,
|
||||||
|
token: String,
|
||||||
|
http_client: reqwest::Client,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
base_url,
|
||||||
|
token,
|
||||||
|
http_client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn with_proxy(self, proxy: String) -> reqwest::Result<Self> {
|
pub fn with_proxy(self, proxy: String) -> reqwest::Result<Self> {
|
||||||
let proxy = reqwest::Proxy::all(proxy)?;
|
let proxy = reqwest::Proxy::all(proxy)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@@ -65,6 +77,15 @@ impl Client {
|
|||||||
Ok(resp.bytes_stream())
|
Ok(resp.bytes_stream())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn upload_config(&self, config: &Config) -> Result<()> {
|
||||||
|
do_req(self.req(Method::POST, "configs")?.json(config), &self.token).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_config(&self) -> Result<Config> {
|
||||||
|
self.get_json("config").await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn get_json<T: serde::de::DeserializeOwned>(&self, path: impl Display) -> Result<T> {
|
pub async fn get_json<T: serde::de::DeserializeOwned>(&self, path: impl Display) -> Result<T> {
|
||||||
self.req_json(self.get(&path)?).await
|
self.req_json(self.get(&path)?).await
|
||||||
}
|
}
|
||||||
@@ -133,6 +154,18 @@ impl<'t> Cluster<'t> {
|
|||||||
Ok(resp.bytes().await.map_err(Error::Read)?.to_vec())
|
Ok(resp.bytes().await.map_err(Error::Read)?.to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn set_ca_cert(&self, ca: &str, cert: Vec<u8>) -> Result<()> {
|
||||||
|
let req = self.dls.req(
|
||||||
|
Method::PUT,
|
||||||
|
format!("clusters/{}/CAs/{ca}/certificate", self.name),
|
||||||
|
)?;
|
||||||
|
let req = req
|
||||||
|
.body(cert)
|
||||||
|
.header("Content-Type", "application/x-x509-ca-cert");
|
||||||
|
do_req(req, &self.dls.token).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn kube_sign(&self, sign_req: &KubeSignReq) -> Result<Vec<u8>> {
|
pub async fn kube_sign(&self, sign_req: &KubeSignReq) -> Result<Vec<u8>> {
|
||||||
let req = (self.dls).req(Method::POST, format!("clusters/{}/kube/sign", self.name))?;
|
let req = (self.dls).req(Method::POST, format!("clusters/{}/kube/sign", self.name))?;
|
||||||
let req = req.json(sign_req);
|
let req = req.json(sign_req);
|
||||||
@@ -154,7 +187,7 @@ impl<'t> Host<'t> {
|
|||||||
pub async fn asset(
|
pub async fn asset(
|
||||||
&self,
|
&self,
|
||||||
asset_name: &str,
|
asset_name: &str,
|
||||||
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>> {
|
) -> Result<impl Stream<Item = reqwest::Result<Bytes>> + use<>> {
|
||||||
let req = self.dls.get(format!("hosts/{}/{asset_name}", self.name))?;
|
let req = self.dls.get(format!("hosts/{}/{asset_name}", self.name))?;
|
||||||
let resp = do_req(req, &self.dls.token).await?;
|
let resp = do_req(req, &self.dls.token).await?;
|
||||||
Ok(resp.bytes_stream())
|
Ok(resp.bytes_stream())
|
||||||
@@ -162,17 +195,23 @@ impl<'t> Host<'t> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(rename_all = "PascalCase")]
|
#[serde(default, rename_all = "PascalCase")]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[serde(default, deserialize_with = "deserialize_null_as_default")]
|
#[serde(deserialize_with = "deserialize_null_as_default", alias = "clusters")]
|
||||||
pub clusters: Vec<ClusterConfig>,
|
pub clusters: Vec<ClusterConfig>,
|
||||||
#[serde(default, deserialize_with = "deserialize_null_as_default")]
|
#[serde(deserialize_with = "deserialize_null_as_default", alias = "hosts")]
|
||||||
pub hosts: Vec<HostConfig>,
|
pub hosts: Vec<HostConfig>,
|
||||||
#[serde(default, deserialize_with = "deserialize_null_as_default")]
|
#[serde(
|
||||||
|
deserialize_with = "deserialize_null_as_default",
|
||||||
|
alias = "hosttemplates"
|
||||||
|
)]
|
||||||
pub host_templates: Vec<HostConfig>,
|
pub host_templates: Vec<HostConfig>,
|
||||||
#[serde(default, rename = "SSLConfig")]
|
#[serde(default, rename = "SSLConfig", alias = "sslconfig")]
|
||||||
pub ssl_config: String,
|
pub ssl_config: String,
|
||||||
#[serde(default, deserialize_with = "deserialize_null_as_default")]
|
#[serde(
|
||||||
|
deserialize_with = "deserialize_null_as_default",
|
||||||
|
alias = "extracacerts"
|
||||||
|
)]
|
||||||
pub extra_ca_certs: Map<String, String>,
|
pub extra_ca_certs: Map<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,40 +229,58 @@ where
|
|||||||
#[derive(serde::Deserialize, serde::Serialize)]
|
#[derive(serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(rename_all = "PascalCase")]
|
#[serde(rename_all = "PascalCase")]
|
||||||
pub struct ClusterConfig {
|
pub struct ClusterConfig {
|
||||||
|
#[serde(alias = "name")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
#[serde(alias = "bootstrappods")]
|
||||||
pub bootstrap_pods: String,
|
pub bootstrap_pods: String,
|
||||||
|
#[serde(alias = "addons")]
|
||||||
pub addons: String,
|
pub addons: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Default, serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(rename_all = "PascalCase")]
|
#[serde(rename_all = "PascalCase")]
|
||||||
pub struct HostConfig {
|
pub struct HostConfig {
|
||||||
|
#[serde(alias = "name")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
#[serde(
|
||||||
|
default,
|
||||||
|
skip_serializing_if = "Option::is_none",
|
||||||
|
alias = "cluster_name",
|
||||||
|
alias = "clustername"
|
||||||
|
)]
|
||||||
pub cluster_name: Option<String>,
|
pub cluster_name: Option<String>,
|
||||||
|
|
||||||
#[serde(rename = "IPs")]
|
#[serde(rename = "IPs", alias = "ips")]
|
||||||
pub ips: Vec<IpAddr>,
|
pub ips: Vec<IpAddr>,
|
||||||
|
|
||||||
#[serde(default, skip_serializing_if = "Map::is_empty")]
|
#[serde(default, skip_serializing_if = "Map::is_empty", alias = "labels")]
|
||||||
pub labels: Map<String, String>,
|
pub labels: Map<String, String>,
|
||||||
#[serde(default, skip_serializing_if = "Map::is_empty")]
|
#[serde(default, skip_serializing_if = "Map::is_empty", alias = "annotations")]
|
||||||
pub annotations: Map<String, String>,
|
pub annotations: Map<String, String>,
|
||||||
|
|
||||||
#[serde(rename = "IPXE", skip_serializing_if = "Option::is_none")]
|
#[serde(
|
||||||
|
rename = "IPXE",
|
||||||
|
skip_serializing_if = "Option::is_none",
|
||||||
|
alias = "ipxe"
|
||||||
|
)]
|
||||||
pub ipxe: Option<String>,
|
pub ipxe: Option<String>,
|
||||||
|
|
||||||
|
#[serde(alias = "initrd")]
|
||||||
pub initrd: String,
|
pub initrd: String,
|
||||||
|
#[serde(alias = "kernel")]
|
||||||
pub kernel: String,
|
pub kernel: String,
|
||||||
|
#[serde(alias = "versions")]
|
||||||
pub versions: Map<String, String>,
|
pub versions: Map<String, String>,
|
||||||
|
|
||||||
/// initrd config template
|
/// initrd config template
|
||||||
|
#[serde(alias = "bootstrapconfig")]
|
||||||
pub bootstrap_config: String,
|
pub bootstrap_config: String,
|
||||||
/// files to add to the final initrd config, with rendering
|
/// files to add to the final initrd config, with rendering
|
||||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
#[serde(default, skip_serializing_if = "Vec::is_empty", alias = "initrdfiles")]
|
||||||
pub initrd_files: Vec<crate::File>,
|
pub initrd_files: Vec<crate::File>,
|
||||||
|
|
||||||
/// system config template
|
/// system config template
|
||||||
|
#[serde(alias = "config")]
|
||||||
pub config: String,
|
pub config: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+192
-4
@@ -1,4 +1,13 @@
|
|||||||
pub fn hash_password(salt: &[u8], passphrase: &str) -> argon2::Result<[u8; 32]> {
|
use openssl::symm::Mode;
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use tokio::{fs, io::AsyncWriteExt};
|
||||||
|
|
||||||
|
pub type Salt = [u8; 16];
|
||||||
|
pub type Key = [u8; 32];
|
||||||
|
pub type Hash = Vec<u8>;
|
||||||
|
|
||||||
|
pub fn hash_password(salt: &[u8], passphrase: &str) -> Result<Key> {
|
||||||
let hash = argon2::hash_raw(
|
let hash = argon2::hash_raw(
|
||||||
passphrase.as_bytes(),
|
passphrase.as_bytes(),
|
||||||
salt,
|
salt,
|
||||||
@@ -6,12 +15,191 @@ pub fn hash_password(salt: &[u8], passphrase: &str) -> argon2::Result<[u8; 32]>
|
|||||||
variant: argon2::Variant::Argon2id,
|
variant: argon2::Variant::Argon2id,
|
||||||
hash_length: 32,
|
hash_length: 32,
|
||||||
time_cost: 1,
|
time_cost: 1,
|
||||||
mem_cost: 65536,
|
mem_cost: 64 << 10,
|
||||||
thread_mode: argon2::ThreadMode::Parallel,
|
thread_mode: argon2::ThreadMode::Parallel,
|
||||||
lanes: 4,
|
lanes: 4,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
)?;
|
)
|
||||||
|
.map_err(Error::Hash)?;
|
||||||
|
|
||||||
unsafe { Ok(hash.try_into().unwrap_unchecked()) }
|
Ok(hash.try_into().unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Store {
|
||||||
|
path: PathBuf,
|
||||||
|
key: Option<Key>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Store {
|
||||||
|
pub fn new(path: impl Into<PathBuf>) -> Self {
|
||||||
|
Self {
|
||||||
|
path: path.into(),
|
||||||
|
key: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn unlock(&mut self, passphrase: &str) -> Result<()> {
|
||||||
|
let keys = self.read_keys().await?;
|
||||||
|
|
||||||
|
let salt = keys.salt;
|
||||||
|
|
||||||
|
let user_key = hash_password(&salt, passphrase)?;
|
||||||
|
let user_hash = openssl::sha::sha512(&user_key);
|
||||||
|
|
||||||
|
for nk in keys.keys {
|
||||||
|
if nk.hash != user_hash {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// build iv+data from salt
|
||||||
|
let mut enc_data = Vec::with_capacity(salt.len() + nk.enc_key.len());
|
||||||
|
enc_data.extend_from_slice(&salt);
|
||||||
|
enc_data.extend_from_slice(&nk.enc_key);
|
||||||
|
|
||||||
|
let key = crypt(&enc_data, &user_key, Mode::Decrypt)?;
|
||||||
|
let key = key.try_into().map_err(|_| Error::InvalidKey)?;
|
||||||
|
|
||||||
|
self.key = Some(key);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Error::KeyNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read(&self, path: impl AsRef<Path>) -> Result<Vec<u8>> {
|
||||||
|
let enc_data = self.read_file(path.as_ref().with_extension("data")).await?;
|
||||||
|
self.decrypt(&enc_data)
|
||||||
|
}
|
||||||
|
pub async fn read_to_string(&self, path: impl AsRef<Path>) -> Result<String> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
String::from_utf8(self.read(path).await?).map_err(|_| Error::InvalidUtf8(path.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn write(&self, path: impl AsRef<Path>, data: &[u8]) -> Result<()> {
|
||||||
|
let enc_data = self.encrypt(data)?;
|
||||||
|
safe_write(self.path.join(&path).with_extension("data"), &enc_data).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_keys(&self) -> Result<Keys<'_>> {
|
||||||
|
let keys = self.read_file(".keys").await?;
|
||||||
|
let Some(keys) = keys.strip_prefix(b"{json}") else {
|
||||||
|
return Err(Error::InvalidKeys);
|
||||||
|
};
|
||||||
|
|
||||||
|
serde_json::from_slice(keys).map_err(Error::KeysParse)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_file(&self, subpath: impl AsRef<Path>) -> Result<Vec<u8>> {
|
||||||
|
let path = self.path.join(subpath);
|
||||||
|
fs::read(&path).await.map_err(|e| Error::Read(path, e))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn encrypt(&self, src: &[u8]) -> Result<Vec<u8>> {
|
||||||
|
self.crypt(src, Mode::Encrypt)
|
||||||
|
}
|
||||||
|
fn decrypt(&self, src: &[u8]) -> Result<Vec<u8>> {
|
||||||
|
self.crypt(src, Mode::Decrypt)
|
||||||
|
}
|
||||||
|
fn crypt(&self, src: &[u8], mode: Mode) -> Result<Vec<u8>> {
|
||||||
|
let key = self.key.as_ref().ok_or(Error::NotUnlocked)?;
|
||||||
|
crypt(src, key, mode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn safe_write(path: impl AsRef<Path>, contents: &[u8]) -> Result<()> {
|
||||||
|
let path = path.as_ref();
|
||||||
|
let tmp = path.with_added_extension("new");
|
||||||
|
|
||||||
|
let tmp_err = |e| Error::Write(tmp.clone(), e);
|
||||||
|
|
||||||
|
let mut file = fs::OpenOptions::new()
|
||||||
|
.create(true)
|
||||||
|
.truncate(true)
|
||||||
|
.write(true)
|
||||||
|
.open(&tmp)
|
||||||
|
.await
|
||||||
|
.map_err(tmp_err)?;
|
||||||
|
|
||||||
|
file.write_all(contents).await.map_err(tmp_err)?;
|
||||||
|
file.sync_all().await.map_err(tmp_err)?;
|
||||||
|
file.shutdown().await.map_err(tmp_err)?;
|
||||||
|
|
||||||
|
fs::rename(tmp, &path)
|
||||||
|
.await
|
||||||
|
.map_err(|e| Error::Write(path.into(), e))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crypt(mut src: &[u8], key: &Key, mode: Mode) -> Result<Vec<u8>> {
|
||||||
|
use openssl::symm::{Cipher, Crypter};
|
||||||
|
|
||||||
|
let mut iv = [0u8; 16];
|
||||||
|
let mut dst: Vec<u8>;
|
||||||
|
let crypt_dst: &mut [u8];
|
||||||
|
|
||||||
|
match mode {
|
||||||
|
Mode::Encrypt => {
|
||||||
|
getrandom::fill(&mut iv).unwrap();
|
||||||
|
dst = vec![0u8; iv.len() + src.len()];
|
||||||
|
dst[..iv.len()].copy_from_slice(&iv);
|
||||||
|
crypt_dst = &mut dst[iv.len()..];
|
||||||
|
}
|
||||||
|
Mode::Decrypt => {
|
||||||
|
iv = src[..iv.len()]
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| Error::DecryptInputToSmall)?;
|
||||||
|
src = &src[iv.len()..];
|
||||||
|
dst = vec![0u8; src.len()];
|
||||||
|
crypt_dst = &mut dst;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cipher = Cipher::aes_256_cfb128();
|
||||||
|
Crypter::new(cipher, mode, key, Some(&iv))
|
||||||
|
.expect("Failed to init AES")
|
||||||
|
.update(src, crypt_dst)
|
||||||
|
.expect("AES CFB encrypt/decrypt failed");
|
||||||
|
|
||||||
|
Ok(dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error("hash error: {0}")]
|
||||||
|
Hash(argon2::Error),
|
||||||
|
#[error("read {0} failed: {1}")]
|
||||||
|
Read(PathBuf, std::io::Error),
|
||||||
|
#[error("write {0} failed: {1}")]
|
||||||
|
Write(PathBuf, std::io::Error),
|
||||||
|
#[error("read {0} failed: invalid UTF-8")]
|
||||||
|
InvalidUtf8(PathBuf),
|
||||||
|
#[error("invalid keys data")]
|
||||||
|
InvalidKeys,
|
||||||
|
#[error("invalid key")]
|
||||||
|
InvalidKey,
|
||||||
|
#[error("keys parse error: {0}")]
|
||||||
|
KeysParse(serde_json::Error),
|
||||||
|
#[error("key not found")]
|
||||||
|
KeyNotFound,
|
||||||
|
#[error("store not unlocked")]
|
||||||
|
NotUnlocked,
|
||||||
|
#[error("decrypt input too small")]
|
||||||
|
DecryptInputToSmall,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[serde(rename_all = "PascalCase")]
|
||||||
|
struct Keys<'t> {
|
||||||
|
salt: Salt,
|
||||||
|
keys: Vec<NamedKey<'t>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||||
|
#[serde(rename_all = "PascalCase")]
|
||||||
|
struct NamedKey<'t> {
|
||||||
|
name: Cow<'t, str>,
|
||||||
|
hash: Hash,
|
||||||
|
enc_key: Key,
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -60,7 +60,7 @@ impl<'t> Dynlay<'t> {
|
|||||||
|
|
||||||
// mount layer
|
// mount layer
|
||||||
info!("mounting layer");
|
info!("mounting layer");
|
||||||
sudo("mount", &["-t", "squashfs", lay_path, &mount_path_str]).await?;
|
sudo("mount", &[lay_path, &mount_path_str]).await?;
|
||||||
|
|
||||||
let mut paths = spawn_walk_dir(mount_path.clone());
|
let mut paths = spawn_walk_dir(mount_path.clone());
|
||||||
while let Some(result) = paths.recv().await {
|
while let Some(result) = paths.recv().await {
|
||||||
|
|||||||
+97
-12
@@ -1,3 +1,5 @@
|
|||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
pub mod apply;
|
pub mod apply;
|
||||||
pub mod bootstrap;
|
pub mod bootstrap;
|
||||||
pub mod cgroup;
|
pub mod cgroup;
|
||||||
@@ -9,7 +11,7 @@ pub mod logger;
|
|||||||
pub mod proxy;
|
pub mod proxy;
|
||||||
pub mod rc;
|
pub mod rc;
|
||||||
|
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub layers: Vec<String>,
|
pub layers: Vec<String>,
|
||||||
pub root_user: RootUser,
|
pub root_user: RootUser,
|
||||||
@@ -23,14 +25,14 @@ pub struct Config {
|
|||||||
pub users: Vec<User>,
|
pub users: Vec<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct RootUser {
|
pub struct RootUser {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub password_hash: Option<String>,
|
pub password_hash: Option<String>,
|
||||||
pub authorized_keys: Vec<String>,
|
pub authorized_keys: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Default, Clone, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct Mount {
|
pub struct Mount {
|
||||||
pub dev: String,
|
pub dev: String,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
@@ -40,14 +42,14 @@ pub struct Mount {
|
|||||||
pub options: Option<String>,
|
pub options: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct Group {
|
pub struct Group {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub gid: Option<u32>,
|
pub gid: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
@@ -56,22 +58,45 @@ pub struct User {
|
|||||||
pub gid: Option<u32>,
|
pub gid: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct File {
|
pub struct File {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub mode: Option<u32>,
|
pub mode: Option<u32>,
|
||||||
#[serde(flatten)]
|
pub kind: Option<FileKind>,
|
||||||
pub kind: FileKind,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub content: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub content64: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub symlink: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub parts: Option<Vec<FilePart>>,
|
||||||
|
#[serde(default, skip_serializing_if = "is_false")]
|
||||||
|
pub dir: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
fn is_false(b: &bool) -> bool {
|
||||||
|
!b
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
||||||
#[serde(rename_all = "lowercase")]
|
#[serde(rename_all = "lowercase")]
|
||||||
pub enum FileKind {
|
pub enum FileKind {
|
||||||
|
#[default]
|
||||||
|
Skip,
|
||||||
Content(String),
|
Content(String),
|
||||||
Content64(String),
|
Content64(String),
|
||||||
|
Parts(Vec<FilePart>),
|
||||||
Symlink(String),
|
Symlink(String),
|
||||||
Dir(bool),
|
Dir,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum FilePart {
|
||||||
|
Content(String),
|
||||||
|
Content64(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
@@ -86,11 +111,71 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn base64_decode(s: &str) -> Result<Vec<u8>, base64::DecodeError> {
|
pub fn base64_decode(s: &str) -> Result<Vec<u8>, base64::DecodeError> {
|
||||||
use base64::{Engine as _, prelude::BASE64_STANDARD_NO_PAD as B64};
|
use base64::{prelude::BASE64_STANDARD_NO_PAD as B64, Engine as _};
|
||||||
B64.decode(s.trim_end_matches('='))
|
B64.decode(s.trim_end_matches('='))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn base64_encode(b: &[u8]) -> String {
|
pub fn base64_encode(b: &[u8]) -> String {
|
||||||
use base64::{Engine as _, prelude::BASE64_STANDARD as B64};
|
use base64::{prelude::BASE64_STANDARD as B64, Engine as _};
|
||||||
B64.encode(b)
|
B64.encode(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'t> File {
|
||||||
|
pub fn kind(&'t self) -> Cow<'t, FileKind> {
|
||||||
|
self.kind.as_ref().map(Cow::Borrowed).unwrap_or_else(|| {
|
||||||
|
use FileKind::*;
|
||||||
|
Cow::Owned(if let Some(ref s) = self.content {
|
||||||
|
Content(s.clone())
|
||||||
|
} else if let Some(ref s) = self.content64 {
|
||||||
|
Content64(s.clone())
|
||||||
|
} else if let Some(ref s) = self.symlink {
|
||||||
|
Symlink(s.clone())
|
||||||
|
} else if let Some(ref p) = self.parts {
|
||||||
|
Parts(p.clone())
|
||||||
|
} else if self.dir {
|
||||||
|
Dir
|
||||||
|
} else {
|
||||||
|
Skip
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_symlink(&self) -> bool {
|
||||||
|
if let Some(ref kind) = self.kind {
|
||||||
|
matches!(kind, FileKind::Symlink(_))
|
||||||
|
} else {
|
||||||
|
self.symlink.is_some()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileKind {
|
||||||
|
pub fn content<'t>(&'t self) -> Result<Option<Cow<'t, [u8]>>, base64::DecodeError> {
|
||||||
|
use FileKind::*;
|
||||||
|
Ok(match self {
|
||||||
|
Content(content) => Some(Cow::Borrowed(content.as_bytes())),
|
||||||
|
Content64(content) => {
|
||||||
|
let content = base64_decode(content)?;
|
||||||
|
Some(Cow::Owned(content))
|
||||||
|
}
|
||||||
|
Parts(parts) => {
|
||||||
|
let mut assembly = Vec::new();
|
||||||
|
for part in parts {
|
||||||
|
assembly.extend(part.content()?.into_iter());
|
||||||
|
}
|
||||||
|
Some(Cow::Owned(assembly))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FilePart {
|
||||||
|
pub fn content(&self) -> Result<Cow<'_, [u8]>, base64::DecodeError> {
|
||||||
|
use FilePart::*;
|
||||||
|
Ok(match self {
|
||||||
|
Content(content) => Cow::Borrowed(content.as_bytes()),
|
||||||
|
Content64(content) => Cow::Owned(base64_decode(content)?),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+91
-53
@@ -1,15 +1,16 @@
|
|||||||
use async_compression::tokio::write::{ZstdDecoder, ZstdEncoder};
|
use async_compression::tokio::write::{ZstdDecoder, ZstdEncoder};
|
||||||
use chrono::{DurationRound, TimeDelta, Utc};
|
use chrono::{DurationRound, TimeDelta, Utc};
|
||||||
use eyre::{format_err, Result};
|
use eyre::{Result, format_err};
|
||||||
use log::{debug, error, warn};
|
use log::{debug, error, warn};
|
||||||
|
use std::ffi::OsStr;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter},
|
io::{self, AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader, BufWriter},
|
||||||
process,
|
process::{Child, Command},
|
||||||
sync::mpsc,
|
sync::mpsc,
|
||||||
time::{sleep, Duration},
|
time::{Duration, sleep},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{cgroup, fs};
|
use crate::{cgroup, fs};
|
||||||
@@ -22,67 +23,92 @@ const TRUNC_DELTA: TimeDelta = TimeDelta::hours(1);
|
|||||||
const FLUSH_INTERVAL: Duration = Duration::from_secs(1);
|
const FLUSH_INTERVAL: Duration = Duration::from_secs(1);
|
||||||
const WRITE_RETRY_DELAY: Duration = Duration::from_secs(1);
|
const WRITE_RETRY_DELAY: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
pub struct Logger<'t> {
|
pub struct Logger {
|
||||||
pub log_path: &'t str,
|
pub log_path: PathBuf,
|
||||||
pub log_name: &'t str,
|
pub log_name: PathBuf,
|
||||||
pub with_prefix: bool,
|
pub with_prefix: bool,
|
||||||
|
pub cgroup: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> Logger<'t> {
|
impl Logger {
|
||||||
pub async fn run(&self, cgroup: Option<String>, command: &str, args: &[String]) -> Result<()> {
|
pub async fn setup<I, S>(&self, command: impl AsRef<OsStr>, args: I) -> fs::Result<Command>
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = S>,
|
||||||
|
S: AsRef<OsStr>,
|
||||||
|
{
|
||||||
// make sure we can at least open the log before starting the command
|
// make sure we can at least open the log before starting the command
|
||||||
let archives_path = &format!("{path}/archives", path = self.log_path);
|
let archives_path = &self.log_path.join("archives");
|
||||||
(fs::create_dir_all(archives_path).await)
|
fs::create_dir_all(archives_path).await?;
|
||||||
.map_err(|e| format_err!("failed to create archives dir: {e}"))?;
|
|
||||||
let archives_read_dir = (fs::read_dir(archives_path).await)
|
|
||||||
.map_err(|e| format_err!("failed to list archives: {e}"))?;
|
|
||||||
|
|
||||||
let mut prev_stamp = trunc_ts(Utc::now());
|
let archives_read_dir = fs::read_dir(archives_path).await?;
|
||||||
let mut current_log = BufWriter::new(self.open_log(prev_stamp).await?);
|
|
||||||
|
let prev_stamp = trunc_ts(Utc::now());
|
||||||
|
|
||||||
tokio::spawn(compress_archives(
|
tokio::spawn(compress_archives(
|
||||||
archives_read_dir,
|
archives_read_dir,
|
||||||
self.log_name.to_string(),
|
self.log_name.clone(),
|
||||||
prev_stamp.format(TS_FORMAT).to_string(),
|
prev_stamp.format(TS_FORMAT).to_string(),
|
||||||
));
|
));
|
||||||
|
|
||||||
// start the command
|
// create the command
|
||||||
let mut cmd = process::Command::new(command);
|
let mut cmd = Command::new(command);
|
||||||
cmd.args(args).stdout(Stdio::piped()).stderr(Stdio::piped());
|
|
||||||
if let Some(cgroup) = cgroup.as_deref() {
|
|
||||||
let mut cg_path = PathBuf::from(cgroup::ROOT);
|
|
||||||
cg_path.push(cgroup);
|
|
||||||
cg_path.push(self.log_name);
|
|
||||||
|
|
||||||
use std::io::ErrorKind as K;
|
cmd.args(args);
|
||||||
match tokio::fs::create_dir(&cg_path).await {
|
|
||||||
Ok(_) => debug!("created dir {}", cg_path.display()),
|
if let Some(cgroup) = self.cgroup.as_deref() {
|
||||||
Err(e) if e.kind() == K::AlreadyExists => {
|
let cg_path = PathBuf::from(cgroup::ROOT)
|
||||||
debug!("existing dir {}", cg_path.display())
|
.join(cgroup)
|
||||||
}
|
.join(&self.log_name);
|
||||||
Err(e) => return Err(fs::Error::CreateDir(cg_path, e).into()),
|
|
||||||
}
|
fs::create_dir_all(&cg_path).await?;
|
||||||
|
|
||||||
let procs_file = cg_path.join("cgroup.procs");
|
let procs_file = cg_path.join("cgroup.procs");
|
||||||
debug!("procs file {}", procs_file.display());
|
debug!("procs file {}", procs_file.display());
|
||||||
fs::write(&procs_file, b"0").await?;
|
|
||||||
|
unsafe { cmd.pre_exec(move || std::fs::write(&procs_file, b"0")) };
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut child = cmd.spawn().map_err(|e| format_err!("exec failed: {e}"))?;
|
Ok(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
let (tx, mut rx) = mpsc::channel(8);
|
pub fn spawn(self, mut cmd: Command) -> std::io::Result<Child> {
|
||||||
|
// setup outputs for capture
|
||||||
|
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
|
||||||
|
|
||||||
|
// spawn
|
||||||
|
let mut child = cmd.spawn()?;
|
||||||
|
|
||||||
|
// capture outputs
|
||||||
|
let (tx, rx) = mpsc::channel(8);
|
||||||
|
|
||||||
tokio::spawn(copy("stdout", child.stdout.take().unwrap(), tx.clone()));
|
tokio::spawn(copy("stdout", child.stdout.take().unwrap(), tx.clone()));
|
||||||
tokio::spawn(copy("stderr", child.stderr.take().unwrap(), tx));
|
tokio::spawn(copy("stderr", child.stderr.take().unwrap(), tx));
|
||||||
|
|
||||||
|
// log outputs
|
||||||
|
tokio::spawn(self.log_stream(rx));
|
||||||
|
|
||||||
|
Ok(child)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Result<!> when stable
|
||||||
|
pub async fn exec(self, cmd: Command) -> Result<()> {
|
||||||
|
let mut child = self.spawn(cmd)?;
|
||||||
|
|
||||||
// forward signals
|
// forward signals
|
||||||
if let Some(child_pid) = child.id() {
|
if let Some(child_pid) = child.id() {
|
||||||
forward_signals_to(child_pid as i32);
|
forward_signals_to(child_pid as i32);
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle output
|
let status = child.wait().await?;
|
||||||
|
std::process::exit(status.code().unwrap_or(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn log_stream(self, mut rx: mpsc::Receiver<LogItem>) {
|
||||||
let mut flush_ticker = tokio::time::interval(FLUSH_INTERVAL);
|
let mut flush_ticker = tokio::time::interval(FLUSH_INTERVAL);
|
||||||
|
|
||||||
|
let mut prev_stamp = trunc_ts(Utc::now());
|
||||||
|
let mut current_log = BufWriter::new(self.eventually_open_log(prev_stamp).await);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
tokio::select!(
|
tokio::select!(
|
||||||
r = rx.recv() => {
|
r = rx.recv() => {
|
||||||
@@ -102,15 +128,11 @@ impl<'t> Logger<'t> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let status = child.wait().await?;
|
|
||||||
|
|
||||||
// finalize
|
// finalize
|
||||||
while let Err(e) = current_log.flush().await {
|
while let Err(e) = current_log.shutdown().await {
|
||||||
error!("final log flush failed: {e}");
|
error!("final log shutdown failed: {e}");
|
||||||
sleep(WRITE_RETRY_DELAY).await;
|
sleep(WRITE_RETRY_DELAY).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::process::exit(status.code().unwrap_or(-1));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn log_item(
|
async fn log_item(
|
||||||
@@ -143,6 +165,18 @@ impl<'t> Logger<'t> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn eventually_open_log(&self, ts: Timestamp) -> File {
|
||||||
|
loop {
|
||||||
|
match self.open_log(ts).await {
|
||||||
|
Ok(log) => break log,
|
||||||
|
Err(e) => {
|
||||||
|
error!("open log failed: {e}");
|
||||||
|
sleep(WRITE_RETRY_DELAY).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn open_log(&self, ts: Timestamp) -> Result<File> {
|
async fn open_log(&self, ts: Timestamp) -> Result<File> {
|
||||||
let log_file = &self.archive_path(ts);
|
let log_file = &self.archive_path(ts);
|
||||||
|
|
||||||
@@ -153,8 +187,9 @@ impl<'t> Logger<'t> {
|
|||||||
.open(log_file)
|
.open(log_file)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let link_src = &PathBuf::from(self.log_path)
|
let link_src = &self
|
||||||
.join(self.log_name)
|
.log_path
|
||||||
|
.join(&self.log_name)
|
||||||
.with_added_extension("log");
|
.with_added_extension("log");
|
||||||
let link_tgt = &self.archive_rel_path(ts);
|
let link_tgt = &self.archive_rel_path(ts);
|
||||||
|
|
||||||
@@ -171,23 +206,21 @@ impl<'t> Logger<'t> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn archive_path(&self, ts: Timestamp) -> PathBuf {
|
fn archive_path(&self, ts: Timestamp) -> PathBuf {
|
||||||
PathBuf::from(self.log_path).join(self.archive_rel_path(ts))
|
self.log_path.join(self.archive_rel_path(ts))
|
||||||
}
|
}
|
||||||
fn archive_rel_path(&self, ts: Timestamp) -> PathBuf {
|
fn archive_rel_path(&self, ts: Timestamp) -> PathBuf {
|
||||||
PathBuf::from("archives").join(self.archive_file(ts))
|
PathBuf::from("archives").join(self.archive_file(ts))
|
||||||
}
|
}
|
||||||
fn archive_file(&self, ts: Timestamp) -> String {
|
fn archive_file(&self, ts: Timestamp) -> PathBuf {
|
||||||
format!(
|
self.log_name
|
||||||
"{name}.{ts}.log",
|
.with_added_extension(ts.format(TS_FORMAT).to_string())
|
||||||
name = self.log_name,
|
.with_added_extension("log")
|
||||||
ts = ts.format(TS_FORMAT),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn forward_signals_to(pid: i32) {
|
fn forward_signals_to(pid: i32) {
|
||||||
use nix::{
|
use nix::{
|
||||||
sys::signal::{kill, Signal},
|
sys::signal::{Signal, kill},
|
||||||
unistd::Pid,
|
unistd::Pid,
|
||||||
};
|
};
|
||||||
use signal_hook::{consts::*, low_level::register};
|
use signal_hook::{consts::*, low_level::register};
|
||||||
@@ -266,7 +299,7 @@ async fn copy(stream_name: &'static str, out: impl AsyncRead + Unpin, tx: mpsc::
|
|||||||
line.extend_from_slice(buf);
|
line.extend_from_slice(buf);
|
||||||
out.consume(len);
|
out.consume(len);
|
||||||
send_line!();
|
send_line!();
|
||||||
} else if buf.len() > remaining {
|
} else if buf.len() >= remaining {
|
||||||
line.extend_from_slice(&buf[..remaining]);
|
line.extend_from_slice(&buf[..remaining]);
|
||||||
out.consume(remaining);
|
out.consume(remaining);
|
||||||
send_line!();
|
send_line!();
|
||||||
@@ -287,7 +320,12 @@ pub fn trunc_ts(ts: Timestamp) -> Timestamp {
|
|||||||
.expect("duration_trunc failed")
|
.expect("duration_trunc failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn compress_archives(mut read_dir: fs::ReadDir, log_name: String, exclude_ts: String) {
|
async fn compress_archives(
|
||||||
|
mut read_dir: fs::ReadDir,
|
||||||
|
log_name: impl AsRef<Path>,
|
||||||
|
exclude_ts: String,
|
||||||
|
) {
|
||||||
|
let log_name = log_name.as_ref();
|
||||||
loop {
|
loop {
|
||||||
let Ok(Some(entry)) =
|
let Ok(Some(entry)) =
|
||||||
(read_dir.next_entry().await).inspect_err(|e| error!("archive dir read failed: {e}"))
|
(read_dir.next_entry().await).inspect_err(|e| error!("archive dir read failed: {e}"))
|
||||||
|
|||||||
@@ -1 +1,495 @@
|
|||||||
|
use eyre::format_err;
|
||||||
|
use log::{error, info, warn};
|
||||||
|
use nix::sys::signal::Signal;
|
||||||
|
use std::collections::{BTreeMap as Map, BTreeSet as Set};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
use tokio::{
|
||||||
|
io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, copy},
|
||||||
|
net::{UnixListener, UnixStream},
|
||||||
|
sync::{RwLock, mpsc, watch},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{cgroup, fs};
|
||||||
|
|
||||||
|
mod runner;
|
||||||
|
|
||||||
|
use runner::{Child, State};
|
||||||
|
|
||||||
|
const CFG_PATH: &str = "/etc/direktil/rc.yaml";
|
||||||
|
const SOCK_PATH: &str = "/run/dkl-rc/ctl.sock"; // Path::new when stable
|
||||||
|
|
||||||
|
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
||||||
|
pub struct Config {
|
||||||
|
#[serde(default, skip_serializing_if = "Map::is_empty")]
|
||||||
|
pub cgroups: Map<String, CgroupConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, serde::Deserialize)]
|
||||||
|
pub struct CgroupConfig {
|
||||||
|
pub controllers: String,
|
||||||
|
#[serde(default, skip_serializing_if = "Map::is_empty")]
|
||||||
|
pub settings: Map<String, String>,
|
||||||
|
#[serde(default, skip_serializing_if = "Map::is_empty")]
|
||||||
|
pub services: Map<String, Service>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Service = Vec<String>;
|
||||||
|
|
||||||
|
static MANAGER: LazyLock<RwLock<Manager>> = LazyLock::new(|| RwLock::new(Manager::default()));
|
||||||
|
|
||||||
|
type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
enum Error {
|
||||||
|
#[error("invalid command: {0:?}")]
|
||||||
|
InvalidCommand(String),
|
||||||
|
#[error("config read failed: {0}")]
|
||||||
|
ConfigRead(fs::Error),
|
||||||
|
#[error("config parse failed: {0}")]
|
||||||
|
ConfigParse(serde_yaml::Error),
|
||||||
|
#[error("cgroup setup failed: {0}")]
|
||||||
|
CgroupSetup(fs::Error),
|
||||||
|
#[error("invalid key (cgroup/service)")]
|
||||||
|
InvalidKey,
|
||||||
|
#[error("unknown cgroup: {0:?}")]
|
||||||
|
UnknownCgroup(String),
|
||||||
|
#[error("unknown service: {0:?}")]
|
||||||
|
UnknownService(String),
|
||||||
|
#[error("invalid signal: {0:?}")]
|
||||||
|
InvalidSignal(String),
|
||||||
|
#[error("process exited")]
|
||||||
|
ProcessExited,
|
||||||
|
#[error("nothing running under {0:?}")]
|
||||||
|
NotRunning(String),
|
||||||
|
#[error("kill failed: {0:?}")]
|
||||||
|
KillFailed(nix::Error),
|
||||||
|
#[error("service runner is dead")]
|
||||||
|
RunnerDead,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run() -> eyre::Result<()> {
|
||||||
|
info!("starting");
|
||||||
|
|
||||||
|
tokio::spawn(wait_terminate());
|
||||||
|
|
||||||
|
let _ = reload_config().await;
|
||||||
|
tokio::spawn(wait_reload());
|
||||||
|
|
||||||
|
if let Some(sock_dir) = PathBuf::from(SOCK_PATH).parent() {
|
||||||
|
let _ = tokio::fs::DirBuilder::new()
|
||||||
|
.mode(0o700)
|
||||||
|
.create(sock_dir)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = tokio::fs::remove_file(SOCK_PATH).await;
|
||||||
|
let listener = UnixListener::bind(SOCK_PATH)?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let Ok((conn, _)) = listener.accept().await else {
|
||||||
|
warn!("listener closed");
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
tokio::spawn(async move { handle(conn).await });
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup().await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup() {
|
||||||
|
let _ = tokio::fs::remove_file(SOCK_PATH).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn ctl<I, S>(args: I) -> eyre::Result<()>
|
||||||
|
where
|
||||||
|
I: IntoIterator<Item = S>,
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
let args: Vec<_> = args.into_iter().map(|s| s.into()).collect();
|
||||||
|
let args = format!("{}\n", args.join(" "));
|
||||||
|
|
||||||
|
match ctl_exec(args.as_bytes()).await {
|
||||||
|
Ok(mut rd) => {
|
||||||
|
copy(&mut rd, &mut tokio::io::stdout()).await?;
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ctl_exec(request: &[u8]) -> eyre::Result<BufReader<UnixStream>> {
|
||||||
|
let mut conn = UnixStream::connect(SOCK_PATH)
|
||||||
|
.await
|
||||||
|
.map_err(|e| format_err!("{SOCK_PATH}: {e}"))?;
|
||||||
|
|
||||||
|
conn.write_all(request).await?;
|
||||||
|
|
||||||
|
let mut rd = BufReader::with_capacity(64, conn);
|
||||||
|
|
||||||
|
let mut code = String::new();
|
||||||
|
rd.read_line(&mut code).await?;
|
||||||
|
let code: i32 = code.trim_ascii().parse()?;
|
||||||
|
|
||||||
|
if code != 0 {
|
||||||
|
let mut err = String::new();
|
||||||
|
rd.read_to_string(&mut err).await?;
|
||||||
|
return Err(format_err!("{}", err.trim_ascii_end()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(rd)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle(mut conn: UnixStream) {
|
||||||
|
let (rd, mut wr) = conn.split();
|
||||||
|
let mut rd = BufReader::with_capacity(64, rd).lines();
|
||||||
|
|
||||||
|
let Ok(Some(line)) = rd.next_line().await else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut line = line.split_ascii_whitespace();
|
||||||
|
|
||||||
|
macro_rules! next {
|
||||||
|
() => {{
|
||||||
|
match line.next() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => return,
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
let r = match next!() {
|
||||||
|
"ls" => Ok(Some(ls().await)),
|
||||||
|
"status" => Ok(Some(status().await)),
|
||||||
|
"reload-config" => reload_config().await.map(|_| None),
|
||||||
|
"start" => start(next!()).await.map(|_| None),
|
||||||
|
"stop" => stop(next!()).await.map(|_| None),
|
||||||
|
"reload" => reload(next!()).await.map(|_| None),
|
||||||
|
"sig" => sig(next!(), next!()).await.map(|_| None),
|
||||||
|
cmd => Err(Error::InvalidCommand(cmd.into())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = match r {
|
||||||
|
Ok(None) => wr.write_all(b"0\n").await,
|
||||||
|
Ok(Some(s)) => wr.write_all(format!("0\n{s}\n").as_bytes()).await,
|
||||||
|
Err(e) => wr.write_all(format!("1\n{e}\n").as_bytes()).await,
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = wr.shutdown().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_terminate() {
|
||||||
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
let Ok(mut sig) = signal(SignalKind::terminate())
|
||||||
|
.inspect_err(|e| error!("failed to listen to SIGTERM (will be ignored): {e}"))
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
sig.recv().await;
|
||||||
|
|
||||||
|
info!("SIGTERM received, terminating");
|
||||||
|
MANAGER.write().await.terminate().await;
|
||||||
|
|
||||||
|
cleanup().await;
|
||||||
|
log::logger().flush();
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_reload() {
|
||||||
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
let Ok(mut sig) = signal(SignalKind::hangup())
|
||||||
|
.inspect_err(|e| error!("failed to listen to SIGHUP (will be ignored): {e}"))
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
loop {
|
||||||
|
sig.recv().await;
|
||||||
|
let _ = reload_config().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn reload_config() -> Result<()> {
|
||||||
|
let cfg = (fs::read(CFG_PATH).await)
|
||||||
|
.map_err(Error::ConfigRead)
|
||||||
|
.inspect_err(|e| error!("{e}"))?;
|
||||||
|
|
||||||
|
let cfg = serde_yaml::from_slice::<Config>(&cfg)
|
||||||
|
.map_err(Error::ConfigParse)
|
||||||
|
.inspect_err(|e| error!("{CFG_PATH}: {e}"))?;
|
||||||
|
|
||||||
|
info!("applying new config");
|
||||||
|
let r = MANAGER.write().await.apply_config(cfg).await;
|
||||||
|
match &r {
|
||||||
|
Ok(_) => info!("applied new config"),
|
||||||
|
Err(e) => info!("failed to apply new config: {e}"),
|
||||||
|
}
|
||||||
|
r
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ls() -> String {
|
||||||
|
let mut keys = String::new();
|
||||||
|
for (i, k) in MANAGER.read().await.runners.keys().enumerate() {
|
||||||
|
if i != 0 {
|
||||||
|
keys.push('\n');
|
||||||
|
}
|
||||||
|
keys.push_str(k);
|
||||||
|
}
|
||||||
|
keys
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn status() -> String {
|
||||||
|
let status = MANAGER.read().await.status();
|
||||||
|
|
||||||
|
let mut table = tabled::builder::Builder::new();
|
||||||
|
table.push_record(["cgroup", "service", "PID", "state", "msg"]);
|
||||||
|
|
||||||
|
for (cg_svc, child) in status {
|
||||||
|
let (cg, svc) = cg_svc.split_once('/').unwrap();
|
||||||
|
let pid = child.pid.map(|p| p.to_string());
|
||||||
|
table.push_record([
|
||||||
|
cg,
|
||||||
|
svc,
|
||||||
|
pid.as_deref().unwrap_or("◌"),
|
||||||
|
&format!("{:?}", child.state),
|
||||||
|
child.msg.as_deref().unwrap_or("◌"),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
(table.build())
|
||||||
|
.with(tabled::settings::Style::psql())
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start(key: &str) -> Result<()> {
|
||||||
|
MANAGER.write().await.start(key).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stop(key: &str) -> Result<()> {
|
||||||
|
MANAGER.write().await.stop(key).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn reload(key: &str) -> Result<()> {
|
||||||
|
MANAGER.read().await.reload(key).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sig(key: &str, sig: &str) -> Result<()> {
|
||||||
|
let sig: Signal = sig.parse().map_err(|_| Error::InvalidSignal(sig.into()))?;
|
||||||
|
signal(key, sig).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn child_for(key: &str) -> Result<Child> {
|
||||||
|
MANAGER.read().await.child_for(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn signal(key: &str, sig: Signal) -> Result<()> {
|
||||||
|
child_for(key).await?.kill(sig)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn child_key(cg: &str, svc: &str) -> String {
|
||||||
|
[cg, svc].join("/")
|
||||||
|
}
|
||||||
|
fn split_key(key: &str) -> Result<(&str, &str)> {
|
||||||
|
key.split_once('/').ok_or(Error::InvalidKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Manager {
|
||||||
|
cfg: Config,
|
||||||
|
procs: Map<String, watch::Receiver<Child>>,
|
||||||
|
runners: Map<String, mpsc::Sender<runner::Cmd>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Manager {
|
||||||
|
fn status(&self) -> Vec<(String, Child)> {
|
||||||
|
(self.procs.iter())
|
||||||
|
.map(|(n, c)| (n.clone(), c.borrow().clone()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn child_for(&self, key: &str) -> Result<Child> {
|
||||||
|
(self.procs.get(key))
|
||||||
|
.map(|c| c.borrow().clone())
|
||||||
|
.ok_or_else(|| Error::NotRunning(key.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn apply_config(&mut self, new_cfg: Config) -> Result<()> {
|
||||||
|
// create and configure cgroups
|
||||||
|
for (name, cg) in &new_cfg.cgroups {
|
||||||
|
let cg_path = PathBuf::from(cgroup::ROOT).join(name);
|
||||||
|
fs::create_dir_all(&cg_path)
|
||||||
|
.await
|
||||||
|
.map_err(Error::CgroupSetup)?;
|
||||||
|
|
||||||
|
fs::write(
|
||||||
|
cg_path.join("cgroup.subtree_control"),
|
||||||
|
cg.controllers.as_bytes(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(Error::CgroupSetup)?;
|
||||||
|
|
||||||
|
for (setting, value) in &cg.settings {
|
||||||
|
fs::write(cg_path.join(setting), value.as_bytes())
|
||||||
|
.await
|
||||||
|
.map_err(Error::CgroupSetup)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_svcs: Set<_> = new_cfg.service_keys().collect();
|
||||||
|
|
||||||
|
// stop removed services
|
||||||
|
let to_stop = Map::from_iter(self.runners.extract_if(.., |k, _| !new_svcs.contains(k)));
|
||||||
|
let mut stopped = Set::new();
|
||||||
|
for (key, runner_cmd) in to_stop {
|
||||||
|
if runner_cmd.send(runner::Cmd::Stop).await.is_err() {
|
||||||
|
// runner already dead
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
stopped.insert(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
// start added services
|
||||||
|
for (key, cg, svc, service) in new_cfg.services() {
|
||||||
|
if self.runners.contains_key(&key) {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let cmd = self.spawn_runner(key, cg, svc, service.clone());
|
||||||
|
if let Err(e) = cmd.send(runner::Cmd::Start).await {
|
||||||
|
error!("runner instantly died: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait & cleanup stopped
|
||||||
|
for key in stopped {
|
||||||
|
let Some(mut child_rx) = self.procs.remove(&key) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let _ = child_rx
|
||||||
|
.wait_for(|c| matches!(c.state, State::Finalized))
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.cfg = new_cfg;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn terminate(&mut self) {
|
||||||
|
self.runners.clear();
|
||||||
|
|
||||||
|
for child in self.procs.values_mut() {
|
||||||
|
let _ = child
|
||||||
|
.wait_for(|c| matches!(c.state, State::Finalized))
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.procs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runner(&mut self, key: &str) -> Result<mpsc::Sender<runner::Cmd>> {
|
||||||
|
if let Some(c) = self.runners.get(key) {
|
||||||
|
return Ok(c.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let (cg, svc) = split_key(key)?;
|
||||||
|
let service = self.cfg.service(key)?;
|
||||||
|
|
||||||
|
Ok(self.spawn_runner(key.into(), cg, svc, service.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn_runner(
|
||||||
|
&mut self,
|
||||||
|
key: String,
|
||||||
|
cg: &str,
|
||||||
|
svc: &str,
|
||||||
|
service: Service,
|
||||||
|
) -> mpsc::Sender<runner::Cmd> {
|
||||||
|
let (runner, child_rx, cmds_tx) = runner::new(cg, svc, service);
|
||||||
|
|
||||||
|
tokio::spawn(runner.run());
|
||||||
|
|
||||||
|
self.procs.insert(key.clone(), child_rx);
|
||||||
|
self.runners.insert(key, cmds_tx.clone());
|
||||||
|
|
||||||
|
cmds_tx
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cmd(&mut self, key: &str, cmd: runner::Cmd) -> Result<()> {
|
||||||
|
if self.runner(key)?.send(cmd).await.is_err() {
|
||||||
|
// runner died
|
||||||
|
self.runners.remove(key);
|
||||||
|
return Err(Error::RunnerDead);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start(&mut self, key: &str) -> Result<()> {
|
||||||
|
self.cmd(key, runner::Cmd::Start).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stop(&mut self, key: &str) -> Result<()> {
|
||||||
|
self.cmd(key, runner::Cmd::Stop).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn reload(&self, key: &str) -> Result<()> {
|
||||||
|
let proc = (self.procs.get(key)) //
|
||||||
|
.ok_or_else(|| Error::UnknownService(key.into()))?;
|
||||||
|
proc.borrow().reload()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
fn cgroup(&self, cg: &str) -> Result<&CgroupConfig> {
|
||||||
|
self.cgroups
|
||||||
|
.get(cg)
|
||||||
|
.ok_or_else(|| Error::UnknownCgroup(cg.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn service(&self, key: &str) -> Result<&Service> {
|
||||||
|
let (cg, svc) = split_key(key)?;
|
||||||
|
self.cgroup(cg)?.service(svc)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn service_keys(&self) -> impl Iterator<Item = String> {
|
||||||
|
(self.cgroups.iter())
|
||||||
|
.map(|(cg_name, cg)| cg.services.keys().map(move |n| child_key(cg_name, n)))
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn services(&self) -> impl Iterator<Item = (String, &String, &String, &Service)> {
|
||||||
|
(self.cgroups.iter())
|
||||||
|
.map(|(cg_name, cg)| {
|
||||||
|
cg.services
|
||||||
|
.iter()
|
||||||
|
.map(move |(n, service)| (child_key(cg_name, n), cg_name, n, service))
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CgroupConfig {
|
||||||
|
fn service(&self, svc: &str) -> Result<&Vec<String>> {
|
||||||
|
self.services
|
||||||
|
.get(svc)
|
||||||
|
.ok_or_else(|| Error::UnknownService(svc.into()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn complete() -> Vec<String> {
|
||||||
|
let mut r = vec![];
|
||||||
|
let Ok(rd) = ctl_exec(b"ls\n").await else {
|
||||||
|
return r;
|
||||||
|
};
|
||||||
|
let mut rd = rd.lines();
|
||||||
|
while let Some(line) = rd.next_line().await.ok().flatten() {
|
||||||
|
r.push(line);
|
||||||
|
}
|
||||||
|
r
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,263 @@
|
|||||||
|
use log::{error, warn};
|
||||||
|
use nix::{
|
||||||
|
sys::signal::{Signal, kill},
|
||||||
|
unistd::Pid,
|
||||||
|
};
|
||||||
|
use std::num::NonZero;
|
||||||
|
use tokio::{
|
||||||
|
process, select,
|
||||||
|
sync::{mpsc, watch},
|
||||||
|
time::{Duration, Instant, sleep, sleep_until},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{Error, Result, Service};
|
||||||
|
use crate::logger::Logger;
|
||||||
|
|
||||||
|
const LOG_PATH: &str = "/var/log";
|
||||||
|
|
||||||
|
const TERM_DELAY: Duration = Duration::from_secs(30);
|
||||||
|
const KILL_DELAY: Duration = Duration::from_secs(10);
|
||||||
|
const RESTART_DELAY: Duration = Duration::from_secs(8);
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum Cmd {
|
||||||
|
Start,
|
||||||
|
Stop,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Clone, Copy, Debug)]
|
||||||
|
pub enum State {
|
||||||
|
#[default]
|
||||||
|
NeverStarted,
|
||||||
|
Starting,
|
||||||
|
Running,
|
||||||
|
Crashed,
|
||||||
|
Stopping,
|
||||||
|
Stopped,
|
||||||
|
Finalized,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(
|
||||||
|
cg: impl Into<String>,
|
||||||
|
svc: impl Into<String>,
|
||||||
|
service: Service,
|
||||||
|
) -> (Runner, watch::Receiver<Child>, mpsc::Sender<Cmd>) {
|
||||||
|
let (manager, child_rx) = ProcessManager::new(service);
|
||||||
|
let (cmds_tx, cmds_rx) = mpsc::channel(1);
|
||||||
|
let r = Runner {
|
||||||
|
cg: cg.into(),
|
||||||
|
svc: svc.into(),
|
||||||
|
cmds_rx,
|
||||||
|
manager,
|
||||||
|
};
|
||||||
|
(r, child_rx, cmds_tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Runner {
|
||||||
|
cg: String,
|
||||||
|
svc: String,
|
||||||
|
cmds_rx: mpsc::Receiver<Cmd>,
|
||||||
|
manager: ProcessManager,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Runner {
|
||||||
|
pub async fn run(mut self) {
|
||||||
|
self.manager.update(State::NeverStarted);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let cmd = select! {
|
||||||
|
cmd = self.manager.manage() => {
|
||||||
|
cmd
|
||||||
|
}
|
||||||
|
cmd = self.cmds_rx.recv() => {
|
||||||
|
let Some(cmd) = cmd else {
|
||||||
|
break; // command side dropped
|
||||||
|
};
|
||||||
|
Some(cmd)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(cmd) = cmd {
|
||||||
|
self.process_cmd(cmd).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.process_cmd(Cmd::Stop).await;
|
||||||
|
self.manager.update(State::Finalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn process_cmd(&mut self, cmd: Cmd) {
|
||||||
|
let cg = &self.cg;
|
||||||
|
let svc = &self.svc;
|
||||||
|
|
||||||
|
match cmd {
|
||||||
|
Cmd::Start => {
|
||||||
|
self.manager.start(cg, svc).await;
|
||||||
|
}
|
||||||
|
Cmd::Stop => {
|
||||||
|
self.manager.stop().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ProcessManager {
|
||||||
|
service: Service,
|
||||||
|
child_tx: watch::Sender<Child>,
|
||||||
|
process: Option<process::Child>,
|
||||||
|
restart_deadline: Option<Instant>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessManager {
|
||||||
|
fn new(service: Service) -> (Self, watch::Receiver<Child>) {
|
||||||
|
let (child_tx, child_rx) = watch::channel(Child::default());
|
||||||
|
let pm = Self {
|
||||||
|
service,
|
||||||
|
child_tx,
|
||||||
|
process: None,
|
||||||
|
restart_deadline: None,
|
||||||
|
};
|
||||||
|
(pm, child_rx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// runs a management iteration (ie: waiting for the child or a restart deadline).
|
||||||
|
async fn manage(&mut self) -> Option<Cmd> {
|
||||||
|
if let Some(process) = self.process.as_mut() {
|
||||||
|
let msg = match process.wait().await {
|
||||||
|
Ok(status) => status.to_string(),
|
||||||
|
Err(e) => e.to_string(),
|
||||||
|
};
|
||||||
|
self.crashed(msg);
|
||||||
|
self.process = None;
|
||||||
|
self.restart_deadline = Some(Instant::now() + RESTART_DELAY);
|
||||||
|
None
|
||||||
|
} else if let Some(deadline) = self.restart_deadline {
|
||||||
|
sleep_until(deadline).await;
|
||||||
|
Some(Cmd::Start)
|
||||||
|
} else {
|
||||||
|
std::future::pending().await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start(&mut self, cg: &str, svc: &str) {
|
||||||
|
if self.process.is_some() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.update(State::Starting);
|
||||||
|
|
||||||
|
let logger = Logger {
|
||||||
|
log_path: LOG_PATH.into(),
|
||||||
|
log_name: svc.into(),
|
||||||
|
with_prefix: false,
|
||||||
|
cgroup: Some(cg.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut args = self.service.iter();
|
||||||
|
let Some(cmd) = args.next() else {
|
||||||
|
error!("{cg}/{svc}: empty command");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Ok(cmd) = (logger.setup(cmd, args).await)
|
||||||
|
.inspect_err(|e| self.crashed(format!("setup failed: {e}")))
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Ok(child) = logger
|
||||||
|
.spawn(cmd)
|
||||||
|
.inspect_err(|e| self.crashed(format!("exec failed: {e}")))
|
||||||
|
else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
self.process = Some(child);
|
||||||
|
self.restart_deadline = None;
|
||||||
|
self.update(State::Running);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stop(&mut self) {
|
||||||
|
self.restart_deadline = None;
|
||||||
|
|
||||||
|
let Some(mut process) = self.process.take() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some(pid) = process.id() else {
|
||||||
|
let _ = process.wait().await; // already dead, reap it
|
||||||
|
self.update(State::Stopped);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let pid = pid as i32;
|
||||||
|
self.update_full(pid, State::Stopping, None);
|
||||||
|
let pid = Pid::from_raw(pid);
|
||||||
|
|
||||||
|
let _ = kill(pid, Signal::SIGTERM).inspect_err(|e| error!("kill -TERM {pid} failed: {e}"));
|
||||||
|
|
||||||
|
select! {
|
||||||
|
_ = process.wait() => {
|
||||||
|
self.update(State::Stopped);
|
||||||
|
return
|
||||||
|
},
|
||||||
|
_ = sleep(TERM_DELAY) => {
|
||||||
|
warn!("process {pid} did not exit during the grace period, killing");
|
||||||
|
let _ = process.kill().await.inspect_err(|e| error!("kill -KILL {pid} failed: {e}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
select! {
|
||||||
|
_ = process.wait() => {
|
||||||
|
self.update(State::Stopped);
|
||||||
|
return
|
||||||
|
},
|
||||||
|
_ = sleep(KILL_DELAY) => {
|
||||||
|
error!("process {pid} still alive after SIGKILL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_pid(&self) -> i32 {
|
||||||
|
(self.process.as_ref())
|
||||||
|
.and_then(|c| Some(c.id()? as i32))
|
||||||
|
.unwrap_or(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&self, state: State) {
|
||||||
|
let pid = self.process_pid();
|
||||||
|
self.update_full(pid, state, None);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_full(&self, pid: i32, state: State, msg: Option<String>) {
|
||||||
|
self.child_tx.send_replace(Child {
|
||||||
|
pid: NonZero::new(pid),
|
||||||
|
state,
|
||||||
|
msg,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn crashed(&self, msg: String) {
|
||||||
|
let pid = self.process_pid();
|
||||||
|
self.update_full(pid, State::Crashed, Some(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Default)]
|
||||||
|
pub struct Child {
|
||||||
|
pub pid: Option<NonZero<i32>>,
|
||||||
|
pub state: State,
|
||||||
|
pub msg: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Child {
|
||||||
|
pub fn reload(&self) -> Result<()> {
|
||||||
|
self.kill(Signal::SIGHUP)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn kill(&self, sig: Signal) -> Result<()> {
|
||||||
|
let Some(pid) = self.pid else {
|
||||||
|
return Err(Error::ProcessExited);
|
||||||
|
};
|
||||||
|
kill(Pid::from_raw(pid.into()), sig).map_err(|e| Error::KillFailed(e))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user