home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Demo / PCDUO / data1.cab / Script_Samples / GRPFLUPD.SCP < prev    next >
Encoding:
Text File  |  2003-11-28  |  12.9 KB  |  400 lines

  1. // GRPFLUPD.SCP - Sample Script which udates specified file(s) on the 
  2. // Client(s) in a Group.
  3. // Copyright (c) 2001, Vector Networks Limited
  4. // All Rights Reserved
  5. //
  6. // Revision History:
  7. // 6.10 30-Apr-01 DB - Created, using FLUPDATE v6.00. 12-Oct-00.
  8. //      01-May-01 DB  - Add Group and Network processing code.
  9. //      02-May-01 DB  - Add file comparison code and test dialup.
  10. //      03-May-01 DB  - Fix remote/local dial/hangup code.
  11. //      04-May-01 DB  - Add error tracking and retry restart code.
  12. // 7.0 20-Mar-02 DB  - Separate built-in $INCLUDEs to separate files.
  13.  
  14. // This Script is be used to ensure that all Clients (or all Clients in a Group) 
  15. // have an up to date version of one or more specified files. The filename(s) 
  16. // of the file(s) that need to be checked must be predefined. To pass multiple 
  17. // filenames to the Script, use a vertical bar | to separate the names. Specific 
  18. // Clients or Groups to be checked can also be predefined.
  19.  
  20. $INCLUDE "ADDTOLOG.SCP"
  21. $INCLUDE "FCOMPARE.SCP"
  22. // $INCLUDE "GTCTLDIR.SCP"
  23. $INCLUDE "GTCTLFL.SCP"
  24. $INCLUDE "GTGROUP.SCP"
  25. $INCLUDE "GTREMOTE.SCP"
  26. $INCLUDE "PARSE.SCP"
  27. $INCLUDE "RINSTR.SCP"
  28. $INCLUDE "TOKENS.SCP"
  29.  
  30. //  Predefined variables:
  31. //    GroupName = "" Updates all Clients in this Group
  32. //    Password = "GBBLLYDGKK" Encrypted password for remote network
  33. //    RootPath = <Control Directory>\<Group Name> Root directory for Group updates
  34. //    Username = "SCRIPTING/GBBLLYDGKK" Username and password for Connect
  35. //
  36. //  The following are not currently implemented:
  37. //    ClientName = "" Template name for Lookup (use "*" for any)
  38.  
  39. Function Main ()
  40.   Dim ClientName as String, GroupName as String
  41.   Dim Username as String, Password as String
  42.   Dim FileNames as String, FileNameList as List
  43.   Dim RootPath as String, OutputPath as String, LogFilePath as String
  44.   Dim GroupList as List, ClientCount, Member as String, SplitLine as List 
  45.   Dim Name as String, Address as String, Transport as Integer
  46.   Dim Location as Integer, Network as String, NetworkInfo as List
  47.   Dim DialNumber as String, LastDialled as String
  48.   Dim NextTransport as Integer, LastTransport as Integer
  49.   Dim FileCopyCount, FileUpdateCount, FileComparison
  50.   Dim ConnectErrors, DialErrors, FileErrors, TotalErrors
  51.   Dim ErrorsForThisClient, RetryList as List
  52.   Dim Handle, Line as String, Lines
  53.     Dim CtrlFile as String, CLFile as String,TempList as List
  54.     Dim Cmp as Integer, Atr as Integer, ClientInfo as List, CtrlDir as String
  55.     Dim x, FilePath as String
  56.  
  57.   // Initialise counter variables
  58.  
  59.   ClientCount = 0
  60.   FileCopyCount = 0
  61.   FileUpdateCount = 0
  62.   Lines = 0
  63.   ConnectErrors = 0
  64.   DialErrors = 0
  65.   FileErrors = 0
  66.   TotalErrors = 0
  67.  
  68.   // Trim predefined variables before use
  69.  
  70.   ClientName = Trim (ClientName)
  71.   GroupName = Trim (GroupName)
  72.   UserName = Trim (UserName)
  73.   RootPath = Trim (RootPath)
  74.   FileNames = Trim (FileNames)
  75.  
  76.   //  Look for the Group's root directory. See if we can find it.
  77.  
  78.   If (GroupName = "") Then
  79.     GroupName = "TEST"
  80.   Endif
  81.  
  82.   If (RootPath = "") Then
  83.     RootPath = GetControlDir ()
  84.     RootPath = RootPath + "\" + GroupName
  85.   Endif
  86.  
  87.   If (FileExists (RootPath + "\" + "RETRY.LST")) Then
  88.     Print "Loading Clients from Group Retry List"
  89.     Handle = Open (RootPath + "\" + "RETRY.LST", FILE_READ)
  90.  
  91.     If (Handle  != 0) Then
  92.       Do Until EOF (Handle)
  93.         Line = ReadLine (Handle)
  94.         Lines = Lines + 1
  95.         Print "Line ", Lines, " <", Line, ">"
  96.  
  97.         If Trim (Line) != "" Then
  98.           SplitLine = TokenList (Line, "|")
  99.           If Items (SplitLine) > 4 Then
  100.             Print "Adding : ", Line
  101.             AddItem (GroupList, Line)
  102.           Else
  103.             Print "Skipping : ", Line
  104.           Endif
  105.         Else
  106.           Print "Skipping blank line"
  107.         Endif
  108.       Loop
  109.  
  110.       Print "Summary"
  111.       Print Lines, " lines processed"
  112.       If (Items (GroupList) > 0) Then
  113.         Print "Retry List contains ", Items (GroupList), " Clients."
  114.       Else
  115.         Print "Retry List is empty."
  116.       Endif
  117.       Close (Handle)
  118.     Endif
  119.   Else
  120.     Print "Selecting all Clients from Group: ", GroupName
  121.     GroupList = GetGroup (GroupName, "")
  122.   Endif
  123.  
  124.   // Make sure that at least one Client has been found.
  125.  
  126.   If Items (GroupList) > 0 then
  127.  
  128.     //  Now, look for any update files. 
  129.     //  They should be in the Group's root directory. 
  130.  
  131.     If (RootPath = "") Then
  132.       RootPath = GetControlDir ()
  133.       RootPath = RootPath + "\" + GroupName
  134.     Endif
  135.  
  136.     If (DirExists (RootPath)) Then
  137.       Print "Update file path : ", RootPath, "\files"
  138.  
  139.       //  Look to see if there are any files there...
  140.  
  141.       If (GetFileList (RootPath + "\files\*.*", FileNameList) > 0) Then
  142.         Print Items (FileNameList), " update file(s) found."
  143.       Else
  144.         Print "ERROR: No update files found for Group : ", GroupName
  145.         Print "Script Exiting."
  146.         Goto Summary
  147.       Endif
  148.     Else
  149.       Print "Update file path : ", RootPath, "\", GroupName, " not found!"
  150.       Print "Script Exiting."
  151.       Goto Summary
  152.     Endif
  153.  
  154.     // Execute the following loop on all Clients found.
  155.  
  156.     For Each Member in GroupList
  157.       Trim (Member)
  158.       Print "Processing Client ", Member, "..."
  159.       ClientInfo = TokenList (Member, "|")
  160.  
  161.       //  Check the token count. We need 5 of them.
  162.  
  163.       If (Items (ClientInfo) < 5) Then
  164.         Print "WARNING: Insufficient tokens found in line ", Member, ":"
  165.         For Each Token in ClientInfo
  166.           Print Token
  167.         Next
  168.       Else
  169.         Name = GetItem (ClientInfo, 1)
  170.         Address = GetItem (ClientInfo, 2)
  171.         Transport = CInt (GetItem (ClientInfo, 3))
  172.         Location = CInt (GetItem (ClientInfo, 4))
  173.         Network = GetItem (ClientInfo, 5)
  174.  
  175.         Print "Client Information : "
  176.         Print "  Name : ", Name
  177.         Print "  Address : ", Address
  178.         Print "  Transport : ", Transport
  179.         Print "  Location : ", Location
  180.         Print "  Network : ", Network
  181.  
  182.         //  Zero the error count for this Client
  183.  
  184.         ErrorsForThisClient = 0
  185.  
  186.         //  If we have a remote network, indicated by Location > 0,
  187.         //  then look it up in the Known Remote Networks file.
  188.  
  189.         If (Location > 0) Then
  190.           NetworkDetails = GetRemote (Network, "")
  191.           NetworkInfo = TokenList (NetworkDetails, "|")
  192.           DialNumber = GetItem (NetworkInfo, 3)
  193.  
  194.           //  See if we are already dialled to this location
  195.  
  196.           If (DialNumber != LastDialled) Then
  197.             If (LastDialled != "") Then
  198.               Print "Hanging up : ", LastDialled
  199.               Hangup ()
  200.               LastDialled = ""
  201.  
  202.               //  I bet we have to wait for a while here...
  203.  
  204.               Wait (10)
  205.             Endif
  206.           Endif
  207.         Else
  208.  
  209.           //  This Client is local, not at a remote location. Clear the
  210.           //  dial number so we don't try to call it...
  211.  
  212.           DialNumber = ""
  213.  
  214.           //  If we are connected to a remote network, hang up.
  215.  
  216.           If (LastDialled != "") Then
  217.             Print "Hanging up : ", LastDialled
  218.             Hangup ()
  219.             LastDialled = ""
  220.             Wait (10)
  221.           Endif
  222.         Endif
  223.  
  224.         //  Next, check the Client's transport against the last one
  225.         //  we were using, if any. If there is a difference, we have 
  226.         //  to call SetTransport to change over.
  227.  
  228.         If (Location > 0) Then
  229.           NextTransport = Transport + T_REMOTE
  230.         Else
  231.           NextTransport = Transport
  232.         Endif
  233.  
  234.         If (NextTransport != LastTransport) Then
  235.           Print "Changing transport from : ", LastTransport, " to ", NextTransport
  236.           If (Location > 0) Then
  237.             SetTransport (Transport,  T_REMOTE)
  238.             LastTransport = Transport + T_REMOTE
  239.           Else
  240.             SetTransport (Transport)
  241.             LastTransport = Transport
  242.           Endif
  243.         Endif
  244.  
  245.         If (DialNumber != "") Then
  246.           If (Dial (DialNumber, Password)) Then
  247.             LastDialled = DialNumber
  248.           Else
  249.             Print "ERROR: No answer from network : ", Network, " on ", DialNumber
  250.             LastDialled = ""
  251.  
  252.             //  Bump the error count for this Client and leave the
  253.             //  end of loop retry logic to sort out what to do about it.
  254.  
  255.             DialErrors = DialErrors + 1
  256.             ErrorsForThisClient = ErrorsForThisClient + 1
  257.             Goto LoopEnd
  258.           Endif
  259.         Endif
  260.  
  261.         LogFilePath = RootPath + "\" + Name + ".LOG"
  262.  
  263.         // Attempt to connect to the Client.
  264.  
  265.         If Connect (Name, UserName) then
  266.           Print "Connected to Client ", ClientCount, " ", Name 
  267.           AddToLog (LogFilePath, "Connected to Client : " + Name)
  268.  
  269.           //  Now, see if we can send the update files.
  270.           //  First, check the Client's output path.
  271.  
  272.           OutputPath = GetInstallDir ()
  273.           OutputPath = ">" + OutputPath + "\Files"
  274.  
  275.           If (!DirExists (OutputPath)) Then
  276.             If (MkDir (OutputPath)) Then
  277.               AddToLog (LogFilePath, "Created directory : " + OutputPath)
  278.             Endif
  279.           Endif
  280.  
  281.           //  Compare the input and output files to see if we 
  282.           //  need to update any existing files at the Client
  283.  
  284.           For Each File in FileNameList 
  285.             FileComparison = FileCompare (RootPath  + "\files\" + File, OutputPath + "\" + File)
  286.  
  287.             //  We don't care about file attribute differences, 
  288.             // so 0 and 1 mean that both files are the same
  289.  
  290.             If (FileComparison > 1) Then
  291.               If (Copy (RootPath  + "\files\" + File, OutputPath)) Then
  292.  
  293.                 //  5 means output file not found. Anything else means that the 
  294.                 //  output file existed but was different (not necessarily older).
  295.  
  296.                 If (FileComparison = 5) Then
  297.                   AddToLog (LogFilePath, "Copied file : " + File)
  298.                   FileCopyCount = FileCopyCount + 1
  299.                 Else
  300.                   AddToLog (LogFilePath, "Updated file : " + File)
  301.                   FileUpdateCount = FileUpdateCount + 1
  302.                 Endif
  303.               Else
  304.                 Print "WARNING: Unable to copy file : ", File, " to Client : ", Name
  305.  
  306.                 //  Bump the error counts
  307.  
  308.                 FileErrors = FileErrors + 1
  309.                 ErrorsForThisClient = ErrorsForThisClient + 1
  310.               Endif
  311.             Endif
  312.           Next
  313.  
  314.           Disconnect (Name)
  315.           AddToLog (LogFilePath, "Disconnected from : " + Name)
  316.           ClientCount = ClientCount + 1
  317.         Else
  318.           AddToLog (LogFilePath, "Unable to Connect to Client : " + Name 
  319.           ConnectErrors = ConnectErrors + 1
  320.           ErrorsForThisClient = ErrorsForThisClient + 1
  321.         Endif
  322.       Endif
  323.  
  324.       //  At the end of the Client loop, we just check whether there
  325.       //  were any errors. If there were, then we will need to record
  326.       //  the Client's network details for retrying later. These are in
  327.       //  the Member string variable.
  328.  
  329. LoopEnd:
  330.  
  331.       If (ErrorsForThisClient >0) Then
  332.         TotalErrors = TotalErrors + ErrorsForThisClient
  333.         AddItem (RetryList, Member)
  334.       Endif
  335.     Next
  336.  
  337.     //  Tidy up any remote connections
  338.  
  339.     If (LastDialled != "") Then
  340.       Print "Hanging up : ", LastDialled
  341.       Hangup ()
  342.       LastDialled = ""
  343.     Endif
  344.  
  345.     //  and turn off the dialup transport
  346.  
  347.     If (LastTransport > T_REMOTE) Then
  348.       LastTransport = LastTransport - T_REMOTE
  349.       SetTransport (LastTransport)
  350.     Endif
  351.   Else
  352.       Print "No Clients were found!"
  353.   Endif
  354.  
  355.   //  Check for any errors and queue a retry for later.
  356.   //  Here, we simply overwrite any old retry list.
  357.  
  358.   If (TotalErrors > 0) Then
  359.  
  360.     //  Double-check (run-time error if it's empty)...
  361.  
  362.     If (Items (RetryList) > 0) Then
  363.  
  364.       //  Create a new retry list file, overwriting any existing file
  365.  
  366.       If (FileExists (RootPath + "\" + "RETRY.LST")) Then
  367.         Handle = Open (RootPath + "\" + "RETRY.LST", FILE_OVERWRITE)
  368.       Else
  369.         Handle = Open (RootPath + "\" + "RETRY.LST", FILE_CREATE)
  370.       Endif
  371.  
  372.       If (Handle  != 0) Then
  373.         For Each Member in RetryList
  374.           Print "Adding : ", Member, " to Retry List"
  375.           WriteLine Handle, Member
  376.         Next
  377.         Close (Handle)
  378.       Endif
  379.     Endif
  380.   Else
  381.     If (FileExists (RootPath + "\" + "RETRY.LST")) Then
  382.       Print "Deleting Retry List"
  383.       Delete (RootPath + "\" + "RETRY.LST")
  384.     Endif
  385.   Endif
  386.  
  387.   // Print a summary
  388.  
  389. Summary:
  390.   Print "Number of Clients found: ", ClientCount
  391.   Print "  Number of files copied: ", FileCopyCount
  392.   Print "  Number of files updated: ", FileUpdateCount
  393.   If TotalErrors > 0 Then
  394.     Print "Total Errors:", TotalErrors
  395.     Print "Dial Errors:", DialErrors
  396.     Print "Connect Errors:", ConnectErrors
  397.     Print "File Errors:", FileErrors
  398.   Endif
  399. End Function
  400.