From 1c0365c73d9fce06d8bb056ee9423cb48794f85c Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Tue, 22 Aug 2006 17:32:00 +0000 Subject: [PATCH] ebl Add new autochanger functions git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@3332 91ce42f0-d328-0410-95d8-f526ca767f89 --- gui/bweb/cgi/bweb.pl | 61 ++++++++----- gui/bweb/lib/Bconsole.pm | 10 +-- gui/bweb/lib/Bweb.pm | 168 ++++++++++++++++++++++++++++++++--- gui/bweb/tpl/config_edit.tpl | 3 + gui/bweb/tpl/config_view.tpl | 2 +- 5 files changed, 201 insertions(+), 43 deletions(-) diff --git a/gui/bweb/cgi/bweb.pl b/gui/bweb/cgi/bweb.pl index ee0b59af77..ed41655b91 100755 --- a/gui/bweb/cgi/bweb.pl +++ b/gui/bweb/cgi/bweb.pl @@ -114,42 +114,56 @@ if ($action eq 'begin') { # main display $bweb->display_medias(); } elsif ($action eq 'eject') { - my $a = Bweb::Autochanger::get('S1_L80', $bweb); + my $arg = $bweb->get_form("ach"); + my $a = Bweb::Autochanger::get($arg->{ach}, $bweb); + + if ($a) { + $a->status(); + foreach my $slot (CGI::param('slot')) { + print $a->{error} unless $a->send_to_io($slot); + } - $a->status(); - foreach my $slot (CGI::param('slot')) { - print $a->{error} unless $a->send_to_io($slot); - } + foreach my $media (CGI::param('media')) { + my $slot = $a->get_media_slot($media); + print $a->{error} unless $a->send_to_io($slot); + } - foreach my $media (CGI::param('media')) { - my $slot = $a->get_media_slot($media); - print $a->{error} unless $a->send_to_io($slot); + $a->display_content(); } - $a->display_content(); - } elsif ($action eq 'eject_media') { $bweb->eject_media(); } elsif ($action eq 'clear_io') { - my $a = Bweb::Autochanger::get('S1_L80', $bweb); - $a->status(); - $a->clear_io(); - $a->display_content(); - + my $arg = $bweb->get_form('ach'); + + my $a = Bweb::Autochanger::get($arg->{ach}, $bweb); + if (defined $a) { + $a->status(); + $a->clear_io(); + $a->display_content(); + } } elsif ($action eq 'ach_view') { # TODO : get autochanger name and create it $bweb->connect_db(); - my $a = Bweb::Autochanger::get('S1_L80', $bweb); - $a->status(); - $a->display_content(); + my $arg = $bweb->get_form('ach'); + + my $a = Bweb::Autochanger::get($arg->{ach}, $bweb); + if ($a) { + $a->status(); + $a->display_content(); + } + +} elsif ($action eq 'ach_add') { + $bweb->ach_add(); } elsif ($action eq 'ach_load') { my $arg = $bweb->get_form('ach', 'drive', 'slot'); + + my $a = Bweb::Autochanger::get($arg->{ach}, $bweb); - if (defined $arg->{ach} and defined $arg->{drive} and defined $arg->{slot}) + if (defined $a and defined $arg->{drive} and defined $arg->{slot}) { - my $a = Bweb::Autochanger::get('S1_L80', $bweb); my $b = new Bconsole(pref => $conf, timeout => 300, log_stdout => 1) ; # TODO : use template here print "
\n";
@@ -163,9 +177,10 @@ if ($action eq 'begin') {		# main display
 } elsif ($action eq 'ach_unload') {
     my $arg = $bweb->get_form('drive', 'slot', 'ach');
 
-    if (defined $arg->{ach} and defined $arg->{drive} and defined $arg->{slot})
+    my $a = Bweb::Autochanger::get($arg->{ach}, $bweb);
+
+    if (defined $a and defined $arg->{drive} and defined $arg->{slot})
     {
-	my $a = Bweb::Autochanger::get('S1_L80', $bweb);
 	my $b = new Bconsole(pref => $conf, timeout => 300, log_stdout => 1) ;
 	# TODO : use template here
 	print "
\n";
@@ -257,8 +272,6 @@ if ($action eq 'begin') {		# main display
 	}
     }
 
-  
-
 } elsif ($action eq 'running') {
     $bweb->display_running_jobs(1);
 
diff --git a/gui/bweb/lib/Bconsole.pm b/gui/bweb/lib/Bconsole.pm
index 9bac7558b6..a23b6f7876 100644
--- a/gui/bweb/lib/Bconsole.pm
+++ b/gui/bweb/lib/Bconsole.pm
@@ -392,17 +392,17 @@ package main;
 print "test sans conio\n";
 
 my $c = new Bconsole(pref => {
-    bconsole => '/usr/local/bacula/sbin/bconsole -c /usr/local/bacula/etc/bconsole.conf',
+    bconsole => '/tmp/bacula/sbin/bconsole -n -c /tmp/bacula/etc/bconsole.conf',
 },
-		     debug => 1);
+		     debug => 0);
 
 print "fileset : ", join(',', $c->list_fileset()), "\n";
 print "job : ",     join(',', $c->list_job()), "\n";
 print "storage : ", join(',', $c->list_storage()), "\n";
 #print "prune : " . $c->prune_volume('000001'), "\n";
 #print "update : " . $c->send_cmd_with_drive('update slots storage=SDLT-1-2'), "\n";
-print "label : ", join(',', $c->label_barcodes(storage => 'SDLT-1-2',
-					       slots => 6,
-					       drive => 0)), "\n";
+#print "label : ", join(',', $c->label_barcodes(storage => 'SDLT-1-2',
+#					       slots => 6,
+#					       drive => 0)), "\n";
 
 
diff --git a/gui/bweb/lib/Bweb.pm b/gui/bweb/lib/Bweb.pm
index 15172036c8..457f47bca5 100644
--- a/gui/bweb/lib/Bweb.pm
+++ b/gui/bweb/lib/Bweb.pm
@@ -203,6 +203,7 @@ our %k_re = ( dbi      => qr/^(dbi:(Pg|mysql):(?:\w+=[\w\d\.-]+;?)+)$/i,
 	      bconsole    => qr!^(.+)?$!,
 	      syslog_file => qr!^(.+)?$!,
 	      log_dir     => qr!^(.+)?$!,
+	      ach_list    => qr!^(.+)?$!,
 	      );
 
 =head1 FUNCTION
@@ -467,20 +468,92 @@ use base q/Bweb::Gui/;
 =cut
 
 # TODO : get autochanger definition from config/dump file
-my %ach_list ;
+my $ach_list ;
 
 sub get
 {
     my ($name, $bweb) = @_;
-    my $a = new Bweb::Autochanger(debug => $bweb->{debug}, 
-				  bweb => $bweb,
-				  name => 'S1_L80',
-				  precmd => 'sudo',
-				  drive_name => ['S1_L80_SDLT0', 'S1_L80_SDLT1'],
-				  );
+    
+    unless ($name) {
+	return $bweb->error("Can't get your autochanger name ach");
+    }
+
+    unless ($ach_list) {
+	unless (get_defined_ach($bweb)) {
+	    return undef;
+	}
+    }
+    
+    my $a = $ach_list->{$name};
+
+    unless ($a) {
+	$bweb->error("Can't get your autochanger $name from your ach_list");
+	return undef;
+    }
+
+    $a->{bweb} = $bweb;
+
     return $a;
 }
 
+sub get_defined_ach
+{
+    my ($bweb) = @_;
+    if (defined $bweb->{info}->{ach_list}) {
+	if (open(FP, "<$bweb->{info}->{ach_list}")) {
+	    my $f=''; my $tmpbuffer;
+	    while(read FP,$tmpbuffer,4096)
+	    {
+		$f .= $tmpbuffer;
+	    }
+	    close(FP);
+	    no strict; # I have no idea of the contents of the file
+	    eval '$ach_list = ' . $f ;
+	    use strict;
+	} else {
+	    return $bweb->error("Can't open $bweb->{info}->{ach_list} $!");
+	}
+    } else {
+	return $bweb->error("Can't find your ach_list file in your configuration");
+    }
+
+    $bweb->debug($ach_list);
+
+    return 1;
+}
+
+sub register
+{
+    my ($ach, $bweb) = @_;
+    my $err;
+
+    if (defined $bweb->{info}->{ach_list})
+    {
+	unless ($ach_list) {
+	    get_defined_ach($bweb) ;
+	}
+
+	$ach_list->{$ach->{name}} = $ach;
+
+	if (open(FP, ">$bweb->{info}->{ach_list}")) {
+	    print FP Data::Dumper::Dumper($ach_list);
+	    close(FP);
+	} else {
+	    $err = $!;
+	    $err .= "\nCan you put this in $bweb->{info}->{ach_list}\n";
+	    $err .= Data::Dumper::Dumper($ach_list);
+	}
+    } else {
+	$err = "ach_list isn't defined";
+    }
+
+    if ($err) {
+	return $bweb->error("Can't find to your ach_list (see bweb configuration) $err");
+    }
+    
+    return 1;
+}
+
 sub new
 {
     my ($class, %arg) = @_;
@@ -1292,7 +1365,12 @@ sub get_form
                  ach    => 1,
                  jobtype=> 1,
 		 );
-
+    my %opt_p = (		# option with path
+		 mtxcmd => 1,
+		 precmd => 1,
+		 device => 1,
+		 );
+    
     foreach my $i (@what) {
 	if (exists $opt_i{$i}) {# integer param
 	    my $value = CGI::param($i) || $opt_i{$i} ;
@@ -1319,6 +1397,11 @@ sub get_form
 	} elsif ($i =~ /^q(\w+)s$/) { #[ 'arg1', 'arg2']
 	    $ret{$i} = [ map { { name => $self->dbh_quote($_) } } 
 			          CGI::param($1) ];
+	} elsif (exists $opt_p{$i}) {
+	    my $value = CGI::param($i) || '';
+	    if ($value =~ /^([\w\d\.\/\s:\@\-]+)$/) {
+		$ret{$i} = $1;
+	    }
 	}
     }
 
@@ -1396,6 +1479,19 @@ FROM Job
 
     }
 
+    if ($what{db_devices}) {
+	my $query = "
+SELECT Device.Name AS name
+FROM Device
+";
+
+	my $devices = $self->dbh_selectall_hashref($query, 'name');
+
+	$ret{db_devices} = [sort {lc($a->{name}) cmp lc($b->{name}) } 
+			       values %$devices] ;
+
+    }
+
     return \%ret;
 }
 
@@ -1991,8 +2087,8 @@ SELECT InChanger     AS online,
        Media.Recycle AS recycle,
        Media.VolRetention AS volretention,
        Media.LastWritten  AS lastwritten,
-       Media.VolReadTime/100000  AS volreadtime,
-       Media.VolWriteTime/100000  AS volwritetime,
+       Media.VolReadTime/1000000  AS volreadtime,
+       Media.VolWriteTime/1000000 AS volwritetime,
        $self->{sql}->{FROM_UNIXTIME}(
           $self->{sql}->{UNIX_TIMESTAMP}(Media.LastWritten) 
         + $self->{sql}->{TO_SEC}(Media.VolRetention)
@@ -2535,6 +2631,11 @@ sub eject_media
     unless ($arg->{jmedias}) {
 	return $self->error("Can't get media selection");
     }
+
+    my $a = Bweb::Autochanger::get($arg->{ach}, $self);
+    unless ($a) {
+	return 0;
+    }
     
     my $query = "
 SELECT Media.VolumeName  AS volumename,
@@ -2549,9 +2650,8 @@ WHERE Media.VolumeName IN ($arg->{jmedias})
 
     my $all = $self->dbh_selectall_hashref($query, 'volumename');
 
-    my $a = Bweb::Autochanger::get('S1_L80', $self);
-
     $a->status();
+
     foreach my $vol (values %$all) {
 	print "eject $vol->{volumename} from $vol->{storage} : ";
 	if ($a->send_to_io($vol->{slot})) {
@@ -2579,6 +2679,47 @@ sub restore
 # TODO : make this internal to not eject tape ?
 use Bconsole;
 
+sub ach_add
+{
+    my ($self) = @_;
+    my $arg = $self->get_form('ach', 'mtxcmd', 'device', 'precmd');
+
+    my $b = new Bconsole(pref => $self->{info});    
+    my @storages = $b->list_storage() ;
+
+    unless ($arg->{ach}) {
+	$arg->{devices} = [ map { { name => $_ } } @storages ];
+	return $self->display($arg, "ach_add.tpl");
+    }
+
+    my @drives ;
+    foreach my $drive (CGI::param('drive'))
+    {
+	unless (grep(/^$drive$/,@storages)) {
+	    return $self->error("Can't find $drive in storage list");
+	}
+
+	my $index = CGI::param("index_$drive");
+	unless (defined $index and $index =~ /^(\d+)$/) {
+	    return $self->error("Can't get $drive index");
+	}
+
+	$drives[$index] = $drive;
+    }
+
+    unless (@drives) {
+	return $self->error("Can't get drives from Autochanger");
+    }
+
+    my $a = new Bweb::Autochanger(name   => $arg->{ach},
+				  precmd => $arg->{precmd},
+				  drive_name => \@drives,
+				  device => $arg->{device},
+				  mtxcmd => $arg->{mtxcmd});
+
+    return Bweb::Autochanger::register($a, $self) ;
+}
+
 sub delete
 {
     my ($self) = @_;
@@ -2671,11 +2812,12 @@ sub label_barcodes
     }
 
     my $slots = '';
+    my $t = 300 ;
     if ($arg->{slots}) {
 	$slots = join(",", @{ $arg->{slots} });
+	$t += 60*scalar( @{ $arg->{slots} }) ;
     }
 
-    my $t = 60*scalar( @{ $arg->{slots} }) + 300 ;
     my $b = new Bconsole(pref => $self->{info}, timeout => $t,log_stdout => 1);
     print "

This command can take long time, be patient...

"; print "
" ;
diff --git a/gui/bweb/tpl/config_edit.tpl b/gui/bweb/tpl/config_edit.tpl
index 8d2e33bdfc..c294659b4e 100644
--- a/gui/bweb/tpl/config_edit.tpl
+++ b/gui/bweb/tpl/config_edit.tpl
@@ -35,6 +35,9 @@
      template_dir : 
            
          
+     ach_list : 
+           
+         
      bconsole : 
            
          
diff --git a/gui/bweb/tpl/config_view.tpl b/gui/bweb/tpl/config_view.tpl
index 37ddc80fe0..695c1a32b2 100644
--- a/gui/bweb/tpl/config_view.tpl
+++ b/gui/bweb/tpl/config_view.tpl
@@ -4,7 +4,6 @@
 
 
- @@ -13,6 +12,7 @@ + -- 2.39.5
SQL connection
SQL Connection
DBI :
user :
email_media :
Bweb Configuration
template_dir :
ach_list :
graph_font :
bconsole :
debug :