Skip to content

Commit

Permalink
Rework ProxyConfigurationApplyAction
Browse files Browse the repository at this point in the history
  • Loading branch information
rjpmestre committed Feb 24, 2025
1 parent 3275d4a commit 8ec6f22
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@


import com.redhat.rhn.domain.org.Org;
import com.redhat.rhn.domain.server.MinionSummary;
import com.redhat.rhn.domain.server.Pillar;

import com.suse.manager.webui.services.SaltServerActionService;
import com.suse.proxy.ProxyConfigUtils;
import com.suse.salt.netapi.calls.LocalCall;
import com.suse.salt.netapi.calls.modules.State;
import com.suse.salt.netapi.utils.Xor;

import com.google.gson.reflect.TypeToken;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -36,6 +36,7 @@
*/
public class ProxyConfigurationApplyAction extends Action {

private static final String APPLY_PROXY_CONFIG = "proxy.apply_proxy_config";
private final Pillar pillar;
private final Map<String, Object> proxyConfigFiles;

Expand All @@ -47,6 +48,7 @@ public class ProxyConfigurationApplyAction extends Action {
*/
public ProxyConfigurationApplyAction(Pillar pillarIn, Map<String, Object> proxyConfigFilesIn, Org orgIn) {
this.setActionType(ActionFactory.TYPE_PROXY_CONFIGURATION_APPLY);
this.setId(ActionFactory.TYPE_PROXY_CONFIGURATION_APPLY.getId().longValue());
this.pillar = pillarIn;
this.proxyConfigFiles = proxyConfigFilesIn;
this.setOrg(orgIn);
Expand All @@ -61,18 +63,30 @@ public Map<String, Object> getProxyConfigFiles() {
}

/**
* Get the apply_proxy_config local call
* @param minions the minions
* Builds the LocalCall for the apply_proxy_config state apply with the pillar and config files data
* @return the apply_proxy_config local call
*/
public Map<LocalCall<?>, List<MinionSummary>> getApplyProxyConfigAction(List<MinionSummary> minions) {
public LocalCall<Xor<String, Map<String, State.ApplyResult>>> getApplyProxyConfigCall() {
Map<String, Object> data = new HashMap<>();
data.putAll(ProxyConfigUtils.applyProxyConfigDataFromPillar(getPillar()));
data.putAll(getProxyConfigFiles());

return State.apply(
Collections.singletonList(APPLY_PROXY_CONFIG),
Optional.of(data),
Optional.of(false), Optional.of(false),
new TypeToken<Xor<String, Map<String, State.ApplyResult>>>() { }
);
}

public LocalCall<Map<String, State.ApplyResult>> getApplyProxyConfigCallSimple() {
Map<String, Object> data = new HashMap<>();
data.putAll(ProxyConfigUtils.applyProxyConfigDataFromPillar(getPillar()));
data.putAll(getProxyConfigFiles());

return Map.of(
State.apply(Collections.singletonList(SaltServerActionService.APPLY_PROXY_CONFIG), Optional.of(data)),
minions
return State.apply(
Collections.singletonList(APPLY_PROXY_CONFIG),
Optional.of(data)
);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016--2025 SUSE LLC
* Copyright (c) 2016 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
Expand Down Expand Up @@ -44,7 +44,6 @@
import com.redhat.rhn.domain.action.ActionFactory;
import com.redhat.rhn.domain.action.ActionStatus;
import com.redhat.rhn.domain.action.ActionType;
import com.redhat.rhn.domain.action.ProxyConfigurationApplyAction;
import com.redhat.rhn.domain.action.appstream.AppStreamAction;
import com.redhat.rhn.domain.action.appstream.AppStreamActionDetails;
import com.redhat.rhn.domain.action.channel.SubscribeChannelsAction;
Expand Down Expand Up @@ -221,7 +220,6 @@ public class SaltServerActionService {
public static final String APPSTREAMS_CONFIGURE = "appstreams.configure";
public static final String PARAM_APPSTREAMS_ENABLE = "param_appstreams_enable";
public static final String PARAM_APPSTREAMS_DISABLE = "param_appstreams_disable";
public static final String APPLY_PROXY_CONFIG = "proxy.apply_proxy_config";

/** SLS pillar parameter name for the list of update stack patch names. */
public static final String PARAM_UPDATE_STACK_PATCHES = "param_update_stack_patches";
Expand Down Expand Up @@ -363,9 +361,6 @@ else if (ActionFactory.TYPE_COCO_ATTESTATION.equals(actionType)) {
else if (ActionFactory.TYPE_APPSTREAM_CONFIGURE.equals(actionType)) {
return appStreamAction(minions, (AppStreamAction) actionIn);
}
else if (ActionFactory.TYPE_PROXY_CONFIGURATION_APPLY.equals(actionType)) {
return ((ProxyConfigurationApplyAction) actionIn).getApplyProxyConfigAction(minions);
}
else {
if (LOG.isDebugEnabled()) {
LOG.debug("Action type {} is not supported with Salt", actionType != null ? actionType.getName() : "");
Expand Down Expand Up @@ -1895,10 +1890,9 @@ private Map<Boolean, Set<MinionSummary>> callAsyncActionChainStart(
*
* @param action the action to be executed
* @param minion minion on which the action will be executed
* @return a map with the results for all calls
*/
public Map<LocalCall<?>, Optional<JsonElement>> executeSSHAction(Action action, MinionServer minion) {
return executeSSHAction(action, minion, false);
public void executeSSHAction(Action action, MinionServer minion) {
executeSSHAction(action, minion, false);
}

/**
Expand All @@ -1907,13 +1901,8 @@ public Map<LocalCall<?>, Optional<JsonElement>> executeSSHAction(Action action,
* @param action the action to be executed
* @param minion minion on which the action will be executed
* @param forcePkgRefresh set to true if a package list refresh should be scheduled at the end
* @return a map with the results for all calls
*/
public Map<LocalCall<?>, Optional<JsonElement>> executeSSHAction(
Action action, MinionServer minion, boolean forcePkgRefresh
) {
Map<LocalCall<?>, Optional<JsonElement>> results = new HashMap<>();

public void executeSSHAction(Action action, MinionServer minion, boolean forcePkgRefresh) {
Optional<ServerAction> serverAction = action.getServerActions().stream()
.filter(sa -> sa.getServerId().equals(minion.getId()))
.findFirst();
Expand Down Expand Up @@ -2004,7 +1993,6 @@ public Map<LocalCall<?>, Optional<JsonElement>> executeSSHAction(
sa.setCompletionTime(new Date());
}
}, jsonResult -> {
results.put(call, Optional.of(jsonResult));
String function = (String) call.getPayload().get("fun");

/* bsc#1197591 ssh push reboot has an answer that is not a failure but the action needs to stay
Expand Down Expand Up @@ -2042,11 +2030,9 @@ else if (sa.getStatus().equals(ActionFactory.STATUS_QUEUED)) {
sa.setStatus(STATUS_FAILED);
sa.setResultMsg("Minion is down or could not be contacted.");
sa.setCompletionTime(new Date());
results.put(call, Optional.empty());
});
}
});
return results;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@

package com.suse.proxy.update;

import static com.suse.utils.Predicates.isAbsent;

import com.redhat.rhn.GlobalInstanceHolder;
import com.redhat.rhn.domain.action.ProxyConfigurationApplyAction;
import com.redhat.rhn.manager.action.ActionManager;

import com.suse.salt.netapi.calls.LocalCall;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.suse.salt.netapi.calls.modules.State;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -38,45 +32,52 @@
public class ProxyConfigUpdateApplySaltState implements ProxyConfigUpdateContextHandler {
private static final Logger LOG = LogManager.getLogger(ProxyConfigUpdateApplySaltState.class);

public static final String FAIL_APPLY_MESSAGE = "Failed to apply proxy configuration salt state.";
private static final String FAIL_APPLY_MESSAGE = "Failed to apply proxy configuration salt state.";

@Override
public void handle(ProxyConfigUpdateContext context) {

ProxyConfigurationApplyAction action = new ProxyConfigurationApplyAction(
context.getPillar(),
context.getProxyConfigFiles(),
context.getUser().getOrg()
);
action.setName("Apply proxy configuration: " + context.getProxyMinion().getMinionId());
ActionManager.addServerToAction(context.getProxyMinion(), action);
Map<LocalCall<?>, Optional<JsonElement>> applySaltStateResponse =
GlobalInstanceHolder.SALT_SERVER_ACTION_SERVICE.executeSSHAction(action, context.getProxyMinion());

if (isAbsent(applySaltStateResponse)) {
context.getErrorReport().register(FAIL_APPLY_MESSAGE);
LOG.error(FAIL_APPLY_MESSAGE + " No response.");
return;
}
else if (applySaltStateResponse.size() != 1) {
context.getErrorReport().register(FAIL_APPLY_MESSAGE);
LOG.error(FAIL_APPLY_MESSAGE + " Unexpected response size. {}", applySaltStateResponse);
return;
}
Optional<Map<String, State.ApplyResult>> stringApplyResultMap = context.getSaltApi().callSync(
action.getApplyProxyConfigCall(),
context.getProxyMinion().getMinionId())
.map(
result -> result.fold(
error -> {
context.getErrorReport().register(error);
return null;
},
applyResults -> {
if (applyResults.isEmpty()) {
context.getErrorReport().register(FAIL_APPLY_MESSAGE);
LOG.error(FAIL_APPLY_MESSAGE + " Unexpected response size. {}", applyResults.size());
return null;
}

List<String> failedStates = applyResults.entrySet().stream()
.filter(p -> !p.getValue().isResult())
.map(e ->
String.format(
"name: %s, comment: %s",
e.getKey(),
e.getValue().getComment()
))
.toList();
if (!failedStates.isEmpty()) {
context.getErrorReport().register(FAIL_APPLY_MESSAGE);
LOG.debug(FAIL_APPLY_MESSAGE + " Fail applying: {}", failedStates);
}
return applyResults;
}));

JsonElement jsonElement = applySaltStateResponse.values().iterator().next().orElse(null);
if (jsonElement == null || !jsonElement.isJsonObject()) {
if (stringApplyResultMap.isEmpty()) {
context.getErrorReport().register(FAIL_APPLY_MESSAGE);
LOG.error(FAIL_APPLY_MESSAGE + " Unexpected response format. {}", jsonElement);
return;
LOG.error(FAIL_APPLY_MESSAGE + " No apply results.");
}
JsonObject jsonObject = jsonElement.getAsJsonObject();
for (String key : jsonObject.keySet()) {
JsonElement jsonElementChild = jsonObject.get(key);
if (!jsonElementChild.getAsJsonObject().get("result").getAsBoolean()) {
context.getErrorReport().register(FAIL_APPLY_MESSAGE);
LOG.error(FAIL_APPLY_MESSAGE + " Failing entry: {}", jsonElementChild);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ mgrpxy_installed:
[Install]
WantedBy=multi-user.target
- require:
- file: /etc/uyuni/proxy/config.yaml
- file: /etc/uyuni/proxy/httpd.yaml
- file: /etc/uyuni/proxy/ssh.yaml
- pkg: podman_installed
- pkg: mgrpxy_installed
# The system will run this service to enable apply_proxy_config.service after reboot
enable_apply_proxy_config_service:
Expand All @@ -121,10 +127,6 @@ enable_apply_proxy_config_service:
- enable: True
- require:
- file: /etc/systemd/system/apply_proxy_config.service
- file: /etc/uyuni/proxy/config.yaml
- file: /etc/uyuni/proxy/httpd.yaml
- file: /etc/uyuni/proxy/ssh.yaml
{% else %}
Expand Down

0 comments on commit 8ec6f22

Please sign in to comment.