Skip to content
Permalink
Browse files

[JENKINS-13153] - Use directory from env:BASE when writing jenkins.co…

…pies (#2992)

[JENKINS-13153] - Use directory from env:BASE when writing jenkins.copies
  • Loading branch information...
hplatou authored and oleg-nenashev committed Sep 2, 2017
1 parent 8189c2c commit 0efdf8fb4f8c56f1f32fb390c472cb2e98e67f56
Showing with 18 additions and 5 deletions.
  1. +18 −5 core/src/main/java/hudson/lifecycle/WindowsServiceLifecycle.java
@@ -60,20 +60,20 @@ public WindowsServiceLifecycle() {
*/
private void updateJenkinsExeIfNeeded() {
try {
File rootDir = Jenkins.getInstance().getRootDir();
File baseDir = getBaseDir();

URL exe = getClass().getResource("/windows-service/jenkins.exe");
String ourCopy = Util.getDigestOf(exe.openStream());

for (String name : new String[]{"hudson.exe","jenkins.exe"}) {
try {
File currentCopy = new File(rootDir,name);
File currentCopy = new File(baseDir,name);
if(!currentCopy.exists()) continue;
String curCopy = new FilePath(currentCopy).digest();

if(ourCopy.equals(curCopy)) continue; // identical

File stage = new File(rootDir,name+".new");
File stage = new File(baseDir,name+".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 "+name);
@@ -107,8 +107,8 @@ public void rewriteHudsonWar(File by) throws IOException {
String baseName = dest.getName();
baseName = baseName.substring(0,baseName.indexOf('.'));

File rootDir = Jenkins.getInstance().getRootDir();
File copyFiles = new File(rootDir,baseName+".copies");
File baseDir = getBaseDir();
File copyFiles = new File(baseDir,baseName+".copies");

try (FileWriter w = new FileWriter(copyFiles, true)) {
w.write(by.getAbsolutePath() + '>' + getHudsonWar().getAbsolutePath() + '\n');
@@ -144,6 +144,19 @@ public void restart() throws IOException, InterruptedException {
if(r!=0)
throw new IOException(baos.toString());
}

private static final File getBaseDir() {
File baseDir;

String baseEnv = System.getenv("BASE");
if (baseEnv != null) {
baseDir = new File(baseEnv);
} else {
LOGGER.log(Level.WARNING, "Could not find environment variable 'BASE' for Jenkins base directory. Falling back to JENKINS_HOME");
baseDir = Jenkins.getInstance().getRootDir();
}
return baseDir;
}

private static final Logger LOGGER = Logger.getLogger(WindowsServiceLifecycle.class.getName());
}

0 comments on commit 0efdf8f

Please sign in to comment.
You can’t perform that action at this time.