Unsupported content

 

This version of the documentation is no longer supported. However, the documentation is available for your convenience. You will not be able to leave comments.

JLI script that uses an external library to execute a BLCLI command

This section walks through the example server_properties.jli script, which uses an external library to execute a BLCLI command and set properties on a server. In this script, lines 50-52 reduce the time to update server properties by taking advantage of the JLI's direct interface to the BLCLI.

Before you begin

To enable the execution of system calls from Jython via Java, ensure that the execv module, which is used in this sample script, is available in the Python installation directories.

To import the execv module, download copies of the attached execv$py.class and execv.py files, and store them in the path mentioned in the python.path variable, as configured in the registry file of the Python installation. The registry file, where you can find the python.path variable, is typically found in C:\Program files\Jython\registry (Windows) or /usr/local/jython/registry (UNIX).

Script for setting properties on a server

01 import sys
02 import string as s
03 import re
04 from execv import execv as e
05 import bladelogic.cli.CLI as blcli
06 jli = blcli.CLI()
07 jli.setServiceProfileName("defaultProfile")
08 jli.setRoleName("BLAdmins")
09 jli.connect()
10 serverFile = open(sys.argv[1],'r')
11 allServers = serverFile.readlines()
12 allServers.sort()
13 serverFile.close()
14 
15 def getServerKey(serverName):
16   serverKey = ''
17   cmd = ['Server','getServerDBKeyByName']
18   cmd.append(serverName)
19   serverObject = jli.run(cmd)
20   print serverObject.success()
21   if(serverObject.success()):
22      serverKey = serverObject.returnValue
23   return str(serverKey)
24 
25 def setPropertyValue(serverKey,propertyName,propertyValue):
26   cmd = ['Server','setPropertyValue']
27   cmd.append(serverKey)
28   cmd.append(propertyName)
29   cmd.append(str(propertyValue))
30   result = jli.run(cmd)
31 
32 print 'Getting property keys.'
33 hostKey = 'HOST'
34 osKey = 'OS'
35 descriptionKey = 'DESCRIPTION'
36 print 'Property keys retrieved. Processing server data.'
37 
38 for server in allServers:
39   serverKey = getServerKey(server) 
40   print serverKey
41   server = s.rstrip(server)
42   print 'Processing properties for '+server
43   noverOut = e('nover -c -H '+server).out
44   noverSubbed = re.sub('"','',noverOut)
45   noverStripped = s.rstrip(noverSubbed)
46   nover = re.split(',',noverSubbed)
47   if len(nover) > 1:
48     host = str(nover[0])
49     desc = 'Host Os is '+str(nover[1])
50     try:
51        setPropertyValue(serverKey,hostKey,host)
52        setPropertyValue(serverKey,descriptionKey,desc)
53     except:
54        print 'ERROR:::Failure setting properties. '
55        continue
56     else:
57        print 'Finished for '+server+'.'
58 print 'Server property update completed.'
59 sys.exit(0)

Line 1: Import the Jython sys module. This module has methods for handling system operation.
Line 2: Import the Jython string module as an object named s. The string module handles simple string operations.
Line 3: Import the Jython re module. This module handles regular expressions.
Line 4: Import the execv class from the execv module and name the script object e. The execv module lets you execute system calls from Jython via Java.
Lines 5–8: These lines are explained in Instantiating and executing the JLI.
Line 9: Call the jli.connect function to establish a connection to the application server using the credentials acquired using blcred for making CLI calls.
Line 10: Open a file containing a list of servers in read mode. Assign the file to the file handle serverFile. The NSH Script Job that executes this script builds a target host list file and passes it to this script upon execution. The sys.argv array contains arguments passed from the command line upon execution of the script, with element 0 having a value of the name of the script and element 1 having a value of the first argument.
Line 11: Read all the lines from the open file and assign the return value to an array named allServers.
Line 12: Do an alpha-numeric sort of the allServers array.
Line 13: Close the file handle.
Line 15: Define the function getServerKey. This function takes the serverName variable as an argument.
Line 16: Set the local variable serverKey as an empty string.
Line 17: Initialize the cmd array with BLCLI commands.
Line 18: Append the serverName value to the cmd array.
Line 19: Execute the JLI run command with the argument cmd. Assign the return object to the serverObject variable.
Line 20: Inform the user if the command has executed successfully.
Line 21: Check successful execution of the JLI command.
Line 22: If the JLI successfully executes, assign returnValue to serverKey.
Line 23: Cast serverKey as a string (force value type to string) and return.
Line 25: Define the setPropertyValue function. This function takes the arguments serverKey, propertyName, and propertyValue.
Line 26: Initialize the cmd array with a BLCLI command.
Line 27: Append the serverKey value to the cmd array.
Line 28: Append the propertyName value to the cmd array.
Line 29: Append the propertyValue value to the cmd array.
Line 30: Execute the JLI with the command cmd. Ignore the return object; assume success. The result can be used for debugging purposes.
Line 32: Inform the user of script progress.
Lines 33–35: Set property key values for named properties.
Line 36: Inform the user of script progress.
Line 38: Begin a for loop. For each iteration, set the server variable equal to each element in the allServers array.
Line 39:  Run a BLCLI command to get the server key.
Line 40: Inform the user of the value of the server key.
Line 41: Strip all white space from right side of the server string, in this case you are stripping the \n.
Line 42: Inform the user of the current server.
Line 43: Execute nover –c –H against the remote server via the execv class. Assign output to noverOut. nover is an NSH command that outputs system information about a server, including its hostname, operating system, maintenance release, number of CPUs, CPU speed, and so on.
Line 44: Remove the double-quotes from nover output. Assign result to noverSubbed.
Line 45: Remove any white space from the right side of the noverSubbed string.
Line 46: Split the noverSubbed string into an array at the commas.
Line 47: Check that the nover command returned good data, that the length of the nover array has a length that is greater than 1.
Line 48: Assign the first element of the nover array to the host variable.
Line 49: Assign the second element of the nover array to the desc variable, that is, the host description.
Line 50: Begin a try suite.
Line 51: Call the setPropertyValue function, with the following arguments: serverKey, hostKey, and host.
Line 52: Call the setPropertyValue function, with the following arguments: serverKey, descriptionKey, and desc.
Line 53: An except statement handles exceptions within the try suite.
Line 54: Handle exceptions by informing the user of an error.
Line 55: The continue statement here tells the script to continue executing code, despite an exception.
Line 56: Where no error has occurred, execute this else suite.
Line 57: Inform the user that the script has finished setting properties for the specified server.
Line 58: After the loop finishes running, inform the user that the script has completed.
Line 59: Call the exit() function from the sys module, exiting the script with an exit value of 0.

Was this page helpful? Yes No Submitting... Thank you

Comments