Slave.java 15.9 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, Erik Ramfelt, Martin Eigenbrodt, Stephen Connolly, Tom Huybrechts
 * 
 * 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 26 27 28
package hudson.model;

import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
29
import hudson.Launcher.RemoteLauncher;
K
kohsuke 已提交
30
import hudson.model.Descriptor.FormException;
K
kohsuke 已提交
31 32
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
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;
42 43
import hudson.tasks.DynamicLabeler;
import hudson.tasks.LabelFinder;
K
kohsuke 已提交
44
import hudson.util.ClockDifference;
45
import hudson.util.DescribableList;
K
kohsuke 已提交
46
import hudson.util.FormFieldValidator;
47
import hudson.util.FormFieldValidator.NonNegativeInteger;
K
kohsuke 已提交
48

K
kohsuke 已提交
49 50 51 52
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
53
import java.net.MalformedURLException;
54 55
import java.net.URL;
import java.net.URLConnection;
56 57 58 59 60 61 62 63 64 65 66 67 68
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;

import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
K
kohsuke 已提交
69

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

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

    /**
     * Path to the root of the workspace
K
kohsuke 已提交
95
     * from the view point of this node, such as "/hudson"
K
kohsuke 已提交
96
     */
K
kohsuke 已提交
97
    protected final String remoteFS;
K
kohsuke 已提交
98 99 100 101 102 103 104 105 106 107 108

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

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

K
kohsuke 已提交
109
    /**
110 111
     * Slave availablility strategy.
     */
K
kohsuke 已提交
112
    private RetentionStrategy retentionStrategy;
113 114 115

    /**
     * The starter that will startup this slave.
116
     */
117
    private ComputerLauncher launcher;
K
kohsuke 已提交
118

119 120 121 122
    /**
     * Whitespace-separated labels.
     */
    private String label="";
123
    
124
	private /*almost final*/ DescribableList<NodeProperty<?>,NodePropertyDescriptor> nodeProperties = new DescribableList<NodeProperty<?>,NodePropertyDescriptor>(Hudson.getInstance());
125 126 127 128 129 130

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

131 132 133
    private transient volatile Set<Label> dynamicLabels;
    private transient volatile int dynamicLabelsInstanceHash;

134
    @DataBoundConstructor
K
kohsuke 已提交
135
    public Slave(String name, String nodeDescription, String remoteFS, String numExecutors,
136 137
                 Mode mode, String label, ComputerLauncher launcher, RetentionStrategy retentionStrategy, List<? extends NodeProperty<?>> nodeProperties) throws FormException, IOException {
        this(name,nodeDescription,remoteFS,Util.tryParseNumber(numExecutors, 1).intValue(),mode,label,launcher,retentionStrategy, nodeProperties);
K
kohsuke 已提交
138 139
    }

140 141 142 143 144 145
    @Deprecated
    public Slave(String name, String nodeDescription, String remoteFS, int numExecutors,
            Mode mode, String label, ComputerLauncher launcher, RetentionStrategy retentionStrategy) throws FormException, IOException {
    	this(name, nodeDescription, remoteFS, numExecutors, mode, label, launcher, retentionStrategy, new ArrayList());
    }
    
K
kohsuke 已提交
146
    public Slave(String name, String nodeDescription, String remoteFS, int numExecutors,
147
                 Mode mode, String label, ComputerLauncher launcher, RetentionStrategy retentionStrategy, List<? extends NodeProperty<?>> nodeProperties) throws FormException, IOException {
K
kohsuke 已提交
148
        this.name = name;
K
kohsuke 已提交
149
        this.description = nodeDescription;
K
kohsuke 已提交
150
        this.numExecutors = numExecutors;
K
kohsuke 已提交
151
        this.mode = mode;
K
kohsuke 已提交
152
        this.remoteFS = remoteFS;
153
        this.label = Util.fixNull(label).trim();
154 155
        this.launcher = launcher;
        this.retentionStrategy = retentionStrategy;
156
        getAssignedLabels();    // compute labels now
157 158
        
        this.nodeProperties.replaceBy(nodeProperties);
K
kohsuke 已提交
159

K
d'oh!  
kohsuke 已提交
160
        if (name.equals(""))
K
i18n  
kohsuke 已提交
161
            throw new FormException(Messages.Slave_InvalidConfig_NoName(), null);
K
kohsuke 已提交
162

163 164 165 166 167
        // this prevents the config from being saved when slaves are offline.
        // on a large deployment with a lot of slaves, some slaves are bound to be offline,
        // so this check is harmful.
        //if (!localFS.exists())
        //    throw new FormException("Invalid slave configuration for " + name + ". No such directory exists: " + localFS, null);
K
kohsuke 已提交
168 169 170

//        if (remoteFS.equals(""))
//            throw new FormException(Messages.Slave_InvalidConfig_NoRemoteDir(name), null);
171

172
        if (this.numExecutors<=0)
K
i18n  
kohsuke 已提交
173
            throw new FormException(Messages.Slave_InvalidConfig_Executors(name), null);
K
kohsuke 已提交
174 175
    }

176
    public ComputerLauncher getLauncher() {
177
        return launcher == null ? new JNLPLauncher() : launcher;
178 179
    }

180
    public void setLauncher(ComputerLauncher launcher) {
181
        this.launcher = launcher;
K
kohsuke 已提交
182 183 184 185 186 187
    }

    public String getRemoteFS() {
        return remoteFS;
    }

K
kohsuke 已提交
188 189
    public String getNodeName() {
        return name;
K
kohsuke 已提交
190 191
    }

K
kohsuke 已提交
192 193 194 195
    public void setNodeName(String name) {
        this.name = name; 
    }

K
kohsuke 已提交
196 197 198 199 200 201 202 203 204 205 206 207
    public String getNodeDescription() {
        return description;
    }

    public int getNumExecutors() {
        return numExecutors;
    }

    public Mode getMode() {
        return mode;
    }

K
kohsuke 已提交
208 209 210 211
    public void setMode(Mode mode) {
        this.mode = mode;
    }

212 213 214 215
    public DescribableList<NodeProperty<?>, NodePropertyDescriptor> getNodeProperties() {
    	return nodeProperties;
    }
    
K
kohsuke 已提交
216 217
    public RetentionStrategy getRetentionStrategy() {
        return retentionStrategy == null ? RetentionStrategy.Always.INSTANCE : retentionStrategy;
218 219
    }

K
kohsuke 已提交
220 221
    public void setRetentionStrategy(RetentionStrategy availabilityStrategy) {
        this.retentionStrategy = availabilityStrategy;
222 223
    }

224 225 226
    public String getLabelString() {
        return Util.fixNull(label).trim();
    }
227

228
    public Set<Label> getAssignedLabels() {
229 230
        // todo refactor to make dynamic labels a bit less hacky
        if(labels==null || isChangedDynamicLabels()) {
231 232 233 234 235 236 237 238
            Set<Label> r = new HashSet<Label>();
            String ls = getLabelString();
            if(ls.length()>0) {
                for( String l : ls.split(" +")) {
                    r.add(Hudson.getInstance().getLabel(l));
                }
            }
            r.add(getSelfLabel());
239
            r.addAll(getDynamicLabels());
240 241 242 243 244
            this.labels = Collections.unmodifiableSet(r);
        }
        return labels;
    }

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    /**
     * Check if we should rebuild the list of dynamic labels.
     * @todo make less hacky
     * @return
     */
    private boolean isChangedDynamicLabels() {
        Computer comp = getComputer();
        if (comp == null) {
            return dynamicLabelsInstanceHash != 0;
        } else {
            if (dynamicLabelsInstanceHash == comp.hashCode()) {
                return false;
            }
            dynamicLabels = null; // force a re-calc
            return true;
        }
    }

    /**
     * Returns the possibly empty set of labels that it has been determined as supported by this node.
     *
     * @todo make less hacky
     * @see hudson.tasks.LabelFinder
268 269 270
     *
     * @return
     *      never null.
271 272
     */
    public Set<Label> getDynamicLabels() {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        // another thread may preempt and set dynamicLabels field to null,
        // so a care needs to be taken to avoid race conditions under all circumstances.
        Set<Label> labels = dynamicLabels;
        if (labels != null)     return labels;

        synchronized (this) {
            labels = dynamicLabels;
            if (labels != null)     return labels;

            dynamicLabels = labels = new HashSet<Label>();
            Computer computer = getComputer();
            VirtualChannel channel;
            if (computer != null && (channel = computer.getChannel()) != null) {
                dynamicLabelsInstanceHash = computer.hashCode();
                for (DynamicLabeler labeler : LabelFinder.LABELERS) {
                    for (String label : labeler.findLabels(channel)) {
                        labels.add(Hudson.getInstance().getLabel(label));
290 291
                    }
                }
292 293
            } else {
                dynamicLabelsInstanceHash = 0;
294
            }
295 296

            return labels;
297 298
        }
    }
299

300
    public ClockDifference getClockDifference() throws IOException, InterruptedException {
K
kohsuke 已提交
301
        VirtualChannel channel = getComputer().getChannel();
K
kohsuke 已提交
302 303
        if(channel==null)
            throw new IOException(getNodeName()+" is offline");
K
kohsuke 已提交
304

K
kohsuke 已提交
305
        long startTime = System.currentTimeMillis();
K
kohsuke 已提交
306
        long slaveTime = channel.call(new GetSystemTime());
K
kohsuke 已提交
307
        long endTime = System.currentTimeMillis();
K
kohsuke 已提交
308

309
        return new ClockDifference((startTime+endTime)/2 - slaveTime);
K
kohsuke 已提交
310 311
    }

K
kohsuke 已提交
312
    public Computer createComputer() {
313
        return new SlaveComputer(this);
K
kohsuke 已提交
314 315
    }

316
    public FilePath getWorkspaceFor(TopLevelItem item) {
K
kohsuke 已提交
317 318 319
        FilePath r = getWorkspaceRoot();
        if(r==null)     return null;    // offline
        return r.child(item.getName());
320 321
    }

K
kohsuke 已提交
322
    public FilePath getRootPath() {
323 324 325
        return createPath(remoteFS);
    }

K
kohsuke 已提交
326 327
    /**
     * Root directory on this slave where all the job workspaces are laid out.
K
kohsuke 已提交
328 329
     * @return
     *      null if not connected.
K
kohsuke 已提交
330 331
     */
    public FilePath getWorkspaceRoot() {
K
kohsuke 已提交
332 333 334
        FilePath r = getRootPath();
        if(r==null) return null;
        return r.child("workspace");
K
kohsuke 已提交
335
    }
K
kohsuke 已提交
336

337 338 339 340
    /**
     * Web-bound object used to serve jar files for JNLP.
     */
    public static final class JnlpJar {
K
kohsuke 已提交
341
        private final String fileName;
342

K
kohsuke 已提交
343 344
        public JnlpJar(String fileName) {
            this.fileName = fileName;
345 346 347
        }

        public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
K
kohsuke 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360
            URLConnection con = connect();
            InputStream in = con.getInputStream();
            rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar" );
            in.close();
        }

        private URLConnection connect() throws IOException {
            URL res = getURL();
            return res.openConnection();
        }

        public URL getURL() throws MalformedURLException {
            URL res = Hudson.getInstance().servletContext.getResource("/WEB-INF/" + fileName);
K
kohsuke 已提交
361 362 363
            if(res==null) {
                // during the development this path doesn't have the files.
                res = new URL(new File(".").getAbsoluteFile().toURL(),"target/generated-resources/WEB-INF/"+fileName);
364
            }
K
kohsuke 已提交
365 366
            return res;
        }
367

K
kohsuke 已提交
368 369 370 371 372 373 374
        public byte[] readFully() throws IOException {
            InputStream in = connect().getInputStream();
            try {
                return IOUtils.toByteArray(in);
            } finally {
                in.close();
            }
375 376 377 378
        }

    }

K
kohsuke 已提交
379
    public Launcher createLauncher(TaskListener listener) {
380
        SlaveComputer c = getComputer();
381
        return new RemoteLauncher(listener, c.getChannel(), c.isUnix());
K
kohsuke 已提交
382 383
    }

K
kohsuke 已提交
384
    /**
385
     * Gets the corresponding computer object.
K
kohsuke 已提交
386
     */
387 388
    public SlaveComputer getComputer() {
        return (SlaveComputer)Hudson.getInstance().getComputer(this);
K
kohsuke 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    }

    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);
    }

    public int hashCode() {
        return name.hashCode();
    }

K
kohsuke 已提交
404 405 406 407 408 409 410 411 412
    /**
     * Invoked by XStream when this object is read into memory.
     */
    private Object readResolve() {
        // convert the old format to the new one
        if(command!=null && agentCommand==null) {
            if(command.length()>0)  command += ' ';
            agentCommand = command+"java -jar ~/bin/slave.jar";
        }
413 414 415 416
        if (launcher == null) {
            launcher = (agentCommand == null || agentCommand.trim().length() == 0)
                    ? new JNLPLauncher()
                    : new CommandLauncher(agentCommand);
417
        }
418 419
        if(nodeProperties==null)
            nodeProperties = new DescribableList<NodeProperty<?>,NodePropertyDescriptor>(Hudson.getInstance());
K
kohsuke 已提交
420 421 422
        return this;
    }

K
kohsuke 已提交
423 424 425
    public SlaveDescriptor getDescriptor() {
        return (SlaveDescriptor)Hudson.getInstance().getDescriptor(getClass());
    }
426 427 428 429 430

    public static abstract class SlaveDescriptor extends NodeDescriptor {
        public void doCheckNumExecutors() throws IOException, ServletException {
            new NonNegativeInteger().process();
        }
K
kohsuke 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

        /**
         * Performs syntactical check on the remote FS for slaves.
         */
        public void doCheckRemoteFs(StaplerRequest req, StaplerResponse rsp, @QueryParameter final String value) throws IOException, ServletException {
            new FormFieldValidator(req,rsp,false) {
                protected void check() throws IOException, ServletException {
                    if(Util.fixEmptyAndTrim(value)==null) {
                        error("Remote directory is mandatory");
                        return;
                    }

                    if(value.startsWith("\\\\") || value.startsWith("/net/")) {
                        warning("Are you sure you want to use network mounted file system for FS root? " +
                                "Note that this directory needs not be visible to the master.");
                        return;
                    }

                    ok();
                }
            }.process();
        }
453 454 455
    }


K
kohsuke 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
//
// backwrad compatibility
//
    /**
     * In Hudson < 1.69 this was used to store the local file path
     * to the remote workspace. No longer in use.
     *
     * @deprecated
     *      ... but still in use during the transition.
     */
    private File localFS;

    /**
     * In Hudson < 1.69 this was used to store the command
     * to connect to the remote machine, like "ssh myslave".
     *
     * @deprecated
     */
    private transient String command;
475 476 477 478 479 480
    /**
     * Command line to launch the agent, like
     * "ssh myslave java -jar /path/to/hudson-remoting.jar"
     */
    private transient String agentCommand;

K
kohsuke 已提交
481 482 483 484 485 486 487 488 489 490
    /**
     * Obtains the system clock.
     */
    private static final class GetSystemTime implements Callable<Long,RuntimeException> {
        public Long call() {
            return System.currentTimeMillis();
        }

        private static final long serialVersionUID = 1L;
    }
K
kohsuke 已提交
491
}