5 Replies Latest reply on Apr 24, 2017 12:50 PM by Bill Robinson

    blcli - recursively change all target servers of jobs in folder

    Chris LaMendola

      I'm trying to write a blcli nsh script that will recursively iterate through an entire job folder (including sub job folders) and change the target servers for each job. I've figured out how to list all the jobs in a folder with the listAllJobGroupMembers command, but I can not for the life of me figure out how to turn this output into anything meaningful.

       

      The snippet below is about as far as I've gotten so far...

       

      JOBGROUP_ID=`blcli JobGroup groupNameToId $JOB_GROUP`
      blcli_execute Group listAllJobGroupMembers 5005 "${JOBGROUP_ID}"
      blcli_storeenv GROUPLIST
      

       

       

      This returns a string for each record, but I'm at a loss of what to do with this since I have no idea what to do with it. All I can think of is maybe pulling out the DBkeys for each item and then parsing that to the addTargetServer command, but I'm really hoping there is an easier way than that. I've spent the last couple days looking through the blcli documentation and honestly it is not very helpful for more than looking up different commands... The "examples" for each command are so basic they really aren't all that helpful...

       

       

      Any guidance would be appreciated!

       

      Thanks!

        • 1. Re: blcli - recursively change all target servers of jobs in folder
          Bill Robinson

          so first of all this:

          JOBGROUP_ID=`blcli JobGroup groupNameToId $JOB_GROUP`

          defeats the purpose of using the performance commands.  this spawns a subprocess and consequently a new jvm. and you should always put double quotes around the variables you know can have spaces.

           

          so what's going to be better is to get the dbkeys of the jobs to use w/ the clear and add target commands in the job namespace.  and then you don't need to know the type of each job.  so you will need to use some of the Unreleased blcli commands and documentation

           

          so you can do:

          blcli_execute JobGroup groupNameToId "${JOB_GROUP}"

          blcli_storeenv groupId

          blcli_execute Job findAllHeadersByGroup ${groupId}

          blcli_execute SJobHeader getDBKey

          blcli_execute Utility setTargetObject

          blcli_execute Utility listPrint

          blcli_storeenv jobKeys

          jobKeys="$(awk 'NF' <<< "${jobKeys}")"

           

          while read jobKey

          do

          blcli_execute Job clearTargetServers ${jobKey}

          blcli_storeenv jobKey

          blcli_execute Job addTargetServer ${jobKey} ${serverName}

          done <<< "${jobKeys}"

           

          the commands in the first block above are just a slightly different sequence than the 'Job.listAllByGroup'

          • 2. Re: blcli - recursively change all target servers of jobs in folder
            Chris LaMendola

            Thanks for the info Bill! This is my first attempt at a blcli script, so I'm still learning all the nuances. I'll give those commands a shot and see what happens.

             

            Is the result of each command stored in memory until the next command is executed? I noticed that a few of the commands you listed are executed in sequence without referencing any variables.

             

            Also, in the while loop, why do you re-store the jobKey variable after clearing the TargetServer?

             

             

            Thanks!

            Chris

            • 3. Re: blcli - recursively change all target servers of jobs in folder
              Bill Robinson

              btw - you could have used the 'listAllJobGroupMembers' and then a 'Utility.listPrint' but then you need to do text processing to get the dbkeys out.

               

              "Is the result of each command stored in memory until the next command is executed? I noticed that a few of the commands you listed are executed in sequence without referencing any variables."

              -> right, the 'performance commands' actually use an embeded jvm in nsh and load objects into memory and let you act on them.  in one of the pdfs attached to that unreleased commands page there's a section on get/set/load command types. so like this:

              blcli_execute Job findAllHeadersByGroup ${groupId}

              -> gets the object headers (takes up less memory than the  job object) and loads them into memory

              blcli_execute SJobHeader getDBKey

              -> acts on object (the headers) in memory

               

              "Also, in the while loop, why do you re-store the jobKey variable after clearing the TargetServer?"

              -> because clearing the targets increments the object version and therefore the dbkey (which is a combination of the object id and version)

              • 4. Re: blcli - recursively change all target servers of jobs in folder
                Chris LaMendola

                Thanks for the clarification. I've been testing out the script you gave me quite a bit. I was able to adjust it to add multiple target servers.

                 

                 

                The only issue I'm having so far is that your script doesn't actually update the servers recursively. It only affects any jobs in the parent folder.

                 

                 

                **EDIT**

                I figured out how to do this recursively using findAllByGroupID since it has a recursive flag. I'm sure there's an easier way to do this, but for now it does the trick for me!

                 

                bcli_execute JobGroup groupNameToId "${JOB_GROUP}"
                blcli_storeenv groupId
                blcli_execute Job findAllByGroupId ${groupId} true 
                blcli_execute SJobHeader getDBKey
                blcli_execute Utility setTargetObject
                blcli_execute Utility listPrint
                blcli_storeenv jobKeys
                jobKeys="$(awk 'NF' <<< "${jobKeys}")"
                
                #Loop through jobKeys and
                while read jobKey; do
                     for server in "${serverNames[@]}"; do
                          blcli_execute Job addTargetServer "${jobKey}" "${server}"
                          blcli_storeenv jobKey
                     done 
                
                     for server in "${oldServerName[@]}"; do
                          blcli_execute Job clearTargetServer ${jobKey} "${server}"
                          blcli_storeenv jobKey
                     done
                done <<< "${jobKeys}"
                
                

                 

                 

                I'm marking this as the correct answer since it actually does what was asked in the Title of the thread, but thanks to Bill for getting this started with his initial script!

                • 5. Re: blcli - recursively change all target servers of jobs in folder
                  Bill Robinson

                  if you use 'Job.findAllByGroupId' that has a recurse option.