|
| 1 | +use anyhow::Result; |
| 2 | + |
| 3 | +use ostree_ext::ostree::Deployment; |
| 4 | +use ostree_ext::sysroot::SysrootLock; |
| 5 | + |
| 6 | +use std::fs; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +use crate::task::Task; |
| 10 | + |
| 11 | +use tempfile::TempDir; |
| 12 | + |
| 13 | +const BOOTC_QUADLET_DIR: &'static str = "/etc/containers/systemd/bootc"; |
| 14 | +const QUADLET_BINARY: &'static str = "/usr/lib/systemd/system-generators/podman-system-generator"; |
| 15 | +const SYSTEMD_DIR: &'static str = "/etc/systemd/system"; |
| 16 | + |
| 17 | +pub(crate) struct BoundImageManager { |
| 18 | + quadlet_unit_dir: String, |
| 19 | + units: Vec<String>, |
| 20 | + temp_dir: TempDir, |
| 21 | +} |
| 22 | + |
| 23 | +impl BoundImageManager { |
| 24 | + pub(crate) fn new(deployment: &Deployment, sysroot: &SysrootLock) -> Result<BoundImageManager> { |
| 25 | + let deployment_dir = sysroot.deployment_dirpath(&deployment); |
| 26 | + let quadlet_unit_dir = format!("/{deployment_dir}/{BOOTC_QUADLET_DIR}"); |
| 27 | + |
| 28 | + let temp_dir = TempDir::new()?; |
| 29 | + let bound_image_manager = BoundImageManager { |
| 30 | + quadlet_unit_dir, |
| 31 | + units: Vec::new(), |
| 32 | + temp_dir, |
| 33 | + }; |
| 34 | + Ok(bound_image_manager) |
| 35 | + } |
| 36 | + |
| 37 | + pub(crate) fn run(&mut self) -> Result<()> { |
| 38 | + if Path::new(&self.quadlet_unit_dir).exists() { |
| 39 | + self.run_quadlet()?; |
| 40 | + self.move_units()?; |
| 41 | + self.restart_systemd()?; |
| 42 | + self.start_new_services()?; |
| 43 | + } |
| 44 | + |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | + |
| 48 | + // Run podman-system-generator to generate the systemd units |
| 49 | + // the output is written to a temporary directory |
| 50 | + // in order to track the generated units. |
| 51 | + // The generated units need to be moved to /etc/systemd/system |
| 52 | + // to be started by systemd. |
| 53 | + fn run_quadlet(&self) -> Result<()> { |
| 54 | + Task::new( |
| 55 | + format!("Running quadlet on {:#}", self.quadlet_unit_dir), |
| 56 | + QUADLET_BINARY, |
| 57 | + ) |
| 58 | + .arg(self.temp_dir.path()) |
| 59 | + .env(&"QUADLET_UNIT_DIRS".to_string(), &self.quadlet_unit_dir) |
| 60 | + .run()?; |
| 61 | + |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + fn move_units(&mut self) -> Result<()> { |
| 66 | + let entries = fs::read_dir(self.temp_dir.path())?; |
| 67 | + for bound_image in entries { |
| 68 | + let bound_image = bound_image?; |
| 69 | + let bound_image_path = bound_image.path(); |
| 70 | + let unit_name = bound_image_path.file_name().unwrap().to_str().unwrap(); |
| 71 | + |
| 72 | + //move the unit file from the bootc subdirectory to the root systemd directory |
| 73 | + let systemd_dst = format!("{SYSTEMD_DIR}/{unit_name}"); |
| 74 | + if !Path::new(systemd_dst.as_str()).exists() { |
| 75 | + fs::copy(&bound_image_path, systemd_dst)?; |
| 76 | + } |
| 77 | + |
| 78 | + self.units.push(unit_name.to_string()); |
| 79 | + } |
| 80 | + |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | + |
| 84 | + fn restart_systemd(&self) -> Result<()> { |
| 85 | + Task::new_and_run("Reloading systemd", "/usr/bin/systemctl", ["daemon-reload"])?; |
| 86 | + Ok(()) |
| 87 | + } |
| 88 | + |
| 89 | + fn start_new_services(&self) -> Result<()> { |
| 90 | + //TODO: do this in parallel |
| 91 | + for unit in &self.units { |
| 92 | + Task::new_and_run( |
| 93 | + format!("Starting target: {:#}", unit), |
| 94 | + "/usr/bin/systemctl", |
| 95 | + ["start", unit], |
| 96 | + )?; |
| 97 | + } |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl Drop for BoundImageManager { |
| 103 | + //remove the generated units from the root systemd directory |
| 104 | + //and stop them to remove the services from systemd |
| 105 | + fn drop(&mut self) { |
| 106 | + for unit in &self.units { |
| 107 | + //TODO: error handling |
| 108 | + let _ = fs::remove_file(format!("{SYSTEMD_DIR}/{unit}")); |
| 109 | + let _ = Task::new_and_run( |
| 110 | + format!("Starting target: {:#}", unit), |
| 111 | + "/usr/bin/systemctl", |
| 112 | + ["stop", unit], |
| 113 | + ); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments