Search code examples
weblogicweblogic9.xwlst

How to clone weblogic server in WLST (offline-mode)?


We have a custom weblogic template which has admin server (obviously) and a cluster with 2 managed servers deployed to the same machine. We use that template for our development environments. But now when we move on to performance testing environments, we need to introduce more machines.

It's easy to create a new machine in WLST (offline or online mode). But how do I clone a server (as all of the settings, except weblogic.Name and target machine are the same) in WLST?

It seems to be possible in WebLogic Administration Console, but we need to automate that.


Solution

  • I ended up doing defining the following functions:

    def createServer(wls, name):
      wls.cd("/")
      wls.create(name, 'Server')    
    
    
    ######################################################################
    # Checks if given bean exists
    ######################################################################
    def beanExists(wls, path, name):
      print "Checking if bean: '" + name + "' exists in path: '" + path + "'"
      wls.cd(path)
      wls.setShowLSResult(0)  
      beans = wls.ls('c', 'false', 'c')
      wls.setShowLSResult(1)  
      if (beans.find(name) != -1):
        print "Exists"  
        return 1
      else:
        print "Doesn't exist"  
        return 0
    
    
    ######################################################################
    # Copy bean properties in offline mode.
    ######################################################################
    def copyProperties(wls, originalBeanName, originalBeanPath, newBeanName, newBeanPath, ignoredProperties):
    
      wls.getCommandExceptionHandler().setMode(1)
      wls.getRuntimeEnv().set('exitonerror', 'true')
    
      srcPath = originalBeanPath + "/" + originalBeanName
      targetPath =  newBeanPath + "/" + newBeanName
    
      print "Coping properties from '" + srcPath + "' to '" + targetPath + "'"  
      wls.cd(srcPath)
    
      wls.setShowLSResult(0)
      attributes = wls.ls('a', 'true', 'a')
      children = wls.ls('c', 'true', 'c')
      wls.setShowLSResult(1)
    
      # Copy attributes.
      wls.cd(targetPath)
      for entry in attributes.entrySet(): 
        k = entry.key
        v = entry.value  
        if not(k in ignoredProperties) and not(v is None) and not(v == ''):
          print "Setting property '" + str(k) + "' = '" + str(v) + "' on '" + targetPath + "'"
          if isinstance(v, StringType):
            wls.set(k, v.replace(originalBeanName, newBeanName))
          else:
            wls.set(k, v)
    
      # Copy child bean values.
      for k in children:
        if not(k in ignoredProperties):
          srcBN = srcPath + "/" + k    
          targetBN = targetPath + "/" + k
          print "Coping bean '" + srcBN + "/" + originalBeanName + "'"
          print "Detected bean type as '" + k + "'"
          if beanExists(wls, srcBN, "NO_NAME_0"):      
            print "Changing to NO_NAME_0"
            originalBeanName = "NO_NAME_0"
            newBeanName = "NO_NAME_0"        
          wls.cd(targetPath)        
          wls.create(newBeanName, k)  
          copyProperties(wls, originalBeanName, srcBN, newBeanName, targetBN, ignoredProperties)
    

    After that just call:

    createServer(WLS, 'ServiceServer3')
    copyProperties(WLS, 'ServiceServer1', '/Servers', 'ServiceServer3', '/Servers', ['SSL'])
    

    UPDATE: I wrote more about it in my blog: http://blog.aestasit.com/cloning-objects-in-wlst-offline/. Also the script was tested on WebLogic 11g.