Skip to content

Commit

Permalink
Add logging to build log [JENKINS-31837].
Browse files Browse the repository at this point in the history
  • Loading branch information
candiduslynx committed Sep 6, 2016
1 parent 578b078 commit 56de262
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 42 deletions.
Expand Up @@ -2,6 +2,8 @@

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.*;
import hudson.model.BuildListener;

import java.io.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand All @@ -17,73 +19,76 @@
*
* @version 1.0
*/
public class zFTPConnector
class zFTPConnector
{
// Server info.
/**
* LPAR name or IP to connect to.
*/
protected String server;
private String server;
/**
* FTP port for connection
*/
protected int port;
private int port;

// Credentials.
/**
* UserID.
*/
protected String userID;
private String userID;
/**
* User password.
*/
protected String password;
private String password;

// Wait parameters.
/**
* Time to wait before giving up in milliseconds. If set to <code>0</code> will wait forever.
*/
protected long waitTime;
private long waitTime;

// Job info from JES-like system.
/**
* JobID in JES.
*/
protected String jobID;
private String jobID;
/**
* Jobname in JES.
*/
protected String jobName;
private String jobName;
/**
* Job's MaxCC.
*/
protected String jobCC;
private String jobCC;

// JESINTERFACELEVEL=1
protected boolean JESINTERFACELEVEL1;
private boolean JESINTERFACELEVEL1;

// Work elements.
/**
* Will ask LPAR once in 10 seconds.
*/
protected static final long waitInterval = 10*1000;
private static final long waitInterval = 10*1000;
/**
* FTPClient from <i>Apache Commons-Net</i>. Used for FTP communication.
*/
protected FTPClient FTPClient;
private FTPClient FTPClient;
/**
* Log prefix (default: "zFTPConnector")
*/
protected String logPrefix;
private String logPrefix;
/**
* Pattern for search of jobName
*/
protected static final Pattern JesJobName = Pattern.compile("250-It is known to JES as (.*)");
private static final Pattern JesJobName = Pattern.compile("250-It is known to JES as (.*)");
/**
* Simple logger.
*/
protected static final Logger logger = Logger.getLogger(zFTPConnector.class.getName());

private static final Logger logger = Logger.getLogger(zFTPConnector.class.getName());
/**
* Build listener (if provided).
*/
private BuildListener listener;

/**
* Basic constructor with minimal parameters required.
Expand All @@ -95,7 +100,7 @@ public class zFTPConnector
* @param JESINTERFACELEVEL1 Is FTP server configured for JESINTERFACELEVEL=1?
* @param logPrefix Log prefix.
*/
public zFTPConnector (String server, int port, String userID, String password, boolean JESINTERFACELEVEL1, String logPrefix)
public zFTPConnector(String server, int port, String userID, String password, boolean JESINTERFACELEVEL1, String logPrefix)
{
// Copy values
this.server = server;
Expand All @@ -112,7 +117,9 @@ public zFTPConnector (String server, int port, String userID, String password, b
this.logPrefix = "";
if (logPrefix != null)
this.logPrefix = logPrefix;
logger.info(logPrefix + "created zFTPConnector");
this.log("Created zFTPConnector");

this.listener = null;
}

/**
Expand All @@ -121,7 +128,7 @@ public zFTPConnector (String server, int port, String userID, String password, b
* @return Whether the connection was established using the parameters passed to the constructor.
* @see zFTPConnector#zFTPConnector(String, int, String, String, boolean, String)
*/
protected boolean connect()
private boolean connect()
{
// Perform the connection.
try
Expand All @@ -137,10 +144,10 @@ protected boolean connect()
{
// Bad reply code.
this.FTPClient.disconnect(); // Disconnect from LPAR.
logger.severe(logPrefix + "FTP server refused connection."); // Print error.
this.err("FTP server refused connection."); // Print error.
return false; // Finish with failure.
}
logger.info(logPrefix + "FTP: connected to " + server + ":" + port);
this.log("FTP: connected to " + server + ":" + port);
}
// IOException handling
catch (IOException e)
Expand All @@ -157,7 +164,7 @@ protected boolean connect()
// Do nothing
}
}
logger.severe(logPrefix + "Could not connect to server.");
this.err("Could not connect to server.");
e.printStackTrace();
return false;
}
Expand All @@ -174,7 +181,7 @@ protected boolean connect()
* @see zFTPConnector#zFTPConnector(String, int, String, String, boolean, String)
* @see zFTPConnector#connect()
*/
protected boolean logon()
private boolean logon()
{
// Check whether we are already connected. If not, try to reconnect.
if (!this.FTPClient.isConnected())
Expand All @@ -198,7 +205,7 @@ protected boolean logon()
reply = this.FTPClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.FTPClient.disconnect();
logger.severe(logPrefix + "FTP server refused to change FileType and JESJobName.");
this.err("FTP server refused to change FileType and JESJobName.");
return false;
}
} catch (IOException e) {
Expand All @@ -209,7 +216,7 @@ protected boolean logon()
// do nothing
}
}
logger.severe(logPrefix + "Could not connect to server.");
this.err("Could not connect to server.");
e.printStackTrace();
return false;
}
Expand All @@ -218,6 +225,12 @@ protected boolean logon()
return true;
}

boolean submit(InputStream inputStream, boolean wait, int waitTime, OutputStream outputStream, boolean deleteLogFromSpool, BuildListener buildListener)
{
this.listener = buildListener;
return this.submit(inputStream, wait, waitTime, outputStream, deleteLogFromSpool);
}

/**
* Submit job for execution.
*
Expand All @@ -235,7 +248,7 @@ protected boolean logon()
* @see zFTPConnector#waitForCompletion(OutputStream)
* @see zFTPConnector#deleteJobLog()
*/
public boolean submit(InputStream inputStream, boolean wait, int waitTime, OutputStream outputStream, boolean deleteLogFromSpool)
boolean submit(InputStream inputStream, boolean wait, int waitTime, OutputStream outputStream, boolean deleteLogFromSpool)
{
this.waitTime = ((long)waitTime) * 60 * 1000; // Minutes to milliseconds.

Expand Down Expand Up @@ -269,12 +282,12 @@ public boolean submit(InputStream inputStream, boolean wait, int waitTime, Outpu
break;
}
}
logger.info(logPrefix + "submitted job [" + this.jobID + "]");
inputStream.close();
this.log("Submitted job [" + this.jobID + "]");
inputStream.close();
}
catch (FTPConnectionClosedException e)
{
logger.severe(logPrefix + "Server closed connection.");
this.err("Server closed connection.");
e.printStackTrace();
this.jobCC = "SERVER_CLOSED_CONNECTION";
return false;
Expand Down Expand Up @@ -318,7 +331,7 @@ public boolean submit(InputStream inputStream, boolean wait, int waitTime, Outpu
* @see zFTPConnector#submit(InputStream, boolean, int, OutputStream, boolean)
* @see zFTPConnector#fetchJobLog(OutputStream)
*/
protected boolean waitForCompletion(OutputStream outputStream)
private boolean waitForCompletion(OutputStream outputStream)
{
// Initialize current time and estimated time.
long curr = System.currentTimeMillis();
Expand All @@ -334,7 +347,7 @@ protected boolean waitForCompletion(OutputStream outputStream)
Thread.sleep(waitInterval);
curr = System.currentTimeMillis();
} catch (InterruptedException e) {
logger.severe(logPrefix + "Interrupted.");
this.err("Interrupted.");
this.jobCC = "WAIT_INTERRUPTED";
return false;
}
Expand All @@ -358,7 +371,7 @@ protected boolean waitForCompletion(OutputStream outputStream)
/**
* @return true if job can be listed through FTP.
*/
protected boolean checkJobAvailability ()
private boolean checkJobAvailability()
{
// Verify connection.
if(!this.FTPClient.isConnected())
Expand All @@ -378,7 +391,7 @@ protected boolean checkJobAvailability ()
// Found our jobId
return true;
}
logger.severe(this.logPrefix + "Job [" + this.jobID + "] cannot be found in JES");
this.err("Job [" + this.jobID + "] cannot be found in JES");
this.jobCC = "JOB_NOT_FOUND_IN_JES";
return false;
}
Expand All @@ -397,7 +410,7 @@ protected boolean checkJobAvailability ()
*
* @see zFTPConnector#waitForCompletion(OutputStream)
*/
protected boolean fetchJobLog(OutputStream outputStream)
private boolean fetchJobLog(OutputStream outputStream)
{
// Verify connection.
if(!this.FTPClient.isConnected())
Expand Down Expand Up @@ -431,7 +444,7 @@ protected boolean fetchJobLog(OutputStream outputStream)
/**
* @return Whether job RC was correctly obtained or not.
*/
protected boolean obtainJobRC()
private boolean obtainJobRC()
{

if (this.JESINTERFACELEVEL1) {
Expand Down Expand Up @@ -503,7 +516,7 @@ protected boolean obtainJobRC()
*
* @see zFTPConnector#submit(InputStream, boolean, int, OutputStream, boolean)
*/
protected void deleteJobLog ()
private void deleteJobLog()
{
// Verify connection.
if(!this.FTPClient.isConnected())
Expand All @@ -530,23 +543,44 @@ protected void deleteJobLog ()
*
* @return Current <b><code>jobID</code></b>.
*/
public String getJobID() {
String getJobID() {
return this.jobID;
}
/**
* Get Jobname.
*
* @return Current <b><code>jobName</code></b>.
*/
public String getJobName() {
String getJobName() {
return this.jobName;
}
/**
* Get JobCC.
*
* @return Current <b><code>jobCC</code></b>.
*/
public String getJobCC() {
String getJobCC() {
return this.jobCC;
}

/**
* Log information into logger.info and listener logger
* @param text Text for logging.
*/
private void log(String text) {
logger.info(logPrefix + text);
System.out.println(text);
if (listener != null)
listener.getLogger().println(text);
}
/**
* Log information into logger.severe and listener error logger
* @param text Text for logging.
*/
private void err(String text) {
logger.severe(logPrefix + text);
System.err.println(text);
if (listener != null)
listener.error(text);
}
}
Expand Up @@ -167,7 +167,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

// Submit the job.
boolean result = zFTPConnector.submit(inputStream,this.wait,this.waitTime,outputStream,this.deleteJobFromSpool);
boolean result = zFTPConnector.submit(inputStream,this.wait,this.waitTime,outputStream,this.deleteJobFromSpool, listener);

// Get CC.
String printableCC = zFTPConnector.getJobCC();
Expand All @@ -178,8 +178,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis

// Print the info about the job
logger.info("Job [" + zFTPConnector.getJobID() + "] processing finished.");
listener.getLogger().println(
"Job ["
+ zFTPConnector.getJobID()
+ "] processing finished. Captured RC = ["
+ printableCC + "]");

// If wait was requested try to save the job log.
// If wait was requested try to save the job log.
if(this.wait) {
// Save the log.
try
Expand Down Expand Up @@ -209,7 +214,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

// Return whether the job succeeded or not.
// If JESINTERFACELEVEL is configured, no real R is provided.
// If JESINTERFACELEVEL is configured, no real RC is provided.
return result && (this.JESINTERFACELEVEL1 || (_MaxCC.compareTo(printableCC) >= 0));
}

Expand Down

0 comments on commit 56de262

Please sign in to comment.