Ejemplo n.º 1
0
 /**
  * Called when a new {@link Computer} object is introduced (such as when Hudson started, or when a
  * new slave is added.)
  *
  * <p>The default implementation of this method delegates to {@link #check(Computer)}, but this
  * allows {@link RetentionStrategy} to distinguish the first time invocation from the rest.
  *
  * @since 1.275
  */
 public void start(final T c) {
   Queue.withLock(
       new Runnable() {
         @Override
         public void run() {
           check(c);
         }
       });
 }
Ejemplo n.º 2
0
 /**
  * Updates an existing node on disk. 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()}.
  *
  * @param node the node to be updated.
  * @return {@code true}, if the node was updated. {@code false}, if the node was not in the list
  *     of nodes.
  * @throws IOException if the node could not be persisted.
  */
 public boolean updateNode(final @Nonnull Node node) throws IOException {
   if (node == nodes.get(node.getNodeName())) {
     Queue.withLock(
         new Runnable() {
           @Override
           public void run() {
             jenkins.trimLabels();
           }
         });
     persistNode(node);
     return true;
   }
   return false;
 }
Ejemplo n.º 3
0
 /**
  * Adds a node. If a node of the same name already exists then that node will be replaced.
  *
  * @param node the new node.
  * @throws IOException if the list of nodes could not be persisted.
  */
 public void addNode(final @Nonnull Node node) throws IOException {
   if (node != nodes.get(node.getNodeName())) {
     // TODO we should not need to lock the queue for adding nodes but until we have a way to
     // update the
     // computer list for just the new node
     Queue.withLock(
         new Runnable() {
           @Override
           public void run() {
             nodes.put(node.getNodeName(), node);
             jenkins.updateComputerList();
             jenkins.trimLabels();
           }
         });
     persistNode(node);
   }
 }
Ejemplo n.º 4
0
 /**
  * Sets the list of nodes.
  *
  * @param nodes the new list of nodes.
  * @throws IOException if the new list of nodes could not be persisted.
  */
 public void setNodes(final @Nonnull Collection<? extends Node> nodes) throws IOException {
   Queue.withLock(
       new Runnable() {
         @Override
         public void run() {
           Set<String> toRemove = new HashSet<String>(Nodes.this.nodes.keySet());
           for (Node n : nodes) {
             final String name = n.getNodeName();
             toRemove.remove(name);
             Nodes.this.nodes.put(name, n);
           }
           Nodes.this
               .nodes
               .keySet()
               .removeAll(toRemove); // directory clean up will be handled by save
           jenkins.updateComputerList();
           jenkins.trimLabels();
         }
       });
   save();
 }
Ejemplo n.º 5
0
 /**
  * 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()}.
  *
  * @param node the node instance to remove.
  * @throws IOException if the list of nodes could not be persisted.
  */
 public void removeNode(final @Nonnull Node node) throws IOException {
   if (node == nodes.get(node.getNodeName())) {
     Queue.withLock(
         new Runnable() {
           @Override
           public void run() {
             Computer c = node.toComputer();
             if (c != null) {
               c.recordTermination();
               c.disconnect(OfflineCause.create(hudson.model.Messages._Hudson_NodeBeingRemoved()));
             }
             if (node == nodes.remove(node.getNodeName())) {
               jenkins.updateComputerList();
               jenkins.trimLabels();
             }
           }
         });
     // no need for a full save() so we just do the minimum
     Util.deleteRecursive(new File(getNodesDir(), node.getNodeName()));
   }
 }
Ejemplo n.º 6
0
 /**
  * Loads the nodes from disk.
  *
  * @throws IOException if the nodes could not be deserialized.
  */
 public void load() throws IOException {
   final File nodesDir = getNodesDir();
   final File[] subdirs =
       nodesDir.listFiles(
           new FileFilter() {
             public boolean accept(File child) {
               return child.isDirectory();
             }
           });
   final Map<String, Node> newNodes = new TreeMap<String, Node>();
   if (subdirs != null) {
     for (File subdir : subdirs) {
       try {
         XmlFile xmlFile = new XmlFile(Jenkins.XSTREAM, new File(subdir, "config.xml"));
         if (xmlFile.exists()) {
           Node node = (Node) xmlFile.read();
           newNodes.put(node.getNodeName(), node);
         }
       } catch (IOException e) {
         Logger.getLogger(Nodes.class.getName()).log(Level.WARNING, "could not load " + subdir, e);
       }
     }
   }
   Queue.withLock(
       new Runnable() {
         @Override
         public void run() {
           for (Iterator<Map.Entry<String, Node>> i = nodes.entrySet().iterator(); i.hasNext(); ) {
             if (!(i.next().getValue() instanceof EphemeralNode)) {
               i.remove();
             }
           }
           nodes.putAll(newNodes);
           jenkins.updateComputerList();
           jenkins.trimLabels();
         }
       });
 }