Slave.java 13.7 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;
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 59 60 61 62 63 64
import java.util.ArrayList;
import java.util.Collections;
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 已提交
65

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

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

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

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

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

K
kohsuke 已提交
105
    /**
106 107
     * Slave availablility strategy.
     */
K
kohsuke 已提交
108
    private RetentionStrategy retentionStrategy;
109 110 111

    /**
     * The starter that will startup this slave.
112
     */
113
    private ComputerLauncher launcher;
K
kohsuke 已提交
114

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

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

127
    @DataBoundConstructor
K
kohsuke 已提交
128
    public Slave(String name, String nodeDescription, String remoteFS, String numExecutors,
K
kohsuke 已提交
129 130
                 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 已提交
131 132
    }

M
mindless 已提交
133 134 135
    /**
     * @deprecated since 2009-02-20.
     */
136 137
    @Deprecated
    public Slave(String name, String nodeDescription, String remoteFS, int numExecutors,
K
kohsuke 已提交
138 139
            Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy retentionStrategy) throws FormException, IOException {
    	this(name, nodeDescription, remoteFS, numExecutors, mode, labelString, launcher, retentionStrategy, new ArrayList());
140 141
    }
    
K
kohsuke 已提交
142
    public Slave(String name, String nodeDescription, String remoteFS, int numExecutors,
K
kohsuke 已提交
143
                 Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy retentionStrategy, List<? extends NodeProperty<?>> nodeProperties) throws FormException, IOException {
K
kohsuke 已提交
144
        this.name = name;
K
kohsuke 已提交
145
        this.description = nodeDescription;
K
kohsuke 已提交
146
        this.numExecutors = numExecutors;
K
kohsuke 已提交
147
        this.mode = mode;
148
        this.remoteFS = Util.fixNull(remoteFS).trim();
K
kohsuke 已提交
149
        this.label = Util.fixNull(labelString).trim();
150 151
        this.launcher = launcher;
        this.retentionStrategy = retentionStrategy;
152
        getAssignedLabels();    // compute labels now
153 154
        
        this.nodeProperties.replaceBy(nodeProperties);
K
kohsuke 已提交
155

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

159 160 161 162 163
        // 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 已提交
164 165 166

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

168
        if (this.numExecutors<=0)
K
i18n  
kohsuke 已提交
169
            throw new FormException(Messages.Slave_InvalidConfig_Executors(name), null);
K
kohsuke 已提交
170 171
    }

172
    public ComputerLauncher getLauncher() {
173
        return launcher == null ? new JNLPLauncher() : launcher;
174 175
    }

176
    public void setLauncher(ComputerLauncher launcher) {
177
        this.launcher = launcher;
K
kohsuke 已提交
178 179 180 181 182 183
    }

    public String getRemoteFS() {
        return remoteFS;
    }

K
kohsuke 已提交
184 185
    public String getNodeName() {
        return name;
K
kohsuke 已提交
186 187
    }

K
kohsuke 已提交
188 189 190 191
    public void setNodeName(String name) {
        this.name = name; 
    }

K
kohsuke 已提交
192 193 194 195 196 197 198 199 200 201 202 203
    public String getNodeDescription() {
        return description;
    }

    public int getNumExecutors() {
        return numExecutors;
    }

    public Mode getMode() {
        return mode;
    }

K
kohsuke 已提交
204 205 206 207
    public void setMode(Mode mode) {
        this.mode = mode;
    }

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

K
kohsuke 已提交
216 217
    public void setRetentionStrategy(RetentionStrategy availabilityStrategy) {
        this.retentionStrategy = availabilityStrategy;
218 219
    }

220 221 222
    public String getLabelString() {
        return Util.fixNull(label).trim();
    }
223

224
    public ClockDifference getClockDifference() throws IOException, InterruptedException {
K
kohsuke 已提交
225
        VirtualChannel channel = getChannel();
K
kohsuke 已提交
226 227
        if(channel==null)
            throw new IOException(getNodeName()+" is offline");
K
kohsuke 已提交
228

K
kohsuke 已提交
229
        long startTime = System.currentTimeMillis();
K
kohsuke 已提交
230
        long slaveTime = channel.call(new GetSystemTime());
K
kohsuke 已提交
231
        long endTime = System.currentTimeMillis();
K
kohsuke 已提交
232

233
        return new ClockDifference((startTime+endTime)/2 - slaveTime);
K
kohsuke 已提交
234 235
    }

K
kohsuke 已提交
236
    public Computer createComputer() {
237
        return new SlaveComputer(this);
K
kohsuke 已提交
238 239
    }

240
    public FilePath getWorkspaceFor(TopLevelItem item) {
K
kohsuke 已提交
241 242 243
        FilePath r = getWorkspaceRoot();
        if(r==null)     return null;    // offline
        return r.child(item.getName());
244 245
    }

K
kohsuke 已提交
246
    public FilePath getRootPath() {
247 248 249
        return createPath(remoteFS);
    }

K
kohsuke 已提交
250 251
    /**
     * Root directory on this slave where all the job workspaces are laid out.
K
kohsuke 已提交
252 253
     * @return
     *      null if not connected.
K
kohsuke 已提交
254 255
     */
    public FilePath getWorkspaceRoot() {
K
kohsuke 已提交
256 257 258
        FilePath r = getRootPath();
        if(r==null) return null;
        return r.child("workspace");
K
kohsuke 已提交
259
    }
K
kohsuke 已提交
260

261 262 263 264
    /**
     * Web-bound object used to serve jar files for JNLP.
     */
    public static final class JnlpJar {
K
kohsuke 已提交
265
        private final String fileName;
266

K
kohsuke 已提交
267 268
        public JnlpJar(String fileName) {
            this.fileName = fileName;
269 270 271
        }

        public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
K
kohsuke 已提交
272
            URLConnection con = connect();
273 274 275
            // 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.
            rsp.setHeader("Content-Disposition", "inline; filename=" + fileName);
K
kohsuke 已提交
276 277 278 279 280 281 282 283 284 285 286 287
            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 已提交
288 289
            if(res==null) {
                // during the development this path doesn't have the files.
J
jglick 已提交
290
                res = new URL(new File(".").getAbsoluteFile().toURI().toURL(),"target/generated-resources/WEB-INF/"+fileName);
291
            }
K
kohsuke 已提交
292 293
            return res;
        }
294

K
kohsuke 已提交
295 296 297 298 299 300 301
        public byte[] readFully() throws IOException {
            InputStream in = connect().getInputStream();
            try {
                return IOUtils.toByteArray(in);
            } finally {
                in.close();
            }
302 303 304 305
        }

    }

K
kohsuke 已提交
306
    public Launcher createLauncher(TaskListener listener) {
307
        SlaveComputer c = getComputer();
K
kohsuke 已提交
308
        return new RemoteLauncher(listener, c.getChannel(), c.isUnix()).decorateFor(this);
K
kohsuke 已提交
309 310
    }

K
kohsuke 已提交
311
    /**
312
     * Gets the corresponding computer object.
K
kohsuke 已提交
313
     */
314
    public SlaveComputer getComputer() {
315
        return (SlaveComputer)toComputer();
K
kohsuke 已提交
316 317
    }

318
    @Override
K
kohsuke 已提交
319 320 321 322 323 324 325 326 327
    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);
    }

328
    @Override
K
kohsuke 已提交
329 330 331 332
    public int hashCode() {
        return name.hashCode();
    }

K
kohsuke 已提交
333 334 335 336 337 338 339 340 341
    /**
     * 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";
        }
342 343 344 345
        if (launcher == null) {
            launcher = (agentCommand == null || agentCommand.trim().length() == 0)
                    ? new JNLPLauncher()
                    : new CommandLauncher(agentCommand);
346
        }
347 348
        if(nodeProperties==null)
            nodeProperties = new DescribableList<NodeProperty<?>,NodePropertyDescriptor>(Hudson.getInstance());
K
kohsuke 已提交
349 350 351
        return this;
    }

K
kohsuke 已提交
352
    public SlaveDescriptor getDescriptor() {
353 354 355
        Descriptor d = Hudson.getInstance().getDescriptor(getClass());
        if (d instanceof SlaveDescriptor)
            return (SlaveDescriptor) d;
K
kohsuke 已提交
356 357
        if (d==null)
            throw new IllegalStateException(getClass()+" doesn't have a descriptor");
358
        throw new IllegalStateException(d.getClass()+" needs to extend from SlaveDescriptor");
K
kohsuke 已提交
359
    }
360 361

    public static abstract class SlaveDescriptor extends NodeDescriptor {
K
kohsuke 已提交
362
        public FormValidation doCheckNumExecutors(@QueryParameter String value) {
363
            return FormValidation.validatePositiveInteger(value);
364
        }
K
kohsuke 已提交
365 366 367 368

        /**
         * Performs syntactical check on the remote FS for slaves.
         */
369
        public FormValidation doCheckRemoteFS(@QueryParameter String value) throws IOException, ServletException {
K
kohsuke 已提交
370 371
            if(Util.fixEmptyAndTrim(value)==null)
                return FormValidation.error("Remote directory is mandatory");
K
kohsuke 已提交
372

K
kohsuke 已提交
373 374
            if(value.startsWith("\\\\") || value.startsWith("/net/"))
                return FormValidation.warning("Are you sure you want to use network mounted file system for FS root? " +
375
                        "Note that this directory does not need to be visible to the master.");
K
kohsuke 已提交
376

K
kohsuke 已提交
377
            return FormValidation.ok();
K
kohsuke 已提交
378
        }
379 380 381
    }


K
kohsuke 已提交
382
//
383
// backward compatibility
K
kohsuke 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
//
    /**
     * 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;
M
mindless 已提交
401

402 403 404 405 406 407
    /**
     * Command line to launch the agent, like
     * "ssh myslave java -jar /path/to/hudson-remoting.jar"
     */
    private transient String agentCommand;

K
kohsuke 已提交
408 409 410 411 412 413 414 415 416 417
    /**
     * 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 已提交
418
}