[agent] Legatus agent: pinned-root TLS and mTLS, HTTP client, enroll, job pipeline, renewal, Linux target, CLI (21 tests)

This commit is contained in:
2026-11-17 12:09:57 +01:00
parent f935a41ef8
commit 58c87547db
8 changed files with 1785 additions and 2 deletions
+109 -1
View File
@@ -1 +1,109 @@
fn main() {}
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! `legatus`: enroll, run, status.
use std::path::PathBuf;
use std::process::ExitCode;
use legatus::state::{State, default_dir};
use legatus::target::Linux;
const USAGE: &str = "legatus: the Fabrica Domi agent
usage:
legatus enroll <code> [--force] enroll this machine with the one-time code from Fabrica
legatus run connect to Fabrica and run jobs (the systemd service runs this)
legatus status who this agent is and what runs here
legatus version
options:
--state <dir> state dir (default /var/lib/legatus, or $LEGATUS_STATE)
--apps <dir> where releases go (default /var/lib/legatus/apps)
";
fn main() -> ExitCode {
let mut args: Vec<String> = std::env::args().skip(1).collect();
let mut take = |flag: &str| -> Option<String> {
let i = args.iter().position(|a| a == flag)?;
args.remove(i);
(i < args.len()).then(|| args.remove(i))
};
let state = State::new(
take("--state")
.map(PathBuf::from)
.unwrap_or_else(default_dir),
);
let apps = take("--apps")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(legatus_linux::DEFAULT_ROOT));
let force = match args.iter().position(|a| a == "--force") {
Some(i) => {
args.remove(i);
true
}
None => false,
};
let words: Vec<&str> = args.iter().map(String::as_str).collect();
match words.as_slice() {
["enroll", code] => match legatus::enroll(&state, code, force) {
Ok(f) => {
println!(
"Enrolled as {}/{} with {}. Build here: {}. Deploy here: {}.",
f.team,
f.agent,
f.server,
if f.roles.build { "on" } else { "off" },
if f.roles.deploy { "on" } else { "off" }
);
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("legatus: {e}");
ExitCode::FAILURE
}
},
["run"] => {
let target = Linux::new(apps);
match legatus::run(state, &target) {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("legatus: {e}");
ExitCode::FAILURE
}
}
}
["status"] => match state.load() {
Ok(e) => {
let leaf = domi_pki::inspect(&e.chain_pem).ok();
println!("agent {}/{}", e.file.team, e.file.agent);
println!("server {}", e.file.server);
println!(
"roles build {}, deploy {}",
e.file.roles.build, e.file.roles.deploy
);
if let Some(l) = leaf {
println!("certificate valid until {} (unix)", l.not_after);
}
println!("release keys pinned: {}", e.file.release_keys.len());
for d in legatus::pipeline::Target::deployed(&Linux::new(apps)) {
println!("running {} {}", d.app, d.version);
}
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("legatus: {e}");
ExitCode::FAILURE
}
},
["version"] | ["--version"] => {
println!("legatus {}", legatus::VERSION);
ExitCode::SUCCESS
}
_ => {
eprint!("{USAGE}");
ExitCode::from(2)
}
}
}