|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use std::fs; |
| 3 | +use std::os::unix::fs::PermissionsExt; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +pub fn install_kernel_update_hook(binary: &Path, config: &Path, plugin_path: &Path) -> Result<()> { |
| 7 | + let parent = plugin_path.parent().with_context(|| { |
| 8 | + format!( |
| 9 | + "kernel-install plugin path has no parent: {}", |
| 10 | + plugin_path.display() |
| 11 | + ) |
| 12 | + })?; |
| 13 | + fs::create_dir_all(parent) |
| 14 | + .with_context(|| format!("failed creating plugin directory {}", parent.display()))?; |
| 15 | + |
| 16 | + let script = render_kernel_install_plugin(binary, config)?; |
| 17 | + fs::write(plugin_path, script) |
| 18 | + .with_context(|| format!("failed writing plugin {}", plugin_path.display()))?; |
| 19 | + |
| 20 | + let mut perms = fs::metadata(plugin_path) |
| 21 | + .with_context(|| format!("failed stat {}", plugin_path.display()))? |
| 22 | + .permissions(); |
| 23 | + perms.set_mode(0o755); |
| 24 | + fs::set_permissions(plugin_path, perms) |
| 25 | + .with_context(|| format!("failed chmod +x {}", plugin_path.display()))?; |
| 26 | + |
| 27 | + Ok(()) |
| 28 | +} |
| 29 | + |
| 30 | +pub fn render_kernel_install_plugin(binary: &Path, config: &Path) -> Result<String> { |
| 31 | + let bin = binary |
| 32 | + .to_str() |
| 33 | + .with_context(|| format!("binary path is non-UTF8: {}", binary.display()))?; |
| 34 | + let cfg = config |
| 35 | + .to_str() |
| 36 | + .with_context(|| format!("config path is non-UTF8: {}", config.display()))?; |
| 37 | + |
| 38 | + Ok(format!( |
| 39 | + r#"#!/usr/bin/env bash |
| 40 | +set -euo pipefail |
| 41 | +
|
| 42 | +COMMAND="${{1:-}}" |
| 43 | +KERNEL_VER="${{2:-unknown}}" |
| 44 | +RUSTYUKI_BIN="{bin}" |
| 45 | +RUSTYUKI_CONFIG="{cfg}" |
| 46 | +
|
| 47 | +log() {{ |
| 48 | + echo "[rustyuki-hook] $*" >&2 |
| 49 | +}} |
| 50 | +
|
| 51 | +if [[ ! -x "$RUSTYUKI_BIN" ]]; then |
| 52 | + log "RustyUKI binary not executable: $RUSTYUKI_BIN" |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | +
|
| 56 | +case "$COMMAND" in |
| 57 | + add) |
| 58 | + log "kernel add: $KERNEL_VER; reconciling all installed kernels" |
| 59 | + ;; |
| 60 | + remove) |
| 61 | + log "kernel remove: $KERNEL_VER; reconciling all installed kernels" |
| 62 | + ;; |
| 63 | + *) |
| 64 | + log "kernel command '$COMMAND'; running reconcile" |
| 65 | + ;; |
| 66 | +esac |
| 67 | +
|
| 68 | +exec "$RUSTYUKI_BIN" --config "$RUSTYUKI_CONFIG" reconcile |
| 69 | +"# |
| 70 | + )) |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(test)] |
| 74 | +mod tests { |
| 75 | + use super::render_kernel_install_plugin; |
| 76 | + use std::path::Path; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn plugin_contains_reconcile_and_config() { |
| 80 | + let script = render_kernel_install_plugin( |
| 81 | + Path::new("/usr/local/bin/rustyuki"), |
| 82 | + Path::new("/etc/uki/uki.conf"), |
| 83 | + ) |
| 84 | + .unwrap_or_else(|e| panic!("{e}")); |
| 85 | + |
| 86 | + assert!(script.contains("reconcile")); |
| 87 | + assert!(script.contains("--config \"$RUSTYUKI_CONFIG\"")); |
| 88 | + assert!(script.contains("KERNEL_VER")); |
| 89 | + } |
| 90 | +} |
0 commit comments