Search in sources :

Example 36 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class DeploymentOverlayHandler method doHandle.

@Override
protected void doHandle(CommandContext ctx) throws CommandLineException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    if (!args.hasProperties() || l.isPresent(args) && args.getOtherProperties().isEmpty() && args.getPropertyNames().size() == 1) {
        // list registered overlays
        final ModelNode op = new ModelNode();
        op.get(Util.ADDRESS).setEmptyList();
        op.get(Util.OPERATION).set(Util.READ_CHILDREN_NAMES);
        op.get(Util.CHILD_TYPE).set(Util.DEPLOYMENT_OVERLAY);
        final ModelNode response;
        try {
            response = ctx.getModelControllerClient().execute(op);
        } catch (IOException e) {
            throw new CommandLineException("Failed to execute " + Util.READ_CHILDREN_NAMES, e);
        }
        final ModelNode result = response.get(Util.RESULT);
        if (!result.isDefined()) {
            final String descr = Util.getFailureDescription(response);
            if (descr != null) {
                throw new CommandLineException(descr);
            }
            throw new CommandLineException("The response of " + Util.READ_CHILDREN_NAMES + " is missing result: " + response);
        }
        if (l.isPresent(args)) {
            for (ModelNode node : result.asList()) {
                ctx.printLine(node.asString());
            }
        } else {
            final List<String> names = new ArrayList<String>();
            for (ModelNode node : result.asList()) {
                names.add(node.asString());
            }
            ctx.printColumns(names);
        }
        return;
    }
    final String action = this.action.getValue(args, true);
    if (ADD.equals(action)) {
        add(ctx, true);
    } else if (UPLOAD.equals(action)) {
        upload(ctx, true);
    } else if (LIST_CONTENT.equals(action)) {
        listContent(ctx);
    } else if (LIST_LINKS.equals(action)) {
        listLinks(ctx);
    } else {
        super.doHandle(ctx);
    }
}
Also used : ArrayList(java.util.ArrayList) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 37 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class DeploymentOverlayHandler method upload.

protected ModelNode upload(CommandContext ctx, boolean stream) throws CommandLineException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    final String name = getName(ctx, false);
    final String contentStr = content.getValue(args, true);
    final String[] contentPairs = contentStr.split(",+");
    if (contentPairs.length == 0) {
        throw new CommandFormatException("Overlay content is not specified.");
    }
    final String[] contentNames = new String[contentPairs.length];
    final File[] contentPaths = new File[contentPairs.length];
    for (int i = 0; i < contentPairs.length; ++i) {
        final String pair = contentPairs[i];
        final int equalsIndex = pair.indexOf('=');
        if (equalsIndex < 0) {
            throw new CommandFormatException("Content pair is not following archive-path=fs-path format: '" + pair + "'");
        }
        contentNames[i] = pair.substring(0, equalsIndex);
        if (contentNames[i].length() == 0) {
            throw new CommandFormatException("The archive path is missing for the content '" + pair + "'");
        }
        String path = pair.substring(equalsIndex + 1);
        if (path.length() == 0) {
            throw new CommandFormatException("The filesystem paths is missing for the content '" + pair + "'");
        }
        path = pathCompleter.translatePath(path);
        final File f = new File(path);
        if (!f.exists()) {
            throw new CommandFormatException("Content file doesn't exist " + f.getAbsolutePath());
        }
        contentPaths[i] = f;
    }
    final String deploymentsStr = deployments.getValue(args);
    if (deploymentsStr != null) {
        throw new CommandFormatException(deployments.getFullName() + " can't be used in combination with upload.");
    }
    final ModelControllerClient client = ctx.getModelControllerClient();
    final ModelNode composite = new ModelNode();
    final OperationBuilder opBuilder = stream ? new OperationBuilder(composite, true) : null;
    composite.get(Util.OPERATION).set(Util.COMPOSITE);
    composite.get(Util.ADDRESS).setEmptyList();
    final ModelNode steps = composite.get(Util.STEPS);
    // add the content
    for (int i = 0; i < contentNames.length; ++i) {
        final ModelNode op = new ModelNode();
        ModelNode address = op.get(Util.ADDRESS);
        address.add(Util.DEPLOYMENT_OVERLAY, name);
        address.add(Util.CONTENT, contentNames[i]);
        op.get(Util.OPERATION).set(Util.ADD);
        if (opBuilder != null) {
            op.get(Util.CONTENT).get(Util.INPUT_STREAM_INDEX).set(i);
            opBuilder.addFileAsAttachment(contentPaths[i]);
        } else {
            op.get(Util.CONTENT).get(Util.BYTES).set(Util.readBytes(contentPaths[i]));
        }
        steps.add(op);
    }
    if (redeployAffected.isPresent(args)) {
        // In domain and standalone, this operation redeploy them all.
        addRedeployStep(name, steps);
    } else {
        printWarning(ctx, client, name, contentStr, null);
    }
    if (opBuilder == null) {
        return composite;
    }
    try {
        final ModelNode result = client.execute(opBuilder.build());
        if (!Util.isSuccess(result)) {
            throw new CommandFormatException(Util.getFailureDescription(result));
        }
    } catch (IOException e) {
        throw new CommandFormatException("Failed to add overlay", e);
    }
    return null;
}
Also used : OperationBuilder(org.jboss.as.controller.client.OperationBuilder) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File)

Example 38 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class DeploymentOverlayHandler method add.

protected ModelNode add(CommandContext ctx, boolean stream) throws CommandLineException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    assertNotPresent(allRelevantServerGroups, args);
    final String name = this.name.getValue(args, true);
    final String contentStr = content.getValue(args, true);
    final String[] contentPairs = contentStr.split(",+");
    if (contentPairs.length == 0) {
        throw new CommandFormatException("Overlay content is not specified.");
    }
    final String[] contentNames = new String[contentPairs.length];
    final File[] contentPaths = new File[contentPairs.length];
    for (int i = 0; i < contentPairs.length; ++i) {
        final String pair = contentPairs[i];
        final int equalsIndex = pair.indexOf('=');
        if (equalsIndex < 0) {
            throw new CommandFormatException("Content pair is not following archive-path=fs-path format: '" + pair + "'");
        }
        contentNames[i] = pair.substring(0, equalsIndex);
        if (contentNames[i].length() == 0) {
            throw new CommandFormatException("The archive path is missing for the content '" + pair + "'");
        }
        String path = pair.substring(equalsIndex + 1);
        if (path.length() == 0) {
            throw new CommandFormatException("The filesystem paths is missing for the content '" + pair + "'");
        }
        path = pathCompleter.translatePath(path);
        final File f = new File(path);
        if (!f.exists()) {
            throw new CommandFormatException("Content file doesn't exist " + f.getAbsolutePath());
        }
        contentPaths[i] = f;
    }
    final String[] deployments = getLinks(this.deployments, args);
    final ModelControllerClient client = ctx.getModelControllerClient();
    final ModelNode composite = new ModelNode();
    final OperationBuilder opBuilder = stream ? new OperationBuilder(composite, true) : null;
    composite.get(Util.OPERATION).set(Util.COMPOSITE);
    composite.get(Util.ADDRESS).setEmptyList();
    final ModelNode steps = composite.get(Util.STEPS);
    // add the overlay
    ModelNode op = new ModelNode();
    ModelNode address = op.get(Util.ADDRESS);
    address.add(Util.DEPLOYMENT_OVERLAY, name);
    op.get(Util.OPERATION).set(Util.ADD);
    steps.add(op);
    // add the content
    for (int i = 0; i < contentNames.length; ++i) {
        final String contentName = contentNames[i];
        op = new ModelNode();
        address = op.get(Util.ADDRESS);
        address.add(Util.DEPLOYMENT_OVERLAY, name);
        address.add(Util.CONTENT, contentName);
        op.get(Util.OPERATION).set(Util.ADD);
        if (opBuilder != null) {
            op.get(Util.CONTENT).get(Util.INPUT_STREAM_INDEX).set(i);
            opBuilder.addFileAsAttachment(contentPaths[i]);
        } else {
            op.get(Util.CONTENT).get(Util.BYTES).set(Util.readBytes(contentPaths[i]));
        }
        steps.add(op);
    }
    if (deployments != null) {
        if (ctx.isDomainMode()) {
            final List<String> sg = getServerGroupsToLink(ctx);
            for (String group : sg) {
                // here we don't need a separate check whether the overlay is linked
                // from the server group since it is created in the same op.
                op = new ModelNode();
                address = op.get(Util.ADDRESS);
                address.add(Util.SERVER_GROUP, group);
                address.add(Util.DEPLOYMENT_OVERLAY, name);
                op.get(Util.OPERATION).set(Util.ADD);
                steps.add(op);
                addAddRedeployLinksSteps(ctx, steps, name, group, deployments, false);
            }
        } else {
            addAddRedeployLinksSteps(ctx, steps, name, null, deployments, false);
        }
    } else if (ctx.isDomainMode() && (serverGroups.isPresent(args) || allServerGroups.isPresent(args))) {
        throw new CommandFormatException("server groups are specified but " + this.deployments.getFullName() + " is not.");
    }
    if (opBuilder == null) {
        return composite;
    }
    try {
        final ModelNode result = client.execute(opBuilder.build());
        if (!Util.isSuccess(result)) {
            throw new CommandFormatException(Util.getFailureDescription(result));
        }
    } catch (IOException e) {
        throw new CommandFormatException("Failed to add overlay", e);
    }
    return null;
}
Also used : OperationBuilder(org.jboss.as.controller.client.OperationBuilder) ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) File(java.io.File)

Example 39 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class DeploymentOverlayHandler method buildRequestWithoutHeaders.

@Override
protected ModelNode buildRequestWithoutHeaders(CommandContext ctx) throws CommandFormatException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    final String action = this.action.getValue(args, true);
    try {
        if (REMOVE.equals(action)) {
            return remove(ctx);
        } else if (LINK.equals(action)) {
            return link(ctx);
        } else if (REDEPLOY_AFFECTED.equals(action)) {
            return redeployAffected(ctx);
        } else if (ADD.equals(action)) {
            return add(ctx, false);
        } else if (UPLOAD.equals(action)) {
            return upload(ctx, false);
        } else {
            throw new CommandFormatException("Doesn't know how to build request for action '" + action + "'");
        }
    } catch (CommandFormatException e) {
        throw e;
    } catch (CommandLineException e) {
        throw new CommandFormatException("Failed to build " + action + " request.", e);
    }
}
Also used : CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) CommandLineException(org.jboss.as.cli.CommandLineException)

Example 40 with ParsedCommandLine

use of org.jboss.as.cli.operation.ParsedCommandLine in project wildfly-core by wildfly.

the class DeploymentOverlayHandler method link.

protected ModelNode link(CommandContext ctx) throws CommandLineException {
    final ParsedCommandLine args = ctx.getParsedCommandLine();
    assertNotPresent(allRelevantServerGroups, args);
    final String name = getName(ctx, false);
    final String[] deployments = getLinks(this.deployments, args);
    if (deployments == null) {
        throw new CommandFormatException(this.deployments.getFullName() + " is required.");
    }
    final ModelNode composite = new ModelNode();
    composite.get(Util.OPERATION).set(Util.COMPOSITE);
    composite.get(Util.ADDRESS).setEmptyList();
    final ModelNode steps = composite.get(Util.STEPS);
    final ModelControllerClient client = ctx.getModelControllerClient();
    if (ctx.isDomainMode()) {
        final List<String> sg = getServerGroupsToLink(ctx);
        for (String group : sg) {
            if (!Util.isValidPath(client, Util.SERVER_GROUP, group, Util.DEPLOYMENT_OVERLAY, name)) {
                final ModelNode op = new ModelNode();
                final ModelNode address = op.get(Util.ADDRESS);
                address.add(Util.SERVER_GROUP, group);
                address.add(Util.DEPLOYMENT_OVERLAY, name);
                op.get(Util.OPERATION).set(Util.ADD);
                steps.add(op);
            }
            addAddRedeployLinksSteps(ctx, steps, name, group, deployments, false);
        }
    } else {
        addAddRedeployLinksSteps(ctx, steps, name, null, deployments, false);
    }
    return composite;
/*        try {
            final ModelNode result = client.execute(composite);
            if (!Util.isSuccess(result)) {
                throw new CommandFormatException(Util.getFailureDescription(result));
            }
        } catch (IOException e) {
            throw new CommandFormatException("Failed to link overlay", e);
        }
*/
}
Also used : ModelControllerClient(org.jboss.as.controller.client.ModelControllerClient) CommandFormatException(org.jboss.as.cli.CommandFormatException) ParsedCommandLine(org.jboss.as.cli.operation.ParsedCommandLine) ModelNode(org.jboss.dmr.ModelNode)

Aggregations

ParsedCommandLine (org.jboss.as.cli.operation.ParsedCommandLine)95 Test (org.junit.Test)42 CommandFormatException (org.jboss.as.cli.CommandFormatException)38 ModelNode (org.jboss.dmr.ModelNode)26 CommandLineException (org.jboss.as.cli.CommandLineException)14 OperationRequestAddress (org.jboss.as.cli.operation.OperationRequestAddress)14 File (java.io.File)12 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 DefaultOperationRequestAddress (org.jboss.as.cli.operation.impl.DefaultOperationRequestAddress)8 ModelControllerClient (org.jboss.as.controller.client.ModelControllerClient)8 CommandArgument (org.jboss.as.cli.CommandArgument)4 CommandContext (org.jboss.as.cli.CommandContext)4 OperationFormatException (org.jboss.as.cli.operation.OperationFormatException)4 ZipException (java.util.zip.ZipException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 CommandException (org.aesh.command.CommandException)3 BatchManager (org.jboss.as.cli.batch.BatchManager)3 CommandLineParser (org.jboss.as.cli.operation.CommandLineParser)3 DefaultCallbackHandler (org.jboss.as.cli.operation.impl.DefaultCallbackHandler)3