|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! This module validates server configs and restarts the server service. |
| 5 | +
|
| 6 | +use std::io::Write; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use std::process::{Command, Stdio}; |
| 9 | + |
| 10 | +use switchyard_desktop::install::credentials::KeyStatus; |
| 11 | + |
| 12 | +use crate::config::Config; |
| 13 | + |
| 14 | +/// This function finds the installed helper, which is the only program that reads or writes |
| 15 | +/// server keys. |
| 16 | +fn helper() -> Result<PathBuf, String> { |
| 17 | + Ok( |
| 18 | + server_binary()?.with_file_name(switchyard_desktop::install::executable_name( |
| 19 | + "switchyard-desktop-install", |
| 20 | + )), |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +/// This function runs one of the helper's `key` commands. A key goes to the helper's standard |
| 25 | +/// input, never its command line, where other processes could read it. |
| 26 | +fn key_command(args: &[&str], key: Option<&str>) -> Result<String, String> { |
| 27 | + let helper = helper()?; |
| 28 | + let mut child = Command::new(&helper) |
| 29 | + .arg("key") |
| 30 | + .args(args) |
| 31 | + .stdin(if key.is_some() { |
| 32 | + Stdio::piped() |
| 33 | + } else { |
| 34 | + Stdio::null() |
| 35 | + }) |
| 36 | + .stdout(Stdio::piped()) |
| 37 | + .stderr(Stdio::piped()) |
| 38 | + .spawn() |
| 39 | + .map_err(|error| { |
| 40 | + format!( |
| 41 | + "Could not run {}: {error}. \ |
| 42 | + Run cargo desktop install from the Switchyard repository to install it.", |
| 43 | + helper.display() |
| 44 | + ) |
| 45 | + })?; |
| 46 | + if let (Some(key), Some(mut stdin)) = (key, child.stdin.take()) { |
| 47 | + stdin |
| 48 | + .write_all(key.as_bytes()) |
| 49 | + .map_err(|error| format!("Could not pass the key to the helper: {error}"))?; |
| 50 | + } |
| 51 | + let output = child |
| 52 | + .wait_with_output() |
| 53 | + .map_err(|error| error.to_string())?; |
| 54 | + if output.status.success() { |
| 55 | + return Ok(String::from_utf8_lossy(&output.stdout).into_owned()); |
| 56 | + } |
| 57 | + Err(String::from_utf8_lossy(&output.stderr) |
| 58 | + .trim() |
| 59 | + .trim_start_matches("switchyard-desktop-install: ") |
| 60 | + .to_string()) |
| 61 | +} |
| 62 | + |
| 63 | +/// This function reports which `api_key_env` variables in the server config have a saved key. |
| 64 | +pub fn server_keys(settings: &Config) -> Result<Vec<KeyStatus>, String> { |
| 65 | + let config = std::fs::canonicalize(&settings.config_file).map_err(|error| error.to_string())?; |
| 66 | + let output = key_command(&["status", "--config", &config.display().to_string()], None)?; |
| 67 | + serde_json::from_str(&output).map_err(|error| error.to_string()) |
| 68 | +} |
| 69 | + |
| 70 | +/// This function finds the installed server from the app's installation metadata. |
| 71 | +fn server_binary() -> Result<PathBuf, String> { |
| 72 | + let executable = std::env::current_exe().map_err(|error| { |
| 73 | + format!( |
| 74 | + "Could not find switchyard-server next to the desktop app: {error}. \ |
| 75 | + Run cargo desktop install from the Switchyard repository to install it." |
| 76 | + ) |
| 77 | + })?; |
| 78 | + let metadata = switchyard_desktop::install::bundle_metadata(&executable); |
| 79 | + if metadata.try_exists().map_err(|error| error.to_string())? { |
| 80 | + return switchyard_desktop::install::Settings::load(&metadata) |
| 81 | + .map(|settings| settings.binary("switchyard-server")); |
| 82 | + } |
| 83 | + Ok( |
| 84 | + executable.with_file_name(switchyard_desktop::install::executable_name( |
| 85 | + "switchyard-server", |
| 86 | + )), |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +/// This function asks the installed helper to run the server's dry-run on `config` with the |
| 91 | +/// saved server keys, the same keys the installed server starts with. |
| 92 | +fn check(binary: &Path, config: &Path) -> Result<(), String> { |
| 93 | + let config = std::fs::canonicalize(config).map_err(|error| error.to_string())?; |
| 94 | + let helper = binary.with_file_name(switchyard_desktop::install::executable_name( |
| 95 | + "switchyard-desktop-install", |
| 96 | + )); |
| 97 | + let mut validator = Command::new(&helper); |
| 98 | + validator |
| 99 | + .arg("validate") |
| 100 | + .arg("--server") |
| 101 | + .arg(binary) |
| 102 | + .arg("--config") |
| 103 | + .arg(&config); |
| 104 | + let output = validator.output().map_err(|error| { |
| 105 | + format!( |
| 106 | + "Could not run {}: {error}. \ |
| 107 | + Apply needs the installed server and credential helper. \ |
| 108 | + Run cargo desktop install from the Switchyard repository to install them.", |
| 109 | + helper.display() |
| 110 | + ) |
| 111 | + })?; |
| 112 | + if output.status.success() { |
| 113 | + return Ok(()); |
| 114 | + } |
| 115 | + let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 116 | + let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); |
| 117 | + Err(if stderr.is_empty() { stdout } else { stderr }) |
| 118 | +} |
| 119 | + |
| 120 | +pub fn check_config(settings: &Config) -> Result<(), String> { |
| 121 | + check(&server_binary()?, &settings.config_file) |
| 122 | +} |
| 123 | + |
| 124 | +/// This function runs a command, returning its stdout or a message naming what failed. |
| 125 | +pub fn command(program: &str, args: &[&str]) -> Result<String, String> { |
| 126 | + if program == "open" { |
| 127 | + #[cfg(windows)] |
| 128 | + { |
| 129 | + return switchyard_desktop::install::windows::open( |
| 130 | + args.first().ok_or("Choose a file or URL.")?, |
| 131 | + ) |
| 132 | + .map(|()| String::new()); |
| 133 | + } |
| 134 | + #[cfg(target_os = "linux")] |
| 135 | + { |
| 136 | + return command("xdg-open", args); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + let output = Command::new(program) |
| 141 | + .args(args) |
| 142 | + .output() |
| 143 | + .map_err(|error| format!("Could not run {program}: {error}"))?; |
| 144 | + if !output.status.success() { |
| 145 | + return Err(format!( |
| 146 | + "{program} {} failed: {}", |
| 147 | + args.join(" "), |
| 148 | + String::from_utf8_lossy(&output.stderr).trim() |
| 149 | + )); |
| 150 | + } |
| 151 | + Ok(String::from_utf8_lossy(&output.stdout).into_owned()) |
| 152 | +} |
0 commit comments