Changeset 1668 in ProjectBuilder
- Timestamp:
- Oct 28, 2012, 9:22:41 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
projects/casparbuster/devel/bin/cbusterize
r1665 r1668 109 109 use CasparBuster::Env; 110 110 use CasparBuster::Plugin; 111 use CasparBuster::SSH; 111 112 use File::Basename; 112 113 use File::Path; … … 139 140 my $appname = "cb"; 140 141 $ENV{'PBPROJ'} = $appname; 141 pb_temp_init();142 142 143 143 # Initialize the syntax string … … 174 174 $pbdebug = $debug; 175 175 pb_log_init($debug, $LOG); 176 pb_log(0,"Starting cbusterize\n"); 176 pb_temp_init(); 177 pb_log(1,"Starting cbusterize\n"); 177 178 178 179 # Get conf file in context … … 203 204 pb_log(1, "DEBUG MODE, not doing anything, just printing\nDEBUG: basedir = $basedir\n"); 204 205 pb_log(1, "DEBUG: source = $source\n") if (defined $source); 205 pb_log(1, "DEBUG: machine = $machine\n") if (defined $machine);206 pb_log(1, "DEBUG: machine = $machine\n"); 206 207 207 208 # Use potentially a remote account if defined 208 209 my $account = undef; 209 210 my $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})); 212 213 pb_log(1, "DEBUG: remote account1 = $remote\n") if (defined $remote); 213 214 $remote = getpwuid($<) if (not defined $remote); … … 246 247 } 247 248 248 $ssh2->disconnect() if (defined $machine);249 cb_ssh_close($ssh2); 249 250 250 251 sub cb_busterize { … … 257 258 my $srcdir = undef; 258 259 my $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 } 260 my $realsrc = ""; 261 my $type = ""; 262 263 $ssh2 = cb_ssh_init($remote,$machine,$debug) if (not defined $ssh2); 264 265 $chan = $ssh2->channel(); 266 pb_log(3,"DEBUG: SSH2 chan called\n"); 267 confess "Unable to create channel for $remote\@$machine: $!" if (not defined $chan); 268 if ($debug) { 269 pb_log(1,"DEBUG: launching a shell via Net:SSH2 ($remote\@$machine)\n"); 270 } 271 confess "Unable to launch remote shell through Net:SSH2 ($remote\@$machine)" if (not $chan->shell()); 272 pb_log(3,"DEBUG: SSH2 shell called\n"); 273 274 pb_log(2,"DEBUG: Calling readlink -f $source\n"); 275 print $chan "readlink -f $source\n"; 276 while (<$chan>) { 277 $realsrc = $_; 278 chomp($realsrc); 279 pb_log(3,"DEBUG: Found $realsrc"); 280 } 281 pb_log(2,"DEBUG: Found realsrc = $realsrc\n"); 282 print $chan "LANGUAGE=C stat -c '%F' $realsrc\n"; 283 pb_log(2,"DEBUG: Calling LANGUAGE=C stat -c '%F' $realsrc\n"); 284 while (<$chan>) { 285 $type = $_; 286 chomp($type); 287 pb_log(3,"DEBUG: Found $type"); 288 } 289 pb_log(2,"DEBUG: Found type = $type\n"); 290 if ($type =~ /directory/) { 291 $srcdir = $source; 292 pb_log(1,"DEBUG: Found remote dir = $source\n"); 269 293 } 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"); 307 298 } 308 299 … … 314 305 } 315 306 307 my $scheme = $cb->{'cms'}->{$appname}; 308 316 309 # 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}; 310 my $target = "$basedir/$machine/$srcdir"; 322 311 323 312 # 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)); 325 314 326 315 # Create target if it doesn't exist … … 331 320 pb_mkdir_p($target) || confess "Unable to recursively create $target: $!"; 332 321 } 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 334 323 my $tdir = $target; 335 324 my @tab = (); … … 339 328 pb_log(3,"tdir is now $tdir\n"); 340 329 } 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 } 353 335 354 336 if (defined $srcfile) { 355 337 # File case 356 338 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"); 375 346 } 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"); 377 348 } 378 349 } else { … … 380 351 if ($recur eq "true") { 381 352 # 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); 391 403 } else { 392 pb_ system($cmd);404 pb_log(0,"File type $f->type for $f->name is not handled yet\n"); 393 405 } 394 406 } 407 } 408 if ($debug) { 409 pb_log(1,"DEBUG: please cleanup manually /tmp/cb.$$.tar\n"); 395 410 } else { 396 pb_log(0,"INFO: Directory $target already there\n"); 411 $tar->clear; 412 unlink("/tmp/cb.$$.tar"); 397 413 } 398 414 } else { 399 415 # Only deal with that dir, nothing below, so just created locally 400 416 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"); 402 418 } else { 403 419 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(); 413 425 414 426 pb_log(2,"Exiting cb_busterize\n"); 415 427 } 416 428 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.