Skip to content

Commit

Permalink
[FIXED JENKINS-12823] Clean up temporary config files at the very end…
Browse files Browse the repository at this point in the history
…, not before post-build
  • Loading branch information
imod committed Apr 15, 2012
1 parent 79a9f22 commit 4236d26
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 209 deletions.
@@ -0,0 +1,26 @@
package org.jenkinsci.plugins.configfiles.buildwrapper;

import hudson.FilePath;
import hudson.model.InvisibleAction;

import java.util.ArrayList;
import java.util.List;

/**
* Temporal action to transport information about t files to be deleted to the CleanTempFilesRunListener.
*
* @author Dominik Bartholdi (imod)
* @see org.jenkinsci.plugins.configfiles.buildwrapper.CleanTempFilesRunListener
*/
public class CleanTempFilesAction extends InvisibleAction {

/**
* list of the temp files to be removed - never <code>null</code>
*/
public List<FilePath> tempFiles;

public CleanTempFilesAction(List<FilePath> tempFiles) {
this.tempFiles = tempFiles == null ? new ArrayList<FilePath>() : tempFiles;
}

}
@@ -0,0 +1,40 @@
/**
*
*/
package org.jenkinsci.plugins.configfiles.buildwrapper;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.listeners.RunListener;


/**
* Removes the temporarily created files at 'onComplete()' of each build, doing it at this state, ensures the files are available also for publishers.
*
* @author Dominik Bartholdi (imod)
*/
@Extension
public class CleanTempFilesRunListener extends RunListener<AbstractBuild<?, ?>> {

@Override
public void onCompleted(AbstractBuild<?, ?> build, TaskListener listener) {
CleanTempFilesAction action = build.getAction(CleanTempFilesAction.class);

if (action != null) {
for (FilePath fp : action.tempFiles) {
try {
if (fp != null && fp.exists()) {
fp.delete();
}
} catch (Exception e) {
listener.getLogger().println("[WARN] failed to delete temp file: " + fp.getRemote());
}
}
// remove the action, there is nothing we want to persist on the build
build.getActions().remove(action);
}
}

}
Expand Up @@ -37,77 +37,103 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.kohsuke.stapler.DataBoundConstructor;

public class ConfigFileBuildWrapper extends BuildWrapper {

private List<ManagedFile> managedFiles = new ArrayList<ManagedFile>();

@DataBoundConstructor
public ConfigFileBuildWrapper(List<ManagedFile> managedFiles) {
this.managedFiles = managedFiles;
}

@Override
public Environment setUp(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException,
InterruptedException {

final PrintStream logger = listener.getLogger();

if (build.getWorkspace() == null) {
throw new IllegalStateException("the workspace does not yet exist, can't provision config files - maybe slave is offline?");
}

final Map<ManagedFile, FilePath> file2Path = ManagedFileUtil.provisionConfigFiles(managedFiles, build.getWorkspace(), logger);
final hudson.model.Environment managedFileEnv = new ManagedFilesEnvironment(file2Path);

return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
managedFileEnv.buildEnvVars(env);
}

@Override
public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild build, BuildListener listener) throws IOException, InterruptedException {
return managedFileEnv.tearDown(build, listener);
}
};
}

public List<ManagedFile> getManagedFiles() {
return managedFiles;
}

/**
* Descriptor for {@link ExclusiveBuildWrapper}. Used as a singleton. The
* class is marked as public so that it can be accessed from views.
*/
@Extension(ordinal = 50)
public static final class DescriptorImpl extends BuildWrapperDescriptor {
@Override
public String getDisplayName() {
return Messages.display_name();
}

@Override
public boolean isApplicable(AbstractProject<?, ?> item) {
return true;
}

public Collection<Config> getConfigFiles() {
ExtensionList<ConfigProvider> providers = ConfigProvider.all();
List<Config> allFiles = new ArrayList<Config>();
for (ConfigProvider provider : providers) {
allFiles.addAll(provider.getAllConfigs());
}
return allFiles;
}

}
private List<ManagedFile> managedFiles = new ArrayList<ManagedFile>();

@DataBoundConstructor
public ConfigFileBuildWrapper(List<ManagedFile> managedFiles) {
this.managedFiles = managedFiles;
}

@Override
public Environment setUp(@SuppressWarnings("rawtypes") AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException,
InterruptedException {

final PrintStream logger = listener.getLogger();

if (build.getWorkspace() == null) {
throw new IllegalStateException("the workspace does not yet exist, can't provision config files - maybe slave is offline?");
}

final Map<ManagedFile, FilePath> file2Path = ManagedFileUtil.provisionConfigFiles(managedFiles, build.getWorkspace(), logger);
final ManagedFilesEnvironment env = new ManagedFilesEnvironment(file2Path);
// Temporarily attach info about the files to be deleted to the build - this action gets removed from the build again by 'org.jenkinsci.plugins.configfiles.listener.CleanTempFilesRunListener'
build.addAction(new CleanTempFilesAction(env.getTempFiles()));
return env;
}

public List<ManagedFile> getManagedFiles() {
return managedFiles;
}

@Extension(ordinal = 50)
public static final class DescriptorImpl extends BuildWrapperDescriptor {
@Override
public String getDisplayName() {
return Messages.display_name();
}

@Override
public boolean isApplicable(AbstractProject<?, ?> item) {
return true;
}

public Collection<Config> getConfigFiles() {
ExtensionList<ConfigProvider> providers = ConfigProvider.all();
List<Config> allFiles = new ArrayList<Config>();
for (ConfigProvider provider : providers) {
allFiles.addAll(provider.getAllConfigs());
}
return allFiles;
}

}

public class ManagedFilesEnvironment extends Environment {
private final Map<ManagedFile, FilePath> file2Path;

public ManagedFilesEnvironment(Map<ManagedFile, FilePath> file2Path) {
this.file2Path = file2Path == null ? new HashMap<ManagedFile, FilePath>() : file2Path;
}

@Override
public void buildEnvVars(Map<String, String> env) {
System.out.println("ConfigFileBuildWrapper.ManagedFilesEnvironment.buildEnvVars()");
for (Map.Entry<ManagedFile, FilePath> entry : file2Path.entrySet()) {
ManagedFile mf = entry.getKey();
FilePath fp = entry.getValue();
if (!StringUtils.isBlank(mf.variable)) {
env.put(mf.variable, fp.getRemote());
}
}
}

/**
* Provides access to the files which have to be removed after the build
*
* @return a list of temp files
*/
public List<FilePath> getTempFiles() {
List<FilePath> tempFiles = new ArrayList<FilePath>();
for (Entry<ManagedFile, FilePath> entry : file2Path.entrySet()) {
boolean noTargetGiven = StringUtils.isBlank(entry.getKey().targetLocation);
if (noTargetGiven) {
tempFiles.add(entry.getValue());
}
}
return tempFiles;
}
}

}

0 comments on commit 4236d26

Please sign in to comment.