Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2314 from olivergondza/node-listener
[FIXED JENKINS-33780] Introduce listener for slave creation/update/deletion
  • Loading branch information
olivergondza committed Jun 3, 2016
2 parents 81e00cc + 3ef19cc commit 1444ee6
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 28 deletions.
27 changes: 2 additions & 25 deletions core/src/main/java/hudson/model/Computer.java
Expand Up @@ -67,7 +67,6 @@
import jenkins.model.Jenkins;
import jenkins.util.ContextResettingExecutorService;
import jenkins.security.MasterToSlaveCallable;
import jenkins.security.NotReallyRoleSensitiveCallable;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
Expand Down Expand Up @@ -1411,7 +1410,7 @@ public void doConfigSubmit( StaplerRequest req, StaplerResponse rsp ) throws IOE
}

Node result = node.reconfigure(req, req.getSubmittedForm());
replaceBy(result);
Jenkins.getInstance().getNodesObject().replaceNode(this.getNode(), result);

// take the user back to the agent top page.
rsp.sendRedirect2("../" + result.getNodeName() + '/');
Expand Down Expand Up @@ -1445,28 +1444,6 @@ public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp)
rsp.sendError(SC_BAD_REQUEST);
}

/**
* Replaces the current {@link Node} by another one.
*/
private void replaceBy(final Node newNode) throws ServletException, IOException {
final Jenkins app = Jenkins.getInstance();

// use the queue lock until Nodes has a way of directly modifying a single node.
Queue.withLock(new NotReallyRoleSensitiveCallable<Void, IOException>() {
public Void call() throws IOException {
List<Node> nodes = new ArrayList<Node>(app.getNodes());
Node node = getNode();
int i = (node != null) ? nodes.indexOf(node) : -1;
if(i<0) {
throw new IOException("This agent appears to be removed while you were editing the configuration");
}
nodes.set(i, newNode);
app.setNodes(nodes);
return null;
}
});
}

/**
* Updates Job by its XML definition.
*
Expand All @@ -1475,7 +1452,7 @@ public Void call() throws IOException {
public void updateByXml(final InputStream source) throws IOException, ServletException {
checkPermission(CONFIGURE);
Node result = (Node)Jenkins.XSTREAM2.fromXML(source);
replaceBy(result);
Jenkins.getInstance().getNodesObject().replaceNode(this.getNode(), result);
}

/**
Expand Down
112 changes: 112 additions & 0 deletions core/src/main/java/jenkins/model/NodeListener.java
@@ -0,0 +1,112 @@
/*
* The MIT License
*
* Copyright (c) Red Hat, 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.model;

import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Node;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Listen to {@link Node} CRUD operations.
*
* @author ogondza.
* @since TODO
*/
public abstract class NodeListener implements ExtensionPoint {

private static final Logger LOGGER = Logger.getLogger(NodeListener.class.getName());

/**
* Node is being created.
*/
protected void onCreated(@Nonnull Node node) {}

/**
* Node is being updated.
*/
protected void onUpdated(@Nonnull Node oldOne, @Nonnull Node newOne) {}

/**
* Node is being deleted.
*/
protected void onDeleted(@Nonnull Node node) {}

/**
* Inform listeners that node is being created.
*
* @param node A node being created.
*/
public static void fireOnCreated(@Nonnull Node node) {
for (NodeListener nl: all()) {
try {
nl.onCreated(node);
} catch (Throwable ex) {
LOGGER.log(Level.WARNING, "Listener invocation failed", ex);
}
}
}

/**
* Inform listeners that node is being updated.
*
* @param oldOne Old configuration.
* @param newOne New Configuration.
*/
public static void fireOnUpdated(@Nonnull Node oldOne, @Nonnull Node newOne) {
for (NodeListener nl: all()) {
try {
nl.onUpdated(oldOne, newOne);
} catch (Throwable ex) {
LOGGER.log(Level.WARNING, "Listener invocation failed", ex);
}
}
}

/**
* Inform listeners that node is being removed.
*
* @param node A node being removed.
*/
public static void fireOnDeleted(@Nonnull Node node) {
for (NodeListener nl: all()) {
try {
nl.onDeleted(node);
} catch (Throwable ex) {
LOGGER.log(Level.WARNING, "Listener invocation failed", ex);
}
}
}

/**
* Get all {@link NodeListener}s registered in Jenkins.
*/
public static @Nonnull List<NodeListener> all() {
return ExtensionList.lookup(NodeListener.class);
}
}
29 changes: 29 additions & 0 deletions core/src/main/java/jenkins/model/Nodes.java
Expand Up @@ -34,6 +34,7 @@
import hudson.slaves.EphemeralNode;
import hudson.slaves.OfflineCause;
import java.util.concurrent.Callable;

import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -139,6 +140,7 @@ public void run() {
});
// TODO there is a theoretical race whereby the node instance is updated/removed after lock release
persistNode(node);
NodeListener.fireOnCreated(node);
}
}

Expand Down Expand Up @@ -198,6 +200,31 @@ public Boolean call() throws Exception {
return false;
}

/**
* Replace node of given name.
*
* @return {@code true} if node was replaced.
* @since TODO
*/
public boolean replaceNode(final Node oldOne, final @Nonnull Node newOne) throws IOException {
if (oldOne == nodes.get(oldOne.getNodeName())) {
// use the queue lock until Nodes has a way of directly modifying a single node.
Queue.withLock(new Runnable() {
public void run() {
Nodes.this.nodes.remove(oldOne.getNodeName());
Nodes.this.nodes.put(newOne.getNodeName(), newOne);
jenkins.updateComputerList();
jenkins.trimLabels();
}
});
updateNode(newOne);
NodeListener.fireOnUpdated(oldOne, newOne);
return true;
} else {
return false;
}
}

/**
* Removes a node. If the node instance is not in the list of nodes, then this will be a no-op, even if
* there is another instance with the same {@link Node#getNodeName()}.
Expand All @@ -223,6 +250,8 @@ public void run() {
});
// no need for a full save() so we just do the minimum
Util.deleteRecursive(new File(getNodesDir(), node.getNodeName()));

NodeListener.fireOnDeleted(node);
}
}

Expand Down
51 changes: 51 additions & 0 deletions test/src/test/java/jenkins/model/NodeListenerTest.java
@@ -0,0 +1,51 @@
package jenkins.model;

import hudson.ExtensionList;
import hudson.cli.CLICommand;
import hudson.cli.CLICommandInvoker;
import hudson.cli.CreateNodeCommand;
import hudson.cli.DeleteNodeCommand;
import hudson.cli.GetNodeCommand;
import hudson.cli.UpdateNodeCommand;
import hudson.model.Node;
import hudson.slaves.DumbSlave;
import org.apache.tools.ant.filters.StringInputStream;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import static org.mockito.Mockito.*;

public class NodeListenerTest {

@Rule public JenkinsRule j = new JenkinsRule();

private NodeListener mock;

@Before
public void setUp() {
mock = mock(NodeListener.class);
ExtensionList.lookup(NodeListener.class).add(mock);
}

@Test
public void crud() throws Exception {
DumbSlave slave = j.createSlave();
String xml = cli(new GetNodeCommand()).invokeWithArgs(slave.getNodeName()).stdout();
cli(new UpdateNodeCommand()).withStdin(new StringInputStream(xml)).invokeWithArgs(slave.getNodeName());
cli(new DeleteNodeCommand()).invokeWithArgs(slave.getNodeName());

cli(new CreateNodeCommand()).withStdin(new StringInputStream(xml)).invokeWithArgs("replica");
j.jenkins.getComputer("replica").doDoDelete();

verify(mock, times(2)).onCreated(any(Node.class));
verify(mock, times(1)).onUpdated(any(Node.class), any(Node.class));
verify(mock, times(2)).onDeleted(any(Node.class));
verifyNoMoreInteractions(mock);
}

private CLICommandInvoker cli(CLICommand cmd) {
return new CLICommandInvoker(j, cmd);
}
}
4 changes: 1 addition & 3 deletions test/src/test/resources/hudson/model/node.xml
Expand Up @@ -10,9 +10,7 @@
<outer-class reference="../.."/>
</DESCRIPTOR>
</retentionStrategy>
<launcher class="hudson.slaves.CommandLauncher">
<agentCommand>&quot;/opt/java6/jre/bin/java&quot; -jar &quot;slave.jar&quot;</agentCommand>
</launcher>
<launcher class="hudson.slaves.JNLPLauncher"/>
<label></label>
<nodeProperties/>
<userId>SYSTEM</userId>
Expand Down

0 comments on commit 1444ee6

Please sign in to comment.