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.

No comments: