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();
}
}
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;
}
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/*");
}
}
}
}
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);
}
Aggregations