mirror of
https://github.com/cjfranko/NTP-Timeturner.git
synced 2025-11-08 18:32:02 +00:00
fix: Conditionally compile systemd features for Linux only
Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
parent
b854d29015
commit
12065a08c2
4 changed files with 52 additions and 20 deletions
|
|
@ -18,5 +18,7 @@ actix-files = "0.6"
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
clap = { version = "4.4", features = ["derive"] }
|
clap = { version = "4.4", features = ["derive"] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
|
||||||
|
[target.'cfg(target_os = "linux")'.dependencies]
|
||||||
systemd-journal-logger = "1.0"
|
systemd-journal-logger = "1.0"
|
||||||
|
|
||||||
|
|
|
||||||
8
setup.sh
8
setup.sh
|
|
@ -28,11 +28,15 @@ sudo ln -sf $INSTALL_DIR/timeturner $BIN_DIR/timeturner
|
||||||
echo "✅ Binary installed to $INSTALL_DIR and linked to $BIN_DIR."
|
echo "✅ Binary installed to $INSTALL_DIR and linked to $BIN_DIR."
|
||||||
|
|
||||||
# 4. Install systemd service file
|
# 4. Install systemd service file
|
||||||
echo "⚙️ Installing systemd service..."
|
if [[ "$(uname)" == "Linux" ]]; then
|
||||||
|
echo "⚙️ Installing systemd service for Linux..."
|
||||||
sudo cp timeturner.service /etc/systemd/system/
|
sudo cp timeturner.service /etc/systemd/system/
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl enable timeturner.service
|
sudo systemctl enable timeturner.service
|
||||||
echo "✅ Systemd service installed and enabled."
|
echo "✅ Systemd service installed and enabled."
|
||||||
|
else
|
||||||
|
echo "⚠️ Skipping systemd service installation on non-Linux OS."
|
||||||
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
echo "--- Setup Complete ---"
|
echo "--- Setup Complete ---"
|
||||||
|
|
@ -40,12 +44,14 @@ echo "The TimeTurner daemon is now installed."
|
||||||
echo "The working directory is $INSTALL_DIR."
|
echo "The working directory is $INSTALL_DIR."
|
||||||
echo "A default 'config.yml' will be created there on first run."
|
echo "A default 'config.yml' will be created there on first run."
|
||||||
echo ""
|
echo ""
|
||||||
|
if [[ "$(uname)" == "Linux" ]]; then
|
||||||
echo "To start the service, run:"
|
echo "To start the service, run:"
|
||||||
echo " sudo systemctl start timeturner.service"
|
echo " sudo systemctl start timeturner.service"
|
||||||
echo ""
|
echo ""
|
||||||
echo "To view live logs, run:"
|
echo "To view live logs, run:"
|
||||||
echo " journalctl -u timeturner.service -f"
|
echo " journalctl -u timeturner.service -f"
|
||||||
echo ""
|
echo ""
|
||||||
|
fi
|
||||||
echo "To run the interactive TUI instead, simply run from the project directory:"
|
echo "To run the interactive TUI instead, simply run from the project directory:"
|
||||||
echo " cargo run"
|
echo " cargo run"
|
||||||
echo "Or from anywhere after installation:"
|
echo "Or from anywhere after installation:"
|
||||||
|
|
|
||||||
|
|
@ -103,10 +103,17 @@ async fn main() {
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
println!("🚀 Starting TimeTurner daemon...");
|
println!("🚀 Starting TimeTurner daemon...");
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
systemd_journal_logger::init().unwrap();
|
systemd_journal_logger::init().unwrap();
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
log::info!("TimeTurner daemon started. API server is running.");
|
log::info!("TimeTurner daemon started. API server is running.");
|
||||||
}
|
}
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
{
|
||||||
|
println!("Daemon mode started. API server is running. Logging to system journal is only supported on Linux.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 6️⃣ Set up a LocalSet for the API server and main loop
|
// 6️⃣ Set up a LocalSet for the API server and main loop
|
||||||
let local = LocalSet::new();
|
let local = LocalSet::new();
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,8 @@ use std::process::Command;
|
||||||
|
|
||||||
/// Check if Chrony is active
|
/// Check if Chrony is active
|
||||||
pub fn ntp_service_active() -> bool {
|
pub fn ntp_service_active() -> bool {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
if let Ok(output) = Command::new("systemctl").args(&["is-active", "chrony"]).output() {
|
if let Ok(output) = Command::new("systemctl").args(&["is-active", "chrony"]).output() {
|
||||||
output.status.success()
|
output.status.success()
|
||||||
&& String::from_utf8_lossy(&output.stdout).trim() == "active"
|
&& String::from_utf8_lossy(&output.stdout).trim() == "active"
|
||||||
|
|
@ -12,13 +14,28 @@ pub fn ntp_service_active() -> bool {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
{
|
||||||
|
// systemctl is not available on non-Linux platforms.
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Toggle Chrony (not used yet)
|
/// Toggle Chrony (not used yet)
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn ntp_service_toggle(start: bool) {
|
pub fn ntp_service_toggle(start: bool) {
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
{
|
||||||
let action = if start { "start" } else { "stop" };
|
let action = if start { "start" } else { "stop" };
|
||||||
let _ = Command::new("systemctl").args(&[action, "chrony"]).status();
|
let _ = Command::new("systemctl").args(&[action, "chrony"]).status();
|
||||||
}
|
}
|
||||||
|
#[cfg(not(target_os = "linux"))]
|
||||||
|
{
|
||||||
|
// No-op on non-Linux.
|
||||||
|
// The parameter is unused, but the function is dead code anyway.
|
||||||
|
let _ = start;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn trigger_sync(frame: &LtcFrame, config: &Config) -> Result<String, ()> {
|
pub fn trigger_sync(frame: &LtcFrame, config: &Config) -> Result<String, ()> {
|
||||||
let today_local = Local::now().date_naive();
|
let today_local = Local::now().date_naive();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue