forked from uyuni-project/uyuni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemUnentitler.java
121 lines (104 loc) · 4.45 KB
/
SystemUnentitler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright (c) 2020 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
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/
package com.redhat.rhn.manager.system.entitling;
import com.redhat.rhn.common.validator.ValidatorException;
import com.redhat.rhn.domain.entitlement.Entitlement;
import com.redhat.rhn.domain.server.EntitlementServerGroup;
import com.redhat.rhn.domain.server.Server;
import com.redhat.rhn.domain.server.ServerFactory;
import com.redhat.rhn.manager.entitlement.EntitlementManager;
import com.redhat.rhn.manager.system.ServerGroupManager;
import com.suse.manager.webui.services.iface.MonitoringManager;
import com.suse.manager.webui.services.iface.VirtManager;
import com.suse.manager.webui.services.pillar.MinionPillarManager;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;
/**
* Class for removing entitlements from servers
*/
public class SystemUnentitler {
private static final Logger LOG = Logger.getLogger(SystemUnentitler.class);
private final MonitoringManager monitoringManager;
private final VirtManager virtManager;
private final ServerGroupManager serverGroupManager;
/**
* @param virtManagerIn instance for managing virtual machines.
* @param monitoringManagerIn instance for handling monitoring configuration.
* @param serverGroupManagerIn
*/
public SystemUnentitler(VirtManager virtManagerIn, MonitoringManager monitoringManagerIn,
ServerGroupManager serverGroupManagerIn) {
this.virtManager = virtManagerIn;
this.monitoringManager = monitoringManagerIn;
this.serverGroupManager = serverGroupManagerIn;
}
/**
* Removes all the entitlements related to a server.
* @param server server to be unentitled.
*/
public void removeAllServerEntitlements(Server server) {
Set<Entitlement> entitlements = server.getEntitlements();
entitlements.stream().forEach(e -> unentitleServer(server, e));
}
/**
* Removes an entitlement from the given Server. If the given entitlement is the base entitlement,
* removes all entitlements from the Server.
* @param server the server
* @param ent the entitlement
*/
public void removeServerEntitlement(Server server, Entitlement ent) {
if (!server.hasEntitlement(ent)) {
if (LOG.isDebugEnabled()) {
LOG.debug("server doesnt have entitlement: " + ent);
}
return;
}
if (ent.isBase()) {
removeAllServerEntitlements(server);
}
else {
unentitleServer(server, ent);
}
server.asMinionServer().ifPresent(s -> {
serverGroupManager.updatePillarAfterGroupUpdateForServers(Arrays.asList(s));
if (EntitlementManager.MONITORING.equals(ent)) {
try {
monitoringManager.disableMonitoring(s);
}
catch (ValidatorException | IOException e) {
LOG.warn("Error disabling monitoring: " + e.getMessage());
}
}
if (EntitlementManager.VIRTUALIZATION.equals(ent)) {
virtManager.updateLibvirtEngine(s);
MinionPillarManager.INSTANCE.generatePillar(s, false,
MinionPillarManager.PillarSubset.VIRTUALIZATION);
}
});
}
private void unentitleServer(Server server, Entitlement ent) {
Optional<EntitlementServerGroup> entitlementServerGroup = server.findServerGroupByEntitlement(ent);
if (entitlementServerGroup.isPresent()) {
ServerFactory.addServerHistoryWithEntitlementEvent(server, ent, "removed system entitlement ");
ServerFactory.removeServerFromGroup(server, entitlementServerGroup.get());
}
else {
LOG.error("Cannot remove entitlement: " + ent.getLabel() + " from system: " + server.getId());
}
}
}