Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Remove Script for a Infrastructure file managenet system running embedded perl

by sanju7 (Acolyte)
on Jul 18, 2010 at 00:07 UTC ( [id://850123]=perlquestion: print w/replies, xml ) Need Help??

sanju7 has asked for the wisdom of the Perl Monks concerning the following question:

About the environment :

The script stays on central location but runs through an agent on remote hosts . Central server pushes the script to a scheduler on remote machines to a version of embedded perl similar to standard perl version 5.8.8 .

I wrote my script on simple concept, given a path, it walks down the directory tree from a given path ( Path could be local path like "c:\windows", "/usr/local" or shared path "\\serverhost\share" since agents on windows or linux or mac could run it) , check if any of the directories has any file present or not, then delete those directories if there is no file on them. My script only partially successful, fails on shared server paths.

Here my script part of an automation server / client program runs on remote agents (on windows , linux or mac clients) after following,

<1> Files have been copied from source ($ThisSourceDirectory ) to destination location

<2> Archive(for added security ) of the above files after the copy job from source ($ThisSourceDirectory ) to archive location($ThisMoveTarget) completes.

<3> If archive is ok then cleanup script checks certain condition and upon satisfying removes the empty directory structure from source location.

My Script

#! /usr/bin/perl -w # Testing signiant code # Author: #use strict; use diagnostics; use File::Find; # Variable "Error" contains any error of previous task runs. # Variable "ThisMoveTarget" contains destination archive location path +. If this stores a path (i.e if directories are archived) then # the main body of code (i.e walk down the source location for deletin +g empty directories) should execute my $Error = "0" ; # my $ThisMoveTarget = q|holds location where archive files should go +|; my $ThisMoveTarget = '/home/ssaha/tmp/archive'; # our $DelEmptyFoldrArchive = q|holds yes or no |; our $DelEmptyFoldrArchive = "yes "; # our $ThisSourceDirectory = q|holds the source file location |; our $ThisSourceDirectory = "/home/ssaha/tmp/source"; #our $ThisSourceDirectory = "\\hostname\share-test\"; if (($DelEmptyFoldrArchive eq "yes ")&&($ThisMoveTarget)) { if (($ThisSourceDirectory)&&("$Error" == 0)) { find(\&filter, $ThisSourceDirectory); sub filter() { return unless -f; our $file = $_; print STDERR "$file \n " ; } if ($file) { print STDERR "The $ThisSourceDirectory has files pres +ent \n" ; } elsif (!$file) { finddepth (sub { rmdir $_; }, $ThisSourceDirectory); } } elsif (($ThisSourceDirectory)&&("$Error" != 0)) { print STDERR "JOB Completed with Errors, Source Agent used was SRCHOST +\n" ; } } else { print STDERR "DelEmptyFoldrArchive is $DelEmptyFoldrArchive, + skipping directory deletion. \n" ; } # __END__

Short explanation

My script checks few condition:

(I) Checks if previous task was ok by Error=0 etc

(II) checks if the veriabls such as "sourceDir" , "DestinationDir" are defined etc and Yes/ No string

(III) walks down the directory tree, checks if files present at each location of given directory tree .

(IV) delete all directories which doesnt have any files present on them.

The script is tested with perl on Linux and Windows and it works from central server on agents with the embedded perl as well. However i need following help.

My Issue with this code:

It runs and removes files from windows, Linux (not tested with Mac yet --i.e not as important now)local directories however it doesn't work on shared storage path such as "\\hostname\directory\sub".

The above script when run on the embedded perl on a central server works fine but silently quits when it gets "\\servername\share" type path.

It should ideally

(I) check the source location ( mostly a uri like"\\Sharedhost\directory\source\")

(II) walks the directories down the source "\\Sharedhost\directory\source\ ... \ ... \" etc

(problem) I am not getting how to tell the script to check the ($ThisSourceDirectory) variable and depending what is there (windows style local or shared storage location or mounted directory etc like "c:\dir" or /root/dir" etc --do it straightway and if necessary (i.e "\\servername\dir" type locations) recreate the variable to differentiate hostname from share dir etc and then,

(a) connect the server share

(b)do the check and run cleanup if matches criteria

(c)then exit if ok etc

About my script: Since its an embedded version of perl its not readily accepting external modules so i refrained from using them (however all suggestions are welcome).

Making it work with shared path is an issue. Not getting how to achieve or if it need module or a c routine perhaps to do the network call to remote share host etc. If you can pinpoint and help me construct a better way, have an idea with a code snippet that you think may work would be a grateful . Thanks again for reading this far.

  • Comment on Remove Script for a Infrastructure file managenet system running embedded perl
  • Download Code

Replies are listed 'Best First'.
Re: Remove Script for a Infrastructure file managenet system running embedded perl
by roboticus (Chancellor) on Jul 18, 2010 at 03:00 UTC

    sanju7:

    It looks like you're forgetting that the backslash character is an escape, so if you want *one* backslash, you need to put two in. So the line:

    our $ThisSourceDirectory = "\\hostname\share-test\";

    should be:

    our $ThisSourceDirectory = "\\\\hostname\\share-test\\";

    Or, since perl is perfectly happy with forward slashes instead of backslashes (even on Windows), you could use:

    our $ThisSourceDirectory = "//hostname/share-test/";

    ...roboticus

    I won't even comment on the use of our instead of my.

      Hi roboticus,

      Escaping the backslashes aren't exactly helpful. The error i get is,

      Can't stat \\hostname\share-test: No such file or directory at testArchive.pl line 30

      Can't stat \\hostname\share-test: No such file or directory at testArchive.pl line 42

      This is probably because the script is running from a different server node than the share directories it is trying to stat("\\hostname\share-test"). A test with remote windows shares reachable by "\\{nodename}" came out with this error. Perhaps rmdir is won't connect the share directory from a given path such a way. Thanks for the quick response though.

        sanju7:

        I find the first error message surprising, as I'd expect that the line numbers reported would be lines where the code would actually try to access the files, rather than for calling File::Find's find function. I notice that you've got a prototype on your filter function, so I'd drop the parenthesis. I don't know perl well enough to know if it would/could cause that problem or not, but it's certainly not helpful.

        ...roboticus

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://850123]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-19 23:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found