-
-
Notifications
You must be signed in to change notification settings - Fork 107
move configuration from ToolInstallation to GlobalConfiguration #43
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
Changes from 21 commits
371e27b
67bc346
c252ac8
faf30cd
6b4c6d7
333ca41
ca4eca8
45a03ac
59d1a77
96deff1
379c5e4
1321104
4974276
227611b
d602a6e
7340194
6116410
ac70407
87c93cb
f021a43
fdc79cc
dac27f1
f2c4a24
add8146
d28cab4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| package jenkins.plugins.logstash; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| 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 org.apache.http.client.utils.URIBuilder; | ||
| 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) | ||
| { | ||
| 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(); | ||
| } | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| @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"); | ||
| URI uri; | ||
| try | ||
| { | ||
| uri = (new URIBuilder(descriptor.getHost())) | ||
| .setPort(descriptor.getPort()) | ||
| .setPath("/" + descriptor.getKey()).build(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure if this will work properly for keys that already start with
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is exactly the same behaviour as it was before in the ElasticSearchDao There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK then |
||
| ElasticSearch es = new ElasticSearch(); | ||
| es.setUrl(uri.toURL()); | ||
| es.setUsername(descriptor.getUsername()); | ||
| es.setPassword(descriptor.getPassword()); | ||
| logstashIndexer = es; | ||
| } | ||
| catch (URISyntaxException | MalformedURLException e) | ||
| { | ||
| LOGGER.log(Level.INFO, "Migrating logstash configuration for Elastic Search failed: " + e.toString()); | ||
| } | ||
| 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 (!Objects.equals(logstashIndexer, activeIndexer)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add further comment explaining that we check for equality because |
||
| { | ||
| activeIndexer = logstashIndexer; | ||
| } | ||
| save(); | ||
| return true; | ||
| } | ||
|
|
||
| public static LogstashConfiguration getInstance() | ||
| { | ||
| return GlobalConfiguration.all().get(LogstashConfiguration.class); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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
activeIndexercould be non-null hereThere was a problem hiding this comment.
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