Wednesday, November 7, 2012

OSB deployment using scripts

We can deploy(import) osb project to sbconsole using the wlst script. Exporting of the project from sbconsole and from eclipse workspace also can be done using scripts. Here are the steps

Download import.py and export.py to a local directory(deploy directory)

Create a build properties file in the deploy directory as below.

build.properties
=============

fmw.home=D:/Oracle/Middleware

wls.username      = weblogic
wls.password      = weblogic1
wls.server        = t3://localhost:7001

# properties for workspace export
#config.project="OSB Configuration"
#config.jar=D:/temp/sbconfig.jar
#config.subprojects="ASynchronous"
#config.includeDependencies=true
#workspace.dir=C:/eclipse/user/workspace

# export properties

# specify a project or use None
#export.project    = EJB
export.project    = None
export.jar        = D:/temp/sbconfig.jar
#export.customFile = c:/temp/cust.xml
export.customFile = None

# import properties

# specify a project or use None
import.project    = None
#import.project    = EJB
import.jar        = D:/temp/sbconfig.jar
import.customFile = None
import.customFile = D:/temp/ALSBCustomizationFile.xml

==================end of build.properties==============
Create a build.xml file as below in the deploy.directory

build.xml
===============

<?xml version="1.0" encoding="windows-1252" ?>
<project name="ConfigExport">

<property file="./build.properties"/>
<property name="bea.home"               value="${fmw.home}"/>
<property name="build"                  value="build"/>
<property name="dist"                   value="dist"/>
<property name="domain.export.script"   value="export.py"/>
<property name="domain.import.script"   value="import.py"/>
<property name="eclipse.home" value="${fmw.home}/oepe_11gR1PS4"/>
<property name="export.config.file"     value="export.properties"/>
<property name="import.config.file"     value="import.properties"/>
<property name="metadata.dir" value="${workspace.dir}/.metadata"/>
<property name="osb.home" value="${fmw.home}/Oracle_OSB1"/>
<property name="weblogic.home" value="${fmw.home}/wlserver_10.3"/>

  <path id="class.path">
  <pathelement path="${bea.home}/wlserver_10.3/server/lib/weblogic.jar"/>
  <pathelement path="${bea.home}/Oracle_OSB1/lib/sb-kernel-api.jar"/>
  <pathelement path="${bea.home}/Oracle_OSB1/lib/sb-kernel-impl.jar"/>
        <pathelement path="${bea.home}/Oracle_OSB1/modules/com.bea.common.configfwk_1.6.0.0.jar"/>
  </path>


  <taskdef classpath="${bea.home}/wlserver_10.3/server/lib/weblogic.jar" name="wlst" classname="weblogic.ant.taskdefs.management.WLSTTask"/>


   <target name="exportFromWorkspace">
      <!--delete failonerror="false" includeemptydirs="true"
              dir="${metadata.dir}"/-->
      <java dir="${eclipse.home}"
            jar="${eclipse.home}/plugins/org.eclipse.equinox.launcher_1.1.1.R36x_v20101122_1400.jar"
            fork="true" failonerror="true" maxmemory="768m">
         <jvmarg line="-XX:MaxPermSize=256m"/>
         <arg line="-data ${workspace.dir}"/>
         <arg line="-application com.bea.alsb.core.ConfigExport"/>
         <arg line="-configProject ${config.project}"/>
         <arg line="-configJar ${config.jar}"/>
         <arg line="-configSubProjects ${config.subprojects}"/>
         <arg line="-includeDependencies ${config.includeDependencies}"/>
         <sysproperty key="weblogic.home" value="${weblogic.home}"/>
         <sysproperty key="osb.home" value="${osb.home}"/>
         <sysproperty key="osgi.bundlefile.limit" value="500"/>
         <sysproperty key="harvester.home" value="${osb.home}/harvester"/>
         <sysproperty key="osgi.nl" value="en_US"/>
         <sysproperty key="sun.lang.ClassLoader.allowArraySyntax" value="true"/>
      </java>
   </target>

  <target name="export">
    <echo message="exportscript: ${domain.export.script}"/>
    <java classname="weblogic.WLST" fork="true">
        <arg line="${domain.export.script} ${export.config.file}"/>
        <classpath refid="class.path"/>
    </java>
  </target>

  <target name="import">
    <echo message="importscript: ${domain.import.script}"/>
    <java classname="weblogic.WLST" fork="true">
        <arg line="${domain.import.script} ${import.config.file}"/>
        <classpath refid="class.path"/>
    </java>
  </target>

  <target name="clean">
    <delete dir="${dist}"/>
    <delete dir="${build}"/>
    <mkdir dir="${dist}"/>
    <mkdir dir="${build}"/>
  </target>

 
</project>

=========================== end of build.xml==============

Create an import.properties as below in the deploy directory

import.porperties
==============================

##################################################################
# OSB Admin Security Configuration                              #
##################################################################
adminUrl=t3://localhost:7001
importUser=weblogic
importPassword=weblogic1

##################################################################
# OSB Jar to be exported, optional customization file           #
##################################################################
importJar=D:/temp/sbconfig.jar
customizationFile=D:/temp/ALSBCustomizationFile.xml

##################################################################
# Optional passphrase and project name                           #
##################################################################
#passphrase=osb
#project=default
==================end of import.properties==============

Create a batch file to set the class path

deploy.bat
=============

set FMW_HOME=D:\Oracle\Middleware
set ANT_HOME=%FMW_HOME%\modules\org.apache.ant_1.7.1
set PATH=%ANT_HOME%\bin;%PATH%
set JAVA_HOME=C:\jdk1.7.0
set CLASSPATH=%FMW_HOME%/wlserver_10.3/server/lib/weblogic.jar;%FMW_HOME%/Oracle_OSB1/lib/alsb.jar;%FMW_HOME%/Oracle_OSB1/modules/com.bea.common.configfwk_1.6.0.0.jar

============== end of deploy.bat====================



Open command prompt and navigate to the deploy directory
Run the deploy.bat
To deploy/import run the command :  ant -verbose import
To export from workspace run the command :  ant -verbose exportFromWorkspace


export.py
============
from java.io import FileInputStream
from java.io import FileOutputStream
from java.util import ArrayList
from java.util import Collections
from com.bea.wli.sb.util import EnvValueTypes
from com.bea.wli.config.env import EnvValueQuery;
from com.bea.wli.config import Ref
from com.bea.wli.config.customization import Customization
from com.bea.wli.config.customization import FindAndReplaceCustomization
import sys
#====================================================================
# Utility function to load properties from a config file
#=========================================================================
def exportAll(exportConfigFile):
try:
print "Loading export config from :", exportConfigFile
exportConfigProp = loadProps(exportConfigFile)
adminUrl = exportConfigProp.get("adminUrl")
exportUser = exportConfigProp.get("exportUser")
exportPasswd = exportConfigProp.get("exportPassword")
exportJar = exportConfigProp.get("exportJar")
customFile = exportConfigProp.get("customizationFile")
passphrase = exportConfigProp.get("passphrase")
project = exportConfigProp.get("project")
connectToServer(exportUser, exportPasswd, adminUrl)
ALSBConfigurationMBean = findService("ALSBConfiguration", "com.bea.wli.sb.management.configuration.ALSBConfigurationMBean")
print "ALSBConfiguration MBean found"
print project
if project == None :
ref = Ref.DOMAIN
collection = Collections.singleton(ref)
if passphrase == None :
print "Export the config"
theBytes = ALSBConfigurationMBean.export(collection, true, None)
else:
print "Export and encrypt the config"
theBytes = ALSBConfigurationMBean.export(collection, true,passphrase)
else:
ref = Ref.makeProjectRef(project);
print "Export the project", project
collection = Collections.singleton(ref)
theBytes = ALSBConfigurationMBean.exportProjects(collection,passphrase)
aFile = File(exportJar)
out = FileOutputStream(aFile)
out.write(theBytes)
out.close()
print "ALSB Configuration file: "+ exportJar + " has been exported"
if customFile != None:
print collection
query = EnvValueQuery(None,
Collections.singleton(EnvValueTypes.WORK_MANAGER), collection, false, None, false)
customEnv = FindAndReplaceCustomization('Set the right Work Manager', query, 'Production System Work Manager')
print 'EnvValueCustomization created'
customList = ArrayList()
customList.add(customEnv)
print customList
aFile = File(customFile)
out = FileOutputStream(aFile)
Customization.toXML(customList, out)
out.close()
print "ALSB Dummy Customization file: "+ customFile + " has been created"
except:
raise
#====================================================================
# Utility function to load properties from a config file
#=========================================================================
def loadProps(configPropFile):
propInputStream = FileInputStream(configPropFile)
configProps = Properties()
configProps.load(propInputStream)
return configProps
#====================================================================
# Connect to the Admin Server
#=========================================================================
def connectToServer(username, password, url):
connect(username, password, url)
domainRuntime()
# EXPORT script init
try:
exportAll(sys.argv[1])
except:
print "Unexpected error: ", sys.exc_info()[0]
dumpStack()
raise

import.py
==================
from java.util import HashMap
from java.util import HashSet
from java.util import ArrayList
from java.io import FileInputStream

from com.bea.wli.sb.util import Refs
from com.bea.wli.config.customization import Customization
from com.bea.wli.sb.management.importexport import ALSBImportOperation

import sys

#=======================================================================================
# Entry function to deploy project configuration and resources
#        into a ALSB domain
#=======================================================================================

def importToALSBDomain(importConfigFile):
    try:
        SessionMBean = None
        print '============Loading Deployment config from :==========', importConfigFile
        exportConfigProp = loadProps(importConfigFile)
        adminUrl = exportConfigProp.get("adminUrl")
        importUser = exportConfigProp.get("importUser")
        importPassword = exportConfigProp.get("importPassword")

        importJar = exportConfigProp.get("importJar")
        customFile = exportConfigProp.get("customizationFile")

        passphrase = exportConfigProp.get("passphrase")
        project = exportConfigProp.get("project")

        connectToServer(importUser, importPassword, adminUrl)

        print 'Attempting to import :', importJar, "on ALSB Admin Server listening on :", adminUrl

        theBytes = readBinaryFile(importJar)
        print 'Read file', importJar
        sessionName = createSessionName()
        print 'Created session', sessionName
        SessionMBean = getSessionManagementMBean(sessionName)
        print 'SessionMBean started session'
        ALSBConfigurationMBean = findService(String("ALSBConfiguration.").concat(sessionName), "com.bea.wli.sb.management.configuration.ALSBConfigurationMBean")
        print "ALSBConfiguration MBean found", ALSBConfigurationMBean
        ALSBConfigurationMBean.uploadJarFile(theBytes)
        print 'Jar Uploaded'

        if project == None:
            print 'No project specified, additive deployment performed'
            alsbJarInfo = ALSBConfigurationMBean.getImportJarInfo()
            alsbImportPlan = alsbJarInfo.getDefaultImportPlan()
            alsbImportPlan.setPassphrase(passphrase)
            alsbImportPlan.setPreserveExistingEnvValues(true)
            importResult = ALSBConfigurationMBean.importUploaded(alsbImportPlan)
            SessionMBean.activateSession(sessionName, "Complete test import with customization using wlst")
        else:
            print 'ALSB project', project, 'will get overlaid'
            alsbJarInfo = ALSBConfigurationMBean.getImportJarInfo()
            alsbImportPlan = alsbJarInfo.getDefaultImportPlan()
            alsbImportPlan.setPassphrase(passphrase)
            operationMap=HashMap()
            operationMap = alsbImportPlan.getOperations()
            print
            print 'Default importPlan'
            printOpMap(operationMap)
            set = operationMap.entrySet()

            alsbImportPlan.setPreserveExistingEnvValues(true)

            #boolean
            abort = false
            #list of created ref
            createdRef = ArrayList()

            for entry in set:
                ref = entry.getKey()
                op = entry.getValue()
                #set different logic based on the resource type
                type = ref.getTypeId
                if type == Refs.SERVICE_ACCOUNT_TYPE or type == Refs.SERVICE_PROVIDER_TYPE:
                    if op.getOperation() == ALSBImportOperation.Operation.Create:
                        print 'Unable to import a service account or a service provider on a target system', ref
                        abort = true
                elif op.getOperation() == ALSBImportOperation.Operation.Create:
                    #keep the list of created resources
                    createdRef.add(ref)

            if abort == true :
                print 'This jar must be imported manually to resolve the service account and service provider dependencies'
                SessionMBean.discardSession(sessionName)
                raise

            print
            print 'Modified importPlan'
            printOpMap(operationMap)
            importResult = ALSBConfigurationMBean.importUploaded(alsbImportPlan)

            printDiagMap(importResult.getImportDiagnostics())

            if importResult.getFailed().isEmpty() == false:
                print 'One or more resources could not be imported properly'
                raise

            #customize if a customization file is specified
            #affects only the created resources
            if customFile != None :
                print 'Loading customization File', customFile
                print 'Customization applied to the created resources only', createdRef
                iStream = FileInputStream(customFile)
                customizationList = Customization.fromXML(iStream)
                filteredCustomizationList = ArrayList()
                setRef = HashSet(createdRef)

                # apply a filter to all the customizations to narrow the target to the created resources
                for customization in customizationList:
                    print customization
                    newcustomization = customization.clone(setRef)
                    filteredCustomizationList.add(newcustomization)

                ALSBConfigurationMBean.customize(filteredCustomizationList)

            SessionMBean.activateSession(sessionName, "Complete test import with customization using wlst")

        print "Deployment of : " + importJar + " successful"
    except:
        print "Unexpected error:", sys.exc_info()[0]
        if SessionMBean != None:
            SessionMBean.discardSession(sessionName)
        raise

#=======================================================================================
# Utility function to print the list of operations
#=======================================================================================
def printOpMap(map):
    set = map.entrySet()
    for entry in set:
        op = entry.getValue()
        print op.getOperation(),
        ref = entry.getKey()
        print ref
    print

#=======================================================================================
# Utility function to print the diagnostics
#=======================================================================================
def printDiagMap(map):
    set = map.entrySet()
    for entry in set:
        diag = entry.getValue().toString()
        print diag
    print

#=======================================================================================
# Utility function to load properties from a config file
#=======================================================================================

def loadProps(configPropFile):
    propInputStream = FileInputStream(configPropFile)
    configProps = Properties()
    configProps.load(propInputStream)
    return configProps

#=======================================================================================
# Connect to the Admin Server
#=======================================================================================

def connectToServer(username, password, url):
    connect(username, password, url)
    domainRuntime()

#=======================================================================================
# Utility function to read a binary file
#=======================================================================================
def readBinaryFile(fileName):
    file = open(fileName, 'rb')
    bytes = file.read()
    return bytes

#=======================================================================================
# Utility function to create an arbitrary session name
#=======================================================================================
def createSessionName():
    sessionName = String("SessionScript"+Long(System.currentTimeMillis()).toString())
    return sessionName

#=======================================================================================
# Utility function to load a session MBeans
#=======================================================================================
def getSessionManagementMBean(sessionName):
    SessionMBean = findService("SessionManagement", "com.bea.wli.sb.management.configuration.SessionManagementMBean")
    SessionMBean.createSession(sessionName)
    return SessionMBean



# IMPORT script init
try:
    # import the service bus configuration
    # argv[1] is the export config properties file

    importToALSBDomain(sys.argv[1])

except:
    print "Unexpected error: ", sys.exc_info()[0]
    dumpStack()
    raise


6 comments:

  1. sure would be nice if you provided a way to download import.py and export.py....

    bueller?

    ReplyDelete
  2. Hi

    This is great working script... I am looking for a feature that needs to be included in deployment automations. For development/testing environments, the frequency of deployments are quite high... I was wondering if there was a way a log the events that happen from start to end of a deployment in DEBUG mode and store it in a log file. I can send this file to the developers for troubleshooting purposes if a deployments fails or does not work as per their expectations.

    I tried this solution..
    http://www.javamonamour.org/2011/08/wlst-redirecting-all-output-to-file.html
    but it just logs the print commands of the script. I was wondering if it can also include the java deployment logs in this...
    I have been looking for a way to do this... but with my limited scripting skills I am finding it hard to find a solution... can u provide some help..

    Thanks

    Anto

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I have a limited knowledge on OSB so curious to know if there a way to "export from workspace" using a single liner command.... I.e caliing export.py with some parameters.

    If its not possible. Request you to please help to understand, Objective is to get command out of line of code mentioned above in your script for export from workspace.

    ReplyDelete