final String path = this.path.getValue(args);
final File f;
if(path != null) {
f = new File(path);
if(!f.exists()) {
throw new CommandFormatException("Path " + f.getAbsolutePath() + " doesn't exist.");
}
if(!unmanaged && f.isDirectory()) {
throw new CommandFormatException(f.getAbsolutePath() + " is a directory.");
}
} else {
f = null;
}
if (isCliArchive(f)) {
final ModelNode request = buildRequest(ctx);
if(request == null) {
throw new CommandFormatException("Operation request wasn't built.");
}
try {
final ModelNode result = client.execute(request);
if(Util.isSuccess(result)) {
return;
} else {
throw new CommandFormatException("Failed to execute archive script: " + Util.getFailureDescription(result));
}
} catch (IOException e) {
throw new CommandFormatException("Failed to execute archive script: " + e.getLocalizedMessage(), e);
}
}
String name = this.name.getValue(args);
if(name == null) {
if(f == null) {
throw new CommandFormatException("Either path or --name is required.");
}
name = f.getName();
}
final String runtimeName = rtName.getValue(args);
final boolean force = this.force.isPresent(args);
final boolean disabled = this.disabled.isPresent(args);
final String serverGroups = this.serverGroups.getValue(args);
final boolean allServerGroups = this.allServerGroups.isPresent(args);
if(force) {
if(f == null) {
throw new CommandFormatException(this.force.getFullName() + " requires a filesystem path of the deployment to be added to the deployment repository.");
}
if(disabled || serverGroups != null || allServerGroups) {
throw new CommandFormatException(this.force.getFullName() +
" only replaces the content in the deployment repository and can't be used in combination with any of " +
this.disabled.getFullName() + ", " + this.serverGroups.getFullName() + " or " + this.allServerGroups.getFullName() + '.');
}
if(Util.isDeploymentInRepository(name, client)) {
replaceDeployment(ctx, f, name, runtimeName);
return;
} else if(ctx.isDomainMode()) {
// add deployment to the repository (enabled in standalone, disabled in domain (i.e. not associated with any sg))
final ModelNode request = buildAddRequest(ctx, f, name, runtimeName, unmanaged);
execute(ctx, request, f, unmanaged);
return;
}
// standalone mode will add and deploy
}
if(disabled) {
if(f == null) {
throw new CommandFormatException(this.disabled.getFullName() + " requires a filesystem path of the deployment to be added to the deployment repository.");
}
if(serverGroups != null || allServerGroups) {
throw new CommandFormatException(this.serverGroups.getFullName() + " and " + this.allServerGroups.getFullName() +
" can't be used in combination with " + this.disabled.getFullName() + '.');
}
if(Util.isDeploymentInRepository(name, client)) {
throw new CommandFormatException("'" + name + "' already exists in the deployment repository (use " +
this.force.getFullName() + " to replace the existing content in the repository).");
}
// add deployment to the repository disabled
final ModelNode request = buildAddRequest(ctx, f, name, runtimeName, unmanaged);
execute(ctx, request, f, unmanaged);
return;
}
// actually, the deployment is added before it is deployed
// but this code here is to validate arguments and not to add deployment if something is wrong
final ModelNode deployRequest;
if(ctx.isDomainMode()) {
final List<String> sgList;
if(allServerGroups) {
if(serverGroups != null) {
throw new CommandFormatException(this.serverGroups.getFullName() + " can't appear in the same command with " + this.allServerGroups.getFullName());
}
sgList = Util.getServerGroups(client);
if(sgList.isEmpty()) {
throw new CommandFormatException("No server group is available.");
}
} else if(serverGroups == null) {
final StringBuilder buf = new StringBuilder();
buf.append("One of ");
if(f != null) {
buf.append(this.disabled.getFullName()).append(", ");
}
buf.append(this.allServerGroups.getFullName() + " or " + this.serverGroups.getFullName() + " is missing.");
throw new CommandFormatException(buf.toString());
} else {
sgList = Arrays.asList(serverGroups.split(","));
if(sgList.isEmpty()) {
throw new CommandFormatException("Couldn't locate server group name in '" + this.serverGroups.getFullName() + "=" + serverGroups + "'.");
}
}
deployRequest = new ModelNode();
deployRequest.get(Util.OPERATION).set(Util.COMPOSITE);
deployRequest.get(Util.ADDRESS).setEmptyList();
ModelNode steps = deployRequest.get(Util.STEPS);
for (String serverGroup : sgList) {
steps.add(Util.configureDeploymentOperation(Util.ADD, name, serverGroup));
}
for (String serverGroup : sgList) {
steps.add(Util.configureDeploymentOperation(Util.DEPLOY, name, serverGroup));
}
} else {
if(serverGroups != null || allServerGroups) {
throw new CommandFormatException(this.serverGroups.getFullName() + " and " + this.allServerGroups.getFullName() +
" can't appear in standalone mode.");
}
deployRequest = new ModelNode();
deployRequest.get(Util.OPERATION).set(Util.DEPLOY);
deployRequest.get(Util.ADDRESS, Util.DEPLOYMENT).set(name);
}
if(f != null) {
if(Util.isDeploymentInRepository(name, client)) {
throw new CommandFormatException("'" + name + "' already exists in the deployment repository (use " +
this.force.getFullName() + " to replace the existing content in the repository).");
}
final ModelNode request = new ModelNode();
request.get(Util.OPERATION).set(Util.COMPOSITE);
request.get(Util.ADDRESS).setEmptyList();
final ModelNode steps = request.get(Util.STEPS);
steps.add(buildAddRequest(ctx, f, name, runtimeName, unmanaged));
steps.add(deployRequest);
execute(ctx, request, f, unmanaged);
return;
} else if(!Util.isDeploymentInRepository(name, client)) {
throw new CommandFormatException("'" + name + "' is not found among the registered deployments.");
}
try {
final ModelNode result = client.execute(deployRequest);
if (!Util.isSuccess(result)) {
throw new CommandFormatException(Util.getFailureDescription(result));
}
} catch (IOException e) {
throw new CommandFormatException("Failed to deploy: " + e.getLocalizedMessage(), e);
}
}