Skip to content

Commit

Permalink
Set scheduler if possible on package list refresh
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Arlt <parlt@suse.com>
  • Loading branch information
parlt91 committed May 2, 2023
1 parent 0f45f2c commit 259b66f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
15 changes: 8 additions & 7 deletions java/code/src/com/redhat/rhn/manager/action/ActionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1188,37 +1188,38 @@ public static PackageAction schedulePackageRefresh(User scheduler, Server server
/**
* Schedule a package list refresh without a user.
*
* @param schedulerOrg the organization the server belongs to
* @param user the user that scheduled the action
* @param server the server
* @return the scheduled PackageRefreshListAction
* @throws TaskomaticApiException if there was a Taskomatic error
* (typically: Taskomatic is down)
*/
public static PackageAction schedulePackageRefresh(Org schedulerOrg, Server server)
public static PackageAction schedulePackageRefresh(Optional<User> user, Server server)
throws TaskomaticApiException {
Date earliest = new Date();
return schedulePackageRefresh(schedulerOrg, server, earliest);
return schedulePackageRefresh(user, server, earliest);
}

/**
* Schedule a package list refresh without a user.
*
* @param schedulerOrg the organization the server belongs to
* @param user the organization the server belongs to
* @param server the server
* @param earliest The earliest time this action should be run.
* @return the scheduled PackageRefreshListAction
* @throws TaskomaticApiException if there was a Taskomatic error
* (typically: Taskomatic is down)
*/
public static PackageAction schedulePackageRefresh(Org schedulerOrg, Server server,
public static PackageAction schedulePackageRefresh(Optional<User> user, Server server,
Date earliest) throws TaskomaticApiException {
checkSaltOrManagementEntitlement(server.getId());

Action action = ActionFactory.createAction(
ActionFactory.TYPE_PACKAGES_REFRESH_LIST);
action.setName(ActionFactory.TYPE_PACKAGES_REFRESH_LIST.getName());
action.setOrg(schedulerOrg);
action.setSchedulerUser(null);
action.setOrg(server.getOrg());

action.setSchedulerUser(user.orElse(null));
action.setEarliestAction(earliest);

ServerAction sa = new ServerAction();
Expand Down
2 changes: 1 addition & 1 deletion java/code/src/com/suse/manager/reactor/SaltReactor.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public Stream<EventMessage> eventToMessages(BeaconEvent beaconEvent) {
() -> MinionServerFactory.findByMinionId(beaconEvent.getMinionId())
.ifPresent(minionServer -> {
try {
ActionManager.schedulePackageRefresh(minionServer.getOrg(), minionServer);
ActionManager.schedulePackageRefresh(Optional.empty(), minionServer);
}
catch (TaskomaticApiException e) {
LOG.error("Could not schedule package refresh for minion: {}", minionServer.getMinionId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.redhat.rhn.domain.server.MinionServer;
import com.redhat.rhn.domain.server.MinionServerFactory;
import com.redhat.rhn.domain.server.VirtualInstance;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.manager.action.ActionManager;
import com.redhat.rhn.taskomatic.TaskomaticApiException;

Expand Down Expand Up @@ -55,6 +56,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;


Expand Down Expand Up @@ -145,6 +147,7 @@ public void execute(EventMessage msg) {
if (!jobResult.isPresent()) {
return;
}
AtomicReference<Optional<User>> scheduler = new AtomicReference<>(Optional.empty());
JsonElement jsonResult = jobResult.get();
// The Salt reactor triggers a "suma-action-chain" job (mgractionchains.resume) at
// 'minion/startup/event/'. This means the result might not be a JSON in case of
Expand All @@ -161,6 +164,7 @@ public void execute(EventMessage msg) {
.flatMap(ActionChainFactory::getActionChain);

actionChain.ifPresent(ac -> {
scheduler.set(Optional.ofNullable(ac.getUser()));
ac.getEntries().stream()
.flatMap(ace -> ace.getAction().getServerActions().stream())
.filter(sa -> sa.getServer().asMinionServer()
Expand Down Expand Up @@ -203,23 +207,27 @@ public void execute(EventMessage msg) {
.stream() // handlePackageChange for all the results in actions chain result.
.anyMatch(Boolean.TRUE::equals);
if (packageRefreshNeeded) {
schedulePackageRefresh(jobReturnEvent.getMinionId());
schedulePackageRefresh(scheduler.get(), jobReturnEvent.getMinionId());
}
});

//For all jobs except when action chains are involved or the action was in test mode
if (!isActionChainInvolved && !isFunctionTestMode && handlePackageChanges(jobReturnEvent,
Optional.ofNullable(function).map(Xor::right), jobResult)) {
Date earliest = new Date();
Optional<User> scheduler = Optional.empty();
if (actionId.isPresent()) {
Optional<Action> action = Optional.ofNullable(ActionFactory.lookupById(actionId.get()));
if (action.isPresent() && action.get().getActionType().equals(ActionFactory.TYPE_DIST_UPGRADE)) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 30);
earliest = calendar.getTime();
if (action.isPresent()) {
scheduler = Optional.of(action.get().getSchedulerUser());
if (action.get().getActionType().equals(ActionFactory.TYPE_DIST_UPGRADE)) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 30);
earliest = calendar.getTime();
}
}
}
schedulePackageRefresh(jobReturnEvent.getMinionId(), earliest);
schedulePackageRefresh(scheduler, jobReturnEvent.getMinionId(), earliest);
}

// Check if event was triggered in response to state scheduled at minion start-up event
Expand Down Expand Up @@ -316,19 +324,19 @@ private boolean handlePackageChanges(JobReturnEvent jobReturnEvent, Optional<Xor
* Schedule package refresh on the minion
* @param minionId ID of the minion for which package refresh should be scheduled
*/
private void schedulePackageRefresh(String minionId) {
schedulePackageRefresh(minionId, new Date());
private void schedulePackageRefresh(Optional<User> user, String minionId) {
schedulePackageRefresh(user, minionId, new Date());
}

/**
* Schedule package refresh on the minion
* @param minionId ID of the minion for which package refresh should be scheduled
* @param earliest The earliest time this action should be run.
*/
private void schedulePackageRefresh(String minionId, Date earliest) {
private void schedulePackageRefresh(Optional<User> user, String minionId, Date earliest) {
MinionServerFactory.findByMinionId(minionId).ifPresent(minionServer -> {
try {
ActionManager.schedulePackageRefresh(minionServer.getOrg(), minionServer, earliest);
ActionManager.schedulePackageRefresh(user, minionServer, earliest);
}
catch (TaskomaticApiException e) {
LOG.error(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import com.redhat.rhn.domain.server.VirtualInstanceFactory;
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.PackageListItem;
import com.redhat.rhn.manager.action.ActionManager;
import com.redhat.rhn.manager.kickstart.cobbler.CobblerXMLRPCHelper;
Expand Down Expand Up @@ -729,6 +730,7 @@ else if (stateApplyResult.getChanges() != null) {
.contains(SYSTEM_REBOOT)).orElse(false));

boolean refreshPkg = false;
Optional<User> scheduler = Optional.empty();
for (Map.Entry<String, StateApplyResult<Ret<JsonElement>>> entry : actionChainResult.entrySet()) {
String stateIdKey = entry.getKey();
StateApplyResult<Ret<JsonElement>> stateResult = entry.getValue();
Expand All @@ -740,13 +742,13 @@ else if (stateApplyResult.getChanges() != null) {
// only reboot needs special handling,
// for salt pkg update there's no need to split the sls in case of salt-ssh minions

Action action = ActionFactory.lookupById(stateId.getActionId());
if (stateResult.getName().map(x -> x.fold(Arrays::asList, List::of)
.contains(SYSTEM_REBOOT)).orElse(false) && stateResult.isResult()) {
Action rebootAction = ActionFactory.lookupById(stateId.getActionId());

if (rebootAction.getActionType().equals(ActionFactory.TYPE_REBOOT)) {
if (action.getActionType().equals(ActionFactory.TYPE_REBOOT)) {
Optional<ServerAction> rebootServerAction =
rebootAction.getServerActions().stream()
action.getServerActions().stream()
.filter(sa -> sa.getServer().asMinionServer().isPresent() &&
sa.getServer().asMinionServer().get()
.getMinionId().equals(minionId))
Expand All @@ -765,16 +767,18 @@ else if (stateApplyResult.getChanges() != null) {
if (stateResult.isResult() &&
saltUtils.shouldRefreshPackageList(stateResult.getName(),
Optional.of(stateResult.getChanges().getRet()))) {
scheduler = Optional.of(action.getSchedulerUser());
refreshPkg = true;
}
}
}
Optional<MinionServer> minionServer = MinionServerFactory.findByMinionId(minionId);
if (refreshPkg) {
Optional<User> finalScheduler = scheduler;
minionServer.ifPresent(minion -> {
LOG.info("Scheduling a package profile update for minion {}", minionId);
try {
ActionManager.schedulePackageRefresh(minion.getOrg(), minion);
ActionManager.schedulePackageRefresh(finalScheduler, minion);
}
catch (TaskomaticApiException e) {
LOG.error("Could not schedule package refresh for minion: {}", minion.getMinionId(), e);
Expand Down Expand Up @@ -2513,7 +2517,7 @@ else if (sa.getStatus().equals(ActionFactory.STATUS_QUEUED)) {
LOG.info("Scheduling a package profile update");

try {
ActionManager.schedulePackageRefresh(minion.getOrg(), minion);
ActionManager.schedulePackageRefresh(Optional.of(action.getSchedulerUser()), minion);
}
catch (TaskomaticApiException e) {
LOG.error("Could not schedule package refresh for minion: {}", minion.getMinionId());
Expand Down
2 changes: 2 additions & 0 deletions java/spacewalk-java.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Change default scheduler from (none) to (system) (bsc#1210838)
- Set user for package list refresh action if possible (bsc#1210838)
- Do not throw on missing saltboot group
- fix ISE when neither SCC credentials nor a local mirror is configured
- only set self_update URL if functionality is not disabled in
Expand Down

0 comments on commit 259b66f

Please sign in to comment.