Search in sources :

Example 1 with ProcStarter

use of hudson.Launcher.ProcStarter in project nodejs-plugin by jenkinsci.

the class NodeJSInstaller method installIfNecessaryMSI.

private boolean installIfNecessaryMSI(FilePath expected, URL archive, TaskListener listener, String message) throws IOException, InterruptedException {
    try {
        URLConnection con;
        try {
            con = ProxyConfiguration.open(archive);
            con.connect();
        } catch (IOException x) {
            if (expected.exists()) {
                // Cannot connect now, so assume whatever was last unpacked is still OK.
                if (listener != null) {
                    listener.getLogger().println("Skipping installation of " + archive + " to " + expected.getRemote() + ": " + x);
                }
                return false;
            } else {
                throw x;
            }
        }
        long sourceTimestamp = con.getLastModified();
        FilePath timestamp = expected.child(".timestamp");
        if (expected.exists()) {
            if (timestamp.exists() && sourceTimestamp == timestamp.lastModified()) {
                // already up to date
                return false;
            }
            expected.deleteContents();
        } else {
            expected.mkdirs();
        }
        if (listener != null) {
            listener.getLogger().println(message);
        }
        FilePath temp = expected.createTempDir("_temp", "");
        FilePath msi = temp.child("nodejs.msi");
        msi.copyFrom(archive);
        try {
            Launcher launch = temp.createLauncher(listener);
            ProcStarter starter = launch.launch().cmds(new File("cmd"), "/c", "for %A in (.) do msiexec TARGETDIR=\"%~sA\" /a " + temp.getName() + "\\nodejs.msi /qn /L* " + temp.getName() + "\\log.txt");
            starter = starter.pwd(expected);
            int exitCode = starter.join();
            if (exitCode != 0) {
                throw new IOException("msiexec failed. exit code: " + exitCode + " Please see the log file " + temp.child("log.txt").getRemote() + " for more informations.", null);
            }
            if (listener != null) {
                listener.getLogger().println("msi install complete");
            }
            // remove temporary folder
            temp.deleteRecursive();
            // remove the double msi file in expected folder
            FilePath duplicatedMSI = expected.child("nodejs.msi");
            if (duplicatedMSI.exists()) {
                duplicatedMSI.delete();
            }
        } catch (IOException e) {
            throw new IOException("Failed to install " + archive, e);
        }
        timestamp.touch(sourceTimestamp);
        return true;
    } catch (IOException e) {
        throw new IOException("Failed to install " + archive + " to " + expected.getRemote(), e);
    }
}
Also used : FilePath(hudson.FilePath) Launcher(hudson.Launcher) ProcStarter(hudson.Launcher.ProcStarter) IOException(java.io.IOException) File(java.io.File) URLConnection(java.net.URLConnection)

Aggregations

FilePath (hudson.FilePath)1 Launcher (hudson.Launcher)1 ProcStarter (hudson.Launcher.ProcStarter)1 File (java.io.File)1 IOException (java.io.IOException)1 URLConnection (java.net.URLConnection)1