#[cfg(any(target_os = "macos", test))]
pub mod plist;
#[cfg(any(target_os = "macos", target_os = "linux", test))]
pub mod read_back;
#[cfg(any(target_os = "linux", test))]
pub mod unit;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
mod unsupported;
#[cfg(target_os = "linux")]
use linux as platform;
#[cfg(target_os = "macos")]
use macos as platform;
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
use unsupported as platform;
use std::path::PathBuf;
use anyhow::Result;
#[cfg(any(target_os = "macos", target_os = "linux"))]
use serde::Serialize;
use crate::home::Home;
#[cfg(all(any(target_os = "macos", target_os = "linux"), debug_assertions))]
pub(crate) fn skip_exec() -> bool {
std::env::var_os("MOAT_SERVICE_SKIP_EXEC").is_some_and(|v| v == "1")
}
#[cfg(all(any(target_os = "macos", target_os = "linux"), not(debug_assertions)))]
pub(crate) fn skip_exec() -> bool {
false
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "state", rename_all = "kebab-case")]
pub enum State {
NotInstalled,
Stopped,
Running,
DriftBlocked,
Crashlooping,
Unknown { details: String },
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
impl State {
pub fn describe(&self, path: &std::path::Path) -> String {
match self {
Self::NotInstalled => {
"not installed; `openmoat proxy install` keeps the proxy running".to_owned()
}
Self::Stopped => format!("installed but stopped ({})", path.display()),
Self::Running => format!("installed and running ({})", path.display()),
Self::DriftBlocked => format!(
"installed; the proxy exited because of policy-lock drift. Fix the drift \
(`openmoat doctor`), then `openmoat proxy install` restarts it ({})",
path.display()
),
Self::Crashlooping => format!(
"installed; restart budget exhausted. Check the service log, then \
`openmoat proxy install` to retry ({})",
path.display()
),
Self::Unknown { details } => format!("installed; state unknown: {details}"),
}
}
}
pub trait Manager {
fn install(&self, binary: &std::path::Path, moat_home: &std::path::Path) -> Result<PathBuf>;
fn uninstall(&self) -> Result<Option<PathBuf>>;
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn file_path(&self) -> Result<PathBuf>;
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn render(&self, binary: &std::path::Path, moat_home: &std::path::Path) -> Result<String>;
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn environment(&self) -> fn(&str) -> Option<Vec<String>>;
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn state(&self) -> Result<State>;
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn restart(&self) -> Result<()>;
}
pub fn manager(home: &Home) -> Result<Box<dyn Manager>> {
platform::manager(home)
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub fn read_back(
manager: &dyn Manager,
binary: &std::path::Path,
moat_home: &std::path::Path,
) -> Vec<String> {
let Ok(path) = manager.file_path() else {
return Vec::new();
};
let text = match std::fs::read(&path) {
Ok(text) => text,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Vec::new(),
Err(e) => return vec![format!("cannot be read: {e}")],
};
let expected = match manager.render(binary, moat_home) {
Ok(expected) => expected,
Err(e) => return vec![format!("cannot be compared: {e:#}")],
};
let installed = read_back::Installed {
text: &text,
mode: mode(&path),
};
read_back::findings(&expected, &installed, manager.environment())
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn mode(path: &std::path::Path) -> Option<u32> {
use std::os::unix::fs::PermissionsExt as _;
std::fs::metadata(path).ok().map(|m| m.permissions().mode())
}
pub fn display_name() -> &'static str {
platform::DISPLAY
}
#[cfg(all(test, any(target_os = "macos", target_os = "linux")))]
mod tests {
use super::*;
#[test]
fn state_describe_names_the_fix_for_each_variant() {
let path = std::path::Path::new("/x/plist");
assert!(State::NotInstalled.describe(path).contains("not installed"));
assert!(State::Stopped.describe(path).contains("stopped"));
assert!(State::Running.describe(path).contains("running"));
assert!(State::DriftBlocked.describe(path).contains("drift"));
assert!(State::Crashlooping.describe(path).contains("restart"));
assert!(
State::Unknown {
details: "no bus".into(),
}
.describe(path)
.contains("no bus")
);
}
}