rel-eng/packages/spacewalk-utils | 2
utils/Makefile | 5 -
utils/delete-old-systems-interactive | 166 +++++++++++++++++++++++++++++++++++
utils/spacewalk-utils.spec | 5 -
4 files changed, 174 insertions(+), 4 deletions(-)
New commits:
commit 430f730f2861cd5543b8d621f4290e9cd578b53e
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Thu Apr 1 09:53:32 2010 +0200
Automatic commit of package [spacewalk-utils] release [0.9.6-1].
diff --git a/rel-eng/packages/spacewalk-utils b/rel-eng/packages/spacewalk-utils
index c78bdbe..9a927c9 100644
--- a/rel-eng/packages/spacewalk-utils
+++ b/rel-eng/packages/spacewalk-utils
@@ -1 +1 @@
-0.9.5-1 utils/
+0.9.6-1 utils/
diff --git a/utils/spacewalk-utils.spec b/utils/spacewalk-utils.spec
index 159b568..9a0b9fb 100644
--- a/utils/spacewalk-utils.spec
+++ b/utils/spacewalk-utils.spec
@@ -1,7 +1,7 @@
%define rhnroot %{_prefix}/share/rhn
Name: spacewalk-utils
-Version: 0.9.5
+Version: 0.9.6
Release: 1%{?dist}
Summary: Utilities that may be run against a Spacewalk server.
@@ -57,6 +57,9 @@ rm -rf $RPM_BUILD_ROOT
%changelog
+* Thu Apr 01 2010 Miroslav Suchý <msuchy(a)redhat.com> 0.9.6-1
+- add script delete-old-systems-interactive
+
* Tue Mar 16 2010 Michael Mraka <michael.mraka(a)redhat.com> 0.9.5-1
- added repo urls and gpg keys to spacewalk-common-channel.ini
commit b97c3c5a84790cfd4ed00764db7e8822dfe2a3b2
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Thu Apr 1 09:47:50 2010 +0200
allow to store password in file for other users than admin
diff --git a/utils/delete-old-systems-interactive b/utils/delete-old-systems-interactive
index 7139c89..22920c2 100755
--- a/utils/delete-old-systems-interactive
+++ b/utils/delete-old-systems-interactive
@@ -9,12 +9,6 @@ use Frontier::Client;
my $idle_time;
my ($host, $username) = ( 'localhost', 'admin' );
my $passwd;
-my $passwdfile = "/etc/rhn/$username-password";
-open PASSWD, $passwdfile or die "Error reading password file [$passwdfile]:
$!\n";
-$passwd = <PASSWD>;
-chomp $passwd;
-close PASSWD;
-
my $force;
Getopt::Long::GetOptions(
@@ -29,6 +23,14 @@ if (not defined $idle_time) {
die "Need --idle parameter\n";
}
+if (not defined $passwd) {
+ my $passwdfile = "/etc/rhn/$username-password";
+ open PASSWD, $passwdfile or die "Error reading password file [$passwdfile]:
$!\n";
+ $passwd = <PASSWD>;
+ chomp $passwd;
+ close PASSWD;
+}
+
my ($t, $w) = ($idle_time =~ /^(\d+)(\D)$/);
if (not defined $w) {
commit 14778a668790b01c11a134ca1762153905bcd17d
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Thu Apr 1 09:38:42 2010 +0200
add man page for delete-old-systems-interactive
diff --git a/utils/Makefile b/utils/Makefile
index d89ed69..a493f1a 100644
--- a/utils/Makefile
+++ b/utils/Makefile
@@ -33,8 +33,9 @@ ifneq ($(DOCBOOK),)
# install man pages
all :: $(MANS)
pod2man --section=8 spacewalk-api spacewalk-api.8
+ pod2man --section=8 delete-old-systems-interactive delete-old-systems-interactive.8
install :: $(MANS) $(PREFIX)/$(MANDIR)
- $(INSTALL_DATA) $(MANS) spacewalk-api.8* $(PREFIX)/$(MANDIR)/man8
+ $(INSTALL_DATA) $(MANS) spacewalk-api.8* delete-old-systems-interactive.8
$(PREFIX)/$(MANDIR)/man8
install :: $(CONFIGS) $(PREFIX)/$(CONFDIR)
$(INSTALL_DATA) $(CONFIGS) $(PREFIX)/$(CONFDIR)
endif
diff --git a/utils/delete-old-systems-interactive b/utils/delete-old-systems-interactive
index 6c9439a..7139c89 100755
--- a/utils/delete-old-systems-interactive
+++ b/utils/delete-old-systems-interactive
@@ -99,3 +99,66 @@ if (not $client->call('system.delete_systems', $session,
[map $_->{id}, @$system
} else {
print "All systems deleted\n";
}
+
+=pod
+
+=head1 NAME
+
+delete-old-systems-interactive - delete inactive systems from Spacewalk server.
+
+=head1 SYNOPSIS
+
+delete-old-systems-interactive [OPTIONS] --idle time_delta
+
+=head1 DESCRIPTION
+
+delete-old-systems-interactive - will use Spacewalk API to determine when each registred
system checked in
+last time and optionaly will delete systems which have been inactive for time_delta.
+
+This script will list all registred systems and their last check in. And will if none
system is idle more
+then time_delta, it will write summary:
+
+ Total systems [127], none idle
+
+If some systems are idle more then time_delta, you will get summary like:
+
+ Total systems [127], would delete [51]
+
+None system is deleted by default, unless you specify --force option.
+
+=head1 OPTIONS
+
+--idle time_delta
+ Search for system, which are inactive for more then time_delta.
+ If only number is specified, it is interpreted as days. You can use suffixes m
(for minutes),
+ h (hours), d (days) and w (weeks). E.g. "--idle 10" and "--idle
10d" is the same and means
+ ten days, "--idle 2w" means two weeks.
+
+--host host
+ Hostname of you Spacewalk server. If not set, localhost is used by default.
+
+--username login
+ Your username. By default "admin".
+
+--password pass
+ Your password. If not specified on command line, then it is read from
/etc/rhn/$username-password
+
+--force
+ If specified, then idle systems are deleted.
+
+=head1 EXAMPLES
+
+ delete-old-systems-interactive --server=spacewalk.com --idle 30d
+
+=head1 AUTHOR
+
+Miroslav Suchy <msuchy(a)redhat.com>,
+Jan Pazdziora <jpazdziora(a)redhat.com>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (c) 2009 Red Hat, Inc.
+Released under GNU General Public License, version 2 (GPLv2).
+
+=cut
+
commit fb8ee30b9726a07c4dc5225819a045d54a87cc04
Author: Miroslav Suchý <msuchy(a)redhat.com>
Date: Thu Apr 1 09:13:12 2010 +0200
add script for deleting inactive systems
diff --git a/utils/Makefile b/utils/Makefile
index 821d310..d89ed69 100644
--- a/utils/Makefile
+++ b/utils/Makefile
@@ -8,7 +8,7 @@ SUBDIR = utils
FILES = __init__ systemSnapshot migrateSystemProfile
SCRIPTS = sw-system-snapshot migrate-system-profile spacewalk-api apply_errata \
- scr.cgi spacewalk-common-channels
+ scr.cgi spacewalk-common-channels delete-old-systems-interactive
CONFIGS = spacewalk-common-channels.ini
DOCBOOK = $(wildcard /usr/bin/docbook2man)
diff --git a/utils/delete-old-systems-interactive b/utils/delete-old-systems-interactive
new file mode 100755
index 0000000..6c9439a
--- /dev/null
+++ b/utils/delete-old-systems-interactive
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings FATAL => 'all';
+
+use Getopt::Long ();
+use Frontier::Client;
+
+my $idle_time;
+my ($host, $username) = ( 'localhost', 'admin' );
+my $passwd;
+my $passwdfile = "/etc/rhn/$username-password";
+open PASSWD, $passwdfile or die "Error reading password file [$passwdfile]:
$!\n";
+$passwd = <PASSWD>;
+chomp $passwd;
+close PASSWD;
+
+my $force;
+
+Getopt::Long::GetOptions(
+ 'idle=s' => \$idle_time,
+ 'host=s' => \$host,
+ 'username=s' => \$username,
+ 'password=s' => \$passwd,
+ 'force' => \$force,
+);
+
+if (not defined $idle_time) {
+ die "Need --idle parameter\n";
+}
+
+my ($t, $w) = ($idle_time =~ /^(\d+)(\D)$/);
+
+if (not defined $w) {
+ $t = $idle_time;
+ $w = 'd';
+}
+if ($w eq 'm') { $idle_time = $t * 60; }
+elsif ($w eq 'h') { $idle_time = $t * 60 * 60; }
+elsif ($w eq 'd') { $idle_time = $t * 60 * 60 * 24; }
+elsif ($w eq 'w') { $idle_time = $t * 60 * 60 * 24 * 7; }
+else {
+ die "Unknown idle parameter [$idle_time]\n";
+}
+
+my $not_before = time - $idle_time;
+my ($sec, $min, $h, $d, $m, $y) = gmtime($not_before);
+my $cutoff = sprintf '%04d%02d%02dT%02d:%02d:%02d',
+ $y + 1900,$m + 1, $d, $h, $min, $sec;
+print "Lookup on [$host] systems with last checkin before [$cutoff] GMT\n";
+
+my $client = new Frontier::Client(url => "http://$host/rpc/api",
debug=>0);
+my $session = $client->call('auth.login', $username, $passwd)
+ or die "Failed to login to [$host]\n";
+
+my $systems = $client->call('system.list_user_systems', $session);
+$systems = [
+ sort { $a->{last_checkin} cmp $b->{last_checkin} }
+ map { if (ref $_->{last_checkin}) {
+ $_->{last_checkin} = $_->{last_checkin}->value
+ } else {
+ $_->{last_checkin} =~ s/-//g;
+ }; $_;} @$systems
+];
+
+my $delete = 0;
+my $total = @$systems;
+$cutoff =~ s/-//g;
+$cutoff =~ tr/T/ /;
+for (my $i = 0; $i < @$systems; $i++) {
+ my $sys = $systems->[$i];
+ my $sys_date = $sys->{last_checkin};
+ if (not $sys_date =~ / /) {
+ $sys_date .= ' 23:59:59';
+ }
+ print "System [$sys->{name}] id [$sys->{id}] last checking
[$sys->{last_checkin}] ";
+ if ($sys_date lt $cutoff) {
+ print " -> delete";
+ $delete ++;
+ } else {
+ splice @$systems, $i, 1;
+ $i--;
+ }
+ print "\n";
+}
+if (not $delete ) {
+ print "Total systems [$total], none idle\n";
+ exit;
+}
+if (not $force) {
+ print "Total systems [$total], would delete [$delete]\n";
+ exit;
+}
+
+print "Total systems [$total], will delete [$delete]\n";
+
+if (not $client->call('system.delete_systems', $session, [map $_->{id},
@$systems ])) {
+ print "Error deleting system [sys->{id}]\n";
+} else {
+ print "All systems deleted\n";
+}