Slave.java 13.9 KB
Newer Older
K
kohsuke 已提交
1 2 3
/*
 * The MIT License
 * 
4
 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt, Martin Eigenbrodt, Stephen Connolly, Tom Huybrechts
K
kohsuke 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 
 * 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;
30
import hudson.diagnosis.OldDataMonitor;
K
kohsuke 已提交
31
import hudson.model.Descriptor.FormException;
K
kohsuke 已提交
32 33
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
34 35 36 37 38 39 40 41 42
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 已提交
43
import hudson.util.ClockDifference;
44
import hudson.util.DescribableList;
K
kohsuke 已提交
45
import hudson.util.FormValidation;
K
kohsuke 已提交
46

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

import javax.servlet.ServletException;

import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.DataBoundConstructor;
62
import org.kohsuke.stapler.HttpResponse;
63 64 65
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
K
kohsuke 已提交
66

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

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

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

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

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

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

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

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

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

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

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

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

K
kohsuke 已提交
160 161
//        if (remoteFS.equals(""))
//            throw new FormException(Messages.Slave_InvalidConfig_NoRemoteDir(name), null);
162

163
        if (this.numExecutors<=0)
K
i18n  
kohsuke 已提交
164
            throw new FormException(Messages.Slave_InvalidConfig_Executors(name), null);
K
kohsuke 已提交
165 166
    }

167
    public ComputerLauncher getLauncher() {
168
        return launcher == null ? new JNLPLauncher() : launcher;
169 170
    }

171
    public void setLauncher(ComputerLauncher launcher) {
172
        this.launcher = launcher;
K
kohsuke 已提交
173 174 175 176 177 178
    }

    public String getRemoteFS() {
        return remoteFS;
    }

K
kohsuke 已提交
179 180
    public String getNodeName() {
        return name;
K
kohsuke 已提交
181 182
    }

K
kohsuke 已提交
183 184 185 186
    public void setNodeName(String name) {
        this.name = name; 
    }

K
kohsuke 已提交
187 188 189 190 191 192 193 194 195 196 197 198
    public String getNodeDescription() {
        return description;
    }

    public int getNumExecutors() {
        return numExecutors;
    }

    public Mode getMode() {
        return mode;
    }

K
kohsuke 已提交
199 200 201 202
    public void setMode(Mode mode) {
        this.mode = mode;
    }

203 204 205 206
    public DescribableList<NodeProperty<?>, NodePropertyDescriptor> getNodeProperties() {
    	return nodeProperties;
    }
    
K
kohsuke 已提交
207 208
    public RetentionStrategy getRetentionStrategy() {
        return retentionStrategy == null ? RetentionStrategy.Always.INSTANCE : retentionStrategy;
209 210
    }

K
kohsuke 已提交
211 212
    public void setRetentionStrategy(RetentionStrategy availabilityStrategy) {
        this.retentionStrategy = availabilityStrategy;
213 214
    }

215 216 217
    public String getLabelString() {
        return Util.fixNull(label).trim();
    }
218

219
    public ClockDifference getClockDifference() throws IOException, InterruptedException {
K
kohsuke 已提交
220
        VirtualChannel channel = getChannel();
K
kohsuke 已提交
221 222
        if(channel==null)
            throw new IOException(getNodeName()+" is offline");
K
kohsuke 已提交
223

K
kohsuke 已提交
224
        long startTime = System.currentTimeMillis();
K
kohsuke 已提交
225
        long slaveTime = channel.call(new GetSystemTime());
K
kohsuke 已提交
226
        long endTime = System.currentTimeMillis();
K
kohsuke 已提交
227

228
        return new ClockDifference((startTime+endTime)/2 - slaveTime);
K
kohsuke 已提交
229 230
    }

K
kohsuke 已提交
231
    public Computer createComputer() {
232
        return new SlaveComputer(this);
K
kohsuke 已提交
233 234
    }

235
    public FilePath getWorkspaceFor(TopLevelItem item) {
K
kohsuke 已提交
236 237 238
        FilePath r = getWorkspaceRoot();
        if(r==null)     return null;    // offline
        return r.child(item.getName());
239 240
    }

K
kohsuke 已提交
241
    public FilePath getRootPath() {
242 243 244
        return createPath(remoteFS);
    }

K
kohsuke 已提交
245 246
    /**
     * Root directory on this slave where all the job workspaces are laid out.
K
kohsuke 已提交
247 248
     * @return
     *      null if not connected.
K
kohsuke 已提交
249 250
     */
    public FilePath getWorkspaceRoot() {
K
kohsuke 已提交
251 252
        FilePath r = getRootPath();
        if(r==null) return null;
253
        return r.child(WORKSPACE_ROOT);
K
kohsuke 已提交
254
    }
K
kohsuke 已提交
255

256 257 258
    /**
     * Web-bound object used to serve jar files for JNLP.
     */
259
    public static final class JnlpJar implements HttpResponse {
K
kohsuke 已提交
260
        private final String fileName;
261

K
kohsuke 已提交
262 263
        public JnlpJar(String fileName) {
            this.fileName = fileName;
264 265 266
        }

        public void doIndex( StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
K
kohsuke 已提交
267
            URLConnection con = connect();
268 269
            // 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.
270 271
            // 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 已提交
272 273 274 275 276
            InputStream in = con.getInputStream();
            rsp.serveFile(req, in, con.getLastModified(), con.getContentLength(), "*.jar" );
            in.close();
        }

277 278 279 280
        public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
            doIndex(req,rsp);
        }

K
kohsuke 已提交
281 282 283 284 285 286
        private URLConnection connect() throws IOException {
            URL res = getURL();
            return res.openConnection();
        }

        public URL getURL() throws MalformedURLException {
287 288 289
            String name = fileName;
            if (name.equals("hudson-cli.jar"))  name="jenkins-cli.jar";
            URL res = Hudson.getInstance().servletContext.getResource("/WEB-INF/" + name);
K
kohsuke 已提交
290 291
            if(res==null) {
                // during the development this path doesn't have the files.
292
                res = new URL(new File(".").getAbsoluteFile().toURI().toURL(),"target/generated-resources/WEB-INF/"+name);
293
            }
K
kohsuke 已提交
294 295
            return res;
        }
296

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

    }

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

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

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

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

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

K
kohsuke 已提交
356
    public SlaveDescriptor getDescriptor() {
357
        Descriptor d = Hudson.getInstance().getDescriptorOrDie(getClass());
358 359 360
        if (d instanceof SlaveDescriptor)
            return (SlaveDescriptor) d;
        throw new IllegalStateException(d.getClass()+" needs to extend from SlaveDescriptor");
K
kohsuke 已提交
361
    }
362 363

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

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

K
kohsuke 已提交
375
            if(value.startsWith("\\\\") || value.startsWith("/net/"))
376
                return FormValidation.warning(Messages.Slave_Network_Mounted_File_System_Warning());
K
kohsuke 已提交
377

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


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

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

K
kohsuke 已提交
409 410 411 412 413 414 415 416 417 418
    /**
     * 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;
    }
419 420 421 422 423

    /**
     * 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 已提交
424
}