Page MenuHomec4science

hpc_delaccounts
No OneTemporary

File Metadata

Created
Mon, Nov 25, 11:38

hpc_delaccounts

#!/usr/bin/perl -w
# cleanup_accounts - delete empty slurm accounts
#
# Author : EPFL-SCITAS
# Date : March 2016
use strict;
use FindBin;
use lib "$FindBin::RealBin/../lib/blib";
use HPCUsers;
use Slurm;
use Log;
use Cmd;
use Cwd 'abs_path';
# -----------------------------------------------------------------------------
# Command line options
#
my %opt;
my $logfile = "hpcusers.log";
sub print_usage() {
print STDERR << "EOF";
Delete all empty slurm accounts.
By default, perform a dry run by only printing commands to be run.
usage: $0 [-g] [-h]
OPTIONS
-g : go for real by applying the required modifications, and log commands in $logfile.
-h : this (help) message
example: $0 -g
EOF
exit;
}
sub init()
{
use Getopt::Std;
my $opt_string = 'hg';
getopts("$opt_string", \%opt) or print_usage();
# pathname of logfile
my $cluster = Slurm::get_cluster_name();
my $logdir = "$FindBin::RealBin/../var/";
if ($cluster ne "" && -e "$logdir") {
$Log::logfile = abs_path($logdir . $cluster . "_" . $logfile);
} else {
$Log::logfile = "/var/log/" . $logfile;
}
print_usage() if $opt{h};
$Cmd::dry_run = 1;
$Cmd::dry_run = 0 if $opt{g};
print_usage() if (scalar @ARGV > 1);
}
# Remove empty slurm accounts
sub cleanup_slurm_accounts()
{
my $nb_iterations = 0;
rescan:
return if ++$nb_iterations > 25; # we never know, to avoid infinite loop
foreach my $accountname (Slurm::all_accounts()) {
if (Slurm::is_account_empty($accountname)) {
Log::output_last_cmd_result() if Slurm::delete_account($accountname);
# if slurm account tree is modified, we need to rescan accounts from scratch
goto rescan if ($Cmd::result{status} eq "reinitialized");
}
}
}
# -----------------------------------------------------------------------------
&init();
&HPCUsers::init();
&cleanup_slurm_accounts();

Event Timeline