Git Product home page Git Product logo

wsadminlib's Introduction

wsadminlib's People

Contributors

backtozero avatar covener avatar dekuntz avatar floyd-fuh avatar gpoul avatar hharsunen avatar wsadminlib avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wsadminlib's Issues

Coding style

Hi there,

I'm teaching Python classes right now and some of those people will use IBM Websphere with Python. I thought I give it a short look. As the first hit was the IBM homepage I took that version of wsadminlib.py. I use the function "compareIntLists" as a worst-practice example of how not to code Python. The version here on github is a lot better (and even works correctly). Why does IBM still serve this very old and broken version of wsadminlib.py? At the moment I'm very concerned about my students who will use this kind of code. A couple of suggestions:

  • Have you thought about not using "global" 15 times?
  • Introducing classes (object oriented design) and structuring the code as a module would help.
  • Why is there an "eval" call? The function "isDefined" is never called. As a security guy I consider "eval" harmful in general. There are other ways of checking if a variable is defined (try and catch a NameError).
  • You have commented code, dead code and unused variables. Why not just remove it (you still have the old code in git)? You can use one of the Python debuggers to see variables when debugging. Would make your code much more readable.
  • You have inconsistent docstrings
  • Doctests would help a lot. You kind of wrote something like that in the "compareIntLists" function, but it is not a real doctest. Doctests could help you a lot to test code.
  • The usage of #endif and #endDef and semicolons makes your code very un-pythonic.
  • You use camelCase for functions and variables. That's fine as long as your coding guidelines say it has to be like that (contrary to PEP 8).
  • You are string parsing quiet a lot, which makes the code pretty hard to read and is not best-practice design.
  • You don't use list comprehensions or generator functions at all. Makes your code again un-pythonic.

As a start I would suggest you run pep8 over the script and fix some of the 6879 issues:

$ pip-2.7 install pep8
$ python -m pep8 wsadminlib.py
...snip...
$ python -m pep8 wsadminlib.py | wc -l
6879

Just my suggestions...

cheers,
floyd

setInitialStateOfAllListenerPortsInCluster() cannot work as designed

I don't see how setInitialStateOfAllListenerPortsInCluster() can work as designed. The function makes use of listListenerPortsOnServer() which returns JMX object names. However, the function attempts to use a JMX object name with AdminConfig.showAttribute() which expects a configuration ID.

lPorts = listListenerPortsOnServer( nodeName, serverName )
for lPort in lPorts:
     sop(m, "Setting ListenerPort %s initial state to %s" % (lPort, state))
     stateManagement = AdminConfig.showAttribute( lPort, "stateManagement" )

broken exception handling

The exception handling in the following functions is broken. This is an simple fix. I'm working on a patch.

deleteProxyServerByNodeAndName()
propsToLists()
stringListToList()

incorrect code in getServerId

The function getServerId() includes a secondary search for items of type ProxyServer:

if id == None:
        id = getObjectByNodeAndName(nodename, "ProxyServer", servername)

From the function description it appears that the author intended to return a server with serverType APPLICATION_SERVER. If one could not be found, the author intended as a fallback to search for a server with serverType PROXY_SERVER. This mistakenly conflates serverType=PROXY_SERVER with configuration type ProxyServer. The configuration type ProxyServer is associated with a component of a an application server, not the server itself.

Their are two reasons why getObjectByNodeAndName(nodename, "ProxyServer", servername) will always return None:

  1. Server is the parent type for ProxyServer. If the search for items of Server returned None, it's not going to find items of type ProxyServer either.
  2. When you create a WAS proxy server, the default name attribute for the ProxyServer component is "ProxyServer" not the name of the server.

The search getObjectByNodeAndName(nodename, 'Server', servername) is already sufficient to return servers of any type. This same functionality is done correctly in getServerByNodeAndName().

minor issues with ensureHostAlias()

The function ensureHostAlias() works but has superfluous code.

The following lines can be removed:

    # Force port to be a string - could be string or int on input
    port = "%d" % int( port )

The variable port is subsequently passed to either addHostAlias() or hostAliasExists(). Both of these functions already cast port to String.

The following lines can also be removed:

    host_id = getVirtualHostByName( virtualhostname )
    if host_id == None:
        host_id = createVirtualHost(virtualhostname)

The subsequent function call addHostAlias() is already checking that the virtual host exists. (I don't like that addHostAlias() otherwise creates the virtual host. In my opinion it should raise an exception.)

The following lines can also be removed:

    if not hostAliasExists(virtualhostname, aliashostname, port):
        raise "ERROR: host alias does not exist after creating it"

Again, the function addHostAlias() is already checking that the host alias exists.

wrapper function setWebContainerCustomProperty

The function setWebContainerCustomProperty() is a wrapper for setCustomPropertyOnObject(). I noticed that setWebContainerCustomProperty() does not correctly update web container custom properties that already exist. This occurs when there are more than one property. For instance:

setWebContainerCustomProperty(mynode, server1, "com.ibm.ws.webcontainer.foo", "abc123")
setWebContainerCustomProperty(mynode, server1, "com.ibm.ws.webcontainer.bar", "asdf")

setWebContainerCustomProperty(mynode, server1, "com.ibm.ws.webcontainer.foo", "xyz890")
setWebContainerCustomProperty(mynode, server1, "com.ibm.ws.webcontainer.bar", "jkl;")

will result in the creation of a second instance of properties com.ibm.ws.webcontainer.foo and com.ibm.ws.webcontainer.bar.

The problem occurs because setWebContainerCustomProperty() uses getObjectCustomProperty() which is not correctly creating a list object from showAttribute(object_id,'properties'). Specifically, within getObjectCustomProperty():

x = AdminConfig.showAttribute(object_id,'properties')[1:-1]
if len(x) == 0:
    return None  # no properties set yet

This is code is checking for '[]' that represents an empty list. However, this causes the if-statement,

if x.startswith("["):
    propsidlist = x[1:-1].split(' ')

to never return 'True'. Therefore, the split(' ') never gets run to discover the second property contained in x.

I can provide my version of getObjectCustomProperty(object_id, propname) if anyone is interested.

Lastly, is there any difference in using setWebContainerCustomProperty() versus createWebContainerProp()?

getProxyVirtualHostConfig() & deleteProxyVirtualHostConfig() use a dangerous containment path

Both getProxyVirtualHostConfig() and deleteProxyVirtualHostConfig() contain this line:

pvhc_id = AdminConfig.getid('/ProxyVirtualHostConfig:/')

This containment path is not constrained to a single proxy server. If the cell contains multiple proxy servers, pvhc_id will get multiple config ids:

print AdminConfig.getid('/ProxyVirtualHostConfig:/')
(cells/WebDirect/nodes/azNode/servers/dummyProxy1|proxy-settings.xml#ProxyVirtualHostConfig_1462848765864)
(cells/WebDirect/nodes/azNode/servers/dummyProxy2|proxy-settings.xml#ProxyVirtualHostConfig_1462848779672)

The containment path needs to be constrained like so:

pvhc_id = AdminConfig.getid('/Node:%s/Server:%s/ProxyVirtualHostConfig:/' % ( nodename, proxyname ))

listProxySettings() is broken and should be removed

The function listProxySettings() cannot possibly work and should be removed. It doesn't make sense to pass type ProxySettings to getObjectNameList():

def listProxySettings():
    """Returns list of names of all ProxySettings objects"""
    return getObjectNameList( 'ProxySettings' )

The type ProxySettings does not have attribute name:

'name String' in _splitlines(AdminConfig.attributes( 'ProxySettings' ))
0

And therefore getObjectNameList() will always throw an exception:

com.ibm.ws.scripting.ScriptingException: WASX7080E: Invalid attributes specified for type "ProxySettings" -- "name"

Double entries on Server when updating existing property

I found that the List of properties gotten in getObjectCustomProperty(object_id, propname) is in random order and is only returning the last item. When this item is not the searched for item the function returns a None and an extra entry (with the same name) is made in the server definition.

The solution I made was to explicit create a new list and fill this with the individual returned items.
See #####
def getObjectCustomProperty(object_id, propname):
"""Return the VALUE of the specified custom property of the server, or None if there is none by that name.
This is to be used only on objects that
store their custom properties as a list of Property objects in their 'properties'
attribute. This includes, for example, application servers and SIP containers.
There are other objects that store J2EEResourceProperty objects instead;
don't use this for those.

Intended for private use within wsadminlib only.
Write wrappers to be called externally.
"""
m = 'getObjectCustomProperty'

x = AdminConfig.showAttribute(object_id,'properties')[1:-1]
if len(x) == 0:
    return None  # no properties set yet
sop(m,"Property attribute value found=%s" % (x)) 
sop(m,"==>Property attribute value represent=%s" % repr(x)) 
#print "value of properties attribute=%s" % x
# This can be of the format "[foo(value) bar(baz)]" where the values are "foo(value)",
# "bar(baz)".  It also seems to be able to be just "foo(value)"
# FIXME: should we use _splitlist here?  If so, we need to change it to work when no [] are given
if x.startswith("["):
    propsidlist = x[1:-1].split(' ')
else:
    propsidlist = [x]
#print "List of properties = %s" % repr(propsidlist)
###### This block of code I added to make an explicit list
l1 = []
for id in propsidlist:
    #print "id=%s" % id
    l1.extend(id.strip().split(' '))
for id in l1: 
######### end block 
######### Note that for id in propsidlist: was here original. 
    sop(m,"id=%s" % id ) 
    name = AdminConfig.showAttribute(id, 'name')
    if name == propname:
        return AdminConfig.showAttribute(id, 'value')
return None

stringListListToDict()

The following test case fails:

stringListListToDict(AdminTask.getUserRegistryInfo('[-userRegistryType WIMUserRegistry]'))['ignoreCase'] 

The string returned from getUserRegistryInfo() includes null values in the form of [ [foo bar] [bar ] ] that are not handled correctly. Additionally, the exception handling is broken.

See pull request: Update stringListListToDict #13

cleanup wsadminlibsrv server during tests so they're repeatable

wsadminlibsrv server is created during the test script, but never deleted. The recreation is not gated with a check and causes the script to fail if rerun.

...also drop all but two setVirtualHostMimeTypeForExtension() from the tests, they are surprisingly slow and not super useful for testing wsadminlib.py itself.

Where do we get hitcount0.ear?

Hi, the tests file, wsadminlib.test.py, refers to a "sample app" called hitcount0.ear. Presumably you need that file to run the tests, but I can't find it anywhere. Does it exist?

Cheers,

Martin.

unsafe use of getServerId()

While suggesting a fix for #21, I found several functions that are not checking for a return value of None from getServerId(). This is unsafe because None is a valid scope pattern for functions like AdminConfig.list(object type, pattern). For example:

    server_id = getServerId(nodename,servername)
    pdefs = AdminConfig.list('JavaVirtualMachine', server_id)
    return pdefs.splitlines()[1]

If servername is not valid, the function AdminConfig.list() inadvertently returns every JavaVirtualMachine object in the configuration.

Many functions in the library are already checking for None from getServerId(). So the following functions should be updated for consistency:
getServerServantJvm()
getServerJvmExecution()
setSipStackProperties()
setServerSIPAttributes()
setChannelCustomProperty()
getSipContainerCustomProperty()
setSipContainerCustomProperty()
getOrbId()
createThreadPool()

createStringNameSpaceBinding worked incorrectly when the same string item want to be added on several scopes

Environment: WAS 9.0.0.2 / rhel 7.6

Steps:

  1. Create new string namespacebinding (possibly from was console) with scope=cluster with id=myid, name=myname, value=myvalue
  2. Save and sync
  3. call createStringNameSpaceBinding with the same values (id, name, value) and scope=getCellId()

Expected: string namespacebinding was created on cell
Actual: string namespacebinding not created. Raised error at wasadminlib.py#8816

"Traceback (most recent call last):", 
"  File \"<string>\", line 57, in <module>",  # my code
"  File \"<string>\", line 45, in create_binding", # my code
"  File \"wsadminlib.py\", line 8816, in createStringNameSpaceBinding", 
"    raise \"ERROR: A binding with nameInNamespace %s is already configured in this scope for bindingName %s.\" % (AdminConfig.showAttribute( ns, 'nameInNameSpace' ), AdminConfig.showAttribute( ns, 'name' ))"

Fixme for setCustomProperty should be solved

The setCustomProperty contains a fixme statement. When I tried to use it, it gave me an error message saying that certain properties could not be found for the name I gave.

My use case was that I had to set a lot of custom properties on the DB2 datasource, since the script does not set them by default. (when you configure the datasource using the web application the properties are set)

I managed to get the properties to work using the following code:

Configures a custom datasource property. Right now all the properties are set as strings, beware of this!

def definieerCustomDataSourceProperty(dataSource, key, value):
name = ['name', key]
type = ['type', 'java.lang.String']
description = ['description', ""]
val = ['value', value]
convertedValue = [name, type, description, val]
setFixedCustomProperty(dataSource, convertedValue)
AdminConfig.save()

Fixed version for the wsadminlib library, since that one does not work for my use case.

def setFixedCustomProperty(object_id, value):
propSet = AdminConfig.showAttribute(object_id, 'propertySet')
AdminConfig.create('J2EEResourceProperty', propSet, value)

Currently I can't create a pull request due to timing constraints.

deleteAllObjectsByName() does not work as described.

The function deleteAllObjectsByName() will only work if you pass it a type name. (e.g. 'SIPContainer', 'Library')

This fact is clear from the usage of getObjectNameList():
obj_ids = getObjectNameList( objectname )

The signature of getObjectNameList() implies it expects a type name:
def getObjectNameList( typename ):

The current function name and variable names are very confusing and inconsistent with many other functions in wsadminlib. From the code, this function should be redefined as:
def deleteAllObjectsByType( typename ):

Additionally, even if the user passes a valid type to the function, there will be problems. This line in the function:
id = getObjectByName( objectname, obj_id )
assumes that an object of a given type is uniquely named in the cell. For most objects this is not true.

My preference would be to delete this function. What is the use case of deleting all objects of a certain type? I think it is more dangerous for the user than it is useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.