// 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 [--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 state dir (default /var/lib/legatus, or $LEGATUS_STATE) --apps where releases go (default /var/lib/legatus/apps) "; fn main() -> ExitCode { let mut args: Vec = std::env::args().skip(1).collect(); let mut take = |flag: &str| -> Option { 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) } } }