feat: Add delete-old flag to automatically remove old generations

Signed-off-by: James Alseth <james@jalseth.me>
This commit is contained in:
James Alseth 2024-02-17 19:16:18 -08:00
parent c84ccd0a7a
commit d7bc9fff3c
4 changed files with 39 additions and 0 deletions

View File

@ -71,6 +71,12 @@ To upload keys without building or deploying the rest of the configuration, use
long_help = "Reboots nodes after activation and waits for them to come back up."
)]
reboot: bool,
#[arg(
long,
help = "Delete old NixOS generations.",
long_help = "Automatically delete previous NixOS generation after a succesful deployment. If the reboot flag is enabled, this is performed after the reboot."
)]
delete_old: bool,
#[arg(
long,
alias = "no-substitutes",
@ -153,6 +159,7 @@ pub async fn run(hive: Hive, opts: Opts) -> Result<(), ColmenaError> {
verbose,
no_keys,
reboot,
delete_old,
no_substitute,
no_gzip,
build_on_target,
@ -195,6 +202,7 @@ pub async fn run(hive: Hive, opts: Opts) -> Result<(), ColmenaError> {
options.set_gzip(!no_gzip);
options.set_upload_keys(!no_keys);
options.set_reboot(reboot);
options.set_delete_old(delete_old);
options.set_force_replace_unknown_profiles(force_replace_unknown_profiles);
options.set_evaluator(evaluator);

View File

@ -101,6 +101,9 @@ pub enum JobType {
/// Rebooting a host.
Reboot,
/// Delete old NixOS generations.
DeleteOld,
}
/// A handle to a job.

View File

@ -671,6 +671,26 @@ impl Deployment {
target
};
// Delete old generations
let mut target = if self.options.delete_old {
let job = parent.create_job(JobType::DeleteOld, nodes.clone())?;
job.run_waiting(|job| async move {
job.state(JobState::Running)?;
job.message("Deleting old NixOS generations...".to_string())?;
let host = target.host.as_mut().unwrap();
host.set_job(Some(job.clone()));
host.run_command(&["nix-collect-garbage", "--delete-old"])
.await?;
job.success_with_message("Deleted old NixOS generations.".to_string())?;
Ok(target)
})
.await?
} else {
target
};
// Upload post-activation keys
if self.options.upload_keys {
let job = parent.create_job(JobType::UploadKeys, nodes.clone())?;

View File

@ -19,6 +19,9 @@ pub struct Options {
/// Whether to reboot the hosts after activation.
pub(super) reboot: bool,
/// Whether to automatically delete previous NixOS generations.
pub(super) delete_old: bool,
/// Whether to create GC roots for node profiles.
///
/// If true, .gc_roots will be created under the hive's context
@ -69,6 +72,10 @@ impl Options {
self.reboot = enable;
}
pub fn set_delete_old(&mut self, enable: bool) {
self.delete_old = enable;
}
pub fn set_create_gc_roots(&mut self, enable: bool) {
self.create_gc_roots = enable;
}
@ -101,6 +108,7 @@ impl Default for Options {
gzip: true,
upload_keys: true,
reboot: false,
delete_old: false,
create_gc_roots: false,
force_build_on_target: None,
force_replace_unknown_profiles: false,