ComputerLauncher.java 8.3 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, Stephen Connolly
 * 
 * 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.
 */
24 25 26
package hudson.slaves;

import hudson.ExtensionPoint;
27
import hudson.Extension;
28
import hudson.model.*;
K
kohsuke 已提交
29
import hudson.remoting.Channel;
30 31 32
import hudson.util.DescriptorList;
import hudson.util.StreamTaskListener;

33
import java.io.*;
34 35 36
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.tools.ant.util.DeweyDecimal;
37 38 39

/**
 * Extension point to allow control over how {@link Computer}s are "launched",
40
 * meaning how they get connected to their agent program.
41
 *
K
kohsuke 已提交
42 43 44 45 46 47 48 49
 * <h2>Associated View</h2>
 * <dl>
 * <dt>main.jelly</dt>
 * <dd>
 * This page will be rendered into the top page of the computer (/computer/NAME/)
 * Useful for showing launch related commands and status reports.
 * </dl>
 *
50
 * @author Stephen Connolly
D
Daniel Beck 已提交
51
 * @since 1.216-ish
52
 * @see ComputerConnector
53
 */
K
kohsuke 已提交
54
public abstract class ComputerLauncher extends AbstractDescribableImpl<ComputerLauncher> implements ExtensionPoint {
55 56
    /**
     * Returns true if this {@link ComputerLauncher} supports
R
René Scheibe 已提交
57
     * programmatic launch of the agent in the target {@link Computer}.
58 59 60 61 62 63
     */
    public boolean isLaunchSupported() {
        return true;
    }

    /**
64
     * Launches the agent for the given {@link Computer}.
65 66
     *
     * <p>
67
     * If the agent is launched successfully, {@link SlaveComputer#setChannel(InputStream, OutputStream, TaskListener, Channel.Listener)}
68 69 70
     * should be invoked in the end to notify Hudson of the established connection.
     * The operation could also fail, in which case there's no need to make any callback notification,
     * (except to notify the user of the failure through {@link StreamTaskListener}.)
K
Kohsuke Kawaguchi 已提交
71 72 73
     * Also note that the normal return of this method call does not necessarily signify a successful launch.
     * If someone programmatically calls this method and wants to find out if the launch was a success,
     * use {@link SlaveComputer#isOnline()} at the end.
74
     *
75 76 77 78
     * <p>
     * This method must operate synchronously. Asynchrony is provided by {@link Computer#connect(boolean)} and
     * its correct operation depends on this. 
     *
79 80
     * @param listener
     *      The progress of the launch, as well as any error, should be sent to this listener.
K
kohsuke 已提交
81 82 83 84
     *
     * @throws IOException
     *      if the method throws an {@link IOException} or {@link InterruptedException}, the launch was considered
     *      a failure and the stack trace is reported into the listener. This handling is just so that the implementation
85
     *      of this method doesn't have to diligently catch those exceptions.
86
     */
87
    public void launch(SlaveComputer computer, TaskListener listener) throws IOException , InterruptedException {
K
kohsuke 已提交
88
        // to remain compatible with the legacy implementation that overrides the old signature
89 90 91 92 93 94 95
        launch(computer,cast(listener));
    }

    /**
     * @deprecated as of 1.304
     *  Use {@link #launch(SlaveComputer, TaskListener)}
     */
96
    @Deprecated
97 98 99
    public void launch(SlaveComputer computer, StreamTaskListener listener) throws IOException , InterruptedException {
        throw new UnsupportedOperationException(getClass()+" must implement the launch method");
    }
100 101 102

    /**
     * Allows the {@link ComputerLauncher} to tidy-up after a disconnect.
K
kohsuke 已提交
103 104 105
     *
     * <p>
     * This method is invoked after the {@link Channel} to this computer is terminated.
K
kohsuke 已提交
106 107 108 109 110
     *
     * <p>
     * Disconnect operation is performed asynchronously, so there's no guarantee
     * that the corresponding {@link SlaveComputer} exists for the duration of the
     * operation.
111
     */
112
    public void afterDisconnect(SlaveComputer computer, TaskListener listener) {
K
kohsuke 已提交
113
        // to remain compatible with the legacy implementation that overrides the old signature
114 115 116 117 118 119 120
        afterDisconnect(computer,cast(listener));
    }

    /**
     * @deprecated as of 1.304
     *  Use {@link #afterDisconnect(SlaveComputer, TaskListener)}
     */
121
    @Deprecated
122 123 124 125 126
    public void afterDisconnect(SlaveComputer computer, StreamTaskListener listener) {
    }

    /**
     * Allows the {@link ComputerLauncher} to prepare for a disconnect.
K
kohsuke 已提交
127 128
     *
     * <p>
K
kohsuke 已提交
129 130 131 132
     * This method is invoked before the {@link Channel} to this computer is terminated,
     * thus the channel is still accessible from {@link SlaveComputer#getChannel()}.
     * If the channel is terminated unexpectedly, this method will not be invoked,
     * as the channel is already gone.
K
kohsuke 已提交
133 134 135 136 137
     *
     * <p>
     * Disconnect operation is performed asynchronously, so there's no guarantee
     * that the corresponding {@link SlaveComputer} exists for the duration of the
     * operation.
138
     */
139
    public void beforeDisconnect(SlaveComputer computer, TaskListener listener) {
K
kohsuke 已提交
140
        // to remain compatible with the legacy implementation that overrides the old signature
141 142 143 144 145 146 147
        beforeDisconnect(computer,cast(listener));
    }

    /**
     * @deprecated as of 1.304
     *  Use {@link #beforeDisconnect(SlaveComputer, TaskListener)} 
     */
148
    @Deprecated
149 150 151
    public void beforeDisconnect(SlaveComputer computer, StreamTaskListener listener) {
    }

152 153 154 155 156 157
    private StreamTaskListener cast(TaskListener listener) {
        if (listener instanceof StreamTaskListener)
            return (StreamTaskListener) listener;
        return new StreamTaskListener(listener.getLogger());
    }

158 159
    /**
     * All registered {@link ComputerLauncher} implementations.
160 161 162
     *
     * @deprecated as of 1.281
     *      Use {@link Extension} for registration, and use
163
     *      {@link jenkins.model.Jenkins#getDescriptorList(Class)} for read access.
164
     */
165
    @Deprecated
166
    public static final DescriptorList<ComputerLauncher> LIST = new DescriptorList<>(ComputerLauncher.class);
167 168

    /**
169
     * Given the output of "java -version" in {@code r}, determine if this
170 171 172 173 174 175 176 177 178
     * version of Java is supported, or throw {@link IOException}.
     *
     * @param logger
     *            where to log the output
     * @param javaCommand
     *            the command executed, used for logging
     * @param r
     *            the output of "java -version"
     */
J
Jesse Glick 已提交
179
    protected static void checkJavaVersion(final PrintStream logger, String javaCommand,
180 181 182
                                    final BufferedReader r)
            throws IOException {
        String line;
183
        Pattern p = Pattern.compile("(?i)(?:java|openjdk) version \"([0-9.]+).*\".*");
184
        while (null != (line = r.readLine())) {
185 186 187
            Matcher m = p.matcher(line);
            if (m.matches()) {
                final String versionStr = m.group(1);
188 189
                logger.println(Messages.ComputerLauncher_JavaVersionResult(javaCommand, versionStr));
                try {
190
                    if (new DeweyDecimal(versionStr).isLessThan(new DeweyDecimal("1.8"))) {
191 192 193
                        throw new IOException(Messages
                                .ComputerLauncher_NoJavaFound(line));
                    }
194
                } catch (NumberFormatException x) {
195 196 197 198 199
                    throw new IOException(Messages.ComputerLauncher_NoJavaFound(line));
                }
                return;
            }
        }
200 201
        logger.println(Messages.ComputerLauncher_UnknownJavaVersion(javaCommand));
        throw new IOException(Messages.ComputerLauncher_UnknownJavaVersion(javaCommand));
202
    }
203
}