Changeset 1668 in ProjectBuilder for projects/casparbuster


Ignore:
Timestamp:
Oct 28, 2012, 9:22:41 PM (11 years ago)
Author:
Bruno Cornec
Message:
  • First working version of cbusterize (used for couperin & shorewall)
File:
1 edited

Legend:

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

    r1665 r1668  
    109109use CasparBuster::Env;
    110110use CasparBuster::Plugin;
     111use CasparBuster::SSH;
    111112use File::Basename;
    112113use File::Path;
     
    139140my $appname = "cb";
    140141$ENV{'PBPROJ'} = $appname;
    141 pb_temp_init();
    142142
    143143# Initialize the syntax string
     
    174174$pbdebug = $debug;
    175175pb_log_init($debug, $LOG);
    176 pb_log(0,"Starting cbusterize\n");
     176pb_temp_init();
     177pb_log(1,"Starting cbusterize\n");
    177178
    178179# Get conf file in context
     
    203204pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\n");
    204205pb_log(1, "DEBUG: source = $source\n") if (defined $source);
    205 pb_log(1, "DEBUG: machine = $machine\n") if (defined $machine);
     206pb_log(1, "DEBUG: machine = $machine\n");
    206207
    207208# Use potentially a remote account if defined
    208209my $account = undef;
    209210my $remote = undef;
    210 ($account) = pb_conf_get_if("cbaccount") if (defined $machine);
    211 $remote = $account->{$machine} if ((defined $account) && (defined $machine) && (defined $account->{$machine}));
     211($account) = pb_conf_get_if("cbaccount");
     212$remote = $account->{$machine} if ((defined $account) && (defined $account->{$machine}));
    212213pb_log(1, "DEBUG: remote account1 = $remote\n") if (defined $remote);
    213214$remote = getpwuid($<) if (not defined $remote);
     
    246247}
    247248
    248 $ssh2->disconnect() if (defined $machine);
     249cb_ssh_close($ssh2);
    249250
    250251sub cb_busterize {
     
    257258my $srcdir = undef;
    258259my $srcfile = undef;
    259 my $cmd = undef;
    260 my $sftp;
    261 
    262 if (not defined $machine) {
    263     if (-d $source) {
    264         $srcdir = $source;
    265     } else {
    266         $srcdir = dirname($source);
    267         $srcfile = basename($source);
    268     }
     260my $realsrc = "";
     261my $type = "";
     262
     263$ssh2 = cb_ssh_init($remote,$machine,$debug) if (not defined $ssh2);
     264
     265$chan = $ssh2->channel();
     266pb_log(3,"DEBUG: SSH2 chan called\n");
     267confess "Unable to create channel for $remote\@$machine: $!" if (not defined $chan);
     268if ($debug) {
     269    pb_log(1,"DEBUG: launching a shell via Net:SSH2 ($remote\@$machine)\n");
     270}
     271confess "Unable to launch remote shell through Net:SSH2 ($remote\@$machine)" if (not $chan->shell());
     272pb_log(3,"DEBUG: SSH2 shell called\n");
     273
     274pb_log(2,"DEBUG: Calling readlink -f $source\n");
     275print $chan "readlink -f $source\n";
     276while (<$chan>) {
     277    $realsrc = $_;
     278    chomp($realsrc);
     279    pb_log(3,"DEBUG: Found $realsrc");
     280}
     281pb_log(2,"DEBUG: Found realsrc = $realsrc\n");
     282print $chan "LANGUAGE=C stat -c '%F' $realsrc\n";
     283pb_log(2,"DEBUG: Calling LANGUAGE=C stat -c '%F' $realsrc\n");
     284while (<$chan>) {
     285    $type = $_;
     286    chomp($type);
     287    pb_log(3,"DEBUG: Found $type");
     288}
     289pb_log(2,"DEBUG: Found type = $type\n");
     290if ($type =~ /directory/) {
     291    $srcdir = $source;
     292    pb_log(1,"DEBUG: Found remote dir = $source\n");
    269293} else {
    270     if (not defined $ssh2) {
    271         pb_log(1,"DEBUG: First time so we need to create the SSH::Net2 object\n");
    272         $ssh2 = Net::SSH2->new();
    273         if ($debug >= 3) {
    274             $ssh2->debug(1);
    275         }
    276         $ssh2->connect($machine) || confess "Unable to connect to $remote\@$machine: $!";
    277         my $hdir = (getpwnam(getpwuid($<)))[7];
    278         confess "Unable to connect to $remote\@$machine: $!" if (not $ssh2->auth_publickey($remote,"$hdir/.ssh/id_dsa.pub","$hdir/.ssh/id_dsa"));
    279         $chan = $ssh2->channel();
    280         die "Unable to create channel for $remote\@$machine: $!" if (not defined $chan);
    281         my ($code, $error_name, $error_string) = $ssh2->error();
    282         if ($code ne 0) {
    283             pb_log(0,"code = $code");
    284             pb_log(0,"error_name = $error_name");
    285             pb_log(0,"error_string = $error_string");
    286         }
    287         #$ssh2->blocking(0);
    288         if ($debug) {
    289             pb_log(1,"DEBUG: launching a shell via Net:SSH2 ($remote\@$machine)");
    290         }
    291         confess "Unable to launch remote shell through Net:SSH2 ($remote\@$machine)" if (not $chan->shell());
    292     }
    293     $sftp = $ssh2->sftp;
    294     die "Unable to create sftp channel for $remote\@$machine: $!" if (not defined $sftp);
    295     my %dirs = $sftp->stat("$source/.");
    296     my $res = 0;
    297     $res = -1 if (not defined $dirs{'mode'});
    298     pb_log(2,"DEBUG: Found res = $res\n");
    299     if ($res == 0) {
    300         $srcdir = $source;
    301         pb_log(1,"DEBUG: Found remote dir = $source\n");
    302     } else {
    303         $srcdir = dirname($source);
    304         $srcfile = basename($source);
    305         pb_log(1,"DEBUG: Found remote file = $source\n");
    306     }
     294    # File or something else potentially
     295    $srcdir = dirname($source);
     296    $srcfile = basename($source);
     297    pb_log(1,"DEBUG: Found remote file = $source\n");
    307298}
    308299
     
    314305}
    315306
     307my $scheme = $cb->{'cms'}->{$appname};
     308
    316309# Deduce the target directory from the local structure and the source
    317 my $target = $basedir;
    318 $target .= "/$machine" if defined ($machine);
    319 $target .= "$srcdir";
    320 
    321 my $scheme = $cb->{'cms'}->{$appname};
     310my $target = "$basedir/$machine/$srcdir";
    322311
    323312# If both source and target are dirs, then copy into the parent of the target
    324 $target = basename($target) if ((not defined $srcfile) && (-d $target));
     313#$target = basename($target) if ((not defined $srcfile) && (-d $target));
    325314
    326315# Create target if it doesn't exist
     
    331320        pb_mkdir_p($target) || confess "Unable to recursively create $target: $!";
    332321    }
    333     # Add all the dirs in it to VCS (in reverse order)
     322    # Add all the dirs in it to VCS (in reverse order) if not already in it
    334323    my $tdir = $target;
    335324    my @tab = ();
     
    339328        pb_log(3,"tdir is now $tdir\n");
    340329    }
    341     if ($debug) {
    342         pb_log(0,"INFO: Added to your $scheme system the dirs: ".join(' ',reverse(@tab))."\n");
    343     } else {
    344         pb_vcs_add($scheme,reverse(@tab));
    345     }
    346 }
    347 
    348 # For local handling
    349 my $cmdopt = "";
    350 # Recursive if we copy dirs
    351 $cmdopt = "-r" if (not defined $srcfile);
    352 $cmd = "cp -p $cmdopt $source $target";
     330    if (not $debug) {
     331        pb_vcs_add_if_not_in($scheme,reverse(@tab));
     332    }
     333    pb_log(0,"INFO: Added to your $scheme system the dirs: ".join(' ',reverse(@tab))."\n");
     334}
    353335
    354336if (defined $srcfile) {
    355337    # File case
    356338    if ((! -f "$target/$srcfile") || (defined $force)) {
    357         # doesn't exist locally
    358         if (defined $machine) {
    359             # Remote
    360             cb_ssh_do($remote,$source,$target,$debug);
    361         } else {
    362             # Local
    363             if ($debug) {
    364                 pb_log(1,"DEBUG: launching $cmd\n");
    365             } else {
    366                 pb_system($cmd);
    367             }
    368         }
    369         if ($debug) {
    370             pb_log(1,"DEBUG: Adding $target/$srcfile to your $scheme system\n");
    371         } else {
    372             pb_vcs_add($scheme,"$target/$srcfile");
    373             pb_log(0,"INFO: Created $target/$srcfile and added it to your $scheme system\n");
    374         }
     339        $ssh2->scp_put($source,"$target/$srcfile");
     340        #cb_ssh_do($remote,$machine,$source,"$basedir/$machine",$debug,$ssh2,$chan);
     341       
     342        if (not $debug) {
     343            pb_vcs_add_if_not_in($scheme,"$target/$srcfile");
     344        }
     345        pb_log(0,"INFO: Created $target/$srcfile and added it to your $scheme system if needed\n");
    375346    } else {
    376         pb_log(0,"INFO: File $target/$srcfile already there\n");
     347        pb_log(0,"INFO: File $target/$srcfile already there. Will not add without forcing with -f\n");
    377348    }
    378349} else {
     
    380351    if ($recur eq "true") {
    381352        # with files in it
    382         if ((! -d "$target") || (defined $force)) {
    383             # doesn't exist locally
    384             if (defined $machine) {
    385                 # Remote
    386                 cb_ssh_do($remote,$source,$target,$debug);
    387             } else {
    388                 # Local
    389                 if ($debug) {
    390                     pb_log(1,"DEBUG: launching $cmd\n");
     353        # TODO: if sudo asks a passwd it won't work.
     354        my $cmd = "sudo tar cvhf /tmp/cb.$$.tar $source\n";
     355        if ($debug) {
     356            pb_log(1,"DEBUG: launching through Net:SSH2 ($remote\@$machine) $cmd");
     357        }
     358        print $chan "$cmd";
     359        pb_log(1,"DEBUG: LINE : $_") while <$chan>;
     360        $cmd = "sudo chmod 600 /tmp/cb.$$.tar\n";
     361        if ($debug) {
     362            pb_log(1,"DEBUG: launching through Net:SSH2 ($remote\@$machine) $cmd");
     363        }
     364        print $chan "$cmd";
     365        pb_log(1,"DEBUG: LINE : $_") while <$chan>;
     366        $cmd = "sudo chown $remote /tmp/cb.$$.tar\n";
     367        if ($debug) {
     368            pb_log(1,"DEBUG: launching through Net:SSH2 ($remote\@$machine) $cmd");
     369        }
     370        print $chan "$cmd";
     371        pb_log(1,"DEBUG: LINE : $_") while <$chan>;
     372
     373        if ($debug) {
     374            pb_log(1,"DEBUG: gettting through Net:SSH2 ($remote\@$machine) /tmp/cb.$$.tar\n");
     375        }
     376
     377        $ssh2->scp_get("/tmp/cb.$$.tar","/tmp/cb.$$.tar");
     378
     379        if ($debug) {
     380            pb_log(1,"DEBUG: erasing through Net:SSH2 ($remote\@$machine) /tmp/cb.$$.tar\n");
     381        } else {
     382            print $chan "sudo rm -f /tmp/cb.$$.tar\n";
     383        }
     384        my $tar = Archive::Tar->new("/tmp/cb.$$.tar");
     385        $tar->setcwd("$basedir/$machine");
     386        if ($debug) {
     387            pb_log(1,"DEBUG: Extracting /tmp/cb.$$.tar\n");
     388            foreach my $f ($tar->list_files()) {
     389                pb_log(1,"DEBUG: $f\n");
     390            }
     391        } else {
     392            foreach my $f ($tar->get_files) {
     393                my $sname = $f->name;
     394                $sname =~ s|/$||;
     395                next if ($srcdir =~ /$sname/);
     396                if ($f->is_file) {
     397                    #pb_log(0,"Extracting and Adding ".$f->name." to your $scheme system\n");
     398                    $tar->extract($f);
     399                    pb_vcs_add_if_not_in($scheme,"$basedir/$machine/".$f->name);
     400                } elsif ($f->is_dir) {
     401                    pb_log(0,"Calling recursively cbusterize on ".$f->name."\n");
     402                    cb_busterize("/".$f->name,$recur);
    391403                } else {
    392                     pb_system($cmd);
     404                    pb_log(0,"File type $f->type for $f->name is not handled yet\n");
    393405                }
    394406            }
     407        }
     408        if ($debug) {
     409            pb_log(1,"DEBUG: please cleanup manually /tmp/cb.$$.tar\n");
    395410        } else {
    396             pb_log(0,"INFO: Directory $target already there\n");
     411            $tar->clear;
     412            unlink("/tmp/cb.$$.tar");
    397413        }
    398414    } else {
    399415        # Only deal with that dir, nothing below, so just created locally
    400416        if ($debug) {
    401             pb_log(1,"DEBUG: mkdir -p $target\n");
     417            pb_log(1,"DEBUG: Creatng and Adding $target to your $scheme system\n");
    402418        } else {
    403419            pb_mkdir_p("$target");
    404         }
    405     }
    406     if ($debug) {
    407         pb_log(1,"DEBUG: Adding $target to your $scheme system\n");
    408     } else {
    409         pb_vcs_add($scheme,"$target");
    410         pb_log(0,"INFO: Created $target and added it to your $scheme system\n");
    411     }
    412 }
     420            pb_vcs_add_if_not_in($scheme,"$target");
     421        }
     422    }
     423}
     424$chan->close();
    413425
    414426pb_log(2,"Exiting cb_busterize\n");
    415427}
    416428
    417 sub cb_ssh_do {
    418 
    419 my $remote = shift;
    420 my $source = shift;
    421 my $target = shift;
    422 my $debug = shift;
    423 
    424 # TODO: if sudo asks a passwd it won't work.
    425 my $cmd = "sudo tar cvhf /tmp/cb.$$.tar $source\n";
    426 if ($debug) {
    427     pb_log(1,"DEBUG: launching through Net:SSH2 ($remote\@$machine) $cmd");
    428 }
    429 print $chan "$cmd";
    430 pb_log(1,"DEBUG: LINE : $_") while <$chan>;
    431 $cmd = "sudo chmod 600 /tmp/cb.$$.tar\n";
    432 if ($debug) {
    433     pb_log(1,"DEBUG: launching through Net:SSH2 ($remote\@$machine) $cmd");
    434 }
    435 print $chan "$cmd";
    436 pb_log(1,"DEBUG: LINE : $_") while <$chan>;
    437 $cmd = "sudo chown $remote /tmp/cb.$$.tar\n";
    438 if ($debug) {
    439     pb_log(1,"DEBUG: launching through Net:SSH2 ($remote\@$machine) $cmd");
    440 }
    441 print $chan "$cmd";
    442 pb_log(1,"DEBUG: LINE : $_") while <$chan>;
    443 if ($debug) {
    444     pb_log(1,"DEBUG: gettting through Net:SSH2 ($remote\@$machine) /tmp/cb.$$.tar\n");
    445 }
    446 $ssh2->scp_get("/tmp/cb.$$.tar","/tmp/cb.$$.tar");
    447 if ($debug) {
    448     pb_log(1,"DEBUG: erasing through Net:SSH2 ($remote\@$machine) /tmp/cb.$$.tar\n");
    449 } else {
    450     print $chan "sudo rm -f /tmp/cb.$$.tar\n";
    451 }
    452 my $tar = Archive::Tar->new("/tmp/cb.$$.tar");
    453 $tar->setcwd($target);
    454 if ($debug) {
    455     pb_log(1,"DEBUG: Extracting /tmp/cb.$$.tar\n");
    456     foreach my $f ($tar->list_files()) {
    457         pb_log(1,"DEBUG: $f\n");
    458     }
    459 } else {
    460     $tar->extract();
    461 }
    462 if ($debug) {
    463     pb_log(1,"DEBUG: cleanup\n");
    464 } else {
    465     $tar->clear;
    466     unlink("/tmp/cb.$$.tar");
    467 }
    468 }
     429
Note: See TracChangeset for help on using the changeset viewer.