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 2074613 commit 1f429fe
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 149 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 @@ -40,7 +40,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
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 1f429fe

Please sign in to comment.