From 82379c90d1ff11d9fe2cc822ed5760a00a3e3968 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Fri, 12 Oct 2012 09:40:40 -0700 Subject: [PATCH] Moving old slave log files to their new locations ... based on Jesse's feedback to 1f83338dea89a3fd7248fd8cb371978c9fd751a1 --- core/src/main/java/hudson/model/Computer.java | 41 +++++++++++++++++++ .../test/java/hudson/model/ComputerTest.java | 31 ++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 core/src/test/java/hudson/model/ComputerTest.java diff --git a/core/src/main/java/hudson/model/Computer.java b/core/src/main/java/hudson/model/Computer.java index 2bb671a7a4..ad8cfdb783 100644 --- a/core/src/main/java/hudson/model/Computer.java +++ b/core/src/main/java/hudson/model/Computer.java @@ -29,6 +29,8 @@ import hudson.Util; import hudson.cli.declarative.CLIMethod; import hudson.cli.declarative.CLIResolver; import hudson.console.AnnotatedLargeText; +import hudson.init.InitMilestone; +import hudson.init.Initializer; import hudson.model.Descriptor.FormException; import hudson.model.labels.LabelAtom; import hudson.model.queue.WorkUnit; @@ -73,6 +75,7 @@ import org.kohsuke.stapler.interceptor.RequirePOST; import javax.servlet.ServletException; import java.io.File; +import java.io.FilenameFilter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; @@ -89,6 +92,8 @@ import java.nio.charset.Charset; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.Inet4Address; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.annotation.CheckForNull; import static javax.servlet.http.HttpServletResponse.*; @@ -191,6 +196,8 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces /** * This is where the log from the remote agent goes. + * + * @see #relocateOldLogs() */ protected File getLogFile() { File dir = new File(Jenkins.getInstance().getRootDir(),"logs/slaves/"+nodeName); @@ -1250,6 +1257,40 @@ public /*transient*/ abstract class Computer extends Actionable implements Acces return item; } + /** + * Relocate log files in the old location to the new location. + * + * Files were used to be $JENKINS_ROOT/slave-NAME.log (and .1, .2, ...) + * but now they are at $JENKINS_ROOT/logs/slaves/NAME/slave.log (and .1, .2, ...) + * + * @see #getLogFile() + */ + @Initializer + public static void relocateOldLogs() { + relocateOldLogs(Jenkins.getInstance().getRootDir()); + } + + /*package*/ static void relocateOldLogs(File dir) { + final Pattern logfile = Pattern.compile("slave-(.*)\\.log(\\.[0-9]+)?"); + File[] logfiles = dir.listFiles(new FilenameFilter() { + public boolean accept(File dir, String name) { + return logfile.matcher(name).matches(); + } + }); + if (logfiles==null) return; + + for (File f : logfiles) { + Matcher m = logfile.matcher(f.getName()); + if (m.matches()) { + File newLocation = new File(dir, "logs/slaves/" + m.group(1) + "/slave.log" + Util.fixNull(m.group(2))); + newLocation.getParentFile().mkdirs(); + f.renameTo(newLocation); + } else { + assert false; + } + } + } + public static final PermissionGroup PERMISSIONS = new PermissionGroup(Computer.class,Messages._Computer_Permissions_Title()); /** * Permission to configure slaves. diff --git a/core/src/test/java/hudson/model/ComputerTest.java b/core/src/test/java/hudson/model/ComputerTest.java new file mode 100644 index 0000000000..6a87193f3f --- /dev/null +++ b/core/src/test/java/hudson/model/ComputerTest.java @@ -0,0 +1,31 @@ +package hudson.model; + +import hudson.FilePath; +import org.junit.Test; + +import java.io.File; + +/** + * @author Kohsuke Kawaguchi + */ +public class ComputerTest { + @Test + public void testRelocate() throws Exception { + File d = File.createTempFile("jenkins", "test"); + FilePath dir = new FilePath(d); + try { + dir.delete(); + dir.mkdirs(); + dir.child("slave-abc.log").touch(0); + dir.child("slave-def.log.5").touch(0); + + Computer.relocateOldLogs(d); + + assert dir.list().size()==1; // asserting later that this one child is the logs/ directory + assert dir.child("logs/slaves/abc/slave.log").exists(); + assert dir.child("logs/slaves/def/slave.log.5").exists(); + } finally { + dir.deleteRecursive(); + } + } +}