private Operation getCompositeUpdate(List<ModelNode> updates) {
   ModelNode op = Util.getEmptyOperation("composite", new ModelNode()); // TODO use constant
   ModelNode steps = op.get("steps");
   for (ModelNode update : updates) {
     steps.add(update);
   }
   return OperationBuilder.Factory.create(op).build();
 }
 private Set<String> getDeploymentNames() throws CancellationException, OperationFailedException {
   ModelNode op = Util.getEmptyOperation(READ_CHILDREN_NAMES_OPERATION, new ModelNode());
   op.get(CHILD_TYPE).set(DEPLOYMENT);
   ModelNode response = serverController.execute(OperationBuilder.Factory.create(op).build());
   // TODO use the proper response structure
   ModelNode result = response.get("result");
   Set<String> deploymentNames = new HashSet<String>();
   if (result.isDefined()) {
     List<ModelNode> deploymentNodes = result.asList();
     for (ModelNode node : deploymentNodes) {
       deploymentNames.add(node.asString());
     }
   }
   return deploymentNames;
 }
  @Test
  public void testXmlConfigDemo() throws Exception {
    ModelControllerClient client =
        ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999);
    try {
      ModelNode request = new ModelNode();
      request.get("operation").set("read-config-as-xml");
      request.get("address").setEmptyList();
      ModelNode r = client.execute(OperationBuilder.Factory.create(request).build());

      Assert.assertEquals(SUCCESS, r.require(OUTCOME).asString());
    } finally {
      StreamUtils.safeClose(client);
    }
  }
Exemplo n.º 4
0
  @Override
  protected void doHandle(CommandContext ctx) {

    ModelControllerClient client = ctx.getModelControllerClient();

    if (!ctx.hasArguments()) {
      printList(ctx, Util.getDeployments(client));
      return;
    }

    String filePath = null;
    String name = null;
    String runtimeName = null;

    for (String arg : ctx.getArguments()) {
      if (filePath == null) {
        filePath = arg;
      } else if (name == null) {
        name = arg;
      } else {
        runtimeName = arg;
      }
    }

    if (filePath == null) {
      printList(ctx, Util.getDeployments(client));
      return;
    }

    File f = new File(filePath);
    if (!f.exists()) {
      ctx.printLine("The path doesn't exist: " + f.getAbsolutePath());
      return;
    }

    if (name == null) {
      name = f.getName();
    }

    if (Util.isDeployed(name, client)) {
      if (ctx.hasSwitch("f")) {
        DefaultOperationRequestBuilder builder = new DefaultOperationRequestBuilder();

        ModelNode result;

        // replace
        builder = new DefaultOperationRequestBuilder();
        builder.setOperationName("full-replace-deployment");
        builder.addProperty("name", name);
        if (runtimeName != null) {
          builder.addProperty("runtime-name", runtimeName);
        }

        FileInputStream is = null;
        try {
          is = new FileInputStream(f);
          ModelNode request = builder.buildRequest();
          OperationBuilder op = OperationBuilder.Factory.create(request);
          op.addInputStream(is);
          request.get("input-stream-index").set(0);
          result = client.execute(op.build());
        } catch (Exception e) {
          ctx.printLine("Failed to replace the deployment: " + e.getLocalizedMessage());
          return;
        } finally {
          StreamUtils.safeClose(is);
        }
        if (!Util.isSuccess(result)) {
          ctx.printLine(Util.getFailureDescription(result));
          return;
        }

        ctx.printLine("'" + name + "' re-deployed successfully.");
      } else {
        ctx.printLine("'" + name + "' is already deployed (use -f to force re-deploy).");
      }

      return;
    } else {

      DefaultOperationRequestBuilder builder;

      ModelNode result;

      // add
      builder = new DefaultOperationRequestBuilder();
      builder.setOperationName("add");
      builder.addNode("deployment", name);
      if (runtimeName != null) {
        builder.addProperty("runtime-name", runtimeName);
      }

      FileInputStream is = null;
      try {
        is = new FileInputStream(f);
        ModelNode request = builder.buildRequest();
        OperationBuilder op = OperationBuilder.Factory.create(request);
        op.addInputStream(is);
        request.get("input-stream-index").set(0);
        result = client.execute(op.build());
      } catch (Exception e) {
        ctx.printLine(
            "Failed to add the deployment content to the repository: " + e.getLocalizedMessage());
        return;
      } finally {
        StreamUtils.safeClose(is);
      }
      if (!Util.isSuccess(result)) {
        ctx.printLine(Util.getFailureDescription(result));
        return;
      }

      // deploy
      builder = new DefaultOperationRequestBuilder();
      builder.setOperationName("deploy");
      builder.addNode("deployment", name);
      try {
        ModelNode request = builder.buildRequest();
        result = client.execute(request);
      } catch (Exception e) {
        ctx.printLine("Failed to deploy: " + e.getLocalizedMessage());
        return;
      }
      if (!Util.isSuccess(result)) {
        ctx.printLine(Util.getFailureDescription(result));
        return;
      }
      ctx.printLine("'" + name + "' deployed successfully.");
    }
  }
  /** This method isn't private solely to allow a unit test in the same package to call it */
  void scan() {

    try {
      scanLock.lockInterruptibly();
    } catch (InterruptedException ie) {
      Thread.currentThread().interrupt();
      return;
    }
    try {
      if (scanEnabled) { // confirm the scan is still wanted
        log.tracef(
            "Scanning directory %s for deployment content changes",
            deploymentDir.getAbsolutePath());

        List<ScannerTask> scannerTasks = new ArrayList<ScannerTask>();

        final Set<String> registeredDeployments = getDeploymentNames();
        final Set<String> toRemove = new HashSet<String>(deployed.keySet());
        scanDirectory(deploymentDir, scannerTasks, registeredDeployments, toRemove);

        // Add remove actions to the plan for anything we count as
        // deployed that we didn't find on the scan
        for (String missing : toRemove) {
          // TODO -- minor -- this assumes the deployment was in the root deploymentDir,
          // not a child dir, and therefore puts the '.undeploying' file there
          File parent = deploymentDir;
          scannerTasks.add(new UndeployTask(missing, parent));
        }

        if (scannerTasks.size() > 0) {
          List<ModelNode> updates = new ArrayList<ModelNode>(scannerTasks.size());

          for (ScannerTask task : scannerTasks) {
            final ModelNode update = task.getUpdate();
            if (log.isDebugEnabled()) {
              log.debugf("Deployment scan of [%s] found update action [%s]", deploymentDir, update);
            }
            updates.add(update);
          }

          while (!updates.isEmpty()) {
            ModelNode composite = getCompositeUpdate(updates);
            final ModelNode results =
                serverController.execute(OperationBuilder.Factory.create(composite).build());
            final List<Property> resultList = results.get(RESULT).asPropertyList();
            final List<ModelNode> toRetry = new ArrayList<ModelNode>();
            final List<ScannerTask> retryTasks = new ArrayList<ScannerTask>();
            for (int i = 0; i < resultList.size(); i++) {
              final ModelNode result = resultList.get(i).getValue();
              final ScannerTask task = scannerTasks.get(i);
              final ModelNode outcome = result.get(OUTCOME);
              if (outcome.isDefined() && SUCCESS.equals(outcome.asString())) {
                task.handleSuccessResult();
              } else if (outcome.isDefined() && CANCELLED.equals(outcome.asString())) {
                toRetry.add(updates.get(i));
                retryTasks.add(task);
              } else {
                task.handleFailureResult(result);
              }
            }
            updates = toRetry;
            scannerTasks = retryTasks;
          }
        }
        log.tracef("Scan complete");
      }
    } finally {
      scanLock.unlock();
    }
  }