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 17 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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Contributing
Adding support for new indexers
-------------------------------

* Create a new class in the package `jenkins.plugins.logstash.persistence` that extends `AbstractLogstashIndexerDao`
* Add a new entry to the enum `IndexerType` in `LogstashIndexerDao`
* Add a new mapping to the `INDEXER_MAP` in `IndexerDaoFactory`
* Implement the extension point `jenkins.plugins.logstash.configuration.LogstashIndexer` that will take your configuration.
Override the method `shouldRefreshInstance()` where you decide if a new dao instance must be created because the configuration has changed in the meantime.
* Create a `configure-advanced.jelly` for the UI part of your configuration.
* Create a new class that extends `jenkins.plugins.logstash.persistence.AbstractLogstashIndexerDao`. This class will do the actual work of pushing the logs to the indexer.
19 changes: 15 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipIntegrationTests>true</skipIntegrationTests>
<jenkins-test-harness.version>2.23</jenkins-test-harness.version>
<java.level>7</java.level>
<jenkins.version>1.608</jenkins.version>
<jenkins.version>2.7.4</jenkins.version>
</properties>

<parent>
Expand Down Expand Up @@ -51,10 +52,10 @@

<artifactId>logstash</artifactId>
<packaging>hpi</packaging>
<version>1.4.0-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
<name>Logstash</name>
<description>A Logstash agent to send Jenkins logs to a Logstash indexer.</description>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Logstash+Plugin</url>
<url>https://plugins.jenkins.io/logstash</url>

<dependencies>
<dependency>
Expand Down Expand Up @@ -88,7 +89,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 @@ -142,6 +143,16 @@

<build>
<plugins>

<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<compatibleSinceVersion>2.0</compatibleSinceVersion>
</configuration>
</plugin>

<!-- https://issues.jenkins-ci.org/browse/INFRA-588 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down
168 changes: 168 additions & 0 deletions src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
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 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.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 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;
private transient LogstashIndexer<?> activeIndexer;

public LogstashConfiguration()
{
load();
if (logstashIndexer != null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not always assign? I don't see how activeIndexer could be non-null here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be null when you install the plugin. But the activeIndexer being null is not a problem

{
activeIndexer = logstashIndexer;
}
}

/**
* Returns the current logstash indexer configuration.
*
* @return configuration instance
*/
public LogstashIndexer<?> getLogstashIndexer()
{
return logstashIndexer;
}

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

/**
* Returns the actual instance of the logstash dao.
* @return dao instance
*/
@CheckForNull
public LogstashIndexerDao getIndexerInstance()
{
if (activeIndexer != null)
{
return activeIndexer.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;
}
activeIndexer = logstashIndexer;
}
dataMigrated = true;
save();
}
}

@Override
public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws FormException
{
// when we bind the stapler request we get a new instance of logstashIndexer
staplerRequest.bindJSON(this, json);

if (logstashIndexer != null && !logstashIndexer.equals(activeIndexer))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Objects.equals() seems shorter

{
activeIndexer = logstashIndexer;
}
save();
return true;
}

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

}
Loading