Search in sources :

Example 11 with FilePath

use of hudson.FilePath in project hudson-2.x by hudson.

the class AbstractBuild method getEnvironment.

@Override
public EnvVars getEnvironment(TaskListener log) throws IOException, InterruptedException {
    EnvVars env = super.getEnvironment(log);
    FilePath ws = getWorkspace();
    if (// if this is done very early on in the build, workspace may not be decided yet. see HUDSON-3997
    ws != null)
        env.put("WORKSPACE", ws.getRemote());
    // servlet container may have set CLASSPATH in its launch script,
    // so don't let that inherit to the new child process.
    // see http://www.nabble.com/Run-Job-with-JDK-1.4.2-tf4468601.html
    env.put("CLASSPATH", "");
    JDK jdk = project.getJDK();
    if (jdk != null) {
        Computer computer = Computer.currentComputer();
        if (computer != null) {
            // just in case were not in a build
            jdk = jdk.forNode(computer.getNode(), log);
        }
        jdk.buildEnvVars(env);
    }
    project.getScm().buildEnvVars(this, env);
    if (buildEnvironments != null)
        for (Environment e : buildEnvironments) e.buildEnvVars(env);
    for (EnvironmentContributingAction a : Util.filter(getActions(), EnvironmentContributingAction.class)) a.buildEnvVars(this, env);
    EnvVars.resolve(env);
    return env;
}
Also used : FilePath(hudson.FilePath) EnvVars(hudson.EnvVars)

Example 12 with FilePath

use of hudson.FilePath in project hudson-2.x by hudson.

the class WindowsServiceLifecycle method updateHudsonExeIfNeeded.

/**
     * If <tt>hudson.exe</tt> is old compared to our copy,
     * schedule an overwrite (except that since it's currently running,
     * we can only do it when Hudson restarts next time.)
     */
private void updateHudsonExeIfNeeded() {
    try {
        File rootDir = Hudson.getInstance().getRootDir();
        URL exe = getClass().getResource("/windows-service/hudson.exe");
        String ourCopy = Util.getDigestOf(exe.openStream());
        File currentCopy = new File(rootDir, "hudson.exe");
        if (!currentCopy.exists())
            return;
        String curCopy = new FilePath(currentCopy).digest();
        if (ourCopy.equals(curCopy))
            // identical
            return;
        File stage = new File(rootDir, "hudson.exe.new");
        FileUtils.copyURLToFile(exe, stage);
        Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(), currentCopy.getAbsolutePath(), MOVEFILE_DELAY_UNTIL_REBOOT | MOVEFILE_REPLACE_EXISTING);
        LOGGER.info("Scheduled a replacement of hudson.exe");
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe", e);
    } catch (InterruptedException e) {
        LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe", e);
    }
}
Also used : FilePath(hudson.FilePath) IOException(java.io.IOException) File(java.io.File) URL(java.net.URL)

Example 13 with FilePath

use of hudson.FilePath in project hudson-2.x by hudson.

the class FileParameterDefinition method createValue.

@Override
public ParameterValue createValue(CLICommand command, String value) throws IOException, InterruptedException {
    // capture the file to the server
    FilePath src = new FilePath(command.channel, value);
    File local = File.createTempFile("hudson", "parameter");
    src.copyTo(new FilePath(local));
    FileParameterValue p = new FileParameterValue(getName(), local, src.getName());
    p.setDescription(getDescription());
    p.setLocation(getName());
    return p;
}
Also used : FilePath(hudson.FilePath) File(java.io.File)

Example 14 with FilePath

use of hudson.FilePath in project hudson-2.x by hudson.

the class CommandInterpreter method perform.

public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, TaskListener listener) throws InterruptedException {
    FilePath ws = build.getWorkspace();
    FilePath script = null;
    try {
        try {
            script = createScriptFile(ws);
        } catch (IOException e) {
            Util.displayIOException(e, listener);
            e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_UnableToProduceScript()));
            return false;
        }
        int r;
        try {
            EnvVars envVars = build.getEnvironment(listener);
            // convert variables to all upper cases.
            for (Map.Entry<String, String> e : build.getBuildVariables().entrySet()) envVars.put(e.getKey(), e.getValue());
            r = launcher.launch().cmds(buildCommandLine(script)).envs(envVars).stdout(listener).pwd(ws).join();
        } catch (IOException e) {
            Util.displayIOException(e, listener);
            e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_CommandFailed()));
            r = -1;
        }
        return r == 0;
    } finally {
        try {
            if (script != null)
                script.delete();
        } catch (IOException e) {
            Util.displayIOException(e, listener);
            e.printStackTrace(listener.fatalError(Messages.CommandInterpreter_UnableToDelete(script)));
        }
    }
}
Also used : FilePath(hudson.FilePath) EnvVars(hudson.EnvVars) IOException(java.io.IOException) Map(java.util.Map)

Example 15 with FilePath

use of hudson.FilePath in project hudson-2.x by hudson.

the class Fingerprinter method record.

private void record(AbstractBuild<?, ?> build, BuildListener listener, Map<String, String> record, final String targets) throws IOException, InterruptedException {
    final class Record implements Serializable {

        final boolean produced;

        final String relativePath;

        final String fileName;

        final String md5sum;

        public Record(boolean produced, String relativePath, String fileName, String md5sum) {
            this.produced = produced;
            this.relativePath = relativePath;
            this.fileName = fileName;
            this.md5sum = md5sum;
        }

        Fingerprint addRecord(AbstractBuild build) throws IOException {
            FingerprintMap map = Hudson.getInstance().getFingerprintMap();
            return map.getOrCreate(produced ? build : null, fileName, md5sum);
        }

        private static final long serialVersionUID = 1L;
    }
    final long buildTimestamp = build.getTimeInMillis();
    FilePath ws = build.getWorkspace();
    if (ws == null) {
        listener.error(Messages.Fingerprinter_NoWorkspace());
        build.setResult(Result.FAILURE);
        return;
    }
    List<Record> records = ws.act(new FileCallable<List<Record>>() {

        public List<Record> invoke(File baseDir, VirtualChannel channel) throws IOException {
            List<Record> results = new ArrayList<Record>();
            FileSet src = Util.createFileSet(baseDir, targets);
            DirectoryScanner ds = src.getDirectoryScanner();
            for (String f : ds.getIncludedFiles()) {
                File file = new File(baseDir, f);
                // consider the file to be produced by this build only if the timestamp
                // is newer than when the build has started.
                // 2000ms is an error margin since since VFAT only retains timestamp at 2sec precision
                boolean produced = buildTimestamp <= file.lastModified() + 2000;
                try {
                    results.add(new Record(produced, f, file.getName(), new FilePath(file).digest()));
                } catch (IOException e) {
                    throw new IOException2(Messages.Fingerprinter_DigestFailed(file), e);
                } catch (InterruptedException e) {
                    throw new IOException2(Messages.Fingerprinter_Aborted(), e);
                }
            }
            return results;
        }
    });
    for (Record r : records) {
        Fingerprint fp = r.addRecord(build);
        if (fp == null) {
            listener.error(Messages.Fingerprinter_FailedFor(r.relativePath));
            continue;
        }
        fp.add(build);
        record.put(r.relativePath, fp.getHashString());
    }
}
Also used : FilePath(hudson.FilePath) Serializable(java.io.Serializable) Fingerprint(hudson.model.Fingerprint) AbstractBuild(hudson.model.AbstractBuild) FileSet(org.apache.tools.ant.types.FileSet) VirtualChannel(hudson.remoting.VirtualChannel) IOException(java.io.IOException) FingerprintMap(hudson.model.FingerprintMap) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) IOException2(hudson.util.IOException2)

Aggregations

FilePath (hudson.FilePath)38 IOException (java.io.IOException)17 File (java.io.File)12 URL (java.net.URL)5 EnvVars (hudson.EnvVars)4 AbortException (hudson.AbortException)3 Launcher (hudson.Launcher)3 PrintStream (java.io.PrintStream)3 List (java.util.List)3 BuildListener (hudson.model.BuildListener)2 VirtualChannel (hudson.remoting.VirtualChannel)2 NullSCM (hudson.scm.NullSCM)2 SCM (hudson.scm.SCM)2 WorkspaceList (hudson.slaves.WorkspaceList)2 ArgumentListBuilder (hudson.util.ArgumentListBuilder)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ImmutableList (com.google.common.collect.ImmutableList)1 WindowsSlaveInstaller (hudson.lifecycle.WindowsSlaveInstaller)1 MavenModuleSetBuild (hudson.maven.MavenModuleSetBuild)1