Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move configuration from ToolInstallation to GlobalConfiguration #43

Merged
merged 25 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
371e27b
properly get displayname of node
mwinter69 Dec 11, 2017
67bc346
displayname of node in pipeline
mwinter69 Dec 16, 2017
c252ac8
refactoring
mwinter69 Dec 27, 2017
faf30cd
Merge branch 'master' into configuration
mwinter69 Dec 27, 2017
6b4c6d7
Merge pull request #1 from jenkinsci/master
mwinter69 Dec 27, 2017
333ca41
jenkins and java
mwinter69 Dec 28, 2017
ca4eca8
fix findbugs issues in buildData
mwinter69 Dec 28, 2017
45a03ac
Merge branch 'master' of https://github.com/jenkinsci/logstash-plugin…
mwinter69 Dec 29, 2017
59d1a77
Merge branch 'jenkinsci-master'
mwinter69 Dec 29, 2017
96deff1
Merge pull request #3 from jenkinsci/master
mwinter69 Dec 31, 2017
379c5e4
Merge branch 'master' into configuration
mwinter69 Jan 1, 2018
1321104
javadoc and readme
mwinter69 Jan 1, 2018
4974276
remove unnecessary dependency to workflow-step-api
mwinter69 Jan 1, 2018
227611b
do the comparison if something has changed in the configuration class
mwinter69 Jan 3, 2018
d602a6e
incorporate feedback
mwinter69 Jan 3, 2018
7340194
Merge branch 'configuration2' into configuration
mwinter69 Jan 3, 2018
6116410
adjust link to plugin page
mwinter69 Jan 3, 2018
ac70407
simplify equals
mwinter69 Jan 3, 2018
87c93cb
host based indexers vs uri
mwinter69 Jan 4, 2018
f021a43
add some todos
mwinter69 Jan 4, 2018
fdc79cc
use URL instead of URI
mwinter69 Jan 4, 2018
dac27f1
use URI as persistence for elastic search url
mwinter69 Jan 10, 2018
f2c4a24
fix test
mwinter69 Jan 10, 2018
add8146
use ExpectedException rule
mwinter69 Jan 11, 2018
d28cab4
Merge branch 'master' into configuration
jakub-bochenski Jan 30, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipIntegrationTests>true</skipIntegrationTests>
<jenkins.version>2.7.4</jenkins.version>
<jenkins-test-harness.version>2.23</jenkins-test-harness.version>
</properties>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.608</version>
<version>2.15</version>
</parent>

<repositories>
Expand Down Expand Up @@ -86,7 +88,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.10.0</version>
<version>2.13.0</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -136,6 +138,11 @@
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<version>1.15</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@

/**
* Build wrapper that decorates the build's logger to insert a
* {@link LogstashNote} on each output line.
*
* @author K Jonathan Harker
*/
Expand Down Expand Up @@ -102,13 +101,14 @@ public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) {
return los;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) super.getDescriptor();
}

// Method to encapsulate calls for unit-testing
LogstashWriter getLogStashWriter(AbstractBuild<?, ?> build, OutputStream errorStream) {
return new LogstashWriter(build, errorStream, null);
return new LogstashWriter(build, errorStream, null, build.getCharset());
}

/**
Expand Down
160 changes: 160 additions & 0 deletions src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package jenkins.plugins.logstash;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;

import org.kohsuke.stapler.StaplerRequest;

import com.cloudbees.syslog.MessageFormat;
import com.cloudbees.syslog.integration.jul.SyslogMessageFormatter;

import hudson.DescriptorExtensionList;
import hudson.Extension;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import jenkins.model.GlobalConfiguration;
import jenkins.plugins.logstash.LogstashInstallation.Descriptor;
import jenkins.plugins.logstash.configuration.ElasticSearch;
import jenkins.plugins.logstash.configuration.LogstashIndexer;
import jenkins.plugins.logstash.configuration.LogstashIndexer.LogstashIndexerDescriptor;
import jenkins.plugins.logstash.configuration.RabbitMq;
import jenkins.plugins.logstash.configuration.Redis;
import jenkins.plugins.logstash.configuration.Syslog;
import jenkins.plugins.logstash.persistence.LogstashIndexerDao;
import jenkins.plugins.logstash.persistence.LogstashIndexerDao.IndexerType;
import jenkins.plugins.logstash.persistence.LogstashIndexerDao.SyslogFormat;
import net.sf.json.JSONObject;

@Extension
public class LogstashConfiguration extends GlobalConfiguration
{
private static final Logger LOGGER = Logger.getLogger(LogstashConfiguration.class.getName());
private LogstashIndexer<?> logstashIndexer;
private boolean dataMigrated = false;

public LogstashConfiguration()
{
load();
}

public LogstashIndexer<?> getLogstashIndexer()
{
return logstashIndexer;
}

public void setLogstashIndexer(LogstashIndexer<?> logstashIndexer)
{
this.logstashIndexer = logstashIndexer;
}

@CheckForNull
public LogstashIndexerDao getIndexerInstance()
{
if (logstashIndexer != null)
{
return logstashIndexer.getInstance();
}
return null;
}

public List<?> getIndexerTypes()
{
return LogstashIndexer.all();
}

@Initializer(after = InitMilestone.JOB_LOADED)
public void migrateData()
{
if (!dataMigrated)
{
Descriptor descriptor = LogstashInstallation.getLogstashDescriptor();
if (descriptor.getType() != null)
{
IndexerType type = descriptor.getType();
switch (type)
{
case REDIS:
LOGGER.log(Level.INFO, "Migrating logstash configuration for Redis");
Redis redis = new Redis();
redis.setHost(descriptor.getHost());
redis.setPort(descriptor.getPort());
redis.setKey(descriptor.getKey());
redis.setPassword(descriptor.getPassword());
logstashIndexer = redis;
break;
case ELASTICSEARCH:
LOGGER.log(Level.INFO, "Migrating logstash configuration for Elastic Search");
ElasticSearch es = new ElasticSearch();
es.setHost(descriptor.getHost());
es.setPort(descriptor.getPort());
es.setKey(descriptor.getKey());
es.setUsername(descriptor.getUsername());
es.setPassword(descriptor.getPassword());
logstashIndexer = es;
break;
case RABBIT_MQ:
LOGGER.log(Level.INFO, "Migrating logstash configuration for RabbitMQ");
RabbitMq rabbitMq = new RabbitMq();
rabbitMq.setHost(descriptor.getHost());
rabbitMq.setPort(descriptor.getPort());
rabbitMq.setQueue(descriptor.getKey());
rabbitMq.setUsername(descriptor.getUsername());
rabbitMq.setPassword(descriptor.getPassword());
logstashIndexer = rabbitMq;
break;
case SYSLOG:
LOGGER.log(Level.INFO, "Migrating logstash configuration for SYSLOG");
Syslog syslog = new Syslog();
syslog.setHost(descriptor.getHost());
syslog.setPort(descriptor.getPort());
syslog.setSyslogProtocol(descriptor.getSyslogProtocol());
switch (descriptor.getSyslogFormat())
{
case RFC3164:
syslog.setMessageFormat(MessageFormat.RFC_3164);
break;
case RFC5424:
syslog.setMessageFormat(MessageFormat.RFC_5424);
break;
default:
syslog.setMessageFormat(MessageFormat.RFC_3164);
break;
}
logstashIndexer = syslog;
break;
default:
LOGGER.log(Level.INFO, "unknown logstash Indexer type: " + type);
break;
}
}
dataMigrated = true;
save();
}
}

@Override
public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws FormException
{
JSONObject j = json.getJSONObject("logstashIndexer");
String clazz = j.getString("stapler-class");
if (logstashIndexer == null || !logstashIndexer.getClass().getName().equals(clazz))
{
staplerRequest.bindJSON(this, json);
}
else
{
logstashIndexer.reconfigure(staplerRequest, j);
}
save();
return true;
}

public static LogstashConfiguration getInstance()
{
return GlobalConfiguration.all().get(LogstashConfiguration.class);
}

}
90 changes: 48 additions & 42 deletions src/main/java/jenkins/plugins/logstash/LogstashInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,66 +63,72 @@ public static Descriptor getLogstashDescriptor() {

@Extension
public static final class Descriptor extends ToolDescriptor<LogstashInstallation> {
public IndexerType type;
public SyslogFormat syslogFormat;
public SyslogProtocol syslogProtocol;
public String host;
public Integer port = -1;
public String username;
public String password;
public String key;
private transient IndexerType type;
private transient SyslogFormat syslogFormat;
private transient SyslogProtocol syslogProtocol;
private transient String host;
private transient Integer port = -1;
private transient String username;
private transient String password;
private transient String key;

public Descriptor() {
super();
load();
}

@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
req.bindJSON(this, formData.getJSONObject("logstash"));
save();
return super.configure(req, formData);

public IndexerType getType()
{
return type;
}

@Override
public ToolInstallation newInstance(StaplerRequest req, JSONObject formData) throws FormException {
req.bindJSON(this, formData.getJSONObject("logstash"));
save();
return super.newInstance(req, formData);

public SyslogFormat getSyslogFormat()
{
return syslogFormat;
}

@Override
public String getDisplayName() {
return Messages.DisplayName();

public SyslogProtocol getSyslogProtocol()
{
return syslogProtocol;
}

/*
* Form validation methods
*/
public FormValidation doCheckInteger(@QueryParameter("value") String value) {
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
return FormValidation.error(Messages.ValueIsInt());
}

return FormValidation.ok();

public String getHost()
{
return host;
}

public FormValidation doCheckHost(@QueryParameter("value") String value) {
if (StringUtils.isBlank(value)) {
return FormValidation.warning(Messages.PleaseProvideHost());
}

return FormValidation.ok();
public Integer getPort()
{
return port;
}

public FormValidation doCheckString(@QueryParameter("value") String value) {
if (StringUtils.isBlank(value)) {
return FormValidation.error(Messages.ValueIsRequired());
}

return FormValidation.ok();
public String getUsername()
{
return username;
}


public String getPassword()
{
return password;
}


public String getKey()
{
return key;
}


@Override
public String getDisplayName() {
return Messages.DisplayName();
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/jenkins/plugins/logstash/LogstashNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ private boolean perform(Run<?, ?> run, TaskListener listener) {

// Method to encapsulate calls for unit-testing
LogstashWriter getLogStashWriter(Run<?, ?> run, OutputStream errorStream, TaskListener listener) {
return new LogstashWriter(run, errorStream, listener);
return new LogstashWriter(run, errorStream, listener, run.getCharset());
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
// We don't call Run#getPreviousBuild() so no external synchronization between builds is required
return BuildStepMonitor.NONE;
Expand All @@ -106,6 +107,7 @@ public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends Abstra
return true;
}

@Override
public String getDisplayName() {
return Messages.DisplayName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void eol(byte[] b, int len) throws IOException {
this.flush();

if(!logstash.isConnectionBroken()) {
String line = new String(b, 0, len).trim();
String line = new String(b, 0, len, logstash.getCharset()).trim();
line = ConsoleNote.removeNotes(line);
logstash.write(line);
}
Expand Down
Loading