Slave.java 17.2 KB
Newer Older
K
kohsuke 已提交
1 2
/*
 * The MIT License
3
 *
4 5
 * Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
 * Erik Ramfelt, Martin Eigenbrodt, Stephen Connolly, Tom Huybrechts
6
 *
K
kohsuke 已提交
7 8 9 10 11 12
 * 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:
13
 *
K
kohsuke 已提交
14 15
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
16
 *
K
kohsuke 已提交
17 18 19 20 21 22 23 24
 * 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 已提交
25 26 27 28 29
package hudson.model;

import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
30
import hudson.Launcher.RemoteLauncher;
K
kohsuke 已提交
31
import hudson.model.Descriptor.FormException;
K
kohsuke 已提交
32
import hudson.remoting.Callable;
33 34 35 36 37 38 39 40 41
import hudson.slaves.CommandLauncher;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.DumbSlave;
import hudson.slaves.JNLPLauncher;
import hudson.slaves.NodeDescriptor;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.slaves.RetentionStrategy;
import hudson.slaves.SlaveComputer;
K
kohsuke 已提交
42
import hudson.util.ClockDifference;
43
import hudson.util.DescribableList;
K
kohsuke 已提交
44
import hudson.util.FormValidation;
K
kohsuke 已提交
45

K
kohsuke 已提交
46 47 48 49
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
50
import java.net.MalformedURLException;
51 52
import java.net.URL;
import java.net.URLConnection;
53 54 55 56 57 58
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;

J
Jesse Glick 已提交
59
import javax.annotation.CheckForNull;
J
Jesse Glick 已提交
60
import javax.annotation.Nonnull;
61
import jenkins.model.Jenkins;
62
import jenkins.security.MasterToSlaveCallable;
63 64
import jenkins.slaves.WorkspaceLocator;

65
import org.apache.commons.io.IOUtils;
66
import org.apache.commons.lang.StringUtils;
67
import org.kohsuke.stapler.HttpResponse;
68 69 70
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
K
kohsuke 已提交
71

K
kohsuke 已提交
72 73 74
/**
 * Information about a Hudson slave node.
 *
K
noted  
kohsuke 已提交
75 76 77 78
 * <p>
 * Ideally this would have been in the <tt>hudson.slaves</tt> package,
 * but for compatibility reasons, it can't.
 *
K
kohsuke 已提交
79 80 81
 * <p>
 * TODO: move out more stuff to {@link DumbSlave}.
 *
K
kohsuke 已提交
82 83
 * @author Kohsuke Kawaguchi
 */
84
public abstract class Slave extends Node implements Serializable {
K
kohsuke 已提交
85
    /**
K
kohsuke 已提交
86
     * Name of this slave node.
K
kohsuke 已提交
87
     */
K
kohsuke 已提交
88
    protected String name;
K
kohsuke 已提交
89 90 91 92 93 94 95

    /**
     * Description of this node.
     */
    private final String description;

    /**
96 97 98 99 100 101
     * Path to the root of the workspace from the view point of this node, such as "/hudson", this need not
     * be absolute provided that the launcher establishes a consistent working directory, such as "./.jenkins-slave"
     * when used with an SSH launcher.
     *
     * NOTE: if the administrator is using a relative path they are responsible for ensuring that the launcher used
     * provides a consistent working directory
K
kohsuke 已提交
102
     */
K
kohsuke 已提交
103
    protected final String remoteFS;
K
kohsuke 已提交
104 105 106 107 108 109 110 111 112 113 114

    /**
     * Number of executors of this node.
     */
    private int numExecutors = 2;

    /**
     * Job allocation strategy.
     */
    private Mode mode;

K
kohsuke 已提交
115
    /**
116 117
     * Slave availablility strategy.
     */
K
kohsuke 已提交
118
    private RetentionStrategy retentionStrategy;
119 120 121

    /**
     * The starter that will startup this slave.
122
     */
123
    private ComputerLauncher launcher;
K
kohsuke 已提交
124

125 126 127 128
    /**
     * Whitespace-separated labels.
     */
    private String label="";
129

130
    private /*almost final*/ DescribableList<NodeProperty<?>,NodePropertyDescriptor> nodeProperties = new DescribableList<NodeProperty<?>,NodePropertyDescriptor>(Jenkins.getInstance());
131 132 133 134 135

    /**
     * Lazily computed set of labels from {@link #label}.
     */
    private transient volatile Set<Label> labels;
136

137 138 139 140
    /**
     * Id of user which creates this slave {@link User}.
     */
    private String userId;
141

K
kohsuke 已提交
142
    public Slave(String name, String nodeDescription, String remoteFS, String numExecutors,
K
kohsuke 已提交
143 144
                 Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy retentionStrategy, List<? extends NodeProperty<?>> nodeProperties) throws FormException, IOException {
        this(name,nodeDescription,remoteFS,Util.tryParseNumber(numExecutors, 1).intValue(),mode,labelString,launcher,retentionStrategy, nodeProperties);
K
kohsuke 已提交
145 146
    }

M
mindless 已提交
147 148 149
    /**
     * @deprecated since 2009-02-20.
     */
150 151
    @Deprecated
    public Slave(String name, String nodeDescription, String remoteFS, int numExecutors,
K
kohsuke 已提交
152 153
            Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy retentionStrategy) throws FormException, IOException {
    	this(name, nodeDescription, remoteFS, numExecutors, mode, labelString, launcher, retentionStrategy, new ArrayList());
154
    }
155

J
Jesse Glick 已提交
156
    public Slave(@Nonnull String name, String nodeDescription, String remoteFS, int numExecutors,
K
kohsuke 已提交
157
                 Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy retentionStrategy, List<? extends NodeProperty<?>> nodeProperties) throws FormException, IOException {
K
kohsuke 已提交
158
        this.name = name;
K
kohsuke 已提交
159
        this.description = nodeDescription;
K
kohsuke 已提交
160
        this.numExecutors = numExecutors;
K
kohsuke 已提交
161
        this.mode = mode;
162
        this.remoteFS = Util.fixNull(remoteFS).trim();
K
kohsuke 已提交
163
        this.label = Util.fixNull(labelString).trim();
164 165
        this.launcher = launcher;
        this.retentionStrategy = retentionStrategy;
166
        getAssignedLabels();    // compute labels now
167

168
        this.nodeProperties.replaceBy(nodeProperties);
169 170 171 172 173 174 175
         Slave node = (Slave) Jenkins.getInstance().getNode(name);

       if(node!=null){
            this.userId= node.getUserId(); //slave has already existed
        }
       else{
            User user = User.current();
176
            userId = user!=null ? user.getId() : "anonymous";
177
        }
K
d'oh!  
kohsuke 已提交
178
        if (name.equals(""))
K
i18n  
kohsuke 已提交
179
            throw new FormException(Messages.Slave_InvalidConfig_NoName(), null);
K
kohsuke 已提交
180

K
kohsuke 已提交
181 182
//        if (remoteFS.equals(""))
//            throw new FormException(Messages.Slave_InvalidConfig_NoRemoteDir(name), null);
183

184
        if (this.numExecutors<=0)
K
i18n  
kohsuke 已提交
185
            throw new FormException(Messages.Slave_InvalidConfig_Executors(name), null);
K
kohsuke 已提交
186
    }
187

188 189
    /**
     * Return id of user which created this slave
190
     *
191 192 193 194 195
     * @return id of user
     */
    public String getUserId() {
        return userId;
    }
K
kohsuke 已提交
196

197 198 199
    public void setUserId(String userId){
        this.userId = userId;
    }
200

201
    public ComputerLauncher getLauncher() {
202
        return launcher == null ? new JNLPLauncher() : launcher;
203 204
    }

205
    public void setLauncher(ComputerLauncher launcher) {
206
        this.launcher = launcher;
K
kohsuke 已提交
207 208 209 210 211 212
    }

    public String getRemoteFS() {
        return remoteFS;
    }

K
kohsuke 已提交
213 214
    public String getNodeName() {
        return name;
K
kohsuke 已提交
215 216
    }

J
Jesse Glick 已提交
217 218 219 220
    @Override public String toString() {
        return getClass().getName() + "[" + name + "]";
    }

K
kohsuke 已提交
221
    public void setNodeName(String name) {
222
        this.name = name;
K
kohsuke 已提交
223 224
    }

K
kohsuke 已提交
225 226 227 228 229 230 231 232 233 234 235 236
    public String getNodeDescription() {
        return description;
    }

    public int getNumExecutors() {
        return numExecutors;
    }

    public Mode getMode() {
        return mode;
    }

K
kohsuke 已提交
237 238 239 240
    public void setMode(Mode mode) {
        this.mode = mode;
    }

241
    public DescribableList<NodeProperty<?>, NodePropertyDescriptor> getNodeProperties() {
242
        assert nodeProperties != null;
243 244
    	return nodeProperties;
    }
245

K
kohsuke 已提交
246 247
    public RetentionStrategy getRetentionStrategy() {
        return retentionStrategy == null ? RetentionStrategy.Always.INSTANCE : retentionStrategy;
248 249
    }

K
kohsuke 已提交
250 251
    public void setRetentionStrategy(RetentionStrategy availabilityStrategy) {
        this.retentionStrategy = availabilityStrategy;
252 253
    }

254 255 256
    public String getLabelString() {
        return Util.fixNull(label).trim();
    }
257

258 259 260 261 262 263 264
    @Override
    public void setLabelString(String labelString) throws IOException {
        this.label = Util.fixNull(labelString).trim();
        // Compute labels now.
        getAssignedLabels();
    }

K
Kohsuke Kawaguchi 已提交
265 266 267
    @Override
    public Callable<ClockDifference,IOException> getClockDifferenceCallable() {
        return new GetClockDifference1();
K
kohsuke 已提交
268 269
    }

K
kohsuke 已提交
270
    public Computer createComputer() {
271
        return new SlaveComputer(this);
K
kohsuke 已提交
272 273
    }

274
    public FilePath getWorkspaceFor(TopLevelItem item) {
275 276 277 278 279 280
        for (WorkspaceLocator l : WorkspaceLocator.all()) {
            FilePath workspace = l.locate(item, this);
            if (workspace != null) {
                return workspace;
            }
        }
281

K
kohsuke 已提交
282 283
        FilePath r = getWorkspaceRoot();
        if(r==null)     return null;    // offline
284
        return r.child(item.getFullName());
285 286
    }

287
    @CheckForNull
K
kohsuke 已提交
288
    public FilePath getRootPath() {
289 290 291 292 293 294 295
        final SlaveComputer computer = getComputer();
        if (computer == null) {
            // if computer is null then channel is null and thus we were going to return null anyway
            return null;
        } else {
            return createPath(StringUtils.defaultString(computer.getAbsoluteRemoteFs(), remoteFS));
        }
296 297
    }

K
kohsuke 已提交
298 299
    /**
     * Root directory on this slave where all the job workspaces are laid out.
K
kohsuke 已提交
300 301
     * @return
     *      null if not connected.
K
kohsuke 已提交
302
     */
J
Jesse Glick 已提交
303
    public @CheckForNull FilePath getWorkspaceRoot() {
K
kohsuke 已提交
304 305
        FilePath r = getRootPath();
        if(r==null) return null;
306
        return r.child(WORKSPACE_ROOT);
K
kohsuke 已提交
307
    }
K
kohsuke 已提交
308

309 310 311
    /**
     * Web-bound object used to serve jar files for JNLP.
     */
312
    public static final class JnlpJar implements HttpResponse {
K
kohsuke 已提交
313
        private final String fileName;
314

K
kohsuke 已提交
315 316
        public JnlpJar(String fileName) {
            this.fileName = fileName;
317 318 319
        }

        public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
K
kohsuke 已提交
320
            URLConnection con = connect();
321 322
            // since we end up redirecting users to jnlpJars/foo.jar/, set the content disposition
            // so that browsers can download them in the right file name.
323 324
            // see http://support.microsoft.com/kb/260519 and http://www.boutell.com/newfaq/creating/forcedownload.html
            rsp.setHeader("Content-Disposition", "attachment; filename=" + fileName);
K
kohsuke 已提交
325 326 327 328 329
            InputStream in = con.getInputStream();
            rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar" );
            in.close();
        }

330 331 332 333
        public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
            doIndex(req,rsp);
        }

K
kohsuke 已提交
334 335 336 337 338 339
        private URLConnection connect() throws IOException {
            URL res = getURL();
            return res.openConnection();
        }

        public URL getURL() throws MalformedURLException {
340 341
            String name = fileName;
            if (name.equals("hudson-cli.jar"))  name="jenkins-cli.jar";
342
            URL res = Jenkins.getInstance().servletContext.getResource("/WEB-INF/" + name);
K
kohsuke 已提交
343 344
            if(res==null) {
                // during the development this path doesn't have the files.
345
                res = new URL(new File(".").getAbsoluteFile().toURI().toURL(),"target/jenkins/WEB-INF/"+name);
346
            }
K
kohsuke 已提交
347 348
            return res;
        }
349

K
kohsuke 已提交
350 351 352 353 354 355 356
        public byte[] readFully() throws IOException {
            InputStream in = connect().getInputStream();
            try {
                return IOUtils.toByteArray(in);
            } finally {
                in.close();
            }
357 358 359 360
        }

    }

361 362 363 364 365 366 367
    /**
     * Creates a launcher for the slave.
     *
     * @return
     *      If there is no computer it will return a {@link hudson.Launcher.DummyLauncher}, otherwise it
     *      will return a {@link hudson.Launcher.RemoteLauncher} instead.
     */
K
kohsuke 已提交
368
    public Launcher createLauncher(TaskListener listener) {
369
        SlaveComputer c = getComputer();
370 371 372 373 374 375
        if (c == null) {
            listener.error("Issue with creating launcher for slave " + name + ".");
            return new Launcher.DummyLauncher(listener);
        } else {
            return new RemoteLauncher(listener, c.getChannel(), c.isUnix()).decorateFor(this);
        }
K
kohsuke 已提交
376 377
    }

K
kohsuke 已提交
378
    /**
379
     * Gets the corresponding computer object.
K
kohsuke 已提交
380
     */
381
    public SlaveComputer getComputer() {
382
        return (SlaveComputer)toComputer();
K
kohsuke 已提交
383 384
    }

385
    @Override
K
kohsuke 已提交
386 387 388 389 390 391 392 393 394
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final Slave that = (Slave) o;

        return name.equals(that.name);
    }

395
    @Override
K
kohsuke 已提交
396 397 398 399
    public int hashCode() {
        return name.hashCode();
    }

K
kohsuke 已提交
400 401 402
    /**
     * Invoked by XStream when this object is read into memory.
     */
K
Kohsuke Kawaguchi 已提交
403
    protected Object readResolve() {
K
kohsuke 已提交
404
        // convert the old format to the new one
405 406 407 408
        if (launcher == null) {
            launcher = (agentCommand == null || agentCommand.trim().length() == 0)
                    ? new JNLPLauncher()
                    : new CommandLauncher(agentCommand);
409
        }
410
        if(nodeProperties==null)
411
            nodeProperties = new DescribableList<NodeProperty<?>,NodePropertyDescriptor>(Jenkins.getInstance());
K
kohsuke 已提交
412 413 414
        return this;
    }

K
kohsuke 已提交
415
    public SlaveDescriptor getDescriptor() {
416
        Descriptor d = Jenkins.getInstance().getDescriptorOrDie(getClass());
417 418 419
        if (d instanceof SlaveDescriptor)
            return (SlaveDescriptor) d;
        throw new IllegalStateException(d.getClass()+" needs to extend from SlaveDescriptor");
K
kohsuke 已提交
420
    }
421 422

    public static abstract class SlaveDescriptor extends NodeDescriptor {
K
kohsuke 已提交
423
        public FormValidation doCheckNumExecutors(@QueryParameter String value) {
424
            return FormValidation.validatePositiveInteger(value);
425
        }
K
kohsuke 已提交
426 427 428 429

        /**
         * Performs syntactical check on the remote FS for slaves.
         */
430
        public FormValidation doCheckRemoteFS(@QueryParameter String value) throws IOException, ServletException {
K
kohsuke 已提交
431
            if(Util.fixEmptyAndTrim(value)==null)
432
                return FormValidation.error(Messages.Slave_Remote_Director_Mandatory());
K
kohsuke 已提交
433

K
kohsuke 已提交
434
            if(value.startsWith("\\\\") || value.startsWith("/net/"))
435
                return FormValidation.warning(Messages.Slave_Network_Mounted_File_System_Warning());
K
kohsuke 已提交
436

437
            if (Util.isRelativePath(value)) {
438
                return FormValidation.warning(Messages.Slave_Remote_Relative_Path_Warning());
439 440
            }

K
kohsuke 已提交
441
            return FormValidation.ok();
K
kohsuke 已提交
442
        }
443 444 445
    }


K
kohsuke 已提交
446
//
447
// backward compatibility
K
kohsuke 已提交
448
//
449 450 451
    /**
     * Command line to launch the agent, like
     * "ssh myslave java -jar /path/to/hudson-remoting.jar"
452
     * @deprecated in 1.216
453
     */
454
    @Deprecated
455 456
    private transient String agentCommand;

K
kohsuke 已提交
457
    /**
K
Kohsuke Kawaguchi 已提交
458 459 460 461 462 463 464 465 466 467 468
     * Obtains the clock difference between this side and that side of the channel.
     *
     * <p>
     * This is a hack to wrap the whole thing into a simple {@link Callable}.
     *
     * <ol>
     *     <li>When the callable is sent to remote, we capture the time (on this side) in {@link GetClockDifference2#startTime}
     *     <li>When the other side receives the callable it is {@link GetClockDifference2}.
     *     <li>We capture the time on the other side and {@link GetClockDifference3} gets sent from the other side
     *     <li>When it's read on this side as a return value, it morphs itself into {@link ClockDifference}.
     * </ol>
K
kohsuke 已提交
469
     */
470
    private static final class GetClockDifference1 extends MasterToSlaveCallable<ClockDifference,IOException> {
K
Kohsuke Kawaguchi 已提交
471 472 473 474 475 476 477
        public ClockDifference call() {
            // this method must be being invoked locally, which means the clock is in sync
            return new ClockDifference(0);
        }

        private Object writeReplace() {
            return new GetClockDifference2();
K
kohsuke 已提交
478 479 480 481
        }

        private static final long serialVersionUID = 1L;
    }
482

483
    private static final class GetClockDifference2 extends MasterToSlaveCallable<GetClockDifference3,IOException> {
K
Kohsuke Kawaguchi 已提交
484 485 486 487
        /**
         * Capture the time on the master when this object is sent to remote, which is when
         * {@link GetClockDifference1#writeReplace()} is run.
         */
488
        private final long startTime = System.currentTimeMillis();
K
Kohsuke Kawaguchi 已提交
489 490 491 492 493 494 495 496 497

        public GetClockDifference3 call() {
            return new GetClockDifference3(startTime);
        }

        private static final long serialVersionUID = 1L;
    }

    private static final class GetClockDifference3 implements Serializable {
498
        private final long remoteTime = System.currentTimeMillis();
K
Kohsuke Kawaguchi 已提交
499 500 501 502 503 504 505
        private final long startTime;

        public GetClockDifference3(long startTime) {
            this.startTime = startTime;
        }

        private Object readResolve() {
506 507
            long endTime = System.currentTimeMillis();
            return new ClockDifference((startTime + endTime)/2-remoteTime);
K
Kohsuke Kawaguchi 已提交
508 509 510
        }
    }

511 512 513 514
    /**
     * Determines the workspace root file name for those who really really need the shortest possible path name.
     */
    private static final String WORKSPACE_ROOT = System.getProperty(Slave.class.getName()+".workspaceRoot","workspace");
K
kohsuke 已提交
515
}