Skip to content

Commit

Permalink
Added button for taking offline leniently
Browse files Browse the repository at this point in the history
There is now an optional dependency to lenient
shutdown plugin, allowing admins to take slaves
offline leniently if the Lenient Shutdown Plugin
is already installed.

Change-Id: I6c55998900f2f4702b2b6dd91f048f026c332a6e
  • Loading branch information
fredrikpersson committed Aug 13, 2014
1 parent 91eba68 commit ab7aedb
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 23 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -223,6 +223,12 @@
<artifactId>windows-slaves</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.sonymobile.jenkins.plugins.lenientshutdown</groupId>
<artifactId>lenientshutdown</artifactId>
<version>1.0.0</version>
<optional>true</optional>
</dependency>

</dependencies>

Expand Down
Expand Up @@ -24,6 +24,7 @@

package com.sonyericsson.hudson.plugins.multislaveconfigplugin;

import com.sonymobile.jenkins.plugins.lenientshutdown.PluginImpl;
import hudson.Extension;
import hudson.Functions;
import hudson.Util;
Expand Down Expand Up @@ -784,6 +785,38 @@ public boolean takeOffline(String reason, StaplerRequest req, StaplerResponse rs
return result;
}

/**
* Requests the selected slaves to go offline leniently. Only possible if Lenient Shutdown Plugin is installed.
* @param rsp StaplerResponse.
* @param req StaplerRequest.
* @return false if no nodes were selected or something else goes wrong, otherwise true
*/
@JavaScriptMethod
public boolean takeOfflineLeniently(StaplerRequest req, StaplerResponse rsp) {
// Throws exception on failure. This is handled at a higher level.
Hudson.getInstance().checkPermission(getRequiredPermission());
String currentSessionId = req.getSession().getId();
NodeList nodeList = getNodeList(currentSessionId);

boolean takenOffline = false;

if (isLenientShutdownPluginInstalled()) {
PluginImpl lenientShutdownInstance = PluginImpl.getInstance();

if (nodeList != null) {
takenOffline = true;
for (Node node : nodeList) {
String nodeName = node.getNodeName();
if (!lenientShutdownInstance.isNodeShuttingDown(nodeName)) {
lenientShutdownInstance.setNodeOffline(node.toComputer());
}
}
}
}

return takenOffline;
}

/**
* Connects (not forced) to selected slaves.
* @param rsp StaplerResponse.
Expand Down Expand Up @@ -811,6 +844,14 @@ public boolean connectSlaves(StaplerRequest req, StaplerResponse rsp) {
return result;
}

/**
* Checks if Lenient Shutdown Plugin is installed.
* @return true if the plugin is installed, otherwise false
*/
public static boolean isLenientShutdownPluginInstalled() {
return Jenkins.getInstance().getPlugin("lenientshutdown") != null;
}

/**
* Disconnects from selected slaves.
* @param reason the reason for disconnecting
Expand Down
@@ -1,7 +1,7 @@
<!--
* The MIT License
*
* Copyright 2014 Sony Mobile Communications AB. All rights reserved.
* Copyright 2014 Sony Mobile Communications Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -39,39 +39,48 @@

</l:header>




<l:main-panel>
<h3>${it.displayName} - ${%Manage slaves}</h3>
${%In this section you can manage the slaves chosen in the previous page.}
${%ManageInfo}
<table class="sortable pane bigtable">
<tr align="left"> </tr>
<tr align="left">
<th class="table-cell">
<p>${%Bring the selected slaves online by pressing the button.}</p>
<p>${%OnlineButtonInfo}</p>
<input id="takeOnline" value="${%Bring slaves online}" class="yui-button"/>
<p id="onlineResponse"><st:nbsp/></p>
</th>
<th class="table-cell">
<p>${%Take the selected slaves temporarily offline by pressing the button.}</p>
<p>${%A reason can be typed into the box.}</p>
<p>${%If a node is already offline, this will only change the offline reason.}</p>
<j:choose>
<j:when test="${it.lenientShutdownPluginInstalled}">
<p>${%TempOfflineTwoButtonsInfo}</p>
<p>${%LenientShutdownUsage}</p>
<input id="takeOfflineLeniently" value="${%Take slaves offline leniently}" class="yui-button"/>
<br/><br/>
</j:when>
<j:otherwise>
<p>${%TempOfflineOneButtonInfo}</p>
</j:otherwise>
</j:choose>

<p>${%OfflineReason}</p>
<p>${%OfflineReasonChange}</p>
<input style="width: 100%;" type="text" placeholder="Reason" name="offlineReason" id="offlineReason"/>
<br/>
<input id="takeOffline" value="${%Take slaves offline}" class="yui-button"/>

<p id="offlineResponse"><st:nbsp/></p>
</th>
</tr>
<tr align="left">
<th class="table-cell">
<p>${%Connect to the selected slaves by pressing the button.}</p>
<p>${%ConnectButtonInfo}</p>
<input id="connectSlaves" value="${%Connect to slaves}" class="yui-button"/>
<p id="connectResponse"><st:nbsp/></p>
</th>
<th class="table-cell">
<p>${%Disconnect from selected slaves by pressing the button.}</p>
<p>${%A reason can by typed into the box.}</p>
<p>${%DisconnectButtonInfo}</p>
<p>${%DisconnectReason}</p>
<input style="width: 100%;" type="text" placeholder="Reason" name="disconnectReason" id="disconnectReason"/>
<br/>
<input id="disconnectSlaves" value="${%Disconnect from slaves}" class="yui-button"/>
Expand All @@ -88,35 +97,40 @@

var nodeManageLink = <st:bind value="${it}"/>

makeButton(document.getElementById('takeOnline'), takeOnline);
function takeOnline() {
makeButton(document.getElementById('takeOnline'), function() {
nodeManageLink.takeOnline(function(result) {
displayResult('onlineResponse', result);
});
}
});

makeButton(document.getElementById('takeOffline'), takeOffline);
function takeOffline() {
makeButton(document.getElementById('takeOffline'), function() {
var reason = document.getElementById('offlineReason').value;
nodeManageLink.takeOffline(reason, function(result) {
displayResult('offlineResponse', result);
});
});

var lenientOfflineButton = document.getElementById('takeOfflineLeniently');
if (lenientOfflineButton != null) {
makeButton(lenientOfflineButton, function() {
nodeManageLink.takeOfflineLeniently(function(result) {
displayResult('offlineResponse', result);
});
});
}

makeButton(document.getElementById('connectSlaves'), connectSlaves);
function connectSlaves() {
makeButton(document.getElementById('connectSlaves'), function() {
nodeManageLink.connectSlaves(function(result) {
displayResult('connectResponse', result);
});
}
});

makeButton(document.getElementById('disconnectSlaves'), disconnectSlaves);
function disconnectSlaves() {
makeButton(document.getElementById('disconnectSlaves'), function() {
var reason = document.getElementById('disconnectReason').value;
nodeManageLink.disconnectSlaves(reason, function(result) {
displayResult('disconnectResponse', result);
});
}
});

/**
* A text response to the user pressing the button saying
Expand Down
@@ -0,0 +1,30 @@
ManageInfo=\
In this section you can manage the slaves chosen in the previous page.

OnlineButtonInfo=\
Bring the selected slaves online by pressing the button.

TempOfflineTwoButtonsInfo=\
Take the selected slaves temporarily offline by pressing one of the two buttons below.

TempOfflineOneButtonInfo=\
Take the selected slaves temporarily offline by pressing the button.

LenientShutdownUsage=\
The button for taking offline leniently makes use of Lenient Shutdown Plugin.

OfflineReason=\
A reason can be typed into the box when taking offline.

OfflineReasonChange=\
If a node is already offline, this will only change the offline reason.

ConnectButtonInfo=\
Connect to the selected slaves by pressing the button.

DisconnectButtonInfo=\
Disconnect from selected slaves by pressing the button.

DisconnectReason=\
A reason can by typed into the box.

0 comments on commit ab7aedb

Please sign in to comment.