Skip to content

Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.

Rust
2.4 KB
Raw Blame
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! This module installs the systemd user service for the server.

use std::{
    path::Path,
    process::{Command, Stdio},
    thread,
    time::Duration,
};

use super::{Settings, present, remove, run, write};

const UNIT: &str = "switchyard.service";

pub fn preflight() -> Result<(), String> {
    run(systemctl(&["show-environment"]).stdout(Stdio::null()))
}

pub fn stop(s: &Settings) -> Result<(), String> {
    if present(&s.service_dir.join(UNIT))? {
        run(&mut systemctl(&["stop", UNIT]))?;
    }
    Ok(())
}

pub fn install(s: &Settings, keys_saved: bool) -> Result<(), String> {
    let unit = format!(
        r#"[Unit]
Description=Switchyard local server

[Service]
Type=simple
ExecStart={installer} serve --settings {settings}
Restart=on-failure
RestartSec=10
Environment=RUST_LOG=info

[Install]
WantedBy=default.target
"#,
        installer = systemd_arg(&s.binary("switchyard-desktop-install")),
        settings = systemd_arg(&s.sy_home.join("install.toml")),
    );
    write(&s.service_dir.join(UNIT), unit.as_bytes(), false)?;
    run(&mut systemctl(&["daemon-reload"]))?;
    run(&mut systemctl(&["enable", UNIT]))?;
    run(&mut systemctl(&["restart", UNIT]))?;
    // A server waiting for a key exits and restarts until the key is saved.
    if !keys_saved {
        return Ok(());
    }
    thread::sleep(Duration::from_secs(2));
    run(&mut systemctl(&["is-active", "--quiet", UNIT]))
        .map_err(|e| format!("{e}. See journalctl --user -u switchyard."))
}

/// Removes the service. The binaries stay in SY_HOME.
pub fn uninstall(s: &Settings) -> Result<(), String> {
    let unit = s.service_dir.join(UNIT);
    if present(&unit)? {
        run(&mut systemctl(&["disable", UNIT]))?;
        remove(&unit)?;
    }
    run(&mut systemctl(&["daemon-reload"]))
}

fn systemctl(args: &[&str]) -> Command {
    let mut command = Command::new("systemctl");
    command.arg("--user").args(args);
    command
}

fn systemd_arg(path: &Path) -> String {
    // systemd expands percent specifiers and environment variables inside quoted arguments.
    format!(
        "\"{}\"",
        path.to_string_lossy()
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
            .replace('%', "%%")
            .replace('$', "$$")
    )
}