WAS 8.5 Administration using Jython

C:\WASClient> wsadmin -lang jython -host 192.168.1.5 -port 8879
WASX7209I: Connected to process "dmgr" on node serverCellManager01 using SOAP connector;
The type of process is: DeploymentManager
WASX7031I: For help, enter: "print Help.help()"
wsadmin> execfile("C:\WASClient\scripts\was_scripts.py")
wsadmin> addJVMProperty(‘server1’, ‘myproperty’, ‘value’)
wsadmin> syncNodes()
wsadmin> restartServer(‘server1’)

  1. How to create Application Server?
    wsadmin> createApplicationServer(‘Node01’, ‘MyServer’)
    ‘MyServer(cells/Cell01/nodes/Node01/servers/MyServer|server.xml#Server_1402570244015)’
    wsadmin> AdminConfig.save()

    wsadmin> removeServer(‘Node01’, ‘MyServer’)

    def createApplicationServer (nodeName, serverName, templateName = None):
    if templateName == None:
    templateName = 'default'
    options = '[-name %s -templateName %s -genUniquePorts true]' % (serverName, templateName)
    return AdminTask.createApplicationServer(nodeName, options)
    def removeServer (nodeName, serverName):
    AdminTask.deleteServer('[-serverName %s -nodeName %s]' % (serverName, nodeName))
  2. How to retrieve all Ports used by a Server?

    def getServerPorts (nodeName, serverName): 
    ports = {} 
    serverIndex = AdminConfig.list("ServerIndex", AdminConfig.getid("/Node:" + nodeName + "/")) 
    for serverEntry in AdminConfig.list("ServerEntry", serverIndex).splitlines(): 
      if AdminConfig.showAttribute(serverEntry, "serverName") == serverName: 
         for nep in AdminConfig.list("NamedEndPoint", serverEntry).splitlines(): 
            ports[AdminConfig.showAttribute(nep, "endPointName")] = AdminConfig.showAttribute(AdminConfig.showAttribute(nep, "endPoint"), "port")
    return ports
    def printServerPorts(nodeName, serverName):
    for (name, port) in getServerPorts(nodeName, serverName).items():
      print "%40s - %s" %(name, port) 
  3. How to set Server JVM Classpath
    wsadmin> setClasspath(‘server1’, [‘/app/lib/slf4j.jar’,’/app/lib/logback.jar’])

    def printClasspath(serverName):
    jpd = AdminConfig.list("JavaProcessDef", AdminConfig.getid('/Server:' + serverName))
    jvm = toList(AdminConfig.showAttribute(jpd, "jvmEntries"))[0]
    print "%s" % AdminConfig.showAttribute(jvm, "classpath")
    def setClasspath (serverName, classPaths):
    jpd = AdminConfig.list("JavaProcessDef", AdminConfig.getid('/Server:' + serverName))
    jvm = toList(AdminConfig.showAttribute(jpd, "jvmEntries"))[0]
    for cp in classPaths:
    setObjectProperty(jvm, "classpath", cp)
    def setObjectProperty (objectId, propertyName, propertyValue):
    AdminConfig.modify(objectId, [[propertyName, propertyValue]])
    def removeClasspath (serverName):
    jpd = AdminConfig.list("JavaProcessDef", AdminConfig.getid('/Server:' + serverName))
    jvm = toList(AdminConfig.showAttribute(jpd, "jvmEntries"))[0]
    AdminConfig.unsetAttributes(jvm, "classpath")
  4. How to install Application?
    wsadmin> installApp(‘C:\Sample.war’, ‘node01’, ‘server1’, ‘sample_app’, ‘/sample’, ‘mydomain_vh’)
    wsadmin> installApp(‘C:\Sample.ear’, ‘node01’, ‘server1’, ‘sample_app’, vhName=’mydomain_vh’)

    def installApp(location, nodeName, serverName, appName, ctxRoot=None, vhName=None):
    cellName = AdminConfig.list('Cell')
    options = [ 
        '-nopreCompileJSPs',
        '-distributeApp',
        '-nouseMetaDataFromBinary',
        '-deployejb',
        '-createMBeansForResources',
        '-noreloadEnabled',
        '-deployws',
        '-validateinstall warn',
        '-noprocessEmbeddedConfig',
        '-filepermission .*\.dll=755#.*\.so=755#.*\.a=755#.*\.sl=755',
        '-noallowDispatchRemoteInclude',
        '-noallowServiceRemoteInclude',
        '-appname', appInfo["appName"],
        '-MapModulesToServers', [
                 ['.*', '.*', 'WebSphere:cell='+cellName+',node='+nodeName+',server='+serverName]
                                ]
              ]
    if vhName != None:
        options.append('-MapWebModToVH')
        options.append([['.*', '.*', vhName]])
    if ctxRoot != None:
        options.append('-contextroot')
        options.append(ctxRoot)
    AdminApp.install(location, options)
  5. How to update JVM logs location?
    wsadmin> configureJVMLogs(‘server1’, ‘/logs/Websphere/server1/’)

    def configureJVMLogs (serverName, logDir):
    logDir = logDir.strip()
    if (logDir[len(logDir) - 1] != '/'):
    logDir = logDir + "/"
    server = AdminConfig.getid('/Server:' + serverName)
    setFile(AdminConfig.showAttribute(server, 'errorStreamRedirect'), logDir, 'SystemErr.log')
    setFile(AdminConfig.showAttribute(server, 'outputStreamRedirect'), logDir, 'SystemOut.log')
    def setFile(streamId, logDir, fileName):
    AdminConfig.modify(streamId, [['fileName', logDir + fileName]])
  6. How to Start/Stop Server
    wsadmin> stopServer(‘Node01’, ‘MyServer’)

    def startServer(nodeName, serverName):
    AdminControl.startServer(serverName, nodeName)
    def stopServer(nodeName, serverName):
    server = AdminControl.completeObjectName('WebSphere:type=Server,name=%s,*' % serverName)
    if len(server) > 0:
    AdminControl.stopServer(serverName, nodeName)
    else:
    print 'Server %s already stopped!' % serverName
    def restartServer(nodeName, serverName):
    stopServer(nodeName, serverName)
    startServer(nodeName, serverName)
  7. How to synchronize the Node?
    wsadmin> syncNodes(‘node01’)

    def syncNodes(nodeName):
    nodeSync = AdminControl.completeObjectName('type=NodeSync,node=' + nodeName + ',*')
    AdminControl.invoke(nodeSync, 'sync')
    def syncAllNodes():
    for node in AdminConfig.list('Node').splitlines():
      syncNodes(AdminConfig.showAttribute(node, 'name'))
  8. How to get the Server State?
    wsadmin> showServerStatus(‘server1’)

    def showServerStatus(serverName):
    serverObj = AdminControl.completeObjectName('WebSphere:type=Server,name=' + serverName + ',*')
    if len(serverObj) > 0:
    serverStatus = AdminControl.getAttribute(serverObj, 'state')
    else:
    serverStatus = 'STOPPED'
    print "%15s %s" %(serverName, serverStatus)
    def showAllServerStatus():
    for server in AdminConfig.list('Server').splitlines():
    showServerStatus(AdminConfig.showAttribute(server, 'name'))
  9. Managing VirtualHost Configuration
    wsadmin> createVirtualHostWithAliases(‘myhost’, [[‘host1.com’, 80], [‘secure.host.com’, 443]])
    wsadmin> AdminConfig.save()
    wsadmin> addVirtualHostAlias(‘myhost’, [[‘host2.com’, 80], [‘secure.host2.com’, 443]])
    wsadmin> AdminConfig.save()
    wsadmin> printVirtualHostAliases(‘myhost’)
    host1.com:80
    secure.host.com:443
    host2.com:80
    secure.host2.com:443
    wsadmin> removeVirtualHostAlias(‘myhost’, ‘host1.com’)
    wsadmin> AdminConfig.save()
    wsadmin> removeVirtualHost(‘myhost’)
    wsadmin> AdminConfig.save()

    def printAllVirtualHosts():
    for vh in toList(AdminConfig.list("VirtualHost")):
    print "%s" %(AdminConfig.showAttribute(vh, "name"))
    def printAllVirtualHostWithAliases():
    for vh in toList(AdminConfig.list("VirtualHost")):
    print "%s" %(AdminConfig.showAttribute(vh, "name"))
    for al in toList(AdminConfig.showAttribute(vh, 'aliases')):
      print "\t%s:%s" %(AdminConfig.showAttribute(al, 'hostname'), AdminConfig.showAttribute(al, 'port'))
    def printVirtualHostAliases(virtualHost):
    vh = AdminConfig.getid("/VirtualHost:" + virtualHost)
    if len(vh) > 0:
    for al in toList(AdminConfig.showAttribute(vh, 'aliases')):
      print "%s:%s" % (AdminConfig.showAttribute(al, 'hostname'), AdminConfig.showAttribute(al, 'port'))
    else:
    print "VirtualHost '%s' not found" % (virtualHost)
    def createVirtualHost (virtualHostName): 
    AdminConfig.create("VirtualHost", AdminConfig.list("Cell"), [["name", virtualHostName]]) 
    def createVirtualHostWithAliases (virtualHostName, aliases): 
    virtualHost = AdminConfig.create("VirtualHost", AdminConfig.list("Cell"), [["name", virtualHostName]]) 
    for alias in aliases:
    AdminConfig.create("HostAlias", virtualHost, [["hostname", alias[0]], ["port", alias[1]]])
    def addVirtualHostAlias(virtualHostName, aliases):
      virtualHost = AdminConfig.getid("/VirtualHost:" + virtualHostName)
      for alias in aliases:
        AdminConfig.create("HostAlias", virtualHost, [["hostname", alias[0]], ["port", alias[1]]])
    def removeVirtualHostAlias(virtualHostName, hostName):
    virtualHost = AdminConfig.getid("/VirtualHost:" + virtualHostName)
    for alias in toList(AdminConfig.showAttribute(vh, 'aliases')):
    if AdminConfig.showAttribute(alias, 'hostname') == hostName:
      AdminConfig.remove(alias)
    def removeVirtualHost(virtualHostName):
    virtualHost = AdminConfig.getid("/VirtualHost:" + virtualHostName)
    if len(virtualHost) > 0:
    AdminConfig.remove(virtualHost)
    else:
    print "VirtualHost '%s' not found" % (virtualHost)    

jython script to add Web container > Custom properties

you can run below CMD to add prop for ALL JVMs:
./wsadmin.sh -f add_prop.py all prop_name prop_value

or add on one JVM with JVM name:
./wsadmin.sh -f add_prop.py my_jvm_name prop_name prop_value

Here is the code:

serverName = sys.argv[0]
propName = sys.argv[1]
propValue = sys.argv[2]
def toList(inStr):
    outList=[]
    if (len(inStr)>0 and inStr[0]=='[' and inStr[-1]==']'):
        inStr = inStr[1:-1]
        tmpList = inStr.split(" ")
    else:
        tmpList = inStr.split("\n")
    for item in tmpList:
        item = item.rstrip();
        if (len(item)>0):
           outList.append(item)
    return outList

def printWebContainerProperty(serverName):
  for p in toList(AdminConfig.showAttribute(getWebContainer(serverName), 'properties')):
    print '%60s - %s' % (AdminConfig.showAttribute(p, 'name'), AdminConfig.showAttribute(p, 'value'))

def updateWebContainerProperty(serverName, propName, propValue):
  wc = getWebContainer(serverName)
  updated = 'N'
  for p in toList(AdminConfig.showAttribute(wc, 'properties')):
    if AdminConfig.showAttribute(p, 'name') == propName:
      AdminConfig.modify(p, [['value', propValue]])
      updated = 'Y'
  if updated == 'N':
    setCustomProperties(wc, 'properties', {propName:propValue})

def removeWebContainerProperty(serverName, propName):
  wc = getWebContainer(serverName)
  for p in toList(AdminConfig.showAttribute(wc, 'properties')):
    if AdminConfig.showAttribute(p, 'name') == propName:
      AdminConfig.remove(p)

def getWebContainer(serverName):
  return AdminConfig.list('WebContainer', AdminConfig.getid('/Server:' + serverName))

def setCustomProperties (objectName, propertyName, propertyMap):
  propVals = []
  for (key, value) in propertyMap.items():
    propVals.append([["name", key], ["value", value]])
  setObjectProperty(objectName, propertyName, propVals)

def setObjectProperty (objectName, propertyName, propertyValue):
  AdminConfig.modify(objectName, [[propertyName, propertyValue]])

# Locate all Application Servers if server is 'all'
if (serverName == 'all'):
    servers = AdminConfig.list('Server')
    for aServer in toList(servers):
        type = AdminConfig.showAttribute(aServer,'serverType')
        if (type == 'APPLICATION_SERVER'):
            serverName = AdminConfig.showAttribute(aServer, 'name')
            print serverName
            updateWebContainerProperty(serverName, propName, propValue)
else:
    # Locate specified Server and its JVM
    serverName = sys.argv[0]
    updateWebContainerProperty(serverName, propName, propValue)

# Save changes
if (AdminConfig.hasChanges()):
    AdminConfig.save()

Python Sorting Algorithms

1. bubbleSort

def bubbleSort(arr):
    for i in range(1, len(arr)):
        for j in range(0, len(arr)-i):
            if arr[j] > arr[j+1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

2. radixSort

def radix(arr):

    digit = 0
    max_digit = 1
    max_value = max(arr)
    while 10**max_digit < max_value:
        max_digit = max_digit + 1

    while digit < max_digit:
        temp = [[] for i in range(10)]
        for i in arr:
            t = int((i/10**digit)%10)
            temp[t].append(i)

        coll = []
        for bucket in temp:
            for i in bucket:
                coll.append(i)

        arr = coll
        digit = digit + 1

    return arr

3. selectionSort

def selectionSort(arr):
    for i in range(len(arr) - 1):
        minIndex = i
        for j in range(i + 1, len(arr)):
            if arr[j] < arr[minIndex]:
                minIndex = j
        # exchange when i is not the min
        if i != minIndex:
            arr[i], arr[minIndex] = arr[minIndex], arr[i]
    return arr

4. insertionSort

def insertionSort(arr):
    for i in range(len(arr)):
        preIndex = i-1
        current = arr[i]
        while preIndex >= 0 and arr[preIndex] > current:
            arr[preIndex+1] = arr[preIndex]
            preIndex-=1
        arr[preIndex+1] = current
    return arr

5. shellSort

def shellSort(arr):
    import math
    gap=1
    while(gap < len(arr)/3):
        gap = gap*3+1
    while gap > 0:
        for i in range(gap,len(arr)):
            temp = arr[i]
            j = i-gap
            while j >=0 and arr[j] > temp:
                arr[j+gap]=arr[j]
                j-=gap
            arr[j+gap] = temp
        gap = math.floor(gap/3)
    return arr

6. mergeSort

def mergeSort(arr):
    import math
    if(len(arr)<2):
        return arr
    middle = math.floor(len(arr)/2)
    left, right = arr[0:middle], arr[middle:]
    return merge(mergeSort(left), mergeSort(right))

def merge(left,right):
    result = []
    while left and right:
        if left[0] <= right[0]:
            result.append(left.pop(0));
        else:
            result.append(right.pop(0));
    while left:
        result.append(left.pop(0));
    while right:
        result.append(right.pop(0));
    return result

7. quickSort

def quickSort(arr, left=None, right=None):
    left = 0 if not isinstance(left,(int, float)) else left
    right = len(arr)-1 if not isinstance(right,(int, float)) else right
    if left < right:
        partitionIndex = partition(arr, left, right)
        quickSort(arr, left, partitionIndex-1)
        quickSort(arr, partitionIndex+1, right)
    return arr

def partition(arr, left, right):
    pivot = left
    index = pivot+1
    i = index
    while  i <= right:
        if arr[i] < arr[pivot]:
            swap(arr, i, index)
            index+=1
        i+=1
    swap(arr,pivot,index-1)
    return index-1

def swap(arr, i, j):
    arr[i], arr[j] = arr[j], arr[i]

Publish your own python project on PyPi.org

  1. create a simple project:
    Directory of C:\Hans\packaging_tutorial
C:\Hans\packaging_tutorial>tree /F
Folder PATH listing for volume Windows7_OS
Volume serial number is 006F0074 54AA:C963
C:.
│ LICENSE
│ README.md
│ setup.py
│
└─list_difference
__init__.py

C:\Hans\packaging_tutorial>type setup.py

import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="list_difference_zhuby1973", # Replace with your own username
    version="0.0.2",
    author="Hans Zhu",
    author_email="zhuby1973@gmail.com",
    description="A package to find difference between two list",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

C:\Hans\packaging_tutorial>type README.md

Example Package
This is a simple example package.
  1. Generating distribution archives
    Make sure you have the latest versions of setuptools and wheel installed:
    python -m pip install –user –upgrade setuptools wheel
    Now run this command from the same directory where setup.py is located:
    python setup.py sdist bdist_wheel
    This command should output a lot of text and once completed should generate two files in the dist directory:
    Directory of C:\Hans\packaging_tutorial\dist

05/28/2020 06:31 PM .
05/28/2020 06:31 PM ..
05/28/2020 06:31 PM 2,626 list_difference_zhuby1973-0.0.1-py3-none-
any.whl
05/28/2020 06:31 PM 1,304 list_difference_zhuby1973-0.0.1.tar.gz
2 File(s) 3,930 bytes

  1. Uploading the distribution archives
    you need register an account on pypi.org first!
    You’ll need to install Twine:
    python -m pip install –user –upgrade twine
    Once installed, run Twine to upload all of the archives under dist:
    python -m twine upload –repository pypi dist/*
    You will be prompted for a username and password.
  2. Installing your newly uploaded package
    You can use pip to install your package and verify that it works.
    python -m pip install –index-url https://pypi.org/simple/ –no-deps list_difference_zhuby1973
    Make sure to specify your username in the package name!
    pip should install the package from Test PyPI and the output should look something like this:
    Collecting list_difference_zhuby1973
    Downloading list_difference_zhuby1973-0.0.1-py3-none-any.whl (2.6 kB)
    Installing collected packages: list-difference-zhuby1973
    Successfully installed list-difference-zhuby1973-0.0.1
    You can test that it was installed correctly by importing the package. Run the Python interpreter (make sure you’re still in your virtualenv):

C:> python
and from the interpreter shell import the package: import list_difference

Congratulations, you’ve packaged and distributed a Python project!

Blueprint validation

#!/usr/bin/python
import os
import sys
import yaml
import json
from termcolor import colored
bp_dir = sys.argv[1]
cwd = os.getcwd()
bppath = os.path.join(cwd, bp_dir)
inpath = os.path.join(bppath, 'input')
y_file = os.path.join(bppath, 'blueprint.yaml')
def get_diff(list1, list2):
   difference = set(list1).symmetric_difference(set(list2))
   list_difference = list(difference)
   if len(list_difference) == 0:
       print("input.json parameters are matching with blueprint.yaml")
   else:
       print('Found miss match between YAML and JSON, please check below parameters:')
       print(colored(list_difference, 'red'))
def yaml_json_compare(yaml_file, json_file):
with open(yaml_file, 'r') as stream:
   try:
       dict = yaml.load(stream, Loader=yaml.FullLoader)
       for key, value in dict.items():
           if key == 'inputs':
               dict2 = value
               aList = list(dict2)
               #print(aList)
               with open(json_file) as f:
                   try:
                       data = json.load(f)
                       bList = list(data)
                       print(json_file + " is valid")
                       get_diff(aList, bList)
                   except ValueError as exc:
                       print(json_file + " is invalid")
   except yaml.YAMLError as exc:
       print(exc)
       print("blueprint.yaml is invalid")
def walk(dirname):
   for name in os.listdir(dirname):
       path = os.path.join(dirname, name)
       if os.path.isfile(path):
           yaml_json_compare(y_file, path)
       else:
           walk(path)
if __name__ == '__main__':
   walk(inpath)
here is the output:
(venv) lindas-MacBook-Air:hello linda$ python3 write_excel.py DEMO
/Users/linda/PycharmProjects/hello/DEMO/input/SIT/inputs.json is valid
Found miss match between YAML and JSON, please check below parameters:
['vm_size']
/Users/linda/PycharmProjects/hello/DEMO/input/DEV/inputs.json is valid
Found miss match between YAML and JSON, please check below parameters:
['cost1', 'cost']
/Users/linda/PycharmProjects/hello/DEMO/input/UAT/inputs.json is valid
input.json parameters are matching with blueprint.yaml

Develop web service with NodeJS

npm init –yes
npm i express
npm i -g nodemon
npm install –save express@4.16.3 mysql@2.15.0 body-parser@1.18.2
you can get version info from https://www.npmjs.com/package/body-parser
https://www.npmjs.com/package/mysql

C:\Hans\first-app>nodemon index.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter rs
[nodemon] watching path(s): .
[nodemon] watching extensions: js,mjs,json
[nodemon] starting node index.js
Listening on port 3000
[nodemon] restarting due to changes…
[nodemon] starting node index.js
Listening on port 3000

C:\Hans\first-app>set PORT=5000 (on Linux use export PORT=5000)

check records on http://localhost:3000/api/courses
use postman to POST data

Here is the code:

const express = require('express');
const app = express();

app.use(express.json());

const courses = [
    { id: 1, name: 'course1' },
    { id: 2, name: 'course2' },
    { id: 3, name: 'course3' },
];

app.get('/', (req, res) => {
    res.send('Hello World!!!');
});

app.get('/api/courses', (req, res) => {
    res.send(courses);
});


app.post('/api/courses', (req, res) => {
    const course = {
        id: courses.length + 1,
        name: req.body.name
    };
    courses.push(course);
    res.send(course);
});


app.get('/api/courses/:id', (req, res) => {
   let course = courses.find(c => c.id === parseInt(req.params.id));
   if (!course) res.status(404).send('the id not found');
   res.send(course)
});

//port
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}....`))