Slave.java 6.6 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6
package hudson.model;

import hudson.FilePath;
import hudson.Launcher;
import hudson.Proc;
import hudson.Util;
K
kohsuke 已提交
7
import hudson.model.Descriptor.FormException;
K
kohsuke 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
import hudson.util.ArgumentListBuilder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

/**
 * Information about a Hudson slave node.
 *
 * @author Kohsuke Kawaguchi
 */
public final class Slave implements Node {
    /**
     * Name of this slave node.
     */
    private final String name;

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

    /**
     * Commands to run to post a job on this machine.
     */
    private final String command;

    /**
     * Path to the root of the workspace
     * from within this node, such as "/hudson"
     */
    private final String remoteFS;

    /**
     * Path to the root of the remote workspace of this node,
     * such as "/net/slave1/hudson"
     */
    private final File localFS;

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

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

K
kohsuke 已提交
60
    public Slave(String name, String description, String command, String remoteFS, File localFS, int numExecutors, Mode mode) throws FormException {
K
kohsuke 已提交
61 62 63 64 65 66 67
        this.name = name;
        this.description = description;
        this.command = command;
        this.remoteFS = remoteFS;
        this.localFS = localFS;
        this.numExecutors = numExecutors;
        this.mode = mode;
K
kohsuke 已提交
68

K
d'oh!  
kohsuke 已提交
69
        if (name.equals(""))
K
kohsuke 已提交
70 71 72 73 74
            throw new FormException("Invalid slave configuration. Name is empty", null);
        if (!localFS.exists())
            throw new FormException("Invalid slave configuration for " + name + ". No such directory exists: " + localFS, null);
        if (remoteFS.equals(""))
            throw new FormException("Invalid slave configuration for " + name + ". No remote directory given", null);
K
kohsuke 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    }

    public String getNodeName() {
        return name;
    }

    public String getCommand() {
        return command;
    }

    public String[] getCommandTokens() {
        return Util.tokenize(command);
    }

    public String getRemoteFS() {
        return remoteFS;
    }

    public File getLocalFS() {
        return localFS;
    }

    public String getNodeDescription() {
        return description;
    }

    public FilePath getFilePath() {
        return new FilePath(localFS,remoteFS);
    }

    public int getNumExecutors() {
        return numExecutors;
    }

    public Mode getMode() {
        return mode;
    }

    /**
     * Estimates the clock difference with this slave.
     *
     * @return
     *      difference in milli-seconds.
     *      a large positive value indicates that the master is ahead of the slave,
     *      and negative value indicates otherwise.
     */
    public long getClockDifference() throws IOException {
        File testFile = new File(localFS,"clock.skew");
        FileOutputStream os = new FileOutputStream(testFile);
        long now = new Date().getTime();
        os.close();

        long r = now - testFile.lastModified();

        testFile.delete();

        return r;
    }

    /**
     * Gets the clock difference in HTML string.
     */
    public String getClockDifferenceString() {
        try {
            long diff = getClockDifference();
            if(-1000<diff && diff <1000)
                return "In sync";  // clock is in sync

            long abs = Math.abs(diff);

            String s = Util.getTimeSpanString(abs);
            if(diff<0)
                s += " ahead";
            else
                s += " behind";

            if(abs>100*60) // more than a minute difference
                s = "<span class='error'>"+s+"</span>";

            return s;
        } catch (IOException e) {
            return "<span class='error'>Unable to check</span>";
        }
    }

    public Launcher createLauncher(TaskListener listener) {
        if(command.length()==0) // local alias
            return new Launcher(listener);


        return new Launcher(listener) {
            @Override
            public Proc launch(String[] cmd, String[] env, OutputStream out, FilePath workDir) throws IOException {
                return super.launch(prepend(cmd,env,workDir), env, null, out);
            }

            @Override
            public Proc launch(String[] cmd, String[] env, InputStream in, OutputStream out) throws IOException {
                return super.launch(prepend(cmd,env,CURRENT_DIR), env, in, out);
            }

            @Override
            public boolean isUnix() {
                // Err on Unix, since we expect that to be the common slaves
                return remoteFS.indexOf('\\')==-1;
            }

            private String[] prepend(String[] cmd, String[] env, FilePath workDir) {
                ArgumentListBuilder r = new ArgumentListBuilder();
                r.add(getCommandTokens());
                r.add(getFilePath().child("bin").child("slave").getRemote());
                r.addQuoted(workDir.getRemote());
                for (String s : env) {
                    int index =s.indexOf('=');
                    r.add(s.substring(0,index));
                    r.add(s.substring(index+1));
                }
                r.add("--");
                for (String c : cmd) {
                    // ssh passes the command and parameters in one string.
                    // see RFC 4254 section 6.5.
                    // so the consequence that we need to give
                    // {"ssh",...,"ls","\"a b\""} to list a file "a b".
                    // If we just do
                    // {"ssh",...,"ls","a b"} (which is correct if this goes directly to Runtime.exec),
                    // then we end up executing "ls","a","b" on the other end.
                    //
                    // I looked at rsh source code, and that behave the same way.
                    if(c.indexOf(' ')>=0)
                        r.addQuoted(c);
                    else
                        r.add(c);
                }
                return r.toCommandArray();
            }
        };
    }

    public FilePath getWorkspaceRoot() {
        return getFilePath().child("workspace");
    }

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

    private static final FilePath CURRENT_DIR = new FilePath(new File("."));
}