Skip to content

Commit

Permalink
New linked jobs page for nodes, for JENKINS-20035
Browse files Browse the repository at this point in the history
  • Loading branch information
dominiquebrice committed Sep 23, 2014
1 parent 13889d5 commit 8a491a1
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 3 deletions.
@@ -0,0 +1,169 @@
/*
* The MIT License
*
* Copyright (C) 2014 Dominique Brice
*
* 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.plugins.linkedjobs.actions;

import java.util.Iterator;

import jenkins.model.Jenkins;
import jenkins.plugins.linkedjobs.model.NodeData;
import jenkins.plugins.linkedjobs.settings.GlobalSettings;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Computer;
import hudson.model.Label;
import hudson.model.Node;
import hudson.model.TopLevelItem;

/**
* For each Computer, Jenkins associates an object of this class to a Linked Jobs page
* thanks to {@link jenkins.plugins.linkedjobs.extensions.ComputerExtension}.<br/><br/>
* This class is responsible for building & collecting the data necessary to build
* this additional page.
* @author dominiquebrice
*
*/
public class ComputerLinkedJobsAction implements Action {

/**
* The computer/node associated to this action
*/
private final Computer computer;

public ComputerLinkedJobsAction(Computer c) {
// for now only store the computer reference
// calculation is done when requested by display
this.computer = c;
}

public Node getNode() {
return computer.getNode();
}

public String getIconFileName() {
return "search.png";
}

// name of the additional link in the left-side menu
public String getDisplayName() {
return "Linked Jobs";
}

// relative URL used for the additional link in the left-side menu
public String getUrlName() {
return "linkedjobs";
}

public boolean getDetailedView() {
return GlobalSettings.get().getDetailedView();
}

public boolean getShowSingleNodeJobs() {
return GlobalSettings.get().getShowSingleNodeJobs();
}

// this function finds all jobs that can run only on this node because
// of labels (mis-)configuration, thus with a potential non-redundancy
// issue
public NodeData getExclusiveJobs() {
Node node = computer.getNode();
NodeData result = new NodeData(node);

for (AbstractProject<?, ?> job : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
if (!(job instanceof TopLevelItem)) {
// consider only TopLevelItem - not 100% sure why, though...
continue;
}

if (canRunExclusivelyOnThisNode(job.getAssignedLabel())) {
result.addJob(job);
}
}

return result;
}

private boolean canRunExclusivelyOnThisNode(Label l) {
if (l == null) {
// no label so no exclusivity
return false;
}

Node exclusiveNode = null;

Jenkins jenkins = Jenkins.getInstance();
if (l.matches(jenkins)) {
// this job could run on master, keep track of it
exclusiveNode = jenkins;
}


Iterator<Node> i = jenkins.getNodes().iterator();
while (i.hasNext()) {
Node nodeToTest = i.next();
if (l.matches(nodeToTest)) {
// this label could run on this node!
if (exclusiveNode == null) {
// we haven't found a node where this label can run,
// so let's keep track of this one. Maybe it's the only one?
exclusiveNode = nodeToTest;
}
else {
// wait, this label could already run on exclusiveNode, and now
// it can run on this other node. There's no exclusivity here
exclusiveNode = null;
break;
}
}
}

return (exclusiveNode != null && exclusiveNode.equals(computer.getNode()));
}

// this function finds all jobs that could run on this node,
// based on labels configuration
public NodeData getLinkedJobs() {
Node node = computer.getNode();
NodeData result = new NodeData(node);

for (AbstractProject<?, ?> job : Jenkins.getInstance().getAllItems(AbstractProject.class)) {
if (!(job instanceof TopLevelItem)) {
// consider only TopLevelItem - not 100% sure why, though...
continue;
}

Label jobLabel = job.getAssignedLabel();
if (jobLabel == null) {
// jobs with no label are not considered for this function
continue;
}

if (jobLabel.matches(node)) {
result.addJob(job);
}
}

return result;
}
}
@@ -0,0 +1,26 @@
package jenkins.plugins.linkedjobs.extensions;

import java.util.Collection;
import java.util.Collections;

import jenkins.plugins.linkedjobs.actions.ComputerLinkedJobsAction;
import hudson.Extension;
import hudson.model.Action;
import hudson.model.Computer;
import hudson.model.TransientComputerActionFactory;

/**
* The only role of this extension/factory is to instantiate a {@link ComputerLinkedJobsAction}
* for each computer/node of this Jenkins instance.
* @author domi
*
*/

@Extension
public class ComputerExtension extends TransientComputerActionFactory {

@Override
public Collection<? extends Action> createFor(Computer target) {
return Collections.singletonList(new ComputerLinkedJobsAction(target));
}
}
@@ -0,0 +1,75 @@
<!--
The MIT License
Copyright (C) 2014 Dominique Brice
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.
-->
<!--
This script corresponds to the "Linked Jobs" page, accessible via a new link
in the left-hand side menu for computers/nodes
-->

<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">

<l:layout title="${it.title}">
<!--st:include it="${it.node}" page="sidepanel2.jelly"/-->

<l:main-panel>

<h2>Linked jobs</h2>
<j:set var="nodeData" value="${it.linkedJobs}" />
<j:if test="${nodeData.jobsCount == 0}">
There are no jobs that are linked to this node, based on label configuration.
</j:if>
<j:if test="${nodeData.jobsCount != 0}">
Based on label configuration, the following job(s) could run on this node.<br/><br/>
<j:if test="${it.detailedView}">
<t:projectView jobs="${nodeData.jobs}"/>
</j:if>
<j:if test="${!it.detailedView}">
<j:forEach var="job" items="${nodeData.jobs}">
<a href="${job.absoluteUrl}">${job.name}</a>&#160;
</j:forEach>
</j:if>
</j:if>

<j:if test="${it.showSingleNodeJobs}">
<br/><br/><h2>Exclusive jobs</h2>
<j:set var="nodeData" value="${it.exclusiveJobs}" />
<j:if test="${nodeData.jobsCount == 0}">
There are no single-node jobs that can run only on this node. Well done with your configuration!
</j:if>
<j:if test="${nodeData.jobsCount != 0}">
The following job(s) can run only on this node because of label (mis-)configuration, so you may have trouble if it goes down.<br/><br/>
<j:if test="${it.detailedView}">
<t:projectView jobs="${nodeData.jobs}"/>
</j:if>
<j:if test="${!it.detailedView}">
<j:forEach var="job" items="${nodeData.jobs}">
<a href="${job.absoluteUrl}">${job.name}</a>&#160;
</j:forEach>
</j:if>
</j:if>
</j:if>

</l:main-panel>
</l:layout>
</j:jelly>
Expand Up @@ -29,15 +29,15 @@ 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">
<f:section title="Linked Jobs Plugin Configuration">
<f:entry title="Linked Jobs - Detailed projects list" field="detailedView"
description="Uncheck if you want to see a condensed list of the projects on the Linked Jobs page">
description="Uncheck if you want to see a condensed list of the projects on the Linked Jobs pages">
<f:checkbox />
</f:entry>
<f:entry title="Labels Dashboard - Detailed projects list" field="dashboardOrphanedJobsDetailedView"
description="Uncheck if you want to see condensed lists of jobs on the Labels Dashboard">
<f:checkbox />
</f:entry>
<f:entry title="Labels Dashboard - List single-node jobs" field="showSingleNodeJobs"
description="Check if you want to see the jobs that can run on only one node, on the Labels Dashboard">
<f:entry title="List single-node/exclusive jobs" field="showSingleNodeJobs"
description="Check if you want to see the jobs that can run on only one node (Labels Dashboard, Linked Jobs for nodes)">
<f:checkbox />
</f:entry>
</f:section>
Expand Down

0 comments on commit 8a491a1

Please sign in to comment.