Skip to content

Commit

Permalink
ZNTA-1856 - fix logging on jenkins slave
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Apr 6, 2017
1 parent 2a2ae3a commit 796a1b7
Showing 1 changed file with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
/*
* Copyright 2016, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jenkinsci.plugins.zanata.zanatareposync;

import java.io.File;
Expand Down Expand Up @@ -54,9 +74,11 @@
import jenkins.tasks.SimpleBuildStep;

/**
* Zanata builder that uses the bundled zanata client commands classes.
* A build step that uses the bundled zanata client commands classes to perform
* zanata push and pull.
*
* @author Patrick Huang <a href="mailto:[email protected]">[email protected]</a>
* @author Patrick Huang
* <a href="mailto:[email protected]">[email protected]</a>
*/
public class ZanataSyncStep extends Builder implements SimpleBuildStep,
HasSyncJobDetail {
Expand Down Expand Up @@ -165,7 +187,6 @@ public void setPullFromZanata(boolean pullFromZanata) {
public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener)
throws IOException {
// This is where you 'build' the project.
Handler logHandler = configLogger(listener.getLogger());

StandardUsernameCredentials cred = CredentialsProvider.findCredentialById(zanataCredentialsId, StandardUsernameCredentials.class, build);
if (cred == null) {
Expand All @@ -187,20 +208,18 @@ public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, Task

try {
if (pushToZanata) {
pushToZanata(workspace, zanataSyncService);
pushToZanata(workspace, zanataSyncService, listener);
}
if (pullFromZanata) {
Git git =
Git.with(listener, new EnvVars(EnvVars.masterEnvVars));
GitSyncService
gitSyncService = new GitSyncService(this, git);
pullFromZanata(workspace, zanataSyncService, gitSyncService);
pullFromZanata(workspace, zanataSyncService, gitSyncService, listener);
}
} catch (IOException | InterruptedException e) {
logger(listener).println("Zanata Sync failed:" + e.getMessage());
throw new RuntimeException(e);
} finally {
removeLogger(logHandler);
}
}

Expand All @@ -220,20 +239,29 @@ private static Handler configLogger(PrintStream printStream) {
return loggerHandler;
}

private static void removeLogger(Handler appender) {
java.util.logging.Logger.getLogger("org.zanata").removeHandler(appender);
private static void removeLogger(Handler handler) {
if (handler != null) {
java.util.logging.Logger.getLogger("org.zanata").removeHandler(handler);
}
}

private static void pullFromZanata(FilePath workspace,
final ZanataSyncServiceImpl service, GitSyncService gitSyncService)
final ZanataSyncServiceImpl service, GitSyncService gitSyncService,
TaskListener listener)
throws IOException, InterruptedException {
workspace.act(new FilePath.FileCallable<Void>() {

@Override
public Void invoke(File f, VirtualChannel channel)
throws IOException, InterruptedException {
service.pullFromZanata(f.toPath());
gitSyncService.syncTranslationToRepo(f.toPath());
Handler handler = null;
try {
handler = configLogger(listener.getLogger());
service.pullFromZanata(f.toPath());
gitSyncService.syncTranslationToRepo(f.toPath());
} finally {
removeLogger(handler);
}
return null;
}

Expand All @@ -245,14 +273,19 @@ public void checkRoles(RoleChecker roleChecker)
}

private static void pushToZanata(FilePath workspace,
final ZanataSyncServiceImpl service)
final ZanataSyncServiceImpl service, TaskListener listener)
throws IOException, InterruptedException {
workspace.act(new FilePath.FileCallable<Void>() {
@Override
public Void invoke(File f, VirtualChannel channel)
throws IOException, InterruptedException {

service.pushToZanata(f.toPath());
Handler handler = null;
try {
handler = configLogger(listener.getLogger());
service.pushToZanata(f.toPath());
} finally {
removeLogger(handler);
}
return null;
}

Expand Down

0 comments on commit 796a1b7

Please sign in to comment.