11package jenkins .plugins .openstack .compute ;
22
3+ import hudson .Extension ;
4+ import hudson .model .AsyncPeriodicWork ;
5+ import hudson .model .Executor ;
6+ import hudson .model .Result ;
7+ import hudson .model .TaskListener ;
8+ import hudson .slaves .OfflineCause ;
39import java .util .ArrayList ;
410import java .util .HashMap ;
511import java .util .List ;
612import java .util .Map ;
713import java .util .NoSuchElementException ;
814import java .util .logging .Level ;
915import java .util .logging .Logger ;
10-
11- import com .google .common .collect .ArrayListMultimap ;
12- import com .google .common .collect .ListMultimap ;
13- import hudson .model .Executor ;
14- import hudson .model .Result ;
15- import hudson .slaves .OfflineCause ;
16+ import javax .annotation .Nonnull ;
1617import jenkins .model .CauseOfInterruption ;
1718import jenkins .plugins .openstack .compute .internal .DestroyMachine ;
1819import jenkins .plugins .openstack .compute .internal .Openstack ;
1920import org .jenkinsci .plugins .resourcedisposer .AsyncResourceDisposer ;
2021import org .kohsuke .accmod .Restricted ;
2122import org .kohsuke .accmod .restrictions .NoExternalUse ;
22-
23- import hudson .Extension ;
24- import hudson .model .AsyncPeriodicWork ;
25- import hudson .model .TaskListener ;
26- import org .openstack4j .api .exceptions .ClientResponseException ;
27- import org .openstack4j .api .exceptions .StatusCode ;
2823import org .openstack4j .model .compute .Server ;
2924
30- import javax .annotation .Nonnull ;
31-
3225/**
3326 * Periodically ensure Jenkins and resources it manages in OpenStacks are not leaked.
3427 *
3831 * - Servers that are running longer than declared are terminated.
3932 * - Nodes with server missing are terminated.
4033 */
41- @ Extension @ Restricted (NoExternalUse .class )
34+ @ Extension
35+ @ Restricted (NoExternalUse .class )
4236public final class JCloudsCleanupThread extends AsyncPeriodicWork {
4337 private static final Logger LOGGER = Logger .getLogger (JCloudsCleanupThread .class .getName ());
4438
@@ -89,17 +83,20 @@ private void cleanOrphanedFips() {
8983 try {
9084 openstack .destroyFip (fip );
9185 } catch (Exception ex ) {
92- LOGGER .log (Level .WARNING , "Unable to release floating IP " + fip + " leaked from cloud " + cloud .name , ex );
86+ LOGGER .log (
87+ Level .WARNING ,
88+ "Unable to release floating IP " + fip + " leaked from cloud " + cloud .name ,
89+ ex );
9390 }
9491 }
9592 }
9693 }
9794
9895 private void setCloudLastCleanTime () {
99- for (JCloudsCloud cloud : JCloudsCloud .getClouds ()) {
100- if ((System .currentTimeMillis () - cloud .getLastCleanTime ()) < cloud .getCleanfreqToMillis ()) continue ;
101- cloud .setLastCleanTime (System .currentTimeMillis ());
102- }
96+ for (JCloudsCloud cloud : JCloudsCloud .getClouds ()) {
97+ if ((System .currentTimeMillis () - cloud .getLastCleanTime ()) < cloud .getCleanfreqToMillis ()) continue ;
98+ cloud .setLastCleanTime (System .currentTimeMillis ());
99+ }
103100 }
104101
105102 private void terminateNodesPendingDeletion () {
@@ -110,10 +107,14 @@ private void terminateNodesPendingDeletion() {
110107
111108 final OfflineCause offlineCause = comp .getNode ().getFatalOfflineCause ();
112109 if (comp .isPendingDelete ()) {
113- LOGGER .log (Level .INFO , "Deleting pending node " + comp .getName () + ". Reason: " + comp .getOfflineCause ());
110+ LOGGER .log (
111+ Level .INFO , "Deleting pending node " + comp .getName () + ". Reason: " + comp .getOfflineCause ());
114112 deleteComputer (comp );
115113 } else if (offlineCause != null ) {
116- LOGGER .log (Level .WARNING , "Deleting broken node " + comp .getName () + " (" + getTerminalDiagnosis (comp ) + "). Reason: " + comp .getOfflineCause ());
114+ LOGGER .log (
115+ Level .WARNING ,
116+ "Deleting broken node " + comp .getName () + " (" + getTerminalDiagnosis (comp ) + "). Reason: "
117+ + comp .getOfflineCause ());
117118
118119 deleteComputer (comp );
119120 }
@@ -158,7 +159,7 @@ private void deleteComputer(JCloudsComputer comp, CauseOfInterruption coi) {
158159 for (Executor e : comp .getExecutors ()) {
159160 e .interrupt (Result .ABORTED , coi );
160161 }
161- for (Executor e : comp .getOneOffExecutors ()){
162+ for (Executor e : comp .getOneOffExecutors ()) {
162163 e .interrupt (Result .ABORTED , coi );
163164 }
164165 comp .deleteSlave ();
@@ -179,7 +180,8 @@ private void deleteComputer(JCloudsComputer comp, CauseOfInterruption coi) {
179180 for (Server server : servers ) {
180181 ServerScope scope = ServerScope .extract (server );
181182 if (scope .isOutOfScope (server )) {
182- LOGGER .info ("Server " + server .getName () + " run out of its scope " + scope + ". Terminating: " + server );
183+ LOGGER .info ("Server " + server .getName () + " run out of its scope " + scope + ". Terminating: "
184+ + server );
183185 AsyncResourceDisposer .get ().dispose (new DestroyMachine (jc .name , server .getId ()));
184186 } else {
185187 runningServers .get (jc ).add (server );
@@ -192,7 +194,7 @@ private void deleteComputer(JCloudsComputer comp, CauseOfInterruption coi) {
192194
193195 private void terminatesNodesWithoutServers (@ Nonnull HashMap <JCloudsCloud , List <Server >> runningServers ) {
194196 Map <String , JCloudsComputer > jenkinsComputers = new HashMap <>();
195- for (JCloudsComputer computer : JCloudsComputer .getAll ()) {
197+ for (JCloudsComputer computer : JCloudsComputer .getAll ()) {
196198 JCloudsCloud cloud = JCloudsCloud .getByName (computer .getId ().getCloudName ());
197199 if ((System .currentTimeMillis () - cloud .getLastCleanTime ()) < cloud .getCleanfreqToMillis ()) continue ;
198200
@@ -203,7 +205,7 @@ private void terminatesNodesWithoutServers(@Nonnull HashMap<JCloudsCloud, List<S
203205 }
204206
205207 // Eliminate computers we have servers for
206- for (Map .Entry <JCloudsCloud , List <Server >> e : runningServers .entrySet ()) {
208+ for (Map .Entry <JCloudsCloud , List <Server >> e : runningServers .entrySet ()) {
207209 for (Server server : e .getValue ()) {
208210 jenkinsComputers .remove (server .getId ());
209211 }
@@ -226,21 +228,30 @@ private void terminatesNodesWithoutServers(@Nonnull HashMap<JCloudsCloud, List<S
226228 try { // Double check server does not exist before interrupting jobs
227229 Server explicitLookup = cloud .getOpenstack ().getServerById (id );
228230 if (Openstack .isOccupied (explicitLookup )) {
229- LOGGER .severe (getClass ().getSimpleName () + " incorrectly detected orphaned computer for " + explicitLookup );
231+ LOGGER .severe (getClass ().getSimpleName () + " incorrectly detected orphaned computer for "
232+ + explicitLookup );
230233 continue ; // Do not kill it
231234 }
232235 } catch (NoSuchElementException expected ) {
233236 // Gone as expected
234237 }
235238
236- String msg = "OpenStack server (" + id + ") is not running for computer " + computer .getName () + ". Terminating!" ;
239+ String msg = "OpenStack server (" + id + ") is not running for computer " + computer .getName ()
240+ + ". Terminating!" ;
237241 LOGGER .warning (msg );
238242 deleteComputer (computer , new MessageInterruption (msg ));
239243 }
240244 }
241245
242- @ Override protected Level getNormalLoggingLevel () { return Level .OFF ; }
243- @ Override protected Level getSlowLoggingLevel () { return Level .INFO ; }
246+ @ Override
247+ protected Level getNormalLoggingLevel () {
248+ return Level .OFF ;
249+ }
250+
251+ @ Override
252+ protected Level getSlowLoggingLevel () {
253+ return Level .INFO ;
254+ }
244255
245256 private static class MessageInterruption extends CauseOfInterruption {
246257 private static final long serialVersionUID = 7125610351278586647L ;
@@ -251,7 +262,8 @@ private MessageInterruption(String msg) {
251262 this .msg = msg ;
252263 }
253264
254- @ Override public String getShortDescription () {
265+ @ Override
266+ public String getShortDescription () {
255267 return msg ;
256268 }
257269 }
0 commit comments