Saturday 11 March 2017

Scripting in Python and Jython with added OS commands

I was writing a generic ( use anywhere ) script to add a BPM URL to my IBM BPM Advanced 8.5.7 environment.

For me, all of the components ( IBM HTTP Server, WebSphere Plugin and WebSphere Application Server / BPM ) are on the same VM.

For the record, I'm running the VM on OpenStack.

Therefore, I wanted a script that would get the hostname of the VM on which IHS/WAS is running.

This served as source: -


and this was my test script: -

 foo.jy 
import socket

hostname=socket.gethostname()
print("Hostname is " + hostname)


which I tested using Python: -

python foo.jy 

Hostname is bpm857.novalocal

and Jython: -

/opt/ibm/WebSphereProfiles/Dmgr01/bin/wsadmin.sh -lang jython -conntype none -f foo.jy 

WASX7357I: By request, this scripting client is not connected to any server process. Certain configuration and application operations will be available in local mode.
Hostname is bpm857.novalocal


This is the pukka script: -

configureBPMURL.jy

import socket

# Set variables

cellID=AdminControl.getCell()
dePath='/Cell:'+ cellID+'/BPMCellConfigExtension:/BPMDeploymentEnvironment:/'
de=AdminConfig.getid(dePath)
hostname=socket.gethostname()
print("Hostname is " + hostname)

# List existing BPM URLs

bpmurlsid=AdminConfig.getid(dePath+'BPMURLS:/')
bpmurllist=AdminUtilities.convertToList(AdminConfig.list("BPMURL", bpmurlsid))
for item in bpmurllist :
 print AdminConfig.show(item)

# Remove existing BPM URLs

for i in AdminConfig.list('BPMVirtualHostInfo').split():
 AdminConfig.remove(i)

# Create new Virtual Host

webserver_vh = AdminConfig.create('BPMVirtualHostInfo',de,[['name','webserver_vh'],['transportProtocol','https'],['hostname',hostname],['port','8443']],'virtualHosts')
AdminConfig.modify(de,[['defaultVH',webserver_vh]])

# Save and Sync

AdminConfig.save()
AdminNodeManagement.syncActiveNodes()

# Validate

print AdminConfig.show(AdminConfig.list('BPMVirtualHostInfo'))


which worked as follows: -

/opt/ibm/WebSphereProfiles/Dmgr01/bin/wsadmin.sh -lang jython -f configureBPMURL.jy 

Realm/Cell Name: <default>
Username: wasadmin
Password:         
 WASX7209I: Connected to process "dmgr" on node Dmgr using SOAP connector;  The type of process is: DeploymentManager
Hostname is bpm857.novalocal
[scenario EXTERNAL_CLIENT]
[strategies "WCCMConfigStrategy, HttpProtocolHostStrategy"]
[scenario INTERNAL_CLIENT]
[scenario RELATIVE]
[strategies RelativeUrlStrategy]
---------------------------------------------------------------
 AdminNodeManagement:        Synchronize the active nodes
 Usage: AdminNodeManagement.syncActiveNodes()
 Return: If the command is successfully invoked, a value of 1 is returned. 
---------------------------------------------------------------
 
 
Node1
[hostname bpm857.novalocal]
[name webserver_vh]
[port 8443]
[transportProtocol https]


which is nice

This was also useful: -


although the socket.gethostname() was more useful.

No comments:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...