# HG changeset patch # User mobst # Date 1233658850 0 # Node ID b3edfead728b1aed833e709bd656fbdb08d99d94 # Parent 80890c874a905e7f8750f06186a1c131b6596ce9 * Added 1; to end of config.pm. * New config option @config::notify_dirs. * Use mercurial/hg to detect filesystem changes diff -r 80890c874a90 -r b3edfead728b config.pm --- a/config.pm Wed Sep 03 19:31:51 2008 +0000 +++ b/config.pm Tue Feb 03 11:00:50 2009 +0000 @@ -1,2 +1,5 @@ package config; @mailto = qw(root@localhost); +@notify_dirs = qw(/etc); + +1; diff -r 80890c874a90 -r b3edfead728b debian/changelog --- a/debian/changelog Wed Sep 03 19:31:51 2008 +0000 +++ b/debian/changelog Tue Feb 03 11:00:50 2009 +0000 @@ -1,3 +1,15 @@ +logbuch (0.25-1) stable; urgency=low + + * fix dependcies + + -- Marcus Obst Tue, 03 Feb 2009 11:49:53 +0100 + +logbuch (0.25) stable; urgency=low + + * new upstream - file modification detection + + -- Marcus Obst Tue, 03 Feb 2009 11:49:53 +0100 + logbuch (0.24) stable; urgency=low * new upstream - long subject lines diff -r 80890c874a90 -r b3edfead728b debian/control --- a/debian/control Wed Sep 03 19:31:51 2008 +0000 +++ b/debian/control Tue Feb 03 11:00:50 2009 +0000 @@ -3,12 +3,13 @@ Priority: optional Maintainer: Heiko Schlittermann Build-Depends: debhelper (>> 3.0.0), libmailtools-perl, libdbi-perl, - libdbd-mysql-perl + libdbd-mysql-perl, libfile-which-perl Standards-Version: 3.5.2 Package: logbuch Architecture: all -Depends: ${perl:Depends}, libdbi-perl, libdbd-mysql-perl, +Suggests: mercurial +Depends: ${perl:Depends}, libdbi-perl, libdbd-mysql-perl libmailtools-perl Description: Logbuch for server maintainance A simple script for tracking server changes in a logfile diff -r 80890c874a90 -r b3edfead728b log.pl --- a/log.pl Wed Sep 03 19:31:51 2008 +0000 +++ b/log.pl Tue Feb 03 11:00:50 2009 +0000 @@ -3,9 +3,10 @@ # $URL$ my $USAGE = <<'#'; $ME [options] - --[no]db insert into log database [$opt_db] - --[no]mail send mails to @config::mailto [$opt_mail] - --message=s message + --[no]db insert into log database [$opt_db] + --[no]mail send mails to @config::mailto [$opt_mail] + --init-dir=s initialize specified directory with VCS repository + --message=s message - if the message starts with '\@' it's considered to be a message FILE. - message can be: apt[:text]\@ - this will @@ -19,6 +20,7 @@ use File::Basename; use File::Temp qw(tempfile); use File::stat; +use File::Which; use Getopt::Long; use Mail::Mailer; use DBI; @@ -51,18 +53,18 @@ my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim"; my $MAGIC = "#--- all changes below are ignored ---#\n"; -my $opt_db = 1; -my $opt_mail = 1; +my $opt_db = 0; +my $opt_mail = 0; my $opt_message = ""; my $opt_apt = ""; - - +my $opt_initdir = ""; my $Dbh; sub identity(); sub hostname(); sub mailto(); +sub check_hg_bin(); MAIN: { @@ -70,6 +72,7 @@ "mail!" => \$opt_mail, "message=s" => \$opt_message, "type=s" => \$opt_apt, + "init-dir=s" => \$opt_initdir, ) or die eval "\"$USAGE\""; if ($opt_message =~ /^@(.*)/) { @@ -91,6 +94,47 @@ $opt_message =~ s/\n/\n /g; } + if (defined @config::notify_dirs || $opt_initdir) { + check_hg_bin(); + } + + if ($opt_initdir) { + print "$ME: Trying to initialize $opt_initdir as mercurial repository.\n"; + -d $opt_initdir or + die "$ME: directory $opt_initdir does not exist!"; + + system("hg status $opt_initdir > /dev/null 2>&1"); + if (($? >> 8) == 0) { + die "$ME: directory already initialized, skipping\n"; + } + + system("hg init $opt_initdir"); + if ( ($? >> 8) != 0) { + die "E: initialization failed\n"; + } else { + system("cd $opt_initdir && hg addremove && hg commit -m 'initial autocommit'"); + print "$ME: initialization done.\n"; + } + + exit 0; + } + + my $hg_status_text = ""; + if (defined @config::notify_dirs) { + foreach my $dir (@config::notify_dirs) { + -d $dir or next; + + print "$ME: Checking $dir for modifications\n"; + -d "$dir/.hg" or + die "$ME: directory $dir not initialized pleas call: \n", + " # $ME --init-dir $dir \n"; + + system("hg addremove $dir"); + + $hg_status_text .= `cd / && hg status $dir`; + } + } + if ($opt_db) { $Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1}) or die $DBI::errstr; @@ -102,6 +146,12 @@ END { unlink $file if $file; } ($fh, $file) = tempfile(DIR => "/tmp"); + my $auto_message = (not $hg_status_text) ? "" : + "\n" + . " Modified config files since last log entry listend below...\n" + . $hg_status_text + . "\n"; + # Kopftext eintragen print $fh "Date: ", scalar(localtime()), "\n", @@ -110,6 +160,7 @@ "\n", " * $opt_message", "\n", + $auto_message, "\n", $MAGIC, "\n"; if (!-e $LOG) { @@ -180,6 +231,14 @@ close $mailer; print STDERR "Mail sent (to $mailto).\n"; } + + if (defined @config::notify_dirs) { + foreach my $dir (@config::notify_dirs) { + -d $dir or next; + + system("cd $dir && hg commit -m 'autocommit by logbuch'"); + } + } } # Und jetzt das aus der alten Datei dort anhängen @@ -212,5 +271,27 @@ return join(", ", @config::mailto); } + +sub check_hg_bin() +{ + if (not defined which('hg')) { + print STDERR << 'EOF'; + +You requested an operation based on hg/mercurial but this tool is +not installed! + +Either you could change the configuration in /etc/lobbuch/config.pm and +remove lines starting with @notify_dirs, or you could simply install the +required packages: + + # aptitude install mercurial + +Exiting! +EOF + exit 1; + } +} + + # vim:sts=4 sw=4 aw ai sm: