Skip to content

Commit

Permalink
Merge pull request #11 from jenkinsci/ZNTA-1856
Browse files Browse the repository at this point in the history
ZNTA-1856 - fix logging on jenkins slave
  • Loading branch information
Patrick Huang authored Apr 6, 2017
2 parents 2a2ae3a + 85e2550 commit 0e7d62a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,13 @@
<version>4.1.0</version>
<exclusions>
<exclusion>
<!-- jenkins uses JUL!! -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<!-- jenkins uses JUL!! this better match the version used by jenkins -->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.7</version>
</dependency>

<!-- depend on other plugins -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
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,21 @@ 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);
}
if (!pushToZanata && !pullFromZanata) {
logger(listener).println("Both push and pull are disabled. Nothing to do.");
}
} catch (IOException | InterruptedException e) {
logger(listener).println("Zanata Sync failed:" + e.getMessage());
throw new RuntimeException(e);
} finally {
removeLogger(logHandler);
}
}

Expand All @@ -220,20 +242,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 +276,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 0e7d62a

Please sign in to comment.