Add a deactivate subcommand.

This commit is contained in:
R-VdP 2023-02-16 16:43:05 +01:00
parent e2f386c21e
commit e28ba15456
No known key found for this signature in database
4 changed files with 47 additions and 0 deletions

View file

@ -15,3 +15,10 @@ pub fn activate(store_path: StorePath, ephemeral: bool) -> Result<()> {
services::activate(store_path, ephemeral)?;
Ok(())
}
pub fn deactivate() -> Result<()> {
log::info!("Deactivating system-manager");
etc_files::deactivate()?;
services::deactivate()?;
Ok(())
}

View file

@ -50,6 +50,14 @@ pub fn activate(store_path: StorePath, ephemeral: bool) -> Result<()> {
.into_iter()
.try_for_each(|(name, entry)| create_etc_link(&name, &entry, &etc_dir))?;
// TODO register the crated files (including .system-manager-static) in a
// state file, and clean up old files from the previous generation
Ok(())
}
// TODO we need to record in a state file which files we created and then remove them
pub fn deactivate() -> Result<()> {
Ok(())
}

View file

@ -83,6 +83,29 @@ pub fn activate(store_path: StorePath, ephemeral: bool) -> Result<()> {
Ok(())
}
pub fn deactivate() -> Result<()> {
let old_linked_services = read_linked_services()?;
log::debug!("{:?}", old_linked_services);
serialise_linked_services(&HashMap::new())?;
let service_manager = systemd::ServiceManager::new_session()?;
let timeout = Some(Duration::from_secs(30));
// We need to do this before we reload the systemd daemon, so that the daemon
// still knows about these units.
stop_services(&service_manager, &old_linked_services, &timeout)?;
unlink_services(&old_linked_services)?;
// We added all new services and removed old ones, so let's reload the units
// to tell systemd about them.
log::info!("Reloading the systemd daemon...");
service_manager.daemon_reload()?;
log::info!("Done");
Ok(())
}
fn unlink_services(services: &LinkedServices) -> Result<()> {
services
.values()

View file

@ -41,6 +41,7 @@ enum Action {
#[command(flatten)]
build_args: BuildArgs,
},
Deactivate,
/// Generate a new system-manager generation
Generate {
#[command(flatten)]
@ -75,6 +76,10 @@ fn go(action: Action) -> Result<()> {
Action::Build {
build_args: BuildArgs { flake_uri },
} => build(flake_uri),
Action::Deactivate => {
check_root()?;
deactivate()
}
Action::Generate {
build_args: BuildArgs { flake_uri },
} => {
@ -110,6 +115,10 @@ fn activate(store_path: StorePath, ephemeral: bool) -> Result<()> {
system_manager::activate::activate(store_path, ephemeral)
}
fn deactivate() -> Result<()> {
system_manager::activate::deactivate()
}
fn check_root() -> Result<()> {
if !nix::unistd::Uid::is_root(nix::unistd::getuid()) {
anyhow::bail!("We need root permissions.")