Friday, March 9, 2012

Weblogic and ADF : migrateSecurityStore the hard way

I was struggeling to use WSLT to migrate my policy store for our production cluster there where classpath problems and naming problems and I just want to make the process easier for my self.

So I wrote a little util to do the migration if nothing else this post will help me remeber the classes needed for the migration exercise.

Script and classpath (I can post my linux script if anyone wants it)
My jar file that you wont have but I will post the code: common.0.0.1.jar, ./migrator.jar
Non adf etc 3rd party jars: ./lib/commons-cli-1.2.jar;./lib/commons-io-2.0.1.jar;

SET WEBLOGIC_JAR=[your Middleware location]\wlserver_10.3\server\lib\weblogic.jar
SET OLD_CLASSPATH=%CLASSPATH%
SET CLASSPATH=%CLASSPATH%;%WEBLOGIC_JAR%;./lib/adf-controller-security.jar;./migrator.jar;./lib/adf-share-mbeans-wlst.jar;./lib/adfscripting.jar;./lib/commons-cli-1.2.jar;./lib/commons-io-2.0.1.jar;./lib/commons-logging-1.1.1.jar;./lib/common.0.0.1.jar;./lib/ldapjclnt11.jar;./lib/adf-share-base.jar;./lib/adf-share-security.jar;./lib/adf-share-ca.jar;./lib/adfm.jar;./lib/oracle.webservices.standalone.client.jar

%JAVA_HOME%\bin\java za.co.test.adf.common.migrate.MigrationUtil -z C:\Dev\Middleware\user_projects\domains\dev_domain\config\fmwconfig\system-jazn-data.xml -e C:\Dev\wb-ops.0.0.1.ear

SET CLASSPATH=%OLD_CLASSPATH%

Scripts directory
In the directory I run the java from ./scripts I copyed all the .py files needed for my migrator to work.
These can be found in [your Middleware location]\oracle_common\modules\oracle.jps_11.1.1\common\wlstscripts

Migration parameters and what the class does


The migration class will take the location of a jazn-data.xml file OR an ear containing the jazn-data.xml create a temporary migration jps file call the python migration script and write the policies to your specified system-jazn-data.xml file.


-s or --src source policy config name : defaults appPolicy
-d or --dst destination policy config name : defaults domainPolicy
-c or --script Location of all the .py scripts to run wlst
-j or --jazn jazn file to import location
-z or sysjazn system jazn file to copy to full location
-e or --ear Ear file to extract jazn file from : cant be used with jazn");


typical usage is just to use -e ear location and the -z system-jazn location parameters.
 
Java code stuff


Note : 

You can replace the custom streamuilts stuff as follows:

String fileContent = StreamUtil.drainToString(new FileReader(DEFAULT_MIGRATION_SCRIPT_LOCATION));
with

String fileContent =  new String (IOUtils.toByteArray(new FileInputStream(DEFAULT_MIGRATION_SCRIPT_LOCATION)));

package za.co.test.adf.common.migrate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import java.text.MessageFormat;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.io.IOUtils;

import org.python.util.InteractiveInterpreter;

import weblogic.management.scripting.utils.WLSTInterpreter;

import za.co.test.adf.common.util.StreamUtil;


public class MigrationUtil {
  static InteractiveInterpreter interpreter = null;
  private static final String DEFAULT_SCRIPT_LOCATION = "./scripts";
  private static final String DEFAULT_TMP_LOCATION = "./tmp";
  private static final String DEFAULT_WLST_SCRIPT_LOCATION
    = DEFAULT_SCRIPT_LOCATION + "/wlst";
  private static final String DEFAULT_MIGRATION_SCRIPT_LOCATION
    = DEFAULT_SCRIPT_LOCATION + "/migrate-jps-config.xml";
  private static final String DEFAULT_JAZN_SUFFIX = "/jazn-data.xml";
  private static final String SOURCE_OPTION_NAME = "s";
  private static final String DESTINATION_OPTION_NAME = "d";
  private static final String JAZN_OPTION_NAME = "j";
  private static final String SYSJAZN_OPTION_NAME = "z";
  private static final String EAR_OPTION_NAME = "e";
  private static final String SCRIPT_OPTION_NAME = "c";
  public MigrationUtil() {
    super();
  }

  public static void main(String[] args) throws IOException, ParseException {
    String scriptLocation = DEFAULT_WLST_SCRIPT_LOCATION;
    String source = "appPolicy";
    String destination = "domainPolicy";
    String jaznFile = null;
    String systemJaznLocation = null;
    String migrationFileEar = null;

    args = new String[] { "--sysjazn", "C:/Dev/Middleware/user_projects/domains"
      +"/dev_domain/config/fmwconfig/system-jazn-data.xml", "-e",
      "C:\\Dev\\Projects\\WesbankADF\\deploy\\wb-home.0.0.1.ear"};
    CommandLineParser parser = new PosixParser();
    Options options = createOptions();
    CommandLine cmd = parser.parse(options, args);
    if(cmd.hasOption(SOURCE_OPTION_NAME)) {
      source = cmd.getOptionValue(SOURCE_OPTION_NAME);
    }
    if(cmd.hasOption(DESTINATION_OPTION_NAME)) {
      destination = cmd.getOptionValue(DESTINATION_OPTION_NAME);
    }
    if(cmd.hasOption(SCRIPT_OPTION_NAME)) {
      scriptLocation = cmd.getOptionValue(SCRIPT_OPTION_NAME);
    }
    if(cmd.hasOption(JAZN_OPTION_NAME)) {
      jaznFile = cmd.getOptionValue(JAZN_OPTION_NAME);
    }
    if(cmd.hasOption(EAR_OPTION_NAME)) {
      migrationFileEar = cmd.getOptionValue(EAR_OPTION_NAME);
    }
    if((jaznFile == null || jaznFile.length() < 1) &&
     (migrationFileEar == null || migrationFileEar.length() < 1)) {
      throw new ParseException(
        "Either ear option name (-e) or jazn file name (-j) must be populated");
    }

    if(cmd.hasOption(SYSJAZN_OPTION_NAME)) {
      systemJaznLocation = cmd.getOptionValue(SYSJAZN_OPTION_NAME);
    }

    systemJaznLocation = systemJaznLocation.replaceAll("\\\\", "/");
    System.out.println("Performing migration....");
    migrate(scriptLocation, source, destination, jaznFile,
      migrationFileEar, systemJaznLocation);
    System.out.println(".... Done .....");
  }

  public static void migrate(String scriptLocation, String source,
   String destination, String migrationFile,
    String migrationFileEar, String systemJaznLocation) throws IOException {
    StringBuilder builder = null;
    interpreter = new WLSTInterpreter();
    builder = new StringBuilder("sys.path.append('").append(scriptLocation)
      .append("')");
    interpreter.exec(builder.toString());
    interpreter.exec("import sys");
    interpreter.exec("import imp");
    interpreter.exec("import jpsWlstCmd");
    interpreter.exec("srcApp = None");
    interpreter.exec("dstApp = None");
    interpreter.exec("srcFolder = None");
    interpreter.exec("dstFolder = None");
    interpreter.exec("dstLdifFile = None");
    interpreter.exec("srcConfigFile = None");
    interpreter.exec("processPrivRole = None");
    interpreter.exec("resourceTypeFile = None");
    interpreter.exec("overWrite = None");
    interpreter.exec("migrateIdStoreMapping = None");
    interpreter.exec("preserveAppRoleGuid = None");
    interpreter.exec("reportFile = None");
    interpreter.exec("mode = None");
    String configFile =
      getGenerateMigrationJPSFile(source, destination,
        migrationFile, migrationFileEar, systemJaznLocation);
    configFile = configFile.replaceAll("\\\\", "/");
    builder =
        new StringBuilder("jpsWlstCmd.migrateSecurityStore("
          +"type=\"policyStore\", src=\"").append(source).append("\", dst=\"")
         .append(destination)
         .append("\", srcApp=srcApp, dstApp=dstApp, srcFolder=srcFolder,"
         +" dstFolder=dstFolder, dstLdifFile=dstLdifFile, "
         +"srcConfigFile=srcConfigFile, configFile=\"")
         .append(configFile).append("\", processPrivRole=processPrivRole, resourceTypeFile=resourceTypeFile, overWrite=overWrite, migrateIdStoreMapping=migrateIdStoreMapping, preserveAppRoleGuid=preserveAppRoleGuid, reportFile=reportFile, mode=mode)");
    interpreter.exec(builder.toString());
  }

  private static String getGenerateMigrationJPSFile(String source, String destination, String fileName, String earFile,
    String systemJaznLocation) throws IOException {

    File tmpFileLocation = new File(DEFAULT_TMP_LOCATION);
    if(!tmpFileLocation.exists()) {
      tmpFileLocation.mkdirs();
    }

    String fileContent = StreamUtil.drainToString(
      new FileReader(DEFAULT_MIGRATION_SCRIPT_LOCATION));
    String jaznFile = formulateMigrationJAZNFile(
      fileName, earFile, tmpFileLocation);
    jaznFile = jaznFile.replaceAll("\\\\", "/");
    fileContent = MessageFormat.format(fileContent, 
      source, destination, systemJaznLocation, jaznFile);
    File tempFile = createTemporaryFile(fileContent,
       "migrate-jps-config", ".xml", tmpFileLocation);
    return tempFile.getCanonicalPath();
  }

  private static File createTemporaryFile(String prefix, String suffix,
    File tmpFileLocation) throws java.io.IOException,
     java.io.FileNotFoundException {
    File tempFile = File.createTempFile(prefix, suffix, tmpFileLocation);
    tempFile.deleteOnExit();
    return tempFile;
  }

  private static File createTemporaryFile(String fileContent,
    String prefix, String suffix,
    File tmpFileLocation) throws java.io.IOException, 
      java.io.FileNotFoundException {
    File tempFile = createTemporaryFile(prefix, suffix, tmpFileLocation);
    FileOutputStream fos = new FileOutputStream(tempFile);


    IOUtils.write(fileContent, fos);
    IOUtils.closeQuietly(fos);
    return tempFile;
  }

  private static String formulateMigrationJAZNFile(String fileName,
     String earFileName,
    File tmpFileLocation) throws IOException {
    if(fileName != null && fileName.trim().length() > 0) {
      return fileName;
    }

    ZipFile zipFile = new ZipFile(earFileName);
    Enumeration entries = zipFile.entries();
    while(entries.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) entries.nextElement();
      if(!entry.isDirectory()) {
        if(entry.getName().endsWith(DEFAULT_JAZN_SUFFIX)) {
          File tempFile = createTemporaryFile("jazn-data-orig",
            ".xml", tmpFileLocation);
          FileOutputStream fos = new FileOutputStream(tempFile);
          IOUtils.copy(zipFile.getInputStream(entry), fos);
          IOUtils.closeQuietly(fos);
          return tempFile.getCanonicalPath();
        }
      }
    }
    return null;
  }
  private static Options createOptions() {
    Options options = new Options();
    Option source = new Option(SOURCE_OPTION_NAME, 
      "src", true, "source policy config name : defaults appPolicy");
    Option destination =
      new Option(DESTINATION_OPTION_NAME, "dst", true, 
       "destination policy config name : defaults domainPolicy");
    Option scriptLocation =
      new Option(SCRIPT_OPTION_NAME, "script", true, 
       "Loction of all the .py scripts to run wlst");
    Option migrationFile = new Option(JAZN_OPTION_NAME,
      "jazn", true, "jazn file to import location");
    Option systemJaznLocation =
      new Option(SYSJAZN_OPTION_NAME, "sysjazn", true, 
       "system jazn file to copy to full location");
    Option migrationFileEar =
      new Option(EAR_OPTION_NAME, "ear", true, 
       "Ear file to extract jazn file from : cant be used with jazn");
    source.setOptionalArg(true);
    destination.setOptionalArg(true);
    scriptLocation.setOptionalArg(true);
    systemJaznLocation.setRequired(true);
    migrationFile.setOptionalArg(true);
    migrationFileEar.setOptionalArg(true);

    options.addOption(source);
    options.addOption(destination);
    options.addOption(scriptLocation);
    options.addOption(migrationFile);
    options.addOption(systemJaznLocation);
    options.addOption(migrationFileEar);
    return options;
  }
}

No comments:

Post a Comment