Changeset 1490 in ProjectBuilder


Ignore:
Timestamp:
Apr 29, 2012, 7:19:37 PM (12 years ago)
Author:
Bruno Cornec
Message:
  • Fixes in cb to make it work in debug mode at least with package dhcpd.
Location:
projects/casparbuster/devel
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • projects/casparbuster/devel/bin/cb

    r1489 r1490  
    8989use Pod::Usage;
    9090use Data::Dumper;
     91use Time::Local;
     92use Net::SSH2;
    9193use ProjectBuilder::Base;
    9294use ProjectBuilder::Conf;
     
    187189            { RaiseError => 1, AutoCommit => 1 })
    188190            || die "Unable to connect to $db";
     191my $sth;
    189192
    190193if ($precmd ne "") {
    191     my $sth = $dbh->prepare(qq{$precmd}) || die "Unable to create table into $db";
     194    $sth = $dbh->prepare(qq{$precmd}) || die "Unable to create table into $db";
    192195    if ($debug) {
    193196        pb_log(1,"DEBUG: Creating DB $db\n");
     
    224227
    225228# Cleanup
    226 pb_rm_rf($dest);
     229if (not $debug) {
     230    pb_rm_rf($dest);
     231} else {
     232    pb_log(0,"DEBUG: Please remove manually with rm -rf $dest\n")
     233}
    227234$dbh->disconnect;
    228235
     
    237244
    238245# Use potentially a remote account if defined
    239 my $account = undef;
    240 my $remote = $machine;
    241 ($account) = pb_conf_get_if("cbaccount") if (defined $machine);
    242 $remote = $account->{$machine}."@".$machine if ((defined $account) && (defined $account->{$machine}));
    243 pb_log(1, "DEBUG: remote = $remote\n") if (defined $remote);
     246my $remote = undef;
     247my ($account) = pb_conf_get_if("cbaccount") if (defined $machine);
     248$remote = $account->{$machine} if ((defined $account) && (defined $machine) && (defined $account->{$machine}));
     249pb_log(1, "DEBUG: remote account1 = $remote\n") if (defined $remote);
     250$remote = getpwuid($<) if (not defined $remote);
     251pb_log(1, "DEBUG: remote account2 = $remote\n");
    244252
    245253# Now handle plugins if any
     
    298306my $mac = $machine;
    299307$mac = "localhost" if (not defined $machine);
    300 my $all = $dbh->selectall_arrayref("SELECT id,date,file,machine FROM dates WHERE machine=$mac");
    301 # Check what in cbp is in the DB and deploy only if necessary
     308# Allow for errors to occur at DBI level
     309$dbh->{RaiseError} = 0;
     310my $checkdb = 1;
     311my $dbcmd = "SELECT id,date,file,machine FROM dates WHERE machine=\"$mac\"";
     312if (! ($sth = $dbh->prepare(qq{$dbcmd}))) {
     313        pb_log(0,"Unable to prepare DB statement $dbcmd\n");
     314        $checkdb = 0;
     315}
     316# DisAllow for errors to occur at DBI level
     317$dbh->{RaiseError} = 1;
     318my $dbid = ();
     319if ($checkdb == 1) {
     320    $sth->execute();
     321    # Check what in cbp is in the DB and deploy only if necessary
     322    foreach my $k (keys %{$cbp}) {
     323        foreach my $type ('files','dirs') {
     324            foreach my $o (keys %{$cbp->{$k}->{$type}}) {
     325                # Compare with info from DB
     326                foreach my $row ($sth->fetch) {
     327                    next if (not defined $row);
     328                    my ($id, $date, $file, $mac1) = @$row;
     329                    # If less recent than in the DB remove it
     330                    $cbp->{$k}->{$type}->{$o}->{'deleted'} = "true" if ((defined $file) && ($file eq $o) && ($date > $cbp->{$k}->{$type}->{$o}->{'mtime'}));
     331                    $dbid->{$o} = $id;
     332                }
     333            }
     334        }
     335    }
     336    $sth->finish();
     337}
     338pb_log(2,"INFO: cleaned cbp: ".Dumper($cbp)."\n");
     339
     340# Now create a tar containing all the relevant content
     341# We need to loop separately to allow for DB to not exist in the previous loop !
     342my $tdir = undef;
     343if (defined $machine) {
     344    $tdir = "$dest/$machine";
     345} else {
     346    $tdir = "$dest";
     347}
     348chdir("$tdir") || die "ERROR: Unable to chdir to $tdir\n";
     349pb_log(2,"Working now under $tdir\n");
     350
     351my $tar = Archive::Tar->new;
     352$tar->setcwd($tdir);
     353my $curdate = time();
    302354foreach my $k (keys %{$cbp}) {
    303355    foreach my $type ('files','dirs') {
    304         foreach my $o (keys %{$cbp->{$k}->{$type}}) {
    305             # Compare with info from DB
    306             foreach my $row (@$all) {
    307                 my ($id, $date, $file, $mac1) = @$row;
    308                 # If less recent than in the DB remove it
    309                 delete($cbp->{$k}->{$type}->{$o}) if (($file eq $o) && ($date > $cbp->{$k}->{$type}->{$o}->{'mtime'}));
     356        foreach my $o ((keys %{$cbp->{$k}->{$type}})) {
     357            if (not defined $cbp->{$k}->{$type}->{$o}->{'deleted'}) {
     358                $tar->add_files("./$o");
     359                # Add an entry to the DB
     360                if (defined $dbid->{$o}) {
     361                    # Modify an existing entry
     362                    $dbcmd = "UPDATE dates SET date=$curdate,file=$o WHERE id='?'";
     363                    if (not $debug) {
     364                        $sth = $dbh->prepare(qq{$dbcmd});
     365                        $sth = $dbh->execute($dbid->{$o});
     366                    } else {
     367                        pb_log(0,"Executing in DB: $dbcmd with curdate=$curdate,file=$o,id=$dbid->{$o}\n");
     368                    }
     369                } else {
     370                    # Add an new entry
     371                    $dbcmd = "INSERT INTO dates VALUES (NULL,?,?,$mac)";
     372                    if (not $debug) {
     373                        $sth = $dbh->prepare(qq{$dbcmd});
     374                        $sth = $dbh->execute($curdate,$o);
     375                    } else {
     376                        pb_log(0,"Executing in DB: $dbcmd with curdate=$curdate,file=$o,machine=$mac\n");
     377                    }
     378                }
     379                if (not $debug) {
     380                    $sth->finish();
     381                }
    310382            }
    311383        }
    312384    }
    313385}
    314 pb_log(2,"INFO: cleaned cbp: ".Dumper($cbp)."\n");
    315 
    316 # Now create a tar containing all the relevant content
    317 my $tar = Archive::Tar->new;
     386my $tarfile = "$ENV{'TMPDIR'}/cbcontent$$.tar";
     387$tar->write("$tarfile");
     388
     389my $ssh2;
     390my $chan;
     391
     392# deal with content first
     393if (defined $machine) {
     394    # Create remote connection and copy tar file there
     395    $ssh2 = Net::SSH2->new();
     396    $ssh2->connect($machine);
     397    my $hdir = (getpwnam(getpwuid($<)))[7];
     398    if ($ssh2->auth_publickey($remote,"$hdir/.ssh/id_dsa.pub","$hdir/.ssh/id_dsa")) {
     399        $chan = $ssh2->channel();
     400        if (not $debug) {
     401            $chan->exec("if [ ! -d $ENV{'TMPDIR'} ]; then mkdir -p $ENV{'TMPDIR'} fi");
     402            $ssh2->scp_put($tarfile,$ENV{'TMPDIR'});
     403            #$chan->exec("cd / ; tar xf $tarfile");
     404        } else {
     405            pb_log(1,"INFO: creating $ENV{'TMPDIR'} if needed on $remote\@$machine and copying content\n");
     406        }
     407    } else {
     408        pb_log(0,"ERROR: Unable to authenticate to $remote\@$machine\n");
     409        return;
     410    }
     411}
     412
     413# Pointer to function depending whether we're local or remote
     414my $func;
     415if (defined $machine) {
     416    $func = \&{ $chan->exec };
     417} else {
     418    $func = \&pb_system;
     419}
     420
     421if (not $debug) {
     422    &$func("cd / ; tar xf $tarfile");
     423} else {
     424    pb_log(1,"INFO: Extracting (on $mac) $tarfile under /\n");
     425}
     426
    318427foreach my $k (keys %{$cbp}) {
    319     $tar->add_files((keys %{$cbp->{$k}->{'files'}}),(keys %{$cbp->{$k}->{'dirs'}}));
    320     # Add an entryo to the DB
    321 }
    322 my $tarfile = "$ENV{'TMPDIR'}/content$$.tar";
    323 $tar->write("$tarfile");
    324 
    325 my $cmd = "";
    326 # Copy the tar file and extract it + set up modes/uids/gids
     428    foreach my $type ('files','dirs') {
     429        foreach my $o ((keys %{$cbp->{$k}->{$type}})) {
     430            if (not defined $cbp->{$k}->{$type}->{$o}->{'deleted'}) {
     431                if ($debug) {
     432                    pb_log(1,"INFO: Executing (on $mac) sudo chown $cbp->{$k}->{$type}->{$o}->{'uid'}:$cbp->{$k}->{$type}->{$o}->{'gid'} $o\n");
     433                    pb_log(1,"INFO: Executing (on $mac) sudo chmod $cbp->{$k}->{$type}->{$o}->{'mode'} $o\n");
     434                } else {
     435                    &$func("sudo chown $cbp->{$k}->{$type}->{$o}->{'uid'}:$cbp->{$k}->{$type}->{$o}->{'gid'} $o");
     436                    &$func("sudo chmod $cbp->{$k}->{$type}->{$o}->{'mode'} $o");
     437                }
     438            }
     439        }
     440    }
     441    if (defined $cbp->{$k}->{'reloadscript'}) {
     442        if ($debug) {
     443            pb_log(1,"INFO: Executing (on $mac) sudo $cbp->{$k}->{'reloadscript'}\n");
     444        } else {
     445            &$func("sudo $cbp->{$k}->{'reloadscript'}");
     446        }
     447    }
     448}
     449
    327450if (defined $machine) {
    328     $cmd = "scp -p -q $tarfile $remote:/tmp";
    329 } else {
    330     $cmd = "cp -p $source /tmp";
    331 }
    332 
    333 if ($debug) {
    334     pb_log(1,"DEBUG: launching $cmd\n");
    335 } else {
    336     pb_system($cmd);
    337     pb_log(0,"INFO: \n");
     451    # Remote cleanup
     452    $chan->exec("rm -rf $ENV{'TMPDIR'}");
     453    $ssh2->disconnect();
     454}
     455
     456# Cleanup
     457if (not $debug) {
     458    unlink("$tarfile");
     459} else {
     460    pb_log(0,"DEBUG: Please remove manually with rm -f $tarfile\n")
    338461}
    339462pb_log(2,"Exiting cb_distribute\n");
     
    342465sub cb_add_to_cbp {
    343466
     467pb_log(3,"Entering into cb_addto_cbp\n");
    344468my $type = 'files';
    345469if (-d $File::Find::name) {
  • projects/casparbuster/devel/lib/CasparBuster/Plugin.pm

    r1489 r1490  
    7777my $cbp = shift;
    7878
     79pb_log(2,"Entering cb_plugin_get for plugin $plugin\n");
    7980my ($flist,$dlist,$slist) = pb_conf_get_if("cbpluginfiles","cbplugindirs","cbpluginreload");
    8081if ((defined $flist) && (defined $flist->{$plugin}) && ($flist->{$plugin} !~ /^\s*$/)) {
Note: See TracChangeset for help on using the changeset viewer.