Slave.java 16.4 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 69
import java.util.ArrayList;
import java.util.Collection;
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 已提交
70

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

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

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

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

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

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

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

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

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

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

135
    @DataBoundConstructor
K
kohsuke 已提交
136
    public Slave(String name, String nodeDescription, String remoteFS, String numExecutors,
137 138
                 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 已提交
139 140
    }

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

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

164 165 166 167 168
        // 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 已提交
169 170 171

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

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

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

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

    public String getRemoteFS() {
        return remoteFS;
    }

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

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

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

    public int getNumExecutors() {
        return numExecutors;
    }

    public Mode getMode() {
        return mode;
    }

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

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

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

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

229
    public Set<Label> getAssignedLabels() {
230 231
        // todo refactor to make dynamic labels a bit less hacky
        if(labels==null || isChangedDynamicLabels()) {
232 233 234 235 236 237 238 239
            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());
240
            r.addAll(getDynamicLabels());
241 242 243 244 245
            this.labels = Collections.unmodifiableSet(r);
        }
        return labels;
    }

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    /**
     * 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
269 270 271
     *
     * @return
     *      never null.
272 273
     */
    public Set<Label> getDynamicLabels() {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
        // 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));
291 292
                    }
                }
293 294
            } else {
                dynamicLabelsInstanceHash = 0;
295
            }
296 297

            return labels;
298 299
        }
    }
300

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

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

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

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

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

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

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

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

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

        public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
K
kohsuke 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361
            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 已提交
362 363 364
            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);
365
            }
K
kohsuke 已提交
366 367
            return res;
        }
368

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

    }

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

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

    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 已提交
405 406 407 408 409 410 411 412 413
    /**
     * 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";
        }
414 415 416 417
        if (launcher == null) {
            launcher = (agentCommand == null || agentCommand.trim().length() == 0)
                    ? new JNLPLauncher()
                    : new CommandLauncher(agentCommand);
418
        }
K
kohsuke 已提交
419 420 421
        return this;
    }

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

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

        /**
         * 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();
        }
452 453 454
    }


K
kohsuke 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
//
// 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;
474 475 476 477 478 479
    /**
     * Command line to launch the agent, like
     * "ssh myslave java -jar /path/to/hudson-remoting.jar"
     */
    private transient String agentCommand;

K
kohsuke 已提交
480 481 482 483 484 485 486 487 488 489
    /**
     * 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;
    }
490 491 492 493 494 495 496 497 498 499 500

//    static {
//        ConvertUtils.register(new Converter(){
//            public Object convert(Class type, Object value) {
//                if (value != null) {
//                System.out.println("CVT: " + type + " from (" + value.getClass() + ") " + value);
//                } else {
//                    System.out.println("CVT: " + type + " from " + value);
//                }
//                return null;  //To change body of implemented methods use File | Settings | File Templates.
//            }
501
//        }, ComputerLauncher.class);
502
//    }
K
kohsuke 已提交
503
}