Search in sources :

Example 1 with MicroprofileHealthCheckConfiguration

use of fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration in project Payara by payara.

the class SetMPHealthCheckConfiguration method execute.

@Override
public void execute(AdminCommandContext context) {
    ActionReport actionReport = context.getActionReport();
    Subject subject = context.getSubject();
    Config targetConfig = targetUtil.getConfig(target);
    MicroprofileHealthCheckConfiguration config = targetConfig.getExtensionByType(MicroprofileHealthCheckConfiguration.class);
    if (Boolean.TRUE.equals(securityEnabled) || Boolean.parseBoolean(config.getSecurityEnabled())) {
        ActionReport checkUserReport = actionReport.addSubActionsReport();
        ActionReport createUserReport = actionReport.addSubActionsReport();
        if (!defaultMicroprofileUserExists(checkUserReport, subject) && !checkUserReport.hasFailures()) {
            createDefaultMicroprofileUser(createUserReport, subject);
        }
        if (checkUserReport.hasFailures() || createUserReport.hasFailures()) {
            actionReport.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
    }
    try {
        ConfigSupport.apply(configProxy -> {
            if (enabled != null) {
                configProxy.setEnabled(enabled.toString());
            }
            if (endpoint != null) {
                configProxy.setEndpoint(endpoint);
            }
            if (virtualServers != null) {
                configProxy.setVirtualServers(virtualServers);
            }
            if (securityEnabled != null) {
                configProxy.setSecurityEnabled(securityEnabled.toString());
            }
            if (roles != null) {
                configProxy.setRoles(roles);
            }
            actionReport.setActionExitCode(ActionReport.ExitCode.SUCCESS);
            return configProxy;
        }, config);
        actionReport.setMessage("Restart server for change to take effect");
    } catch (TransactionFailure ex) {
        actionReport.failure(LOGGER, "Failed to update HealthCheck configuration", ex);
    }
    // If everything has passed, scrap the subaction reports as we don't want to print them out
    if (!actionReport.hasFailures() && !actionReport.hasWarnings()) {
        actionReport.getSubActionsReport().clear();
    }
}
Also used : TransactionFailure(org.jvnet.hk2.config.TransactionFailure) Config(com.sun.enterprise.config.serverbeans.Config) MicroprofileHealthCheckConfiguration(fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration) ActionReport(org.glassfish.api.ActionReport) Subject(javax.security.auth.Subject)

Example 2 with MicroprofileHealthCheckConfiguration

use of fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration in project Payara by payara.

the class MicroProfileHealthChecker method pingAllInstances.

/**
 * Pings MP health check endpoint of all instances and returns a map containing a {@link Future} returning the ping
 * status code for that instance. Any exceptions thrown in the process will raise a {@link ExecutionException} when
 * the {@link Future} is resolved.
 */
private Map<String, Future<Integer>> pingAllInstances(long timeoutMillis) {
    Map<String, Future<Integer>> tasks = new ConcurrentHashMap<>();
    Map<UUID, Future<ClusterCommandResult>> configs = payaraMicro.executeClusteredASAdmin(GET_MP_CONFIG_STRING);
    for (Server server : domain.getServers().getServer()) {
        @Pattern(regexp = "[A-Za-z0-9_][A-Za-z0-9\\-_\\.;]*", message = "{server.invalid.name}", payload = Server.class) String instanceName = server.getName();
        tasks.put(instanceName, payaraExecutorService.submit(() -> {
            // get the remote server's MP HealthCheck config
            MicroprofileHealthCheckConfiguration healthCheckConfig = server.getConfig().getExtensionByType(MicroprofileHealthCheckConfiguration.class);
            if (healthCheckConfig != null && Boolean.valueOf(healthCheckConfig.getEnabled())) {
                return pingHealthEndpoint(buildURI(server, healthCheckConfig.getEndpoint()));
            }
            return -1;
        }));
    }
    for (InstanceDescriptor instance : payaraMicro.getClusteredPayaras()) {
        String instanceName = instance.getInstanceName();
        if (tasks.containsKey(instanceName)) {
            continue;
        }
        tasks.put(instanceName, payaraExecutorService.submit(() -> {
            ClusterCommandResult mpHealthConfigResult = // 
            configs.get(instance.getMemberUUID()).get(timeoutMillis, MILLISECONDS);
            String values = mpHealthConfigResult.getOutput().split("\n")[1];
            Boolean enabled = Boolean.parseBoolean(values.split(" ")[0]);
            if (enabled) {
                String endpoint = values.split(" ", 2)[1].trim();
                return pingHealthEndpoint(buildURI(instance, endpoint));
            }
            return -1;
        }));
    }
    return tasks;
}
Also used : InstanceDescriptor(fish.payara.micro.data.InstanceDescriptor) Pattern(javax.validation.constraints.Pattern) ClusterCommandResult(fish.payara.micro.ClusterCommandResult) Server(com.sun.enterprise.config.serverbeans.Server) Future(java.util.concurrent.Future) MicroprofileHealthCheckConfiguration(fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID)

Example 3 with MicroprofileHealthCheckConfiguration

use of fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration in project Payara by payara.

the class HealthCheckServletContainerInitializer method onStartup.

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
    // Check if this context is the root one ("/")
    if (ctx.getContextPath().isEmpty()) {
        // Check if there is already a servlet for healthcheck
        Map<String, ? extends ServletRegistration> registrations = ctx.getServletRegistrations();
        MicroprofileHealthCheckConfiguration configuration = Globals.getDefaultHabitat().getService(MicroprofileHealthCheckConfiguration.class);
        if (!Boolean.parseBoolean(configuration.getEnabled())) {
            // MP Healthcheck disabled
            return;
        }
        for (ServletRegistration reg : registrations.values()) {
            if (reg.getClass().equals(HealthCheckServlet.class) || reg.getMappings().contains("/" + configuration.getEndpoint())) {
                return;
            }
        }
        String virtualServers = configuration.getVirtualServers();
        if (!isEmpty(virtualServers) && !asList(virtualServers.split(",")).contains(ctx.getVirtualServerName())) {
            return;
        }
        // Register servlet
        ServletRegistration.Dynamic reg = ctx.addServlet("microprofile-healthcheck-servlet", HealthCheckServlet.class);
        reg.addMapping("/" + configuration.getEndpoint() + "/*");
        if (Boolean.parseBoolean(configuration.getSecurityEnabled())) {
            String[] roles = configuration.getRoles().split(",");
            reg.setServletSecurity(new ServletSecurityElement(new HttpConstraintElement(CONFIDENTIAL, roles)));
            ctx.declareRoles(roles);
            if (Boolean.getBoolean(CREATE_INSECURE_ENDPOINT_TEST)) {
                ServletRegistration.Dynamic insecureReg = ctx.addServlet("microprofile-healthcheck-servlet-insecure", HealthCheckServlet.class);
                insecureReg.addMapping("/" + configuration.getEndpoint() + "-insecure/*");
            }
        }
    }
}
Also used : ServletRegistration(javax.servlet.ServletRegistration) MicroprofileHealthCheckConfiguration(fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration) HttpConstraintElement(javax.servlet.HttpConstraintElement) ServletSecurityElement(javax.servlet.ServletSecurityElement)

Example 4 with MicroprofileHealthCheckConfiguration

use of fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration in project Payara by payara.

the class GetMPHealthCheckConfiguration method execute.

@Override
public void execute(AdminCommandContext context) {
    Config targetConfig = targetUtil.getConfig(target);
    if (targetConfig == null) {
        context.getActionReport().setMessage("No such config name: " + targetUtil);
        context.getActionReport().setActionExitCode(ActionReport.ExitCode.FAILURE);
        return;
    }
    MicroprofileHealthCheckConfiguration healthCheckConfiguration = targetConfig.getExtensionByType(MicroprofileHealthCheckConfiguration.class);
    ColumnFormatter columnFormatter = new ColumnFormatter(OUTPUT_HEADERS);
    Object[] outputValues = { healthCheckConfiguration.getEnabled(), healthCheckConfiguration.getEndpoint(), healthCheckConfiguration.getVirtualServers(), healthCheckConfiguration.getSecurityEnabled(), healthCheckConfiguration.getRoles() };
    columnFormatter.addRow(outputValues);
    context.getActionReport().appendMessage(columnFormatter.toString());
    Map<String, Object> extraPropertiesMap = new HashMap<>();
    extraPropertiesMap.put("enabled", healthCheckConfiguration.getEnabled());
    extraPropertiesMap.put("endpoint", healthCheckConfiguration.getEndpoint());
    extraPropertiesMap.put("virtualServers", healthCheckConfiguration.getVirtualServers());
    extraPropertiesMap.put("securityenabled", healthCheckConfiguration.getSecurityEnabled());
    extraPropertiesMap.put("roles", healthCheckConfiguration.getRoles());
    Properties extraProperties = new Properties();
    extraProperties.put("microprofileHealthCheckConfiguration", extraPropertiesMap);
    context.getActionReport().setExtraProperties(extraProperties);
}
Also used : HashMap(java.util.HashMap) Config(com.sun.enterprise.config.serverbeans.Config) MicroprofileHealthCheckConfiguration(fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration) Properties(java.util.Properties) ColumnFormatter(com.sun.enterprise.util.ColumnFormatter)

Aggregations

MicroprofileHealthCheckConfiguration (fish.payara.microprofile.healthcheck.config.MicroprofileHealthCheckConfiguration)4 Config (com.sun.enterprise.config.serverbeans.Config)2 Server (com.sun.enterprise.config.serverbeans.Server)1 ColumnFormatter (com.sun.enterprise.util.ColumnFormatter)1 ClusterCommandResult (fish.payara.micro.ClusterCommandResult)1 InstanceDescriptor (fish.payara.micro.data.InstanceDescriptor)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 UUID (java.util.UUID)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Future (java.util.concurrent.Future)1 Subject (javax.security.auth.Subject)1 HttpConstraintElement (javax.servlet.HttpConstraintElement)1 ServletRegistration (javax.servlet.ServletRegistration)1 ServletSecurityElement (javax.servlet.ServletSecurityElement)1 Pattern (javax.validation.constraints.Pattern)1 ActionReport (org.glassfish.api.ActionReport)1 TransactionFailure (org.jvnet.hk2.config.TransactionFailure)1