Skip to content

Commit

Permalink
[FIX JENKINS-38391] Show admin monitors on most URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-beck committed Sep 21, 2016
1 parent 123d224 commit a11d9ce
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 2 deletions.
@@ -0,0 +1,136 @@
/*
* The MIT License
*
* Copyright (c) 2016, Daniel Beck, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.management;

import hudson.Extension;
import hudson.Functions;
import hudson.diagnosis.ReverseProxySetupMonitor;
import hudson.model.AdministrativeMonitor;
import hudson.model.PageDecorator;
import hudson.util.HudsonIsLoading;
import hudson.util.HudsonIsRestarting;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Show a notification and popup for active administrative monitors on all pages.
*/
@Extension
@Restricted(NoExternalUse.class)
public class AdministrativeMonitorsDecorator extends PageDecorator {
private final Collection<String> ignoredJenkinsRestOfUrls = new ArrayList<>();

public AdministrativeMonitorsDecorator() {
// redundant
ignoredJenkinsRestOfUrls.add("manage");

// otherwise this would be added to every internal context menu building request
ignoredJenkinsRestOfUrls.add("contextMenu");
}

@Override
public String getDisplayName() {
return Messages.AdministrativeMonitorsDecorator_DisplayName();
}

public int getActiveAdministrativeMonitorsCount() {
return getActiveAdministrativeMonitors().size();
}

public Collection<AdministrativeMonitor> getActiveAdministrativeMonitors() {
Collection<AdministrativeMonitor> active = new ArrayList<>();
Collection<AdministrativeMonitor> ams = new ArrayList<>(Jenkins.getInstance().administrativeMonitors);
for (AdministrativeMonitor am : ams) {
if (am instanceof ReverseProxySetupMonitor) {
// TODO make reverse proxy monitor work when shown on any URL
continue;
}
if (am.isEnabled() && am.isActivated()) {
active.add(am);
}
}
return active;
}

/**
* Whether the administrative monitors notifier should be shown.
* @return true iff the administrative monitors notifier should be shown.
* @throws IOException
* @throws ServletException
*/
public boolean shouldDisplay() throws IOException, ServletException {
if (!Functions.hasPermission(Jenkins.ADMINISTER)) {
return false;
}

if (getActiveAdministrativeMonitorsCount() == 0) {
return false;
}

StaplerRequest req = Stapler.getCurrentRequest();

if (req == null) {
return false;
}
List<Ancestor> ancestors = req.getAncestors();

if (ancestors == null || ancestors.size() == 0) {
// ???
return false;
}

Ancestor a = ancestors.get(ancestors.size() - 1);
Object o = a.getObject();

// don't show while Jenkins is loading
if (o instanceof HudsonIsLoading) {
return false;
}
// … or restarting
if (o instanceof HudsonIsRestarting) {
return false;
}

// don't show for some URLs served directly by Jenkins
if (o instanceof Jenkins) {
String url = a.getRestOfUrl();

if (ignoredJenkinsRestOfUrls.contains(url)) {
return false;
}
}

return true;
}
}
Expand Up @@ -26,6 +26,6 @@ THE SOFTWARE.
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<div class="error">
${%ZFS migration failed.}
<a href="${it.url}/">${%See the log for more details}</a>.
<a href="${rootURL}/${it.url}/">${%See the log for more details}</a>.
</div>
</j:jelly>
Expand Up @@ -26,6 +26,6 @@ THE SOFTWARE.
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<div class="error">
<j:out value="${it.message}"/>
<a href="${it.url}/">${%See the log for more details}</a>.
<a href="${rootURL}/${it.url}/">${%See the log for more details}</a>.
</div>
</j:jelly>
@@ -0,0 +1,159 @@
<!--
The MIT License
Copyright (c) 2016, Daniel Beck, Keith Zantow, CloudBees, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<j:if test="${it.shouldDisplay()}">
<style type="text/css">
#visible-am-container {
float: right;
}
#visible-am-button {
text-align: center;
font-size: 20px;
font-weight: bold;
background-color: #e01716;
color: #fff;;
margin: 0;
line-height: 40px;
text-decoration: none;
min-width: 2em;
display: inline-block;
position: relative;
transition: all .1s;
}
#visible-am-button:hover, #visible-am-button:focus, #visible-am-button:active {
background: #e23635;
}
#visible-am-container.visible #visible-am-button {
box-shadow: inset 0px 1px 14px rgba(0,0,0,.5);
}
#visible-am-container div#visible-am-list {
position: absolute;
top: 35px;
right: 2%;
margin-left:2%;
height: auto;
z-index: 0;
padding: 2em;
border: 1px solid #aa;
text-align: left;
display: block;
background-color: #fff;
border-radius: 5px;
box-shadow: 0 7px 20px rgba(0,0,0,.1);
transition: all .15s cubic-bezier(.84,.03,.21,.96);
opacity: 0;
transform: scale(0);
}
#visible-am-container.visible div#visible-am-list {
opacity: 1;
transform: scale(1);
z-index: 1000;
}
#visible-am-container #visible-am-button:after {
content: '';
display: inline-block;
position: absolute;
bottom: -5px;
left: 32%;
width: 0;
height: 0;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-bottom: 7px solid #fff;
opacity: 0;
transition: all .05s;
}
#visible-am-container.visible #visible-am-button:after {
opacity: 1;
bottom: 4px;
transition: all .25s;
}
#visible-am-container .am-message {
display: block;
line-height: 1.4em;
margin-bottom: 1.4em;
}
</style>
<script>
function amCloser(e) {
var list = document.getElementById('visible-am-list');
var el = e.target;
while (el) {
if (el === list) {
return; // clicked in the list
}
el = el.parentElement;
}
hideVisibleAmList();
};
function amEscCloser(e) {
if (e.keyCode == 27) {
amCloser(e);
}
};
function amContainer() {
return document.getElementById('visible-am-container');
};
function hideVisibleAmList(e) {
amContainer().classList.remove('visible');
document.removeEventListener('click', amCloser);
document.removeEventListener('keydown', amEscCloser);
}
function showVisibleAmList(e) {
amContainer().classList.add('visible');
setTimeout(function() {
document.addEventListener('click', amCloser);
document.addEventListener('keydown', amEscCloser);
}, 1);
}
function toggleVisibleAmList(e) {
if (amContainer().classList.contains('visible')) {
hideVisibleAmList(e);
} else {
showVisibleAmList(e);
}
e.preventDefault();
}
</script>
<div id="visible-am-container">
<a id="visible-am-button" href="#" onclick="toggleVisibleAmList(event)">
${it.activeAdministrativeMonitorsCount}
</a>
<div id="visible-am-list">
<j:forEach var="am" items="${it.activeAdministrativeMonitors}">
<div class="am-message">
<st:include page="message.jelly" it="${am}" />
</div>
</j:forEach>
<p style="text-align: center; margin: 10px 0 0 0;">
<a href="${rootURL}/manage" onclick="document.location.href='${rootURL}/manage';">${%Manage Jenkins}</a>
</p>
</div>
</div>
<script type="text/javascript">
document.getElementById("header").appendChild(document.getElementById("visible-am-container"));
</script>
</j:if>
</j:jelly>
Expand Up @@ -56,3 +56,5 @@ NodesLink.Description=Add, remove, control and monitor the various nodes that Je
ShutdownLink.DisplayName_prepare=Prepare for Shutdown
ShutdownLink.DisplayName_cancel=Cancel Shutdown
ShutdownLink.Description=Stops executing new builds, so that the system can be eventually shut down safely.

AdministrativeMonitorsDecorator.DisplayName=Administrative Monitors Notifier

0 comments on commit a11d9ce

Please sign in to comment.