com.sun.enterprise.v3.admin.cluster.CreateInstanceCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018-2021] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.v3.admin.cluster;
import com.sun.enterprise.config.serverbeans.Domain;
import com.sun.enterprise.config.serverbeans.Node;
import com.sun.enterprise.config.serverbeans.Nodes;
import com.sun.enterprise.config.serverbeans.Server;
import com.sun.enterprise.config.serverbeans.Servers;
import com.sun.enterprise.universal.glassfish.ASenvPropertyReader;
import com.sun.enterprise.util.ExceptionUtil;
import com.sun.enterprise.util.StringUtils;
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.util.io.InstanceDirs;
import fish.payara.util.cluster.PayaraServerNameGenerator;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import jakarta.inject.Inject;
import org.glassfish.api.ActionReport;
import org.glassfish.api.I18n;
import org.glassfish.api.Param;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.api.admin.AdminCommandContext;
import org.glassfish.api.admin.CommandRunner;
import org.glassfish.api.admin.CommandRunner.CommandInvocation;
import org.glassfish.api.admin.ExecuteOn;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.api.admin.RestEndpoint;
import org.glassfish.api.admin.RestEndpoints;
import org.glassfish.api.admin.RuntimeType;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.hk2.api.PerLookup;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.Target;
import org.jvnet.hk2.annotations.Service;
import static com.sun.enterprise.util.SystemPropertyConstants.AGENT_ROOT_PROPERTY;
import static java.util.logging.Level.SEVERE;
import static org.glassfish.api.ActionReport.ExitCode.FAILURE;
import static org.glassfish.api.ActionReport.ExitCode.SUCCESS;
import static org.glassfish.api.ActionReport.ExitCode.WARNING;
import static org.glassfish.api.admin.RestEndpoint.OpType.POST;
/**
* Remote AdminCommand to create an instance. This command is run only on DAS.
* 1. Register the instance on DAS
* 2. Create the file system on the instance node via ssh, DCOM, node agent, or other
* 3. Bootstrap a minimal set of config files on the instance for secure admin.
*
* @author Jennifer Chou
*/
@Service(name = "create-instance")
@I18n("create.instance")
@PerLookup
@ExecuteOn(RuntimeType.DAS)
@RestEndpoints({
@RestEndpoint(configBean=Domain.class,
opType=POST,
path="create-instance",
description="Create Instance")
})
public class CreateInstanceCommand implements AdminCommand {
private static final String NEWLINE = System.lineSeparator();
@Param(name = "node", alias = "nodeagent")
String node;
@Param(name = "config", optional = true)
@I18n("generic.config")
String configRef;
@Param(name = "cluster", optional = true)
String clusterName;
@Param(name = "deploymentgroup", optional = true)
String deploymentGroup;
@Param(name = "lbenabled", optional = true)
private Boolean lbEnabled;
@Param(name = "checkports", optional = true, defaultValue = "true")
private boolean checkPorts;
@Param(optional = true, defaultValue = "false")
private boolean terse;
@Param(name = "portbase", optional = true)
private String portBase;
@Param(name = "systemproperties", optional = true, separator = ':')
private String systemProperties;
@Param(name = "autoname", optional = true, shortName = "a", defaultValue = "false")
private boolean autoName;
@Param(name = "extraterse", optional = true, shortName = "T", defaultValue = "false")
private boolean extraTerse;
@Param(name = "instance_name", primary = true)
private String instance;
@Param(name = "dataGridStartPort", optional = true, alias = "datagridstartport")
private String dataGridStartPort;
@Inject
private CommandRunner commandRunner;
@Inject
private ServiceLocator serviceLocator;
@Inject
private Nodes nodes;
@Inject
private Servers servers;
@Inject
private ServerEnvironment env;
@Inject
private Target targetUtil;
private Logger logger; // set in execute and all references occur after that assignment
private AdminCommandContext ctx;
private Node theNode;
private String nodeHost;
private String nodeDir;
private String installDir;
private String registerInstanceMessage;
@Override
public void execute(AdminCommandContext context) {
ActionReport report = context.getActionReport();
ctx = context;
logger = context.getLogger();
if (!env.isDas()) {
String msg = Strings.get("notAllowed");
logger.warning(msg);
report.setActionExitCode(FAILURE);
report.setMessage(msg);
return;
}
// Make sure Node is valid
theNode = nodes.getNode(node);
if (theNode == null) {
String msg = Strings.get("noSuchNode", node);
logger.warning(msg);
report.setActionExitCode(FAILURE);
report.setMessage(msg);
return;
}
if (lbEnabled != null && clusterName == null) {
String msg = Strings.get("lbenabledNotForStandaloneInstance");
logger.warning(msg);
report.setActionExitCode(FAILURE);
report.setMessage(msg);
return;
}
nodeHost = theNode.getNodeHost();
nodeDir = theNode.getNodeDirAbsolute();
installDir = theNode.getInstallDir();
if (autoName) {
instance = PayaraServerNameGenerator.validateInstanceNameUnique(instance, context);
} else {
if (theNode.isLocal()) {
validateInstanceDirUnique(report, context);
if (report.getActionExitCode() != SUCCESS && report.getActionExitCode() != WARNING) {
// If we couldn't update domain.xml then stop!
return;
}
}
}
// First, update domain.xml by calling _register-instance
CommandInvocation commandInvocation = commandRunner.getCommandInvocation("_register-instance", report, context.getSubject());
ParameterMap commandParameters = new ParameterMap();
commandParameters.add("node", node);
commandParameters.add("config", configRef);
commandParameters.add("cluster", clusterName);
commandParameters.add("deploymentgroup", deploymentGroup);
if (lbEnabled != null) {
commandParameters.add("lbenabled", lbEnabled.toString());
}
if (!checkPorts) {
commandParameters.add("checkports", "false");
}
if (StringUtils.ok(portBase)) {
commandParameters.add("portbase", portBase);
}
commandParameters.add("systemproperties", systemProperties);
commandParameters.add("DEFAULT", instance);
commandInvocation.parameters(commandParameters);
commandInvocation.execute();
if (report.getActionExitCode() != SUCCESS && report.getActionExitCode() != WARNING) {
// If we couldn't update domain.xml then stop!
return;
}
registerInstanceMessage = report.getMessage();
// If nodehost is localhost and installdir is null and config node, update config node
// so installdir is product root. see register-instance above
if (theNode.isLocal() && installDir == null && theNode.getType().equals("CONFIG")) {
commandInvocation = commandRunner.getCommandInvocation("_update-node", report, context.getSubject());
commandParameters = new ParameterMap();
commandParameters.add("installdir", "${com.sun.aas.productRoot}");
commandParameters.add("type", "CONFIG");
commandParameters.add("DEFAULT", theNode.getName());
commandInvocation.parameters(commandParameters);
commandInvocation.execute();
if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS
&& report.getActionExitCode() != ActionReport.ExitCode.WARNING) {
// If we couldn't update domain.xml then stop!
return;
}
}
if (!validateDasOptions()) {
report.setActionExitCode(WARNING);
return;
}
if (theNode.getType().equals("DOCKER")) {
report.appendMessage(
"\n\nSuccessfully registered instance with DAS, now attempting to create Docker container...");
createDockerContainer();
} else {
// Then go create the instance filesystem on the node
createInstanceFilesystem();
}
if (extraTerse) {
report.setMessage(instance);
}
if (StringUtils.ok(dataGridStartPort)) {
commandInvocation = commandRunner.getCommandInvocation("set-hazelcast-configuration", report, context.getSubject());
commandParameters = new ParameterMap();
commandParameters.add("configSpecificDataGridStartPort", dataGridStartPort);
commandParameters.add("target", targetUtil.getConfig(instance).getName());
commandInvocation.parameters(commandParameters);
commandInvocation.execute();
if (report.getActionExitCode() != ActionReport.ExitCode.SUCCESS
&& report.getActionExitCode() != ActionReport.ExitCode.WARNING) {
// If we couldn't update domain.xml then stop!
return;
}
}
}
private void validateInstanceDirUnique(ActionReport report, AdminCommandContext context) {
CommandInvocation listInstancesCommand = commandRunner.getCommandInvocation("list-instances", report, context.getSubject());
ParameterMap commandParameters = new ParameterMap();
commandParameters.add("whichTarget", theNode.getName());
listInstancesCommand.parameters(commandParameters);
listInstancesCommand.execute();
Properties extraProperties = listInstancesCommand.report().getExtraProperties();
if (extraProperties != null) {
List