Node.java 8.0 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe, Stephen Connolly
 * 
 * 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.
 */
K
kohsuke 已提交
24 25
package hudson.model;

K
kohsuke 已提交
26
import hudson.ExtensionPoint;
27
import hudson.FilePath;
28
import hudson.FileSystemProvisioner;
29
import hudson.Launcher;
30
import hudson.tools.ToolLocationNodeProperty;
31
import hudson.node_monitors.NodeMonitor;
32
import hudson.remoting.VirtualChannel;
33
import hudson.security.ACL;
K
kohsuke 已提交
34
import hudson.security.AccessControlled;
35
import hudson.security.Permission;
K
kohsuke 已提交
36
import hudson.slaves.NodeDescriptor;
37 38
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
39
import hudson.util.ClockDifference;
40
import hudson.util.DescribableList;
K
kohsuke 已提交
41
import hudson.util.EnumConverter;
K
kohsuke 已提交
42

K
kohsuke 已提交
43
import java.io.IOException;
K
kohsuke 已提交
44
import java.util.Set;
K
kohsuke 已提交
45
import java.util.List;
46

47 48
import org.kohsuke.stapler.Stapler;

K
kohsuke 已提交
49
/**
K
kohsuke 已提交
50 51 52 53
 * Base type of Hudson slaves (although in practice, you probably extend {@link Slave} to define a new slave type.)
 *
 * <p>
 * As a special case, {@link Hudson} extends from here.
K
kohsuke 已提交
54 55
 *
 * @author Kohsuke Kawaguchi
K
kohsuke 已提交
56
 * @see NodeMonitor
K
kohsuke 已提交
57
 * @see NodeDescriptor
K
kohsuke 已提交
58
 */
59
public abstract class Node extends AbstractModelObject implements Describable<Node>, ExtensionPoint, AccessControlled {
60

61 62 63 64 65 66 67 68
    public String getDisplayName() {
        return getNodeName(); // default implementation
    }

    public String getSearchUrl() {
        return "computer/"+getNodeName();
    }

K
kohsuke 已提交
69
    /**
K
kohsuke 已提交
70
     * Name of this node.
K
kohsuke 已提交
71 72 73 74
     *
     * @return
     *      "" if this is master
     */
75
    public abstract String getNodeName();
K
kohsuke 已提交
76

K
kohsuke 已提交
77 78 79 80 81 82 83 84 85 86
    /**
     * When the user clones a {@link Node}, Hudson uses this method to change the node name right after
     * the cloned {@link Node} object is instantiated.
     *
     * <p>
     * This method is never used for any other purpose, and as such for all practical intents and purposes,
     * the node name should be treated like immutable.
     *
     * @deprecated to indicate that this method isn't really meant to be called by random code.
     */
87
    public abstract void setNodeName(String name);
K
kohsuke 已提交
88

K
kohsuke 已提交
89 90 91
    /**
     * Human-readable description of this node.
     */
92
    public abstract String getNodeDescription();
K
kohsuke 已提交
93 94 95

    /**
     * Returns a {@link Launcher} for executing programs on this node.
K
kohsuke 已提交
96 97 98
     *
     * <p>
     * The callee must call {@link Launcher#decorateFor(Node)} before returning to complete the decoration. 
K
kohsuke 已提交
99
     */
100
    public abstract Launcher createLauncher(TaskListener listener);
K
kohsuke 已提交
101 102 103 104 105 106 107

    /**
     * Returns the number of {@link Executor}s.
     *
     * This may be different from <code>getExecutors().size()</code>
     * because it takes time to adjust the number of executors.
     */
108
    public abstract int getNumExecutors();
K
kohsuke 已提交
109 110

    /**
111
     * Returns {@link Mode#EXCLUSIVE} if this node is only available
K
kohsuke 已提交
112 113 114
     * for those jobs that exclusively specifies this node
     * as the assigned node.
     */
115
    public abstract Mode getMode();
K
kohsuke 已提交
116

117 118 119 120
    /**
     * Gets the corresponding {@link Computer} object.
     *
     * @return
K
kohsuke 已提交
121 122
     *      this method can return null if there's no {@link Computer} object for this node,
     *      such as when this node has no executors at all.
123
     */
124 125 126
    public final Computer toComputer() {
        return Hudson.getInstance().getComputer(this);
    }
127

K
kohsuke 已提交
128 129 130 131
    /**
     * Creates a new {@link Computer} object that acts as the UI peer of this {@link Node}.
     * Nobody but {@link Hudson#updateComputerList()} should call this method.
     */
132
    protected abstract Computer createComputer();
K
kohsuke 已提交
133

134 135 136 137
    /**
     * Returns the possibly empty set of labels that are assigned to this node,
     * including the automatic {@link #getSelfLabel() self label}.
     */
138
    public abstract Set<Label> getAssignedLabels();
139

140
    /*
141 142 143
     * Returns the possibly empty set of labels that it has been determined as supported by this node.
     * @see hudson.tasks.LabelFinder
     */
144
    public abstract Set<Label> getDynamicLabels();
145

146 147 148
    /**
     * Gets the special label that represents this node itself.
     */
149 150 151
    public Label getSelfLabel() {
        return Hudson.getInstance().getLabel(getNodeName());
    }
152

153 154 155 156 157 158
    /**
     * Returns a "workspace" directory for the given {@link TopLevelItem}.
     *
     * <p>
     * Workspace directory is usually used for keeping out the checked out
     * source code, but it can be used for anything.
K
kohsuke 已提交
159 160 161
     *
     * @return
     *      null if this node is not connected hence the path is not available
162
     */
163
    public abstract FilePath getWorkspaceFor(TopLevelItem item);
164

K
kohsuke 已提交
165 166 167 168 169 170 171 172 173 174 175
    /**
     * Gets the root directory of this node.
     *
     * <p>
     * Hudson always owns a directory on every node. This method
     * returns that.
     *
     * @return
     *      null if the node is offline and hence the {@link FilePath}
     *      object is not available.
     */
176
    public abstract FilePath getRootPath();
K
kohsuke 已提交
177

178 179 180
    /**
     * Gets the {@link FilePath} on this node.
     */
181 182 183 184 185 186 187 188
    public FilePath createPath(String absolutePath) {
        Computer computer = toComputer();
        if (computer==null) return null; // offline
        VirtualChannel ch = computer.getChannel();
        if(ch==null)    return null;    // offline
        return new FilePath(ch,absolutePath);
    }

189 190 191 192 193
    public FileSystemProvisioner getFileSystemProvisioner() {
        // TODO: make this configurable or auto-detectable or something else
        return FileSystemProvisioner.DEFAULT;
    }

194 195 196
    /**
     * Gets the {@link NodeProperty} instances configured for this {@link Node}.
     */
197
    public abstract DescribableList<NodeProperty<?>, NodePropertyDescriptor> getNodeProperties();
198

K
kohsuke 已提交
199 200 201 202
    // used in the Jelly script to expose descriptors
    public List<NodePropertyDescriptor> getNodePropertyDescriptors() {
        return NodeProperty.for_(this);
    }
203
    
204 205 206 207 208 209 210 211 212 213 214
    public ACL getACL() {
        return Hudson.getInstance().getAuthorizationStrategy().getACL(this);
    }
    
    public final void checkPermission(Permission permission) {
        getACL().checkPermission(permission);
    }

    public final boolean hasPermission(Permission permission) {
        return getACL().hasPermission(permission);
    }
215

216
    public abstract NodeDescriptor getDescriptor();
K
kohsuke 已提交
217

K
kohsuke 已提交
218 219 220 221
    /**
     * Estimates the clock difference with this slave.
     *
     * @return
222
     *      always non-null.
K
kohsuke 已提交
223 224 225
     * @throws InterruptedException
     *      if the operation is aborted.
     */
226
    public abstract ClockDifference getClockDifference() throws IOException, InterruptedException;
K
kohsuke 已提交
227

K
kohsuke 已提交
228 229 230
    /**
     * Constants that control how Hudson allocates jobs to slaves.
     */
K
kohsuke 已提交
231
    public enum Mode {
S
sogabe 已提交
232 233
        NORMAL(Messages.Node_Mode_NORMAL()),
        EXCLUSIVE(Messages.Node_Mode_EXCLUSIVE());
K
kohsuke 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247

        private final String description;

        public String getDescription() {
            return description;
        }

        public String getName() {
            return name();
        }

        Mode(String description) {
            this.description = description;
        }
K
kohsuke 已提交
248 249

        static {
250
            Stapler.CONVERT_UTILS.register(new EnumConverter(), Mode.class);
251 252
        }
    }
253

K
kohsuke 已提交
254
}