Search in sources :

Example 1 with DeploymentGroup

use of fish.payara.enterprise.config.serverbeans.DeploymentGroup in project Payara by payara.

the class ListInstancesCommand method yesStatus.

private void yesStatus(List<Server> serverList, int timeoutInMsec, Logger logger) {
    // Gather a list of InstanceInfo -- one per instance in domain.xml
    RemoteInstanceCommandHelper helper = new RemoteInstanceCommandHelper(habitat);
    for (Server server : serverList) {
        boolean clustered = server.getCluster() != null;
        int port = helper.getAdminPort(server);
        String host = server.getAdminHost();
        if (standaloneonly && clustered) {
            continue;
        }
        String name = server.getName();
        if (name == null) {
            // can this happen?!?
            continue;
        }
        StringBuilder deploymentGroup = new StringBuilder();
        for (DeploymentGroup dg : server.getDeploymentGroup()) {
            deploymentGroup.append(dg.getName()).append(' ');
        }
        Cluster cluster = domain.getClusterForInstance(name);
        String clusterName = (cluster != null) ? cluster.getName() : null;
        // skip DAS
        if (notDas(name)) {
            ActionReport tReport = habitat.getService(ActionReport.class, "html");
            InstanceInfo ii = new InstanceInfo(habitat, server, port, host, clusterName, deploymentGroup.toString(), logger, timeoutInMsec, tReport, stateService);
            infos.add(ii);
        }
    }
    if (infos.isEmpty()) {
        report.setMessage(NONE);
        return;
    }
    Properties extraProps = new Properties();
    List<Map<String, Object>> instanceList = new ArrayList<>();
    infos.sort(Comparator.comparing(InstanceInfo::getName));
    for (InstanceInfo ii : infos) {
        String name = ii.getName();
        String value = (ii.isRunning()) ? InstanceState.StateType.RUNNING.getDescription() : InstanceState.StateType.NOT_RUNNING.getDescription();
        InstanceState.StateType state = (ii.isRunning()) ? (stateService.setState(name, InstanceState.StateType.RUNNING, false)) : (stateService.setState(name, InstanceState.StateType.NOT_RUNNING, false));
        List<String> failedCmds = stateService.getFailedCommands(name);
        if (state == InstanceState.StateType.RESTART_REQUIRED) {
            if (ii.isRunning()) {
                value = InstanceState.StateType.RESTART_REQUIRED.getDescription();
            }
        }
        Map<String, Object> insDetails = new HashMap<>();
        insDetails.put("name", name);
        insDetails.put("status", value);
        insDetails.put("deploymentgroup", ii.getDeploymentGroups().trim());
        if (state == InstanceState.StateType.RESTART_REQUIRED) {
            insDetails.put("restartReasons", failedCmds);
        }
        if (ii.isRunning()) {
            insDetails.put("uptime", ii.getUptime());
        }
        instanceList.add(insDetails);
    }
    extraProps.put("instanceList", instanceList);
    report.setExtraProperties(extraProps);
    if (long_opt) {
        report.setMessage(InstanceInfo.format(infos));
    } else {
        report.setMessage(InstanceInfo.formatBrief(infos));
    }
}
Also used : RemoteInstanceCommandHelper(com.sun.enterprise.admin.util.RemoteInstanceCommandHelper) ActionReport(org.glassfish.api.ActionReport) InstanceInfo(com.sun.enterprise.util.cluster.InstanceInfo) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 2 with DeploymentGroup

use of fish.payara.enterprise.config.serverbeans.DeploymentGroup in project Payara by payara.

the class ListInstancesCommand method getServersForDeploymentGroup.

private List<Server> getServersForDeploymentGroup() {
    List<Server> result = null;
    DeploymentGroup dg = domain.getDeploymentGroupNamed(whichTarget);
    if (dg != null) {
        result = dg.getInstances();
    }
    return result;
}
Also used : DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 3 with DeploymentGroup

use of fish.payara.enterprise.config.serverbeans.DeploymentGroup in project Payara by payara.

the class CreateResourceRef method chooseRefContainer.

private RefContainer chooseRefContainer(final AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    Class<?>[] allInterfaces = resourceOfInterest.getClass().getInterfaces();
    for (Class<?> resourceInterface : allInterfaces) {
        ResourceConfigCreator resourceConfigCreator = resourceInterface.getAnnotation(ResourceConfigCreator.class);
        if (resourceConfigCreator != null) {
            commandName = resourceConfigCreator.commandName();
        }
    }
    if (commandName != null) {
        List<ServiceHandle<?>> serviceHandles = locator.getAllServiceHandles(new Filter() {

            @Override
            public boolean matches(Descriptor arg0) {
                String name = arg0.getName();
                return name != null && name.equals(commandName);
            }
        });
        for (ServiceHandle<?> handle : serviceHandles) {
            ActiveDescriptor<?> descriptor = handle.getActiveDescriptor();
            if (descriptor.getName().equals(commandName)) {
                if (!descriptor.isReified()) {
                    locator.reifyDescriptor(descriptor);
                }
                AdminCommand service = locator.<AdminCommand>getService(descriptor.getImplementationClass());
                if (service != null) {
                    TargetType targetType = descriptor.getImplementationClass().getAnnotation(TargetType.class);
                    targets = targetType.value();
                    break;
                }
            }
        }
        if (!(isTargetValid = validateTarget(target, targets))) {
            return null;
        }
        Config config = domain.getConfigs().getConfigByName(target);
        if (config != null) {
            return config;
        }
        Server server = configBeansUtilities.getServerNamed(target);
        if (server != null) {
            return server;
        }
        DeploymentGroup deploymentGroup = domain.getDeploymentGroupNamed(target);
        if (deploymentGroup != null) {
            return deploymentGroup;
        }
        return domain.getClusterNamed(target);
    } else {
        report.setMessage(LOCAL_STRINGS.getLocalString("create.resource.ref.failed", "Resource ref {0} creation failed", refName));
        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        return null;
    }
}
Also used : ActionReport(org.glassfish.api.ActionReport) Filter(org.glassfish.hk2.api.Filter) ServiceHandle(org.glassfish.hk2.api.ServiceHandle) TargetType(org.glassfish.config.support.TargetType) ActiveDescriptor(org.glassfish.hk2.api.ActiveDescriptor) Descriptor(org.glassfish.hk2.api.Descriptor) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 4 with DeploymentGroup

use of fish.payara.enterprise.config.serverbeans.DeploymentGroup in project Payara by payara.

the class UpdateResourceRef method execute.

/**
 * Execution method for updating the configuration of a ResourceRef. Will be
 * replicated if the target is a cluster.
 *
 * @param context context for the command.
 */
@Override
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // Make a list of all ResourceRefs that need to change
    List<ResourceRef> resourceRefsToChange = new ArrayList<>();
    // Add the ResourceRef from a named server if the target is a server
    Server server = domain.getServerNamed(target);
    // if the target is a server
    if (server != null) {
        ResourceRef serverResourceRef = server.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (serverResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(serverResourceRef);
    }
    // Add the ResourceRef from a named config if the target is a config
    Config config = domain.getConfigNamed(target);
    // if the target is a config
    if (config != null) {
        ResourceRef configResourceRef = config.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (configResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(configResourceRef);
    }
    // Add the ResourceRefs from a named cluster if the target is a cluster
    Cluster cluster = domain.getClusterNamed(target);
    // if the target is a cluster
    if (cluster != null) {
        ResourceRef clusterResourceRef = cluster.getResourceRef(name);
        // if the ResourceRef doesn't exist
        if (clusterResourceRef == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(clusterResourceRef);
        for (Server instance : cluster.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the cluster contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Add the ResourceRefs from a named Deployment Group if the target is a Deployment Group
    DeploymentGroup dg = domain.getDeploymentGroupNamed(target);
    if (dg != null) {
        ResourceRef ref = dg.getResourceRef(name);
        if (ref == null) {
            report.failure(logger, LOCAL_STRINGS.getLocalString("resource.ref.not.exists", "Target {1} does not have a reference to resource {0}.", name, target));
            return;
        }
        resourceRefsToChange.add(ref);
        for (Server instance : dg.getInstances()) {
            ResourceRef instanceResourceRef = instance.getResourceRef(name);
            // if the server in the dg contains the ResourceRef
            if (instanceResourceRef != null) {
                resourceRefsToChange.add(instanceResourceRef);
            }
        }
    }
    // Apply the configuration to the listed ResourceRefs
    try {
        ConfigSupport.apply(new ConfigCode() {

            @Override
            public Object run(ConfigBeanProxy... params) throws PropertyVetoException, TransactionFailure {
                for (ConfigBeanProxy proxy : params) {
                    if (proxy instanceof ResourceRef) {
                        ResourceRef resourceRefProxy = (ResourceRef) proxy;
                        if (enabled != null) {
                            resourceRefProxy.setEnabled(enabled.toString());
                        }
                    }
                }
                report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
                return true;
            }
        }, resourceRefsToChange.toArray(new ResourceRef[] {}));
    } catch (TransactionFailure ex) {
        report.failure(logger, ex.getLocalizedMessage());
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Server(com.sun.enterprise.config.serverbeans.Server) Config(com.sun.enterprise.config.serverbeans.Config) ArrayList(java.util.ArrayList) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) PropertyVetoException(java.beans.PropertyVetoException) ConfigBeanProxy(org.jvnet.hk2.config.ConfigBeanProxy) ConfigCode(org.jvnet.hk2.config.ConfigCode) ResourceRef(com.sun.enterprise.config.serverbeans.ResourceRef) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Example 5 with DeploymentGroup

use of fish.payara.enterprise.config.serverbeans.DeploymentGroup in project Payara by payara.

the class CreateApplicationRefCommand method execute.

/**
 * Entry point from the framework into the command execution
 * @param context context for the command.
 */
public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    final Logger logger = context.getLogger();
    // retrieve matched version(s) if exist
    List<String> matchedVersions = null;
    if (enabled) {
        try {
            // warn users that they can use version expressions
            VersioningUtils.checkIdentifier(name);
            matchedVersions = new ArrayList<String>(1);
            matchedVersions.add(name);
        } catch (VersioningWildcardException ex) {
            // a version expression is supplied with enabled == true
            report.setMessage(localStrings.getLocalString("wildcard.not.allowed", "WARNING : version expression are available only with --enabled=false"));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        } catch (VersioningSyntaxException ex) {
            report.setMessage(ex.getLocalizedMessage());
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        if (!deployment.isRegistered(name)) {
            report.setMessage(localStrings.getLocalString("application.notreg", "Application {0} not registered", name));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    } else {
        // retrieve matched version(s) if exist
        try {
            matchedVersions = versioningService.getMatchedVersions(name, null);
        } catch (VersioningException e) {
            report.failure(logger, e.getMessage());
            return;
        }
        // this is an unversioned behavior and the given application is not registered
        if (matchedVersions.isEmpty()) {
            report.setMessage(localStrings.getLocalString("ref.not.referenced.target", "Application {0} is not referenced by target {1}", name, target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    ActionReport.MessagePart part = report.getTopMessagePart();
    boolean isVersionExpression = VersioningUtils.isVersionExpression(name);
    // for each matched version
    Iterator it = matchedVersions.iterator();
    while (it.hasNext()) {
        String appName = (String) it.next();
        Application applicationInfo = applications.getApplication(appName);
        List<DeploymentGroup> deploymentGroups = domain.getDeploymentGroupsForInstance(target);
        boolean isAppOnDeploymentGroupInstance = false;
        if (deploymentGroups != null && deploymentGroups.isEmpty()) {
            List<Application> applicationsInTarget = domain.getApplicationsInTarget(target);
            List<String> listOfApplications = new ArrayList<>();
            for (Application application : applicationsInTarget) {
                listOfApplications.add(application.getName());
            }
            if (listOfApplications.contains(appName)) {
                isAppOnDeploymentGroupInstance = true;
                break;
            }
        }
        if (!isAppOnDeploymentGroupInstance) {
            ApplicationRef applicationRef = domain.getApplicationRefInTarget(appName, target);
            if (applicationRef != null) {
                // if a versioned name has been provided to the command
                if (isVersionExpression) {
                    ActionReport.MessagePart childPart = part.addChild();
                    childPart.setMessage(localStrings.getLocalString("appref.already.exists", "Application reference {0} already exists in target {1}.", appName, target));
                } else {
                    // returns failure if an untagged name has been provided to the command
                    report.setMessage(localStrings.getLocalString("appref.already.exists", "Application reference {0} already exists in target {1}.", name, target));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            } else {
                Transaction t = new Transaction();
                if (applicationInfo.isLifecycleModule()) {
                    handleLifecycleModule(context, t);
                    return;
                }
                ReadableArchive archive;
                File file = null;
                DeployCommandParameters commandParams = null;
                Properties contextProps;
                Map<String, Properties> modulePropsMap = null;
                ApplicationConfigInfo savedAppConfig = null;
                try {
                    commandParams = applicationInfo.getDeployParameters(null);
                    commandParams.origin = Origin.create_application_ref;
                    commandParams.command = Command.create_application_ref;
                    commandParams.target = target;
                    commandParams.virtualservers = virtualservers;
                    commandParams.enabled = enabled;
                    if (lbenabled != null) {
                        commandParams.lbenabled = lbenabled;
                    }
                    commandParams.type = applicationInfo.archiveType();
                    contextProps = applicationInfo.getDeployProperties();
                    modulePropsMap = applicationInfo.getModulePropertiesMap();
                    savedAppConfig = new ApplicationConfigInfo(applicationInfo);
                    URI uri = new URI(applicationInfo.getLocation());
                    file = new File(uri);
                    if (!file.exists()) {
                        report.setMessage(localStrings.getLocalString("fnf", "File not found", file.getAbsolutePath()));
                        report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                        return;
                    }
                    archive = archiveFactory.openArchive(file);
                } catch (Exception e) {
                    logger.log(Level.SEVERE, "Error opening deployable artifact : " + file.getAbsolutePath(), e);
                    report.setMessage(localStrings.getLocalString("unknownarchiveformat", "Archive format not recognized"));
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
                try {
                    final ExtendedDeploymentContext deploymentContext = deployment.getBuilder(logger, commandParams, report).source(archive).build();
                    Properties appProps = deploymentContext.getAppProps();
                    appProps.putAll(contextProps);
                    // relativize the location so it could be set properly in
                    // domain.xml
                    String location = DeploymentUtils.relativizeWithinDomainIfPossible(new URI(applicationInfo.getLocation()));
                    appProps.setProperty(ServerTags.LOCATION, location);
                    // relativize the URI properties so they could store in the
                    // domain.xml properly on the instances
                    String appLocation = appProps.getProperty(Application.APP_LOCATION_PROP_NAME);
                    appProps.setProperty(Application.APP_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(appLocation)));
                    String planLocation = appProps.getProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME);
                    if (planLocation != null) {
                        appProps.setProperty(Application.DEPLOYMENT_PLAN_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(planLocation)));
                    }
                    String altDDLocation = appProps.getProperty(Application.ALT_DD_LOCATION_PROP_NAME);
                    if (altDDLocation != null) {
                        appProps.setProperty(Application.ALT_DD_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(altDDLocation)));
                    }
                    String runtimeAltDDLocation = appProps.getProperty(Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME);
                    if (runtimeAltDDLocation != null) {
                        appProps.setProperty(Application.RUNTIME_ALT_DD_LOCATION_PROP_NAME, DeploymentUtils.relativizeWithinDomainIfPossible(new URI(runtimeAltDDLocation)));
                    }
                    savedAppConfig.store(appProps);
                    if (modulePropsMap != null) {
                        deploymentContext.setModulePropsMap(modulePropsMap);
                    }
                    if (enabled) {
                        versioningService.handleDisable(appName, target, deploymentContext.getActionReport(), context.getSubject());
                    }
                    if (domain.isCurrentInstanceMatchingTarget(target, appName, server.getName(), null)) {
                        deployment.deploy(deployment.getSniffersFromApp(applicationInfo), deploymentContext);
                    } else {
                        // send the APPLICATION_PREPARED event for DAS
                        events.send(new Event<DeploymentContext>(Deployment.APPLICATION_PREPARED, deploymentContext), false);
                    }
                    final List<String> targets = new ArrayList<String>(Arrays.asList(commandParams.target.split(",")));
                    List<String> deploymentTarget = new ArrayList<>();
                    // If targets contains Deployment Group, check if the application is already deployed to instances in it.
                    for (String target : targets) {
                        if (isDeploymentGroup(target)) {
                            List<Server> instances = domain.getDeploymentGroupNamed(target).getInstances();
                            for (Server instance : instances) {
                                List<Application> applications = domain.getApplicationsInTarget(instance.getName());
                                List<String> listOfApplications = new ArrayList<>();
                                for (Application application : applications) {
                                    listOfApplications.add(application.getName());
                                }
                                if (!listOfApplications.contains(appName)) {
                                    deploymentTarget.add(instance.getName());
                                }
                            }
                        }
                    }
                    if (report.getActionExitCode().equals(ActionReport.ExitCode.SUCCESS)) {
                        try {
                            deployment.registerAppInDomainXML(null, deploymentContext, t, true);
                        } catch (TransactionFailure e) {
                            logger.warning("failed to create application ref for " + appName);
                        }
                    }
                    // if the target is DAS, we do not need to do anything more
                    if (!isVersionExpression && DeploymentUtils.isDASTarget(target)) {
                        return;
                    }
                    final ParameterMap paramMap = deployment.prepareInstanceDeployParamMap(deploymentContext);
                    if (!deploymentTarget.isEmpty()) {
                        replicateCommand(deploymentTarget, context, paramMap);
                    } else {
                        replicateCommand(targets, context, paramMap);
                    }
                } catch (Exception e) {
                    logger.log(Level.SEVERE, "Error during creating application ref ", e);
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                } finally {
                    try {
                        archive.close();
                    } catch (IOException e) {
                        logger.log(Level.INFO, "Error while closing deployable artifact : " + file.getAbsolutePath(), e);
                    }
                }
            }
        }
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) VersioningWildcardException(org.glassfish.deployment.versioning.VersioningWildcardException) ArrayList(java.util.ArrayList) ActionReport(org.glassfish.api.ActionReport) Logger(java.util.logging.Logger) DeploymentProperties(org.glassfish.deployment.common.DeploymentProperties) Properties(java.util.Properties) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) URI(java.net.URI) Iterator(java.util.Iterator) VersioningException(org.glassfish.deployment.versioning.VersioningException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) ParameterMap(org.glassfish.api.admin.ParameterMap) IOException(java.io.IOException) VersioningException(org.glassfish.deployment.versioning.VersioningException) VersioningWildcardException(org.glassfish.deployment.versioning.VersioningWildcardException) VersioningSyntaxException(org.glassfish.deployment.versioning.VersioningSyntaxException) IOException(java.io.IOException) DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) DeploymentContext(org.glassfish.api.deployment.DeploymentContext) ExtendedDeploymentContext(org.glassfish.internal.deployment.ExtendedDeploymentContext) Transaction(org.jvnet.hk2.config.Transaction) ApplicationConfigInfo(org.glassfish.deployment.common.ApplicationConfigInfo) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File) DeploymentGroup(fish.payara.enterprise.config.serverbeans.DeploymentGroup)

Aggregations

DeploymentGroup (fish.payara.enterprise.config.serverbeans.DeploymentGroup)25 ActionReport (org.glassfish.api.ActionReport)10 Server (com.sun.enterprise.config.serverbeans.Server)9 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)7 ArrayList (java.util.ArrayList)6 Cluster (com.sun.enterprise.config.serverbeans.Cluster)5 Domain (com.sun.enterprise.config.serverbeans.Domain)5 ConfigApiTest (com.sun.enterprise.configapi.tests.ConfigApiTest)5 Test (org.junit.Test)5 DeploymentGroups (fish.payara.enterprise.config.serverbeans.DeploymentGroups)4 ApplicationRef (com.sun.enterprise.config.serverbeans.ApplicationRef)3 ResourceRef (com.sun.enterprise.config.serverbeans.ResourceRef)3 PropertyVetoException (java.beans.PropertyVetoException)3 Logger (java.util.logging.Logger)3 ParameterMap (org.glassfish.api.admin.ParameterMap)3 ConfigBeanProxy (org.jvnet.hk2.config.ConfigBeanProxy)3 DGServerRef (fish.payara.enterprise.config.serverbeans.DGServerRef)2 IOException (java.io.IOException)2 Properties (java.util.Properties)2 DeployCommandParameters (org.glassfish.api.deployment.DeployCommandParameters)2