]> git.sur5r.net Git - bacula/bacula/blobdiff - gui/bweb/lib/Bweb.pm
ebl update LocationLog table when moving media
[bacula/bacula] / gui / bweb / lib / Bweb.pm
index 2e40950b2c56c82beddab9b77bee555e184a8f0f..ecae6088490639526e09f38c641361f5370d95d5 100644 (file)
@@ -219,16 +219,58 @@ sub load
 {
     my ($self) = @_ ;
 
+    unless (open(FP, $self->{config_file}))
+    {
+       return $self->error("$self->{config_file} : $!");
+    }
+    my $f=''; my $tmpbuffer;
+    while(read FP,$tmpbuffer,4096)
+    {
+       $f .= $tmpbuffer;
+    }
+    close(FP);
+
+    my $VAR1;
+
+    no strict; # I have no idea of the contents of the file
+    eval "$f" ;
+    use strict;
+
+    if ($f and $@) {
+       $self->load_old();
+       $self->save();
+       return $self->error("If you update from an old bweb install, your must reload this page and if it's fail again, you have to configure bweb again...") ;
+    }
+
+    foreach my $k (keys %$VAR1) {
+       $self->{$k} = $VAR1->{$k};
+    }
+
+    return 1;
+}
+
+=head1 FUNCTION
+
+    load_old - load old configuration format
+
+=cut
+
+sub load_old
+{
+    my ($self) = @_ ;
+
     unless (open(FP, $self->{config_file}))
     {
        return $self->error("$self->{config_file} : $!");
     }
 
-    while (my $line = <FP>) 
+    while (my $line = <FP>)
     {
        chomp($line);
        my ($k, $v) = split(/\s*=\s*/, $line, 2);
-       $self->{$k} = $v;
+       if ($k_re{$k}) {
+           $self->{$k} = $v;
+       }
     }
 
     close(FP);
@@ -247,15 +289,13 @@ sub save
 
     unless (open(FP, ">$self->{config_file}"))
     {
-       return $self->error("$self->{config_file} : $!");
-    }
-    
-    foreach my $k (keys %$self)
-    {
-       next unless (exists $k_re{$k}) ;
-       print FP "$k = $self->{$k}\n";
+       return $self->error("$self->{config_file} : $!\n" .
+                           "You must add this to your config file\n" 
+                           . Data::Dumper::Dumper($self));
     }
 
+    print FP Data::Dumper::Dumper($self);
+    
     close(FP);       
     return 1;
 }
@@ -276,8 +316,9 @@ sub edit
 sub view
 {
     my ($self) = @_ ;
-
-    $self->display($self, "config_view.tpl");    
+    $self->{achs} = [ map { { name => $_ } } keys %{$self->{ach_list}} ];
+    $self->display($self, "config_view.tpl");
+    delete $self->{achs};
 }
 
 sub modify
@@ -298,7 +339,7 @@ sub modify
        }
     }
 
-    $self->display($self, "config_view.tpl");
+    $self->view();
 
     if ($self->{error}) {      # an error as occured
        $self->display($self, 'error.tpl');
@@ -466,21 +507,6 @@ use base q/Bweb::Gui/;
 
 =cut
 
-# TODO : get autochanger definition from config/dump file
-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'],
-                                 );
-    return $a;
-}
-
 sub new
 {
     my ($class, %arg) = @_;
@@ -832,6 +858,12 @@ sub transfer
     }
 }
 
+sub get_drive_name
+{
+    my ($self, $index) = @_;
+    return $self->{drive_name}->[$index];
+}
+
 # TODO : do a tapeinfo request to get informations
 sub tapeinfo
 {
@@ -1270,7 +1302,7 @@ sub get_form
                 height => 480,
                 jobid  =>   0,
                 slot   =>   0,
-                drive  =>   undef,
+                drive  =>   0,
                 priority => 10,
                 age    => 60*60*24*7,
                 days   => 1,
@@ -1286,7 +1318,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} ;
@@ -1313,6 +1350,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;
+           }
        }
     }
 
@@ -1377,6 +1419,32 @@ FROM FileSet
 
     }
 
+    if ($what{db_jobnames}) {
+       my $query = "
+SELECT DISTINCT Job.Name AS jobname 
+FROM Job
+";
+
+       my $jobnames = $self->dbh_selectall_hashref($query, 'jobname');
+
+       $ret{db_jobnames} = [sort {lc($a->{jobname}) cmp lc($b->{jobname}) } 
+                              values %$jobnames] ;
+
+    }
+
+    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;
 }
 
@@ -1386,7 +1454,7 @@ sub display_graph
 
     my $fields = $self->get_form(qw/age level status clients filesets 
                                   db_clients limit db_filesets width height
-                                  qclients qfilesets/);
+                                  qclients qfilesets qjobnames db_jobnames/);
                                
 
     my $url = CGI::url(-full => 0,
@@ -1972,8 +2040,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)
@@ -2330,7 +2398,7 @@ INSERT LocationLog (Date, Comment, MediaId, LocationId, NewVolStatus)
        (SELECT VolStatus FROM Media WHERE VolumeName = '$media')
       )
 ";
-       
+       $self->dbh_do($query);
        $self->debug($query);
     }
 
@@ -2516,6 +2584,11 @@ sub eject_media
     unless ($arg->{jmedias}) {
        return $self->error("Can't get media selection");
     }
+
+    my $a = $self->ach_get($arg->{ach});
+    unless ($a) {
+       return 0;
+    }
     
     my $query = "
 SELECT Media.VolumeName  AS volumename,
@@ -2530,9 +2603,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})) {
@@ -2560,6 +2632,130 @@ sub restore
 # TODO : make this internal to not eject tape ?
 use Bconsole;
 
+
+sub ach_get
+{
+    my ($self, $name) = @_;
+    
+    unless ($name) {
+       return $self->error("Can't get your autochanger name ach");
+    }
+
+    unless ($self->{info}->{ach_list}) {
+       return $self->error("Could not find any autochanger");
+    }
+    
+    my $a = $self->{info}->{ach_list}->{$name};
+
+    unless ($a) {
+       $self->error("Can't get your autochanger $name from your ach_list");
+       return undef;
+    }
+
+    $a->{bweb} = $self;
+
+    return $a;
+}
+
+sub ach_register
+{
+    my ($self, $ach) = @_;
+
+    $self->{info}->{ach_list}->{$ach->{name}} = $ach;
+    $self->{info}->save();
+    
+    return 1;
+}
+
+sub ach_edit
+{
+    my ($self) = @_;
+    my $arg = $self->get_form('ach');
+    if (!$arg->{ach} 
+       or !$self->{info}->{ach_list} 
+       or !$self->{info}->{ach_list}->{$arg->{ach}}) 
+    {
+       return $self->error("Can't get autochanger name");
+    }
+
+    my $ach = $self->{info}->{ach_list}->{$arg->{ach}};
+
+    my $i=0;
+    $ach->{drives} = 
+       [ map { { name => $_, index => $i++ } } @{$ach->{drive_name}} ] ;
+
+    my $b = new Bconsole(pref => $self->{info});    
+    my @storages = $b->list_storage() ;
+
+    $ach->{devices} = [ map { { name => $_ } } @storages ];
+    
+    $self->display($ach, "ach_add.tpl");
+    delete $ach->{drives};
+    delete $ach->{devices};
+    return 1;
+}
+
+sub ach_del
+{
+    my ($self) = @_;
+    my $arg = $self->get_form('ach');
+
+    if (!$arg->{ach} 
+       or !$self->{info}->{ach_list} 
+       or !$self->{info}->{ach_list}->{$arg->{ach}}) 
+    {
+       return $self->error("Can't get autochanger name");
+    }
+   
+    delete $self->{info}->{ach_list}->{$arg->{ach}} ;
+   
+    $self->{info}->save();
+    $self->{info}->view();
+}
+
+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('drives'))
+    {
+       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});
+
+    $self->ach_register($a) ;
+    
+    $self->{info}->view();
+}
+
 sub delete
 {
     my ($self) = @_;
@@ -2612,7 +2808,6 @@ SELECT Job.Name as name, Client.Name as clientname
     unless ($row) {
        return $self->error("Can't find $arg->{jobid} in catalog");
     }
-    
 
     $query = "
 SELECT Time AS time, LogText AS log
@@ -2652,11 +2847,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} });
     my $b = new Bconsole(pref => $self->{info}, timeout => $t,log_stdout => 1);
     print "<h1>This command can take long time, be patient...</h1>";
     print "<pre>" ;