-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathJCloudsCleanupThread.java
More file actions
242 lines (207 loc) · 9.22 KB
/
JCloudsCleanupThread.java
File metadata and controls
242 lines (207 loc) · 9.22 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package jenkins.plugins.openstack.compute;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.slaves.OfflineCause;
import jenkins.model.CauseOfInterruption;
import jenkins.plugins.openstack.compute.internal.DestroyMachine;
import jenkins.plugins.openstack.compute.internal.Openstack;
import org.jenkinsci.plugins.resourcedisposer.AsyncResourceDisposer;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import hudson.Extension;
import hudson.model.AsyncPeriodicWork;
import hudson.model.TaskListener;
import org.openstack4j.api.exceptions.ClientResponseException;
import org.openstack4j.api.exceptions.StatusCode;
import org.openstack4j.model.compute.Server;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
/**
* Periodically ensure Jenkins and resources it manages in OpenStacks are not leaked.
*
* Currently it ensures:
*
* - Node pending deletion get terminated with their servers.
* - Servers that are running longer than declared are terminated.
* - Nodes with server missing are terminated.
*/
@Extension @Restricted(NoExternalUse.class)
public final class JCloudsCleanupThread extends AsyncPeriodicWork {
private static final Logger LOGGER = Logger.getLogger(JCloudsCleanupThread.class.getName());
private final Long recurrencePeriod;
public JCloudsCleanupThread() {
super("OpenStack slave cleanup");
recurrencePeriod = Long.getLong("jenkins.openstack.cleanupPeriod", TimeUnit.MINUTES.toMillis(10));
LOGGER.log(Level.FINE, "OpenStack cleanup recurrence period is {0}ms", recurrencePeriod);
}
@Override
public long getRecurrencePeriod() {
return recurrencePeriod;
}
@Override
public void execute(TaskListener listener) {
try {
terminateNodesPendingDeletion();
@Nonnull HashMap<JCloudsCloud, List<Server>> runningServers = destroyServersOutOfScope();
terminatesNodesWithoutServers(runningServers);
cleanOrphanedFips();
} catch (JCloudsCloud.LoginFailure ex) {
LOGGER.log(Level.WARNING, "Unable to authenticate: " + ex.getMessage());
} catch (Throwable ex) {
LOGGER.log(Level.SEVERE, "Unable to perform the cleanup", ex);
}
}
private void cleanOrphanedFips() {
for (JCloudsCloud cloud : JCloudsCloud.getClouds()) {
Openstack openstack = cloud.getOpenstack();
List<String> leaked = openstack.getFreeFipIds();
if (leaked.isEmpty()) return;
LOGGER.info("Cleaning up floating IPs leaked from cloud " + cloud.name + ": " + leaked);
for (String fip : leaked) {
try {
openstack.destroyFip(fip);
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Unable to release floating IP " + fip + " leaked from cloud " + cloud.name, ex);
}
}
}
}
private void terminateNodesPendingDeletion() {
for (final JCloudsComputer comp : JCloudsComputer.getAll()) {
if (!comp.isIdle()) continue;
final OfflineCause offlineCause = comp.getFatalOfflineCause();
if (comp.isPendingDelete()) {
LOGGER.log(Level.INFO, "Deleting pending node " + comp.getName() + ". Reason: " + comp.getOfflineCause());
deleteComputer(comp);
} else if (offlineCause != null) {
LOGGER.log(Level.WARNING, "Deleting broken node " + comp.getName() + " (" + getTerminalDiagnosis(comp) + "). Reason: " + comp.getOfflineCause());
deleteComputer(comp);
}
}
}
private String getTerminalDiagnosis(JCloudsComputer comp) {
try {
JCloudsSlave node = comp.getNode();
if (node == null) return "Node is gone";
JCloudsCloud cloud;
try {
cloud = JCloudsCloud.getByName(comp.getId().getCloudName());
} catch (IllegalArgumentException e) {
return "Cloud no longer configured - cannot get more info";
}
Server server;
try {
server = cloud.getOpenstack().getServerById(node.getServerId());
} catch (NoSuchElementException e) {
return "Server does not exist in OpenStack";
}
return server.toString();
// TODO capturing server log might be useful
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Failed diagnosing computer failure", ex);
return "none";
}
}
private void deleteComputer(JCloudsComputer comp) {
try {
comp.deleteSlave();
} catch (Throwable e) {
LOGGER.log(Level.WARNING, "Failed to disconnect and delete " + comp.getName(), e);
}
}
private void deleteComputer(JCloudsComputer comp, CauseOfInterruption coi) {
try {
for (Executor e : comp.getExecutors()) {
e.interrupt(Result.ABORTED, coi);
}
for(Executor e : comp.getOneOffExecutors()){
e.interrupt(Result.ABORTED, coi);
}
comp.deleteSlave();
} catch (Throwable e) {
LOGGER.log(Level.WARNING, "Failed to disconnect and delete " + comp.getName(), e);
}
}
/**
* @return Servers not destroyed as they are in scope.
*/
private @Nonnull HashMap<JCloudsCloud, List<Server>> destroyServersOutOfScope() {
HashMap<JCloudsCloud, List<Server>> runningServers = new HashMap<>();
for (JCloudsCloud jc : JCloudsCloud.getClouds()) {
runningServers.put(jc, new ArrayList<>());
List<Server> servers = jc.getOpenstack().getRunningNodes();
for (Server server : servers) {
ServerScope scope = ServerScope.extract(server);
if (scope.isOutOfScope(server)) {
LOGGER.info("Server " + server.getName() + " run out of its scope " + scope + ". Terminating: " + server);
AsyncResourceDisposer.get().dispose(new DestroyMachine(jc.name, server.getId()));
} else {
runningServers.get(jc).add(server);
}
}
}
return runningServers;
}
private void terminatesNodesWithoutServers(@Nonnull HashMap<JCloudsCloud, List<Server>> runningServers) {
Map<String, JCloudsComputer> jenkinsComputers = new HashMap<>();
for (JCloudsComputer computer: JCloudsComputer.getAll()) {
JCloudsSlave node = computer.getNode();
if (node != null) {
jenkinsComputers.put(node.getServerId(), computer);
}
}
// Eliminate computers we have servers for
for (Map.Entry<JCloudsCloud, List<Server>> e: runningServers.entrySet()) {
for (Server server : e.getValue()) {
jenkinsComputers.remove(server.getId());
}
}
for (Map.Entry<String, JCloudsComputer> entry : jenkinsComputers.entrySet()) {
JCloudsComputer computer = entry.getValue();
String id = entry.getKey();
String cloudName = computer.getId().getCloudName();
JCloudsCloud cloud;
try {
cloud = JCloudsCloud.getByName(cloudName);
} catch (IllegalArgumentException e) {
LOGGER.warning("The cloud " + cloudName + " does not longer exists for " + computer.getName());
// TODO: we ware unable to perform the double lookup - keeping the node alive. Once we are confident
// enough in this, we can do the cleanup anyway
continue;
}
try { // Double check server does not exist before interrupting jobs
Server explicitLookup = cloud.getOpenstack().getServerById(id);
if (Openstack.isOccupied(explicitLookup)) {
LOGGER.severe(getClass().getSimpleName() + " incorrectly detected orphaned computer for " + explicitLookup);
continue; // Do not kill it
}
} catch (NoSuchElementException expected) {
// Gone as expected
}
String msg = "OpenStack server (" + id + ") is not running for computer " + computer.getName() + ". Terminating!";
LOGGER.warning(msg);
deleteComputer(computer, new MessageInterruption(msg));
}
}
@Override protected Level getNormalLoggingLevel() { return Level.OFF; }
@Override protected Level getSlowLoggingLevel() { return Level.INFO; }
private static class MessageInterruption extends CauseOfInterruption {
private static final long serialVersionUID = 7125610351278586647L;
private final String msg;
private MessageInterruption(String msg) {
this.msg = msg;
}
@Override public String getShortDescription() {
return msg;
}
}
}