Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix monitoring ent detection #9590

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions java/code/src/com/redhat/rhn/domain/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -2566,19 +2566,23 @@ boolean isRHEL() {
* This is supposed to cover all RedHat flavors (incl. RHEL, RES and CentOS Linux)
*/
boolean isRedHat6() {
return ServerConstants.REDHAT.equals(getOsFamily()) && getRelease().equals("6");
return ServerConstants.REDHAT.equals(getOsFamily()) &&
(getRelease().equals("6") || getRelease().startsWith("6."));
}

boolean isRedHat7() {
return ServerConstants.REDHAT.equals(getOsFamily()) && getRelease().equals("7");
return ServerConstants.REDHAT.equals(getOsFamily()) &&
(getRelease().equals("7") || getRelease().startsWith("7."));
}

boolean isRedHat8() {
return ServerConstants.REDHAT.equals(getOsFamily()) && getRelease().equals("8");
return ServerConstants.REDHAT.equals(getOsFamily()) &&
(getRelease().equals("8") || getRelease().startsWith("8."));
}

boolean isRedHat9() {
return ServerConstants.REDHAT.equals(getOsFamily()) && getRelease().equals("9");
return ServerConstants.REDHAT.equals(getOsFamily()) &&
(getRelease().equals("9") || getRelease().startsWith("9."));
}

public boolean isRedHat() {
Expand All @@ -2590,19 +2594,13 @@ boolean isAlibaba2() {
}

boolean isAmazon2() {
return ServerConstants.AMAZON.equals(getOsFamily()) && getRelease().equals("2");
return ServerConstants.AMAZON.equals(getOsFamily()) &&
(getRelease().equals("2") || getRelease().startsWith("2."));
}

boolean isAmazon2023() {
return ServerConstants.AMAZON.equals(getOsFamily()) && getRelease().equals("2023");
}

boolean isRocky8() {
return ServerConstants.ROCKY.equals(getOs()) && getRelease().startsWith("8.");
}

boolean isRocky9() {
return ServerConstants.ROCKY.equals(getOs()) && getRelease().startsWith("9.");
return ServerConstants.AMAZON.equals(getOsFamily()) &&
(getRelease().equals("2023") || getRelease().startsWith("2023."));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
import com.redhat.rhn.common.messaging.EventMessage;
import com.redhat.rhn.common.messaging.MessageAction;
import com.redhat.rhn.common.messaging.MessageQueue;
import com.redhat.rhn.common.util.RpmVersionComparator;
import com.redhat.rhn.common.util.StringUtil;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.channel.ChannelArch;
import com.redhat.rhn.domain.channel.ChannelFactory;
import com.redhat.rhn.domain.notification.NotificationMessage;
import com.redhat.rhn.domain.notification.UserNotificationFactory;
import com.redhat.rhn.domain.notification.types.OnboardingFailed;
import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.org.OrgFactory;
import com.redhat.rhn.domain.product.SUSEProduct;
import com.redhat.rhn.domain.role.RoleFactory;
import com.redhat.rhn.domain.server.ContactMethod;
import com.redhat.rhn.domain.server.ManagedServerGroup;
Expand All @@ -50,8 +45,6 @@
import com.redhat.rhn.domain.token.ActivationKey;
import com.redhat.rhn.domain.token.ActivationKeyFactory;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.frontend.dto.EssentialChannelDto;
import com.redhat.rhn.manager.distupgrade.DistUpgradeManager;
import com.redhat.rhn.manager.entitlement.EntitlementManager;
import com.redhat.rhn.manager.formula.FormulaMonitoringManager;
import com.redhat.rhn.manager.system.ServerGroupManager;
Expand Down Expand Up @@ -81,25 +74,20 @@
import com.suse.manager.webui.utils.salt.custom.SumaUtil.PublicCloudInstanceFlavor;
import com.suse.manager.webui.utils.salt.custom.SystemInfo;
import com.suse.salt.netapi.datatypes.target.MinionList;
import com.suse.salt.netapi.errors.SaltError;
import com.suse.salt.netapi.exception.SaltException;
import com.suse.salt.netapi.results.Result;
import com.suse.utils.Opt;

import com.google.gson.reflect.TypeToken;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.security.SecureRandom;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -544,7 +532,7 @@ else if (!minion.getOrg().equals(org)) {

String osfullname = grains.getValueAsString("osfullname");
String osfamily = grains.getValueAsString("os_family");
String osrelease = getOsRelease(minionId, grains);
String osrelease = grains.getOptionalAsString("osrelease").orElse("unknown");

String kernelrelease = grains.getValueAsString("kernelrelease");
String osarch = grains.getValueAsString("osarch");
Expand Down Expand Up @@ -674,18 +662,6 @@ private Optional<String> getActivationKeyLabelFromGrains(ValueMap grains, Option
return activationKeyOverride.isPresent() ? activationKeyOverride : activationKeyFromGrains;
}

/**
* Extract management key label from grains
*
* @param grains
* @return
*/
private Optional<String> getManagementKeyLabelFromGrains(ValueMap grains) {
//apply management key properties that can be set before saving the server
return grains.getMap("susemanager")
.flatMap(suma -> suma.getOptionalAsString("management_key"));
}

/**
* Prepare saltboot configuration for retail minion and apply saltboot state
*
Expand Down Expand Up @@ -945,159 +921,6 @@ private ContactMethod getContactMethod(Optional<ActivationKey> activationKey,
);
}

private static Optional<Channel> lookupBaseChannel(SUSEProduct sp, ChannelArch arch) {
Optional<EssentialChannelDto> productBaseChannelDto =
ofNullable(DistUpgradeManager.getProductBaseChannelDto(sp.getId(), arch));
Optional<Channel> baseChannel = productBaseChannelDto
.flatMap(base -> ofNullable(ChannelFactory.lookupById(base.getId())).map(c -> {
LOG.info("Base channel {} found for OS: {}, version: {}, arch: {}", c.getName(), sp.getName(),
sp.getVersion(), arch.getName());
return c;
}));
if (!baseChannel.isPresent()) {
LOG.warn("Product Base channel not found - refresh SCC sync?");
return empty();
}
return baseChannel;
}

private Optional<String> rpmErrQueryRHELProvidesRelease(String minionId) {
LOG.error("No package providing 'redhat-release' found on RHEL minion {}", minionId);
return empty();
}

private Optional<String> rpmErrQueryRHELRelease(SaltError err, String minionId) {
LOG.error("Error querying 'redhat-release' package on RHEL minion {}: {}", minionId, err);
return empty();
}

private String unknownRHELVersion(String minionId) {
LOG.error("Could not determine OS release version for RHEL minion {}", minionId);
return "unknown";
}

private Map<String, List<String>> parseRHELReleseQuery(String result) {
// Split the result into 3-line chunks per installed package version
String[] resultLines = result.split("\\r?\\n");
List<List<String>> resultItems = new ArrayList<>();
for (int i = 0; i < resultLines.length;) {
List<String> resultItem = new ArrayList<>();
for (int j = 0; j < 3; j++) {
resultItem.add(resultLines[i++]);
}
resultItems.add(resultItem);
}

// Create a map for each 3-line chunk from key-value pairs in each line
return resultItems.stream()
.map(list -> list.stream().map(line -> line.split("="))
.collect(Collectors.toMap(linetoks -> linetoks[0],
linetoks -> Arrays.asList(StringUtils
.splitPreserveAllTokens(linetoks[1], ",")))))
// Get the result map with the biggest rpm version
.max(Comparator.comparing(pkgInfoMap -> pkgInfoMap.get("VERSION").get(0),
new RpmVersionComparator()))
.get();
}

private String getOsRelease(String minionId, ValueMap grains) {
// java port of up2dataUtils._getOSVersionAndRelease()
String osRelease = grains.getValueAsString("osrelease");

if ("redhat".equalsIgnoreCase(grains.getValueAsString("os")) ||
"centos".equalsIgnoreCase(grains.getValueAsString("os")) ||
"oel".equalsIgnoreCase(grains.getValueAsString("os")) ||
"alibaba cloud (aliyun)".equalsIgnoreCase(grains.getValueAsString("os")) ||
"almalinux".equalsIgnoreCase(grains.getValueAsString("os")) ||
("amazon".equalsIgnoreCase(grains.getValueAsString("os")) &&
"2".equalsIgnoreCase(grains.getValueAsString("osmajorrelease"))) ||
"rocky".equalsIgnoreCase(grains.getValueAsString("os"))) {
MinionList target = new MinionList(Arrays.asList(minionId));
Optional<Result<String>> whatprovidesRes = saltApi.runRemoteCommand(target,
"rpm -q --whatprovides --queryformat \"%{NAME}\\n\" redhat-release")
.entrySet()
.stream()
.findFirst()
.map(e -> of(e.getValue()))
.orElse(empty());

osRelease = whatprovidesRes.flatMap(res -> res.fold(
err -> err.fold(err1 -> rpmErrQueryRHELProvidesRelease(minionId),
err2 -> rpmErrQueryRHELProvidesRelease(minionId),
err3 -> rpmErrQueryRHELProvidesRelease(minionId),
err4 -> rpmErrQueryRHELProvidesRelease(minionId),
err5 -> rpmErrQueryRHELProvidesRelease(minionId)),
r -> of(r.split("\\r?\\n")[0]) // Take the first line if multiple results return
))
.flatMap(pkgStr -> {
String[] pkgs = StringUtils.split(pkgStr);
if (pkgs.length > 1) {
LOG.warn("Multiple release packages are installed on minion: {}", minionId);
// Pick the package with the biggest precedence:
// sles_es > redhat > others (centos etc.)
return Arrays.stream(pkgs).max(Comparator.comparingInt(s -> {
switch (s) {
case "sles_es-release-server":
return 2;
case "redhat-release-server":
return 1;
default:
return 0;
}
}));
}
else {
return of(pkgs[0]);
}
})
.flatMap(pkg ->
saltApi.runRemoteCommand(target,
"rpm -q --queryformat \"" +
"VERSION=%{VERSION}\\n" +
"PROVIDENAME=[%{PROVIDENAME},]\\n" +
"PROVIDEVERSION=[%{PROVIDEVERSION},]\\n\" " + pkg)
.entrySet().stream().findFirst().map(Map.Entry::getValue)
.flatMap(res -> res.fold(
err -> err.fold(
err1 -> rpmErrQueryRHELRelease(err1, minionId),
err2 -> rpmErrQueryRHELRelease(err2, minionId),
err3 -> rpmErrQueryRHELRelease(err3, minionId),
err4 -> rpmErrQueryRHELRelease(err4, minionId),
err5 -> rpmErrQueryRHELRelease(err5, minionId)),
Optional::of
))
.map(result -> {
if (result.split("\\r?\\n").length > 3) {
// Output should be 3 lines per installed package version
LOG.warn(String.format(
"Multiple versions of release package '%s' is installed on minion: %s",
pkg, minionId));
}
return this.parseRHELReleseQuery(result);
})
.map(pkgtags -> {
Optional<String> version = Optional
.ofNullable(pkgtags.get("VERSION"))
.map(v -> v.stream().findFirst())
.orElse(empty());
List<String> provideName = pkgtags.get("PROVIDENAME");
List<String> provideVersion = pkgtags.get("PROVIDEVERSION");
int idxReleasever = provideName
.indexOf("system-release(releasever)");
if (idxReleasever > -1) {
version = provideVersion.size() > idxReleasever ?
of(provideVersion.get(idxReleasever)) :
empty();
}
return version;
})
.orElse(empty())
)
.orElseGet(() -> unknownRHELVersion(minionId));
}
return osRelease;
}

private void mapHardwareGrains(MinionServer server, ValueMap grains) {
// for efficiency do this here
server.setRam(grains.getValueAsLong("mem_total").orElse(0L));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@
import com.suse.salt.netapi.calls.modules.Zypper.ProductInfo;
import com.suse.salt.netapi.datatypes.target.MinionList;
import com.suse.salt.netapi.parser.JsonParser;
import com.suse.salt.netapi.results.Result;
import com.suse.salt.netapi.utils.Xor;
import com.suse.utils.Json;

import com.google.gson.Gson;
Expand Down Expand Up @@ -1055,18 +1053,6 @@ void testRegisterRHELMinion(RHELMinionTestCase testCase) throws Exception {
will(returnValue(getSystemInfo(MINION_ID, testCase.productName.toLowerCase(),
baseChannel != null ? key : null)));

allowing(saltServiceMock).runRemoteCommand(
with(any(MinionList.class)),
with("rpm -q --whatprovides --queryformat \"%{NAME}\\n\" redhat-release"));
will(returnValue(singletonMap(MINION_ID,
new Result<>(Xor.right(testCase.availableReleasePackages + "\n")))));

allowing(saltServiceMock).runRemoteCommand(
with(any(MinionList.class)),
with("rpm -q --queryformat \"VERSION=%{VERSION}\\nPROVIDENAME=[%{PROVIDENAME},]\\n" +
"PROVIDEVERSION=[%{PROVIDEVERSION},]\\n\" " + testCase.releasePackage));
will(returnValue(singletonMap(MINION_ID, new Result<>(Xor.right(testCase.packageInfo)))));

allowing(saltServiceMock).redhatProductInfo(MINION_ID);
will(returnValue(Optional.of(new RedhatProductInfo(
Optional.empty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"SSDs": [],
"server": "suma3pg.vagrant.local",
"id": "clisles12-suma3pg.vagrant.local",
"osrelease": "12",
"osrelease": "7.2",
"ps": "ps -efH",
"server_id": 1617825398,
"uuid": "75299898-533c-ff48-8b5c-be026df56f74",
Expand Down
Loading
Loading