提交 1444ee6f 编写于 作者: O Oliver Gondža

Merge pull request #2314 from olivergondza/node-listener

[FIXED JENKINS-33780] Introduce listener for slave creation/update/deletion
...@@ -67,7 +67,6 @@ import hudson.util.NamingThreadFactory; ...@@ -67,7 +67,6 @@ import hudson.util.NamingThreadFactory;
import jenkins.model.Jenkins; import jenkins.model.Jenkins;
import jenkins.util.ContextResettingExecutorService; import jenkins.util.ContextResettingExecutorService;
import jenkins.security.MasterToSlaveCallable; import jenkins.security.MasterToSlaveCallable;
import jenkins.security.NotReallyRoleSensitiveCallable;
import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.accmod.restrictions.DoNotUse;
...@@ -1411,7 +1410,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces ...@@ -1411,7 +1410,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces
} }
Node result = node.reconfigure(req, req.getSubmittedForm()); 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. // take the user back to the agent top page.
rsp.sendRedirect2("../" + result.getNodeName() + '/'); rsp.sendRedirect2("../" + result.getNodeName() + '/');
...@@ -1445,28 +1444,6 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces ...@@ -1445,28 +1444,6 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces
rsp.sendError(SC_BAD_REQUEST); 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. * Updates Job by its XML definition.
* *
...@@ -1475,7 +1452,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces ...@@ -1475,7 +1452,7 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces
public void updateByXml(final InputStream source) throws IOException, ServletException { public void updateByXml(final InputStream source) throws IOException, ServletException {
checkPermission(CONFIGURE); checkPermission(CONFIGURE);
Node result = (Node)Jenkins.XSTREAM2.fromXML(source); Node result = (Node)Jenkins.XSTREAM2.fromXML(source);
replaceBy(result); Jenkins.getInstance().getNodesObject().replaceNode(this.getNode(), result);
} }
/** /**
......
/*
* 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);
}
}
...@@ -34,6 +34,7 @@ import hudson.model.listeners.SaveableListener; ...@@ -34,6 +34,7 @@ import hudson.model.listeners.SaveableListener;
import hudson.slaves.EphemeralNode; import hudson.slaves.EphemeralNode;
import hudson.slaves.OfflineCause; import hudson.slaves.OfflineCause;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.accmod.restrictions.NoExternalUse;
...@@ -139,6 +140,7 @@ public class Nodes implements Saveable { ...@@ -139,6 +140,7 @@ public class Nodes implements Saveable {
}); });
// TODO there is a theoretical race whereby the node instance is updated/removed after lock release // TODO there is a theoretical race whereby the node instance is updated/removed after lock release
persistNode(node); persistNode(node);
NodeListener.fireOnCreated(node);
} }
} }
...@@ -198,6 +200,31 @@ public class Nodes implements Saveable { ...@@ -198,6 +200,31 @@ public class Nodes implements Saveable {
return false; 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 * 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()}. * there is another instance with the same {@link Node#getNodeName()}.
...@@ -223,6 +250,8 @@ public class Nodes implements Saveable { ...@@ -223,6 +250,8 @@ public class Nodes implements Saveable {
}); });
// no need for a full save() so we just do the minimum // no need for a full save() so we just do the minimum
Util.deleteRecursive(new File(getNodesDir(), node.getNodeName())); Util.deleteRecursive(new File(getNodesDir(), node.getNodeName()));
NodeListener.fireOnDeleted(node);
} }
} }
......
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);
}
}
...@@ -10,9 +10,7 @@ ...@@ -10,9 +10,7 @@
<outer-class reference="../.."/> <outer-class reference="../.."/>
</DESCRIPTOR> </DESCRIPTOR>
</retentionStrategy> </retentionStrategy>
<launcher class="hudson.slaves.CommandLauncher"> <launcher class="hudson.slaves.JNLPLauncher"/>
<agentCommand>&quot;/opt/java6/jre/bin/java&quot; -jar &quot;slave.jar&quot;</agentCommand>
</launcher>
<label></label> <label></label>
<nodeProperties/> <nodeProperties/>
<userId>SYSTEM</userId> <userId>SYSTEM</userId>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册