Search in sources :

Example 1 with Attachments

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

the class DeployArchiveCommand method execute.

public CommandResult execute(CommandContext ctx) throws CommandException {
    checkArgument();
    Attachments attachments = new Attachments();
    try {
        ModelNode request = buildRequest(ctx, attachments);
        // if script had no server-side commands, just return
        if (!request.get("steps").isDefined()) {
            return CommandResult.SUCCESS;
        }
        OperationBuilder op = new OperationBuilder(request, true);
        for (String f : attachments.getAttachedFiles()) {
            op.addFileAsAttachment(new File(f));
        }
        ModelNode result;
        try (Operation operation = op.build()) {
            result = ctx.getModelControllerClient().execute(operation);
        }
        if (!Util.isSuccess(result)) {
            throw new CommandException(Util.getFailureDescription(result));
        }
    } catch (IOException e) {
        throw new CommandException("Failed to deploy archive", e);
    } catch (CommandFormatException ex) {
        throw new CommandException(ex);
    } finally {
        attachments.done();
    }
    return CommandResult.SUCCESS;
}
Also used : OperationBuilder(org.jboss.as.controller.client.OperationBuilder) CommandFormatException(org.jboss.as.cli.CommandFormatException) Operation(org.jboss.as.controller.client.Operation) CommandException(org.aesh.command.CommandException) IOException(java.io.IOException) ModelNode(org.jboss.dmr.ModelNode) Attachments(org.jboss.as.cli.Attachments) File(java.io.File)

Example 2 with Attachments

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

the class DeployArchiveCommand method buildRequest.

/**
 * null attachments means that the command is in a batch, non null means
 * command executed.
 *
 * Inside a batch, the attachments must be added to the existing batch and
 * NOT to the temporary batch created to build the composite request.
 * Outside of a batch, the attachments MUST be added to the passed non null
 * attachments.
 *
 * @param context
 * @param attachments
 * @return
 * @throws CommandFormatException
 */
@Override
public ModelNode buildRequest(CommandContext context, Attachments attachments) throws CommandFormatException {
    CommandContext ctx = context;
    TempFileProvider tempFileProvider;
    MountHandle root;
    try {
        String name = "cli-" + System.currentTimeMillis();
        tempFileProvider = TempFileProvider.create(name, Executors.newSingleThreadScheduledExecutor((r) -> new Thread(r, "CLI (un)deploy archive tempFile")), true);
        root = extractArchive(file, tempFileProvider, name);
    } catch (IOException e) {
        e.printStackTrace();
        throw new OperationFormatException("Unable to extract archive '" + file.getAbsolutePath() + "' to temporary location");
    }
    Consumer<Attachments> cl = (a) -> {
        VFSUtils.safeClose(root, tempFileProvider);
    };
    if (attachments != null) {
        attachments.addConsumer(cl);
    }
    final File currentDir = ctx.getCurrentDir();
    ctx.setCurrentDir(root.getMountSource());
    String holdbackBatch = activateNewBatch(ctx);
    try {
        if (script == null) {
            script = getDefaultScript();
        }
        File scriptFile = new File(ctx.getCurrentDir(), script);
        if (!scriptFile.exists()) {
            throw new CommandFormatException("ERROR: script '" + script + "' not found.");
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(scriptFile));
            String line = reader.readLine();
            while (!ctx.isTerminated() && line != null) {
                context.handle(line);
                line = reader.readLine();
            }
        } catch (FileNotFoundException e) {
            throw new CommandFormatException("ERROR: script '" + script + "' not found.");
        } catch (IOException e) {
            throw new CommandFormatException("Failed to read the next command from " + scriptFile.getName() + ": " + e.getMessage(), e);
        } catch (CommandLineException ex) {
            throw new CommandFormatException(ex);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                }
            }
        }
        return ctx.getBatchManager().getActiveBatch().toRequest();
    } catch (CommandFormatException cfex) {
        cl.accept(attachments);
        throw cfex;
    } finally {
        // reset current dir in context
        ctx.setCurrentDir(currentDir);
        discardBatch(ctx, holdbackBatch, attachments, cl);
    }
}
Also used : CommandDefinition(org.aesh.command.CommandDefinition) TempFileProvider(org.jboss.vfs.TempFileProvider) Operation(org.jboss.as.controller.client.Operation) OperationBuilder(org.jboss.as.controller.client.OperationBuilder) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) LegacyBridge(org.jboss.as.cli.impl.aesh.cmd.LegacyBridge) Command(org.aesh.command.Command) Argument(org.aesh.command.option.Argument) HideOptionActivator(org.wildfly.core.cli.command.aesh.activator.HideOptionActivator) CommandResult(org.aesh.command.CommandResult) BatchCompliantCommand(org.wildfly.core.cli.command.BatchCompliantCommand) AccessRequirements(org.jboss.as.cli.impl.aesh.cmd.deployment.security.AccessRequirements) MountHandle(org.jboss.vfs.spi.MountHandle) Option(org.aesh.command.option.Option) VFS(org.jboss.vfs.VFS) CommandWithPermissions(org.jboss.as.cli.impl.aesh.cmd.deployment.security.CommandWithPermissions) IOException(java.io.IOException) BatchManager(org.jboss.as.cli.batch.BatchManager) ControlledCommandActivator(org.jboss.as.cli.impl.aesh.cmd.security.ControlledCommandActivator) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Executors(java.util.concurrent.Executors) CommandException(org.aesh.command.CommandException) Util(org.jboss.as.cli.Util) CommandLineException(org.jboss.as.cli.CommandLineException) Consumer(java.util.function.Consumer) VFSUtils(org.jboss.vfs.VFSUtils) Batch(org.jboss.as.cli.batch.Batch) Permissions(org.jboss.as.cli.impl.aesh.cmd.deployment.security.Permissions) CommandContext(org.jboss.as.cli.CommandContext) CommandFormatException(org.jboss.as.cli.CommandFormatException) ModelNode(org.jboss.dmr.ModelNode) CLICommandInvocation(org.wildfly.core.cli.command.aesh.CLICommandInvocation) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Attachments(org.jboss.as.cli.Attachments) CommandContext(org.jboss.as.cli.CommandContext) OperationFormatException(org.jboss.as.cli.operation.OperationFormatException) MountHandle(org.jboss.vfs.spi.MountHandle) TempFileProvider(org.jboss.vfs.TempFileProvider) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Attachments(org.jboss.as.cli.Attachments) CommandLineException(org.jboss.as.cli.CommandLineException) CommandFormatException(org.jboss.as.cli.CommandFormatException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 3 with Attachments

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

the class DeployArchiveCommand method discardBatch.

static void discardBatch(CommandContext ctx, String holdbackBatch, Attachments attachments, Consumer<Attachments> listener) {
    BatchManager batchManager = ctx.getBatchManager();
    // Get the files attached by the CLI script.
    // Must then add them to the passed attachemnts if non null.
    // If null, then we should have an heldback batch to which we need to add them
    Attachments archiveAttachments = batchManager.getActiveBatch().getAttachments();
    if (attachments != null) {
        for (String f : archiveAttachments.getAttachedFiles()) {
            attachments.addFileAttachment(f);
        }
    }
    batchManager.discardActiveBatch();
    if (holdbackBatch != null) {
        batchManager.activateHeldbackBatch(holdbackBatch);
        Attachments activeAttachments = batchManager.getActiveBatch().getAttachments();
        // that have been transfered when creating the archive batch.
        for (int i = activeAttachments.getAttachedFiles().size(); i < archiveAttachments.getAttachedFiles().size(); i++) {
            activeAttachments.addFileAttachment(archiveAttachments.getAttachedFiles().get(i));
        }
        activeAttachments.addConsumer(listener);
    }
}
Also used : BatchManager(org.jboss.as.cli.batch.BatchManager) Attachments(org.jboss.as.cli.Attachments)

Example 4 with Attachments

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

the class CommandContextImpl method handleOperation.

private void handleOperation(ParsedCommandLine parsedLine) throws CommandFormatException, CommandLineException {
    if (isBatchMode()) {
        String line = parsedLine.getOriginalLine();
        Batch batch = getBatchManager().getActiveBatch();
        final ModelNode request = Util.toOperationRequest(this, parsedLine, batch.getAttachments());
        StringBuilder op = new StringBuilder();
        op.append(getNodePathFormatter().format(parsedLine.getAddress()));
        op.append(line.substring(line.indexOf(':')));
        DefaultBatchedCommand batchedCmd = new DefaultBatchedCommand(this, op.toString(), request, null);
        batch.add(batchedCmd);
    } else {
        Attachments attachments = new Attachments();
        final ModelNode op = Util.toOperationRequest(CommandContextImpl.this, parsedLine, attachments);
        RequestWithAttachments req = new RequestWithAttachments(op, attachments);
        set(Scope.REQUEST, "OP_REQ", req);
        operationHandler.handle(this);
    }
}
Also used : Batch(org.jboss.as.cli.batch.Batch) RequestWithAttachments(org.jboss.as.cli.RequestWithAttachments) DefaultBatchedCommand(org.jboss.as.cli.batch.impl.DefaultBatchedCommand) ModelNode(org.jboss.dmr.ModelNode) RequestWithAttachments(org.jboss.as.cli.RequestWithAttachments) Attachments(org.jboss.as.cli.Attachments)

Example 5 with Attachments

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

the class DeployArchiveCommand method activateNewBatch.

static String activateNewBatch(CommandContext ctx) {
    String currentBatch = null;
    BatchManager batchManager = ctx.getBatchManager();
    Attachments attachments = null;
    if (batchManager.isBatchActive()) {
        Batch current = batchManager.getActiveBatch();
        attachments = current.getAttachments();
        currentBatch = "batch" + System.currentTimeMillis();
        batchManager.holdbackActiveBatch(currentBatch);
    }
    batchManager.activateNewBatch();
    Batch archiveBatch = batchManager.getActiveBatch();
    // computation.
    if (attachments != null) {
        for (String f : attachments.getAttachedFiles()) {
            archiveBatch.getAttachments().addFileAttachment(f);
        }
    }
    return currentBatch;
}
Also used : Batch(org.jboss.as.cli.batch.Batch) BatchManager(org.jboss.as.cli.batch.BatchManager) Attachments(org.jboss.as.cli.Attachments)

Aggregations

Attachments (org.jboss.as.cli.Attachments)6 ModelNode (org.jboss.dmr.ModelNode)4 File (java.io.File)3 IOException (java.io.IOException)3 CommandFormatException (org.jboss.as.cli.CommandFormatException)3 Batch (org.jboss.as.cli.batch.Batch)3 BatchManager (org.jboss.as.cli.batch.BatchManager)3 OperationBuilder (org.jboss.as.controller.client.OperationBuilder)3 CommandException (org.aesh.command.CommandException)2 CommandLineException (org.jboss.as.cli.CommandLineException)2 Operation (org.jboss.as.controller.client.Operation)2 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 Executors (java.util.concurrent.Executors)1 Consumer (java.util.function.Consumer)1 Command (org.aesh.command.Command)1 CommandDefinition (org.aesh.command.CommandDefinition)1 CommandResult (org.aesh.command.CommandResult)1 Argument (org.aesh.command.option.Argument)1