Skip to content

Commit

Permalink
Merge pull request #3 from rundeck/add-logfilter-template
Browse files Browse the repository at this point in the history
logfilter example
  • Loading branch information
ltamaster authored Oct 12, 2018
2 parents 8ebc909 + 82d80e7 commit ebcbe3a
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Java Plugins:
* Notification
* WorkflowStep
* WorkflowNodeStep
* LogFilter

Script Plugins:
* ResourceModelSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class JavaPluginTemplateGenerator extends AbstractTemplateGenerator {
private static final String TEMPLATE_BASE = "templates/java-plugin/"
private static final String JAVA_STRUCTURE = "java-plugin.structure"

private static final List ALLOWED_TEMPLATES = ["ResourceModelSource","Notification","WorkflowStep","WorkflowNodeStep"]
private static final List ALLOWED_TEMPLATES = ["ResourceModelSource","Notification","WorkflowStep","WorkflowNodeStep","LogFilter"]

@Override
Map makeTemplateProperties(final String pluginName, final String providedService) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.plugin.${javaPluginClass.toLowerCase()};

import com.dtolabs.rundeck.core.logging.LogEventControl;
import com.dtolabs.rundeck.core.logging.LogLevel;
import com.dtolabs.rundeck.core.logging.PluginLoggingContext;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.descriptions.PluginProperty;
import com.dtolabs.rundeck.plugins.descriptions.SelectLabels;
import com.dtolabs.rundeck.plugins.descriptions.SelectValues;
import com.dtolabs.rundeck.plugins.logging.LogFilterPlugin;
import java.util.HashMap;
import java.util.Map;

@Plugin(service="LogFilter",name="${sanitizedPluginName}")
@PluginDescription(title="${pluginName}", description="My plugin description")
public class ${javaPluginClass} implements LogFilterPlugin{

@PluginProperty(name = "example header",title = "Example String",description = "Example description")
private String header;

@PluginProperty(
title = "Data type",
description = "Select datatype output",
required = false
)
@SelectValues(
values = {"text/plain", "text/html"},
freeSelect = true
)
@SelectLabels(values = {"TEXT", "HTML"})
String datatype = null;


private boolean started = false;
private StringBuilder buffer;

@Override
public void init(final PluginLoggingContext context) {
started = true;
buffer = new StringBuilder();

if(datatype.equals("text/html")){
buffer.append("<table class='table table-striped'>");
buffer.append("<tr><th>Log Output</th></tr>");
}
}

@Override
public void handleEvent(final PluginLoggingContext context, final LogEventControl event) {
if(event.getEventType().equals("log") && event.getLoglevel().equals(LogLevel.NORMAL) ){

if(datatype.equals("text/html")){
buffer.append("<tr><td>").append("<b>[").append(header).append("]</b> ").append(event.getMessage()).append("</td></tr>");
}else{
buffer.append("[").append(header).append("] ").append(event.getMessage()).append("\\n");
}

event.setLoglevel(LogLevel.DEBUG);
}
}

@Override
public void complete(final PluginLoggingContext context) {
if (started && datatype!=null && buffer.length()>0) {

if(datatype.equals("text/html")){
buffer.append("</table>");
}

Map<String,String> type = new HashMap<>();
type.put("content-data-type", datatype);


context.log(
2,
buffer.toString(),
type
);

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.plugin.${javaPluginClass.toLowerCase()}

import com.dtolabs.rundeck.core.dispatcher.ContextView
import com.dtolabs.rundeck.core.execution.workflow.DataOutput
import com.dtolabs.rundeck.core.logging.LogEventControl
import com.dtolabs.rundeck.core.logging.LogLevel
import com.dtolabs.rundeck.core.logging.PluginLoggingContext
import spock.lang.Specification

class ${javaPluginClass}Spec extends Specification {

def "test preset type "() {
given:
def plugin = new ${javaPluginClass}()
plugin.datatype = datatype
plugin.header = "test"
def sharedoutput = new DataOutput(ContextView.global())
def context = Mock(PluginLoggingContext) {
getOutputContext() >> sharedoutput
}
def events = []
lines.each { line ->
events << Mock(LogEventControl) {
getMessage() >> line
getEventType() >> 'log'
getLoglevel() >> LogLevel.NORMAL
}
}
when:
plugin.init(context)
events.each {
plugin.handleEvent(context, it)
}
plugin.complete(context)

then:
1 * context.log(2, output, meta)

where:
datatype | lines | output | meta
'text/plain' | ['1,2,3', '---', 'a,b,c'] | '[test] 1,2,3\\n[test] ---\\n[test] a,b,c\\n' | ['content-data-type': 'text/plain']
'text/html' | ['1,2,3', '---', 'a,b,c'] | "<table class='table table-striped'><tr><th>Log Output</th></tr><tr><td><b>[test]</b> 1,2,3</td></tr><tr><td><b>[test]</b> ---</td></tr><tr><td><b>[test]</b> a,b,c</td></tr></table>" | ['content-data-type': 'text/html']
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ${pluginName} Rundeck Plugin

This is a log filter plugin.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version = '0.1.0'
defaultTasks 'clean','build'
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
sourceCompatibility = 1.8
ext.rundeckPluginVersion= '2.0'
ext.rundeckVersion= '${rundeckVersion}'


repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compile 'org.rundeck:rundeck-core:3.0.1+'

testCompile 'junit:junit:4.12'
testCompile "org.codehaus.groovy:groovy-all:2.4.15"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
}

ext.pluginClassNames='com.plugin.${sanitizedPluginName}.${javaPluginClass}'
jar {
manifest {
attributes 'Rundeck-Plugin-Classnames': pluginClassNames
attributes 'Rundeck-Plugin-File-Version': version
attributes 'Rundeck-Plugin-Name': '${pluginName}'
attributes 'Rundeck-Plugin-Description': 'Provide a short description of your plugin here.'
attributes 'Rundeck-Plugin-Rundeck-Compatibility-Version': '3.x'
attributes 'Rundeck-Plugin-Tags': 'java,notification'
attributes 'Rundeck-Plugin-Version': rundeckPluginVersion, 'Rundeck-Plugin-Archive': 'true'
}
from("rundeck-verb-artifact.yaml") {
into("")
}
}

task wrapper(type: Wrapper) {
gradleVersion = '4.4.1'
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
build.gradle.template->build.gradle
README.md.template->README.md
icon.png->src/main/resources/resources/icon.png
Plugin.java.template->src/main/java/com/plugin/${javaPluginClass.toLowerCase()}/${javaPluginClass}.java
PluginSpec.groovy.template->src/test/groovy/com/plugin/${javaPluginClass.toLowerCase()}/${javaPluginClass}Spec.groovy

Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ providers:
description: 'Just echo what would be done'
default: true
renderingOptions:
groupName: 'Config'
groupName: 'Config'
- type: String
name: storageprivatekey
title: Storage Private Key
description: Access to storage private key example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-key-type=private"
- type: String
name: storagepassword
title: Storage Password
description: Access to storage password example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-data-type=password"
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ providers:
default: true
renderingOptions:
groupName: 'Config'
- type: String
name: storageprivatekey
title: Storage Private Key
description: Access to storage private key example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-key-type=private"
- type: String
name: storagepassword
title: Storage Password
description: Access to storage password example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-data-type=password"
- name: ${sanitizedPluginName}-FileCopier
service: FileCopier
title: ${pluginName}
Expand Down Expand Up @@ -70,4 +88,22 @@ providers:
description: 'Just echo what would be done'
default: true
renderingOptions:
groupName: 'Config'
groupName: 'Config'
- type: String
name: storageprivatekey
title: Storage Private Key
description: Access to storage private key example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-key-type=private"
- type: String
name: storagepassword
title: Storage Password
description: Access to storage password example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-data-type=password"
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ providers:
description: 'Just echo what would be done'
default: true
renderingOptions:
groupName: 'Config'
groupName: 'Config'
- type: String
name: storageprivatekey
title: Storage Private Key
description: Access to storage private key example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-key-type=private"
- type: String
name: storagepassword
title: Storage Password
description: Access to storage password example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-data-type=password"
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,22 @@ providers:
title: Debug?
description: 'Write debug messages to stderr'
renderingOptions:
groupName: 'Config'
groupName: 'Config'
- type: String
name: storageprivatekey
title: Storage Private Key
description: Access to storage private key example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-key-type=private"
- type: String
name: storagepassword
title: Storage Password
description: Access to storage password example
renderingOptions:
selectionAccessor: "STORAGE_PATH"
valueConversion: "STORAGE_PATH_AUTOMATIC_READ"
storage-path-root: "keys"
storage-file-meta-filter: "Rundeck-data-type=password"
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,20 @@ class JavaPluginTemplateGeneratorTest extends Specification {
new File(tmpDir,"/my-workflownodestep-plugin/src/test/groovy/com/plugin/myworkflownodestepplugin/MyWorkflownodestepPluginSpec.groovy").exists()
}

def "Create Logfilter Template"() {
when:
File tmpDir = File.createTempDir()
JavaPluginTemplateGenerator generator = new JavaPluginTemplateGenerator()
generator.createTemplate("My LogFilter Plugin","LogFilter",tmpDir.absolutePath)
int compileResult = TestUtils.buildGradle(new File(tmpDir,"my-logfilter-plugin"))

then:
compileResult == 0
new File(tmpDir,"/my-logfilter-plugin/build.gradle").exists()
new File(tmpDir,"/my-logfilter-plugin/src/main/resources/resources/icon.png").exists()
new File(tmpDir,"/my-logfilter-plugin/README.md").exists()
new File(tmpDir,"/my-logfilter-plugin/src/main/java/com/plugin/mylogfilterplugin/MyLogfilterPlugin.java").exists()
new File(tmpDir,"/my-logfilter-plugin/src/test/groovy/com/plugin/mylogfilterplugin/MyLogfilterPluginSpec.groovy").exists()
}

}

0 comments on commit ebcbe3a

Please sign in to comment.