Skip to content

Commit

Permalink
Migrate to SLE4SAP
Browse files Browse the repository at this point in the history
  • Loading branch information
admd committed Jan 18, 2024
1 parent 2296091 commit 2baf7ed
Show file tree
Hide file tree
Showing 11 changed files with 554 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
import com.redhat.rhn.common.db.datasource.Row;
import com.redhat.rhn.common.db.datasource.SelectMode;
import com.redhat.rhn.domain.kickstart.KickstartFactory;
import com.redhat.rhn.domain.product.SUSEProduct;
import com.redhat.rhn.domain.product.SUSEProductFactory;
import com.redhat.rhn.domain.product.SUSEProductSet;
import com.redhat.rhn.domain.server.Server;
import com.redhat.rhn.domain.server.ServerFactory;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.manager.entitlement.EntitlementManager;
import com.redhat.rhn.manager.kickstart.cobbler.CobblerXMLRPCHelper;
import com.redhat.rhn.manager.system.SystemManager;

import com.suse.utils.Opt;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -34,6 +38,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* SystemAclHandler
Expand Down Expand Up @@ -190,4 +195,49 @@ public boolean aclSystemHasSaltEntitlementAndContactMethod(Map<String, Object> c
return ret;
}

/**
* Checks if a system is Salt entitled and uses the given
* contact method.
* @param ctx Context Map to pass in
* @param params Parameters to use
* @return true if system is minion and uses the contact method
*/
public boolean aclSystemHasCorrespondingSapChannels(Map<String, Object> ctx, String[] params) {
Long sid = getAsLong(ctx.get("sid"));
boolean ret = false;
if (sid != null && sid == 1000010003l) {
User user = (User) ctx.get("user");
Server server = SystemManager.lookupByIdAndUser(sid, user);
if (server != null) {
Optional<SUSEProductSet> installedProducts = server.getInstalledProductSet();
if (installedProducts.isEmpty()) {
// Installed products are 'unknown'
log.debug("Installed products are 'unknown'");
}
installedProducts.ifPresent(pset -> {
log.debug(pset.toString());
if (pset.getBaseProduct() == null) {
log.error("Server: {} has no base product installed. Check your servers installed products.",
server.getId());
}
});
List<SUSEProduct> baseProductsList = Opt.fold(installedProducts,
() -> {
log.warn("No products installed on this system");
return null;
},
prd -> {
SUSEProduct baseProduct = prd.getBaseProduct();
if (baseProduct == null) {
log.warn("No base product found");
return null;
}
return SUSEProductFactory.findMatchingSAPProducts(baseProduct);
});
ret = !baseProductsList.isEmpty();
}
}
return ret;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.redhat.rhn.common.util.RpmVersionComparator;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.channel.ChannelFactory;
import com.redhat.rhn.domain.channel.ChannelFamily;
import com.redhat.rhn.domain.channel.ChannelFamilyFactory;
import com.redhat.rhn.domain.cloudpayg.CloudRmtHost;
import com.redhat.rhn.domain.rhnpackage.PackageArch;
import com.redhat.rhn.domain.rhnpackage.PackageEvr;
import com.redhat.rhn.domain.rhnpackage.PackageFactory;
Expand Down Expand Up @@ -50,6 +52,7 @@
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

/**
* SUSEProductFactory - the class used to fetch and store
Expand All @@ -68,7 +71,7 @@ private SUSEProductFactory() {
* Insert or update a SUSEProduct.
* @param product SUSE product to be inserted into the database.
*/
public static void save(SUSEProduct product) {
public static void save(SUSEProduct product) {
singleton.saveObject(product);
}

Expand Down Expand Up @@ -516,6 +519,34 @@ public static SUSEProduct lookupByProductId(long productId) {
return (SUSEProduct) c.uniqueResult();
}

/**
* Find matching {@link SUSEProduct} object of SLE for SAP against a given SLE product
* @param productId the product
* @return SUSE product for given productId
*/
public static List<SUSEProduct> findMatchingSAPProducts(SUSEProduct baseProduct) {
CriteriaBuilder criteriaBuilder = getSession().getCriteriaBuilder();
CriteriaQuery<SUSEProduct> criteriaQuery = criteriaBuilder.createQuery(SUSEProduct.class);
Root<SUSEProduct> suseRoot = criteriaQuery.from(SUSEProduct.class);

// Creating the subquery
Subquery<Long> subquery = criteriaQuery.subquery(Long.class);
Root<ChannelFamily> rhnRoot = subquery.from(ChannelFamily.class);
subquery.select(rhnRoot.get("id"))
.where(criteriaBuilder.equal(rhnRoot.get("name"), "SUSE Linux Enterprise Server for SAP"));

// Adding conditions to the main query
criteriaQuery.where(
criteriaBuilder.equal(suseRoot.get("version"), baseProduct.getVersion()),
criteriaBuilder.equal(suseRoot.get("arch"), baseProduct.getArch()),
criteriaBuilder.like(suseRoot.get("name"), "%sap%"),
criteriaBuilder.equal(suseRoot.get("channelFamily"), subquery)
);

List<SUSEProduct> result = getSession().createQuery(criteriaQuery).getResultList();
return result;
}

/**
* Lookup all {@link SUSEProduct} objects and provide a map with productId as key.
* @return map of SUSE products with productId as key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25333,6 +25333,9 @@ given channel.</source>
<trans-unit id="Calendars" xml:space="preserve">
<source>Calendars</source>
</trans-unit>
<trans-unit id="Convert to SLES for SAP" xml:space="preserve">
<source>Convert to SLES for SAP</source>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ public static Set<Action> scheduleSubscribeChannelsAction(User user,
Date earliest,
ActionChain actionChain)
throws TaskomaticApiException {
Set<Action> result = createActions(user, ActionFactory.TYPE_SUBSCRIBE_CHANNELS, "Subscribe channels",
Set<Action> result = createActions(user, ActionFactory.TYPE_SUBSCRIBE_CHANNELS, "Migrate to SLES for SAP",
earliest, actionChain, null, serverIds);
for (Action action : result) {
SubscribeChannelsActionDetails details = new SubscribeChannelsActionDetails();
Expand Down
Loading

0 comments on commit 2baf7ed

Please sign in to comment.