new-integration-tests/src/test/java/oracle/weblogic/kubernetes/actions/impl/Domain.java
public static boolean scaleClusterWithWLDF(String clusterName,
                                             String domainUid,
                                             String domainNamespace,
                                             String domainHomeLocation,
                                             String scalingAction,
                                             int scalingSize,
                                             String opNamespace,
                                             String opServiceAccount,
                                             String myWebAppName,
                                             String curlCommand)
      throws ApiException, IOException, InterruptedException {
    LoggingFacade logger = getLogger();
    // create RBAC API objects for WLDF script
    logger.info("Creating RBAC API objects for WLDF script");
    if (!createRbacApiObjectsForWLDFScript(domainNamespace, opNamespace)) {
      return false;
    }

    // copy scalingAction.sh to Admin Server pod
    // NOTE: you must copy scalingAction.sh to $DOMAIN_HOME/bin/scripts on admin server pod
    String adminServerPodName = domainUid + "-" + ADMIN_SERVER_NAME_BASE;
    V1Pod adminPod = Kubernetes.getPod(domainNamespace, null, adminServerPodName);
    if (adminPod == null) {
      logger.info("The admin pod {0} does not exist in namespace {1}!", adminServerPodName, domainNamespace);
      return false;
    }

    // create $DOMAIN_HOME/bin/scripts directory on admin server pod
    logger.info("Creating directory {0}/bin/scripts on admin server pod", domainHomeLocation);
    ExecResult result = exec(adminPod, null, true,
        "/bin/sh", "-c", "mkdir -p " + domainHomeLocation + "/bin/scripts");
    if (result.exitValue() != 0) {
      return false;
    }

    logger.info("Copying scalingAction.sh to admin server pod");
    try {
      copyFileToPod(domainNamespace, adminServerPodName, null,
          Paths.get(PROJECT_ROOT + "/../src/scripts/scaling/scalingAction.sh"),
          Paths.get(domainHomeLocation + "/bin/scripts/scalingAction.sh"));
    } catch (ApiException apex) {
      logger.severe("Got ApiException while copying file to admin pod {0}", apex.getResponseBody());
      return false;
    } catch (IOException ioex) {
      logger.severe("Got IOException while copying file to admin pod {0}", ioex.getStackTrace());
      return false;
    }

    logger.info("Adding execute mode for scalingAction.sh");
    result = exec(adminPod, null, true,
        "/bin/sh", "-c", "chmod +x " + domainHomeLocation + "/bin/scripts/scalingAction.sh");
    if (result.exitValue() != 0) {
      return false;
    }

    // copy wldf.py and callpyscript.sh to Admin Server pod
    logger.info("Copying wldf.py and callpyscript.sh to admin server pod");
    try {
      copyFileToPod(domainNamespace, adminServerPodName, null,
          Paths.get(RESOURCE_DIR, "python-scripts", "wldf.py"),
          Paths.get("/u01/oracle/wldf.py"));

      copyFileToPod(domainNamespace, adminServerPodName, null,
          Paths.get(RESOURCE_DIR, "bash-scripts", "callpyscript.sh"),
          Paths.get("/u01/oracle/callpyscript.sh"));
    } catch (ApiException apex) {
      logger.severe("Got ApiException while copying file to admin pod {0}", apex.getResponseBody());
      return false;
    } catch (IOException ioex) {
      logger.severe("Got IOException while copying file to admin pod {0}", ioex.getStackTrace());
      return false;
    }

    logger.info("Adding execute mode for callpyscript.sh");
    result = exec(adminPod, null, true,
        "/bin/sh", "-c", "chmod +x /u01/oracle/callpyscript.sh");
    if (result.exitValue() != 0) {
      return false;
    }

    if (!scalingAction.equals("scaleUp") && !scalingAction.equals("scaleDown")) {
      logger.info("Set scaleAction to either scaleUp or scaleDown");
      return false;
    }

    logger.info("Creating WLDF policy rule and action");
    String command = new StringBuffer("/u01/oracle/callpyscript.sh /u01/oracle/wldf.py ")
        .append(ADMIN_USERNAME_DEFAULT)
        .append(" ")
        .append(ADMIN_PASSWORD_DEFAULT)
        .append(" t3://")
        .append(adminServerPodName)
        .append(":7001 ")
        .append(scalingAction)
        .append(" ")
        .append(domainUid)
        .append(" ")
        .append(clusterName)
        .append(" ")
        .append(domainNamespace)
        .append(" ")
        .append(opNamespace)
        .append(" ")
        .append(opServiceAccount)
        .append(" ")
        .append(scalingSize)
        .append(" ")
        .append(myWebAppName).toString();

    result = exec(adminPod, null, true, "/bin/sh", "-c", command);
    if (result.exitValue() != 0) {
      return false;
    }

    // sleep for a while to make sure the diagnostic modules are created
    try {
      Thread.sleep(5000);
    } catch (InterruptedException ex) {
      // ignore
    }

    // call the web app to trigger the WLDF policy which will call the script action to scale the cluster
    CommandParams params = Command
        .defaultCommandParams()
        .command(curlCommand)
        .saveResults(true)
        .redirect(true);

    return Command.withParams(params).execute();
  }
/**
   * Scale the cluster of the domain in the specified namespace with WLDF.
   *
   * @param clusterName name of the WebLogic cluster to be scaled in the domain
   * @param domainUid domainUid of the domain to be scaled
   * @param domainNamespace domain namespace in which the domain exists
   * @param domainHomeLocation domain home location of the domain
   * @param scalingAction scaling action, accepted value: scaleUp or scaleDown
   * @param scalingSize number of servers to be scaled up or down
   * @param opNamespace namespace of WebLogic operator
   * @param opServiceAccount service account of operator
   * @param myWebAppName web app name deployed to the domain used in the WLDF policy expression
   * @param curlCommand curl command to call the web app used in the WLDF policy expression
   * @return true if scaling the cluster succeeds, false otherwise
   * @throws ApiException if Kubernetes client API call fails
   * @throws IOException if an I/O error occurs
   * @throws InterruptedException if any thread has interrupted the current thread
   */
Scale the cluster of the domain in the specified namespace with WLDF.
Scale the cluster of the domain in the specified namespace with WLDF .
public static boolean scaleClusterWithWLDF(String clusterName,
                                             String domainUid,
                                             String domainNamespace,
                                             String domainHomeLocation,
                                             String scalingAction,
                                             int scalingSize,
                                             String opNamespace,
                                             String opServiceAccount,
                                             String myWebAppName,
                                             String curlCommand)
      throws ApiException, IOException, InterruptedException {
    LoggingFacade logger = getLogger();
    logger.info("Creating RBAC API objects for WLDF script");
    if (!createRbacApiObjectsForWLDFScript(domainNamespace, opNamespace)) {
      return false;
    }
    String adminServerPodName = domainUid + "-" + ADMIN_SERVER_NAME_BASE;
    V1Pod adminPod = Kubernetes.getPod(domainNamespace, null, adminServerPodName);
    if (adminPod == null) {
      logger.info("The admin pod {0} does not exist in namespace {1}!", adminServerPodName, domainNamespace);
      return false;
    }
    logger.info("Creating directory {0}/bin/scripts on admin server pod", domainHomeLocation);
    ExecResult result = exec(adminPod, null, true,
        "/bin/sh", "-c", "mkdir -p " + domainHomeLocation + "/bin/scripts");
    if (result.exitValue() != 0) {
      return false;
    }
    logger.info("Copying scalingAction.sh to admin server pod");
    try {
      copyFileToPod(domainNamespace, adminServerPodName, null,
          Paths.get(PROJECT_ROOT + "/../src/scripts/scaling/scalingAction.sh"),
          Paths.get(domainHomeLocation + "/bin/scripts/scalingAction.sh"));
    } catch (ApiException apex) {
      logger.severe("Got ApiException while copying file to admin pod {0}", apex.getResponseBody());
      return false;
    } catch (IOException ioex) {
      logger.severe("Got IOException while copying file to admin pod {0}", ioex.getStackTrace());
      return false;
    }
    logger.info("Adding execute mode for scalingAction.sh");
    result = exec(adminPod, null, true,
        "/bin/sh", "-c", "chmod +x " + domainHomeLocation + "/bin/scripts/scalingAction.sh");
    if (result.exitValue() != 0) {
      return false;
    }
    logger.info("Copying wldf.py and callpyscript.sh to admin server pod");
    try {
      copyFileToPod(domainNamespace, adminServerPodName, null,
          Paths.get(RESOURCE_DIR, "python-scripts", "wldf.py"),
          Paths.get("/u01/oracle/wldf.py"));
      copyFileToPod(domainNamespace, adminServerPodName, null,
          Paths.get(RESOURCE_DIR, "bash-scripts", "callpyscript.sh"),
          Paths.get("/u01/oracle/callpyscript.sh"));
    } catch (ApiException apex) {
      logger.severe("Got ApiException while copying file to admin pod {0}", apex.getResponseBody());
      return false;
    } catch (IOException ioex) {
      logger.severe("Got IOException while copying file to admin pod {0}", ioex.getStackTrace());
      return false;
    }
    logger.info("Adding execute mode for callpyscript.sh");
    result = exec(adminPod, null, true,
        "/bin/sh", "-c", "chmod +x /u01/oracle/callpyscript.sh");
    if (result.exitValue() != 0) {
      return false;
    }
    if (!scalingAction.equals("scaleUp") && !scalingAction.equals("scaleDown")) {
      logger.info("Set scaleAction to either scaleUp or scaleDown");
      return false;
    }
    logger.info("Creating WLDF policy rule and action");
    String command = new StringBuffer("/u01/oracle/callpyscript.sh /u01/oracle/wldf.py ")
        .append(ADMIN_USERNAME_DEFAULT)
        .append(" ")
        .append(ADMIN_PASSWORD_DEFAULT)
        .append(" t3://")
        .append(adminServerPodName)
        .append(":7001 ")
        .append(scalingAction)
        .append(" ")
        .append(domainUid)
        .append(" ")
        .append(clusterName)
        .append(" ")
        .append(domainNamespace)
        .append(" ")
        .append(opNamespace)
        .append(" ")
        .append(opServiceAccount)
        .append(" ")
        .append(scalingSize)
        .append(" ")
        .append(myWebAppName).toString();
    result = exec(adminPod, null, true, "/bin/sh", "-c", command);
    if (result.exitValue() != 0) {
      return false;
    }
    try {
      Thread.sleep(5000);
    } catch (InterruptedException ex) {
    }
    CommandParams params = Command
        .defaultCommandParams()
        .command(curlCommand)
        .saveResults(true)
        .redirect(true);
    return Command.withParams(params).execute();
  }
public static boolean scaleClusterWithWLDF ( String clusterName , String domainUid , String domainNamespace , String domainHomeLocation , String scalingAction , int scalingSize , String opNamespace , String opServiceAccount , String myWebAppName , String curlCommand ) throws ApiException , IOException , InterruptedException { LoggingFacade logger = getLogger ( ) ; logger . info ( "Creating RBAC API objects for WLDF script" ) ; if ( ! createRbacApiObjectsForWLDFScript ( domainNamespace , opNamespace ) ) { return false ; } String adminServerPodName = domainUid + "-" + ADMIN_SERVER_NAME_BASE ; V1Pod adminPod = Kubernetes . getPod ( domainNamespace , null , adminServerPodName ) ; if ( adminPod == null ) { logger . info ( "The admin pod {0} does not exist in namespace {1}!" , adminServerPodName , domainNamespace ) ; return false ; } logger . info ( "Creating directory {0}/bin/scripts on admin server pod" , domainHomeLocation ) ; ExecResult result = exec ( adminPod , null , true , "/bin/sh" , "-c" , "mkdir -p " + domainHomeLocation + "/bin/scripts" ) ; if ( result . exitValue ( ) != 0 ) { return false ; } logger . info ( "Copying scalingAction.sh to admin server pod" ) ; try { copyFileToPod ( domainNamespace , adminServerPodName , null , Paths . get ( PROJECT_ROOT + "/../src/scripts/scaling/scalingAction.sh" ) , Paths . get ( domainHomeLocation + "/bin/scripts/scalingAction.sh" ) ) ; } catch ( ApiException apex ) { logger . severe ( "Got ApiException while copying file to admin pod {0}" , apex . getResponseBody ( ) ) ; return false ; } catch ( IOException ioex ) { logger . severe ( "Got IOException while copying file to admin pod {0}" , ioex . getStackTrace ( ) ) ; return false ; } logger . info ( "Adding execute mode for scalingAction.sh" ) ; result = exec ( adminPod , null , true , "/bin/sh" , "-c" , "chmod +x " + domainHomeLocation + "/bin/scripts/scalingAction.sh" ) ; if ( result . exitValue ( ) != 0 ) { return false ; } logger . info ( "Copying wldf.py and callpyscript.sh to admin server pod" ) ; try { copyFileToPod ( domainNamespace , adminServerPodName , null , Paths . get ( RESOURCE_DIR , "python-scripts" , "wldf.py" ) , Paths . get ( "/u01/oracle/wldf.py" ) ) ; copyFileToPod ( domainNamespace , adminServerPodName , null , Paths . get ( RESOURCE_DIR , "bash-scripts" , "callpyscript.sh" ) , Paths . get ( "/u01/oracle/callpyscript.sh" ) ) ; } catch ( ApiException apex ) { logger . severe ( "Got ApiException while copying file to admin pod {0}" , apex . getResponseBody ( ) ) ; return false ; } catch ( IOException ioex ) { logger . severe ( "Got IOException while copying file to admin pod {0}" , ioex . getStackTrace ( ) ) ; return false ; } logger . info ( "Adding execute mode for callpyscript.sh" ) ; result = exec ( adminPod , null , true , "/bin/sh" , "-c" , "chmod +x /u01/oracle/callpyscript.sh" ) ; if ( result . exitValue ( ) != 0 ) { return false ; } if ( ! scalingAction . equals ( "scaleUp" ) && ! scalingAction . equals ( "scaleDown" ) ) { logger . info ( "Set scaleAction to either scaleUp or scaleDown" ) ; return false ; } logger . info ( "Creating WLDF policy rule and action" ) ; String command = new StringBuffer ( "/u01/oracle/callpyscript.sh /u01/oracle/wldf.py " ) . append ( ADMIN_USERNAME_DEFAULT ) . append ( " " ) . append ( ADMIN_PASSWORD_DEFAULT ) . append ( " t3://" ) . append ( adminServerPodName ) . append ( ":7001 " ) . append ( scalingAction ) . append ( " " ) . append ( domainUid ) . append ( " " ) . append ( clusterName ) . append ( " " ) . append ( domainNamespace ) . append ( " " ) . append ( opNamespace ) . append ( " " ) . append ( opServiceAccount ) . append ( " " ) . append ( scalingSize ) . append ( " " ) . append ( myWebAppName ) . toString ( ) ; result = exec ( adminPod , null , true , "/bin/sh" , "-c" , command ) ; if ( result . exitValue ( ) != 0 ) { return false ; } try { Thread . sleep ( 5000 ) ; } catch ( InterruptedException ex ) { } CommandParams params = Command . defaultCommandParams ( ) . command ( curlCommand ) . saveResults ( true ) . redirect ( true ) ; return Command . withParams ( params ) . execute ( ) ; }
Scale the cluster of the domain in the specified namespace with WLDF.
Scale the cluster of the domain in the specified namespace with WLDF .
// create RBAC API objects for WLDF script // copy scalingAction.sh to Admin Server pod // NOTE: you must copy scalingAction.sh to $DOMAIN_HOME/bin/scripts on admin server pod // create $DOMAIN_HOME/bin/scripts directory on admin server pod // copy wldf.py and callpyscript.sh to Admin Server pod // sleep for a while to make sure the diagnostic modules are created // ignore // call the web app to trigger the WLDF policy which will call the script action to scale the cluster