Tuesday 25 March 2014

OSB: Remove artefacts with wlst

In my current project I needed to restructure my projects. Where I had all projects in one configuration, I wanted multiple configurations to do partial deployments. So I created configurations per highlevel business service, and merged canonical and application specific services into one osb project. But at deployment time, for OSB moved services are actually new services that conflict with the existing services at the current location, because now there are two versions of the same service with the same endpoint-uri exist. That is, it gets OSB in a conflicting state, that it can't activate. So I created a series of scripts to remove artefacts. My starting point was this Oracle forum entry.
  
import wlstModule

from java.util import Collections
from com.bea.wli.sb.util import Refs
from com.bea.wli.config import Ref
from com.bea.wli.sb.management.configuration import SessionManagementMBean
from com.bea.wli.sb.management.configuration import ALSBConfigurationMBean
#from com.bea.wli.config.component import AlreadyExistsException

import sys


#=======================================================================================
# Entry function to delete a  project from the alsbConfiguration 
#=======================================================================================
def deleteProject(alsbConfigurationMBean, projectName):
     try:
          print "Trying to remove " + projectName
          projectRef = Ref(Ref.PROJECT_REF, Ref.DOMAIN, projectName)                  
          if alsbConfigurationMBean.exists(projectRef):
               print "#### removing OSB project: " + projectName
               alsbConfigurationMBean.delete(Collections.singleton(projectRef))
               print "#### removed project: " + projectName
          else:
               failed = "OSB project <" + projectName + "> does not exist"
               print failed
          print
     except:
          print "Error whilst removing project:", sys.exc_info()[0]
          raise
  
#=======================================================================================
# Entry function to undeploy a project from an OSB Configuration
#=======================================================================================
def undeployProjectFromOSBDomain(projectName):
     try:
          domainRuntime()      
          sessionName  = "UndeployProjectStateSession_" + str(System.currentTimeMillis())         
          sessionManagementMBean = findService(SessionManagementMBean.NAME,SessionManagementMBean.TYPE)
          print "SessionMBean started session"
          sessionManagementMBean.createSession(sessionName)
          print 'Created session <', sessionName, '>'
          alsbConfigurationMBean = findService(ALSBConfigurationMBean.NAME + "." + sessionName, ALSBConfigurationMBean.TYPE)
          deleteProject(alsbConfigurationMBean, projectName)   
          sessionManagementMBean.activateSession(sessionName, "Complete project removal with customization using wlst")
     except:
          print "Error whilst removing project:", sys.exc_info()[0]
          discardSession(sessionManagementMBean, sessionName)
          raise

#=======================================================================================
# Entry function to undeploy multiple projects from an OSB Configuration
#=======================================================================================
def undeployProjectsFromOSBDomain(projectNames):
     try:
          domainRuntime()      
          sessionName  = "UndeployProjectStateSession_" + str(System.currentTimeMillis())         
          sessionManagementMBean = findService(SessionManagementMBean.NAME,SessionManagementMBean.TYPE)
          print "SessionMBean started session"
          sessionManagementMBean.createSession(sessionName)
          print 'Created session <', sessionName, '>'
          alsbConfigurationMBean = findService(ALSBConfigurationMBean.NAME + "." + sessionName, ALSBConfigurationMBean.TYPE)
          for projectName in projectNames:
            print '\nDelete project '+projectName
            deleteProject(alsbConfigurationMBean, projectName)   
          sessionManagementMBean.activateSession(sessionName, "Complete project removal with customization using wlst")
     except:
          print "Error whilst removing project:", sys.exc_info()[0]
          discardSession(sessionManagementMBean, sessionName)
          raise

def discardSession(sessionManagementMBean, sessionName):
     if sessionManagementMBean != None:
          if sessionManagementMBean.sessionExists(sessionName):
               sessionManagementMBean.discardSession(sessionName)
               print "Session discarded" 
I did a little restructure so that I can remove multiple projects in one session. This is needed since some projects are inter-related. So must be removed together to get a consistent state. To remove multiple projects I have the following main function:
def main():
  projectNames=["Project1", "Project2", "Project3", "Project4", "Project5", "Project6", "Project7", "Etc." ]
  undeployProjectsFromOSBDomain(projectNames)
# Call the main function
main()
I call this script from an ANT based patch-framework. To succesfully call this wlst script you need the following jar files in your classpath:
  • ${osb.home}/modules/com.bea.common.configfwk_1.7.0.0.jar 
  • ${osb.home}/lib/sb-kernel-api.jar 
  • ${osb.home}/sb-kernel-impl.jar 
  • ${osb.home}/osb-coherence-client.jar (I don't understand why, but this is needed for the Ref-class that is also available in the com.bea.common.configfwk_1.7.0.0.jar) 
But you should also be ok with:
  • ${osb.home}/modules/com.bea.common.configfwk_1.7.0.0.jar 
  • ${osb.home}/lib/alsb.jar (this jar contains references to the other alsb-jar files in the lib folder in it's manifest file)
Here '${osb.home}' relates to the 'Oracle_OSB1' folder in your Fusion Middleware Home.
Based on the scripts above I also created a function to remove a single Proxy service:
#=======================================================================================
# Entry function to find a proxy service
#=======================================================================================
def findProxyService(folder, serviceName, sessionName):
  print "Find proxy service: " + folder + "/" + serviceName
  pxyConf = "ProxyServiceConfiguration." + sessionName
  mbean = findService(pxyConf, 'com.bea.wli.sb.management.configuration.ProxyServiceConfigurationMBean')
  folderRef = Refs.makeParentRef(folder + '/')
  serviceRef = Refs.makeProxyRef(folderRef, serviceName)
  return serviceRef, mbean
#=======================================================================================
# Entry function to remove a proxyservice from OSB configuration 
#=======================================================================================
def undeployProxyFromOSBDomain(relativePath, proxyServiceName):
     try:
          domainRuntime()
          
          sessionName  = "UndeployProxySession_" + str(System.currentTimeMillis())
          print "Trying to remove " + proxyServiceName
          
          sessionManagementMBean = findService(SessionManagementMBean.NAME,SessionManagementMBean.TYPE)
          print "SessionMBean started session"
          sessionManagementMBean.createSession(sessionName)
          print 'Created session <', sessionName, '>'
          serviceRef, sessionBean = findProxyService(relativePath, proxyServiceName, sessionName)
          alsbConfigurationMBean = findService(ALSBConfigurationMBean.NAME + "." + sessionName, ALSBConfigurationMBean.TYPE)
          if alsbConfigurationMBean.exists(serviceRef):
            print "#### removing OSB proxy service: " + proxyServiceName
            alsbConfigurationMBean.delete(Collections.singleton(serviceRef))
            sessionManagementMBean.activateSession(sessionName, "Complete service removal with customization using wlst")
          else:
            failed = "OSB project <" + proxyServiceName + "> does not exist"
            print failed
            discardSession(sessionManagementMBean, sessionName)
            #raise Failure(failed)
          print
     except:
          print "Error whilst removing project:", sys.exc_info()[0]
          discardSession(sessionManagementMBean, sessionName)
          raise
It should not be too hard to create one for business services out of this one. Removing folders is slightly easier:
#=======================================================================================
# Entry function to remove a folder from OSB configuration 
#=======================================================================================
def removeFolderFromOSBDomain(folder):
     try:
          domainRuntime()
         
          sessionName  = "RemoveFolderSession_" + str(System.currentTimeMillis())
          print "Trying to remove " + folder
          
          sessionManagementMBean = findService(SessionManagementMBean.NAME,SessionManagementMBean.TYPE)
          print "SessionMBean started session"
          sessionManagementMBean.createSession(sessionName)
          print 'Created session <', sessionName, '>'
          folderRef = Refs.makeParentRef(folder)
          alsbConfigurationMBean = findService(ALSBConfigurationMBean.NAME + "." + sessionName, ALSBConfigurationMBean.TYPE)
          if alsbConfigurationMBean.exists(folderRef):
            print "#### removing OSB folder: " + folder
            alsbConfigurationMBean.delete(Collections.singleton(folderRef))
            sessionManagementMBean.activateSession(sessionName, "Complete service removal with customization using wlst")
          else:
            failed = "OSB folder <" + folder + "> does not exist"
            print failed
            discardSession(sessionManagementMBean, sessionName)
            #raise Failure(failed)
          print
     except:
          print "Error whilst removing project:", sys.exc_info()[0]
          discardSession(sessionManagementMBean, sessionName)
          #raise
Don't forget the import statements from the first script for these latter ones.

8 comments :

Unknown said...

Hi sir, it's very good article, but when I'm trying to run my script I'm getting following exception.
java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: com/bea/wli/config/component/AlreadyExistsException

what could be the possible reason, I have set all classpaths

Anonymous said...

Hi,

I do think you probably miss a jar. Maybe because you're on a different patch level than I was when writing this article. So try to find out in which library the class com/bea/wli/config/component/AlreadyExistsException is delivered in your case and add that to your classpath.

I've no installment available at this moment. You could try the same question on https://community.oracle.com/community/fusion_middleware/soa_and_process_management/soa_suite_3.

Regards,
Martien

Srikanth Reddy K said...

Hi, Good post.

I have tried it in my Windows Machine and it worked like charm. But when I tried it in my Linux Machine, I got the following error:

Location changed to domainRuntime tree. This is a read-only tree
with DomainMBean as the root MBean.
For more help, use help('domainRuntime')

Error whilst removing project: java.lang.RuntimeException
Problem invoking WLST - Traceback (innermost last):
File "/scratch/idxAdmin/jenkins-workspace/OSB_Export/export.py", line 84, in ?
File "/scratch/idxAdmin/jenkins-workspace/OSB_Export/export.py", line 82, in main
File "/scratch/idxAdmin/jenkins-workspace/OSB_Export/export.py", line 71, in undeployProjectsFromOSBDomain
UnboundLocalError: local: 'sessionManagementMBean'


Could you please help in this?


Anonymous said...

HI,

I'm not sure, but I suspect that the wlst.sh you run on Linux is from a different home or place in the home and lacks the particular class(es) and/or jars.

I'd recommend to check which wlst.sh you use. And ask the same question on community.oracle.com in the SOASuite section.

Regards,
Martien

Srikanth Reddy K said...

I have deployed OSB application on a Linux server. I am able to undeploy the OSB application by running the script mentioned in the earlier post from my windows machine.

Then I try to run the same script from Linux server but is not working.

Not sure why this WLST is working on Windows but not on Linux.


Anonymous said...

Hi Srikanth,

Maybe you could post the question on community.oracle.com, in the weblogic or SOASuite forum.

Then you could elaborate on the fault messages you're getting.


Regards,
Martien

Anonymous said...

Mooi script, werkt ook nog prima in 12c :)

Groeten,
Richard

Anonymous said...

Hi Martien,

Getting below error during deletion

Delete project FUSION-LOGICAL-DEVICECONFIGURATION
Error whilst removing project: exceptions.TypeError
Session discarded
Problem invoking WLST - Traceback (innermost last):
File "/path/abc/osbdelete.py", line 65, in ?
File "/path/abc/osbdelete.py", line 63, in main
File "/path/abc/osbdelete.py", line 48, in undeployProjectsFromOSBDomain
File "/abc/products/Middleware/soa/common/wlst/bpm-wlst-commands.jar!/wlstScriptDir/bpmLifecycleTasks.py", line 75, in deleteProject
TypeError: oracle.bpm.alm.script.legacy.operation.DeleteOperation(): 1st arg can't be coerced to String

I am using wlst and have set osb env too.

BR,
saurav