diff options
author | Maros Marsalek <mmarsale@cisco.com> | 2016-10-12 14:48:17 +0200 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2016-10-17 09:11:21 +0000 |
commit | 7236617f71a2090aa1aebac37e2b7b51330cdc73 (patch) | |
tree | 97011e0210a589adfb48c00b761d8ec59d631f8b /common/common-scripts/src/main/groovy/io | |
parent | 957461dcfd741fc3290e4317c2297c5618b593b5 (diff) |
HONEYCOMB-157 Extract groovy scripts from poms
And put all of them into a dedicated module
Change-Id: Id04c66806a89af68d821a43ef92f0a59220e04e9
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
Diffstat (limited to 'common/common-scripts/src/main/groovy/io')
3 files changed, 249 insertions, 0 deletions
diff --git a/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ModulesListGenerator.groovy b/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ModulesListGenerator.groovy new file mode 100644 index 000000000..c7a74d20e --- /dev/null +++ b/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ModulesListGenerator.groovy @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.common.scripts + +import groovy.text.SimpleTemplateEngine + +import java.nio.file.Paths + +/** + * Generate modules-list file a honeycomb distribution. + */ +class ModulesListGenerator { + + static final def DEFAULT_MODULES_LIST = ModulesListGenerator.getResource("/modules/modulesListDefaultContent").text + + static final def MODULES_LIST_CONTENT_PROPERTY = "distribution.modules" + static final def MODULES_FOLDER = "modules" + static final def MODULE_LIST_FILE_SUFFIX = "-module-config" + static final def SEPARATOR = "," + + public static void generate(project, properties, log) { + // module configuration file extraction + // builds project name from group,artifact and version to prevent overwriting + // while building multiple distribution project + def artifact = project.artifact + def projectName = "${artifact.getGroupId()}_${artifact.getArtifactId()}_${artifact.getVersion()}".replace(".","-") + + log.info "Generating list of modules started by distribution ${projectName}" + + def activeModules = properties.getProperty(MODULES_LIST_CONTENT_PROPERTY, DEFAULT_MODULES_LIST) + .tokenize(SEPARATOR) + .collect { module -> module.trim() } + + log.info "Project ${projectName} : Found modules ${activeModules}" + //creates folder modules + + def outputPath = Paths.get(project.build.outputDirectory, StartupScriptGenerator.MINIMAL_RESOURCES_FOLDER, MODULES_FOLDER) + //creates module folder + outputPath.toFile().mkdirs() + + def outputFile = Paths.get(outputPath.toString(), "${projectName}${MODULE_LIST_FILE_SUFFIX}").toFile() + outputFile.createNewFile(); + log.info("Writing module configuration for distribution ${projectName} to ${outputPath}") + + if (activeModules.isEmpty()) { + outputFile.text = new SimpleTemplateEngine().createTemplate(DEFAULT_MODULES_LIST).make( + ["groupId" : project.groupId, + "artifactId": project.artifactId, + "version" : project.version]).toString() + } else { + activeModules.add(0, "// Generated from ${project.groupId}/${project.artifactId}/${project.version}") + outputFile.text = activeModules.join(System.lineSeparator) + } + } +} diff --git a/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ReadmeGenerator.groovy b/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ReadmeGenerator.groovy new file mode 100644 index 000000000..5010395ea --- /dev/null +++ b/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/ReadmeGenerator.groovy @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.common.scripts + +import groovy.text.SimpleTemplateEngine + +import java.nio.file.Files +import java.nio.file.Paths + +/** + * Check/generate and repair Readme.adoc for a honeycomb module. + */ +class ReadmeGenerator { + + static final def DEFAULT_README = ModulesListGenerator.getResource("/readme/readmeDefaultContent").text + + static final def ADOC_FOLDER = "asciidoc" + static final def README = "Readme" + static final def README_FILE = "${README}.adoc" + static final def README_HTML = "${README}.html" + static final def SITE_FOLDER = "site" + static final def INDEX_HTML = "index.html" + + public static void checkReadme(project, properties, log) { + log.info "Checking ${ADOC_FOLDER}/${README_FILE}" + def asciidoc = Paths.get(project.getBasedir().toString(), ADOC_FOLDER) + def readme = Paths.get(asciidoc.toString(), README_FILE) + if (!Files.exists(readme)) { + log.info "Generating ${readme}" + Files.createDirectories(asciidoc) + Files.createFile(readme) + readme.toFile().text = new SimpleTemplateEngine().createTemplate(DEFAULT_README) + .make(["artifactId": project.artifactId]) + .toString() + } + } + + public static void fixSite(project, properties, log) { + def index = Paths.get(project.build.directory.toString(), SITE_FOLDER, INDEX_HTML) + if (Files.exists(index)) { + log.info "Fixing links in generated site" + def html = index.toFile().text + log.info "Fixing ${ADOC_FOLDER} ${README_HTML} link" + index.toFile().text = html.replaceAll("[./]*${README}\\.html", README_HTML) + } + } +} diff --git a/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/StartupScriptGenerator.groovy b/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/StartupScriptGenerator.groovy new file mode 100644 index 000000000..65245b2e0 --- /dev/null +++ b/common/common-scripts/src/main/groovy/io/fd/honeycomb/common/scripts/StartupScriptGenerator.groovy @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.common.scripts + +import groovy.text.SimpleTemplateEngine + +import java.nio.file.Path +import java.nio.file.Paths + +/** + * Generate startup shell scripts for a honeycomb distribution. + */ +class StartupScriptGenerator { + + static final def DEFAULT_START_SCRIPT_TEMPLATE = StartupScriptGenerator.getResource("/scripts/startScript").text + static final def FORK_SCRIPT_TEMPLATE = StartupScriptGenerator.getResource("/scripts/forkScript").text + static final def KILL_SCRIPT_TEMPLATE = StartupScriptGenerator.getResource("/scripts/killScript").text + static final def README_TEMPLATE = StartupScriptGenerator.getResource("/scripts/README").text + + static final def JVM_PARAMS_KEY = "exec.parameters" + static final def DEFAULT_ADDITIONAL_JVM_PROPERTIES = "" + static final def JVM_DEBUG_PARAMS_KEY = "debug.parameters" + static final def START_SCRIPT_TEMPLATE_KEY = "start.script.template" + static final def MINIMAL_RESOURCES_FOLDER = "honeycomb-minimal-resources" + + static final def STARTUP_SCRIPT_NAME = "honeycomb" + static final def KILL_SCRIPT_NAME = "honeycomb-kill" + static final def FORK_STARTUP_SCRIPT_NAME = "honeycomb-start" + static final def DEFAULT_DEBUG_JVM_PARAMS = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" + + public static void generate(project, properties, log) { + log.info "Generating honeycomb shell scripts for ${project.artifactId}" + + // JVM params, defined in pom or no special params + def additionalJvmParameters = properties.getOrDefault(JVM_PARAMS_KEY, DEFAULT_ADDITIONAL_JVM_PROPERTIES) + log.debug "Additional JVM properties: ${additionalJvmParameters}" + // Startup script template, can be overridden by property in pom + def scriptTemplate = properties.getOrDefault(START_SCRIPT_TEMPLATE_KEY, DEFAULT_START_SCRIPT_TEMPLATE) + log.debug "Template used for startup script: ${scriptTemplate}" + // JVM debug params, defined in pom or no special params + def debugJvmParameters = properties.getOrDefault(JVM_DEBUG_PARAMS_KEY, DEFAULT_DEBUG_JVM_PARAMS) + log.debug "Debug JVM properties: ${additionalJvmParameters}" + + def jvmParameters = "${additionalJvmParameters} -jar \$(dirname \$0)/${project.artifactId}-${project.version}.jar" + def scriptParent = Paths.get(project.build.outputDirectory, MINIMAL_RESOURCES_FOLDER) + scriptParent.toFile().mkdirs() + + def startScriptPath = generateStartupScript(jvmParameters, log, scriptParent, scriptTemplate) + def forkScriptPath = generateForkingStartupScript(scriptParent, log) + def debugScriptPath = generateDebugStartupScript(debugJvmParameters, jvmParameters, log, scriptParent, scriptTemplate) + def killScriptPath = generateKillScript(log, scriptParent) + generateReadme(scriptParent, log, startScriptPath, forkScriptPath, debugScriptPath, killScriptPath, project) + } + + private static def generateReadme(scriptParent, log, + startScriptPath, forkScriptPath, debugScriptPath, killScriptPath, project) { + def readmePath = Paths.get(scriptParent.toString(), "README") + + def readmeContent = new SimpleTemplateEngine().createTemplate(README_TEMPLATE).make( + ["groupId" : project.groupId, + "artifactId" : project.artifactId, + "version" : project.version, + "startScript": startScriptPath.fileName, + "forkScript" : forkScriptPath.fileName, + "debugScript": debugScriptPath.fileName, + "killScript" : killScriptPath.fileName]).toString() + log.info "Writing README to ${readmePath}" + flushScript(readmePath, readmeContent) + } + + private static def generateDebugStartupScript(debugJvmParameters, javaArgs, log, Path scriptParent, + scriptTemplate) { + def exec = "java ${debugJvmParameters} ${javaArgs}" + log.info "Debug script content to be used: ${exec}" + def scriptPath = Paths.get(scriptParent.toString(), "honeycomb-debug") + log.info "Writing shell debug script to ${scriptPath}" + flushScript(scriptPath, new SimpleTemplateEngine().createTemplate(scriptTemplate).make(["exec": exec]).toString()) + } + + private static def generateForkingStartupScript(scriptParent, log) { + def scriptPath = Paths.get(scriptParent.toString(), FORK_STARTUP_SCRIPT_NAME) + log.info "Writing forking startup script to ${scriptPath}" + flushScript(scriptPath, new SimpleTemplateEngine().createTemplate(FORK_SCRIPT_TEMPLATE).make().toString()) + } + + private static def flushScript(filePath, content) { + filePath.toFile().text = content + filePath.toFile().setExecutable(true) + filePath + } + + private static def generateStartupScript(javaArgs, log, scriptParent, scriptTemplate) { + def exec = "java ${javaArgs}" + log.info "Startup script content to be used: ${exec}" + def scriptPath = Paths.get(scriptParent.toString(), STARTUP_SCRIPT_NAME) + log.info "Writing startup script to ${scriptPath}" + flushScript(scriptPath, new SimpleTemplateEngine().createTemplate(scriptTemplate).make(["exec": exec]).toString()) + } + + private static def generateKillScript(log, scriptParent) { + def scriptPath = Paths.get(scriptParent.toString(), KILL_SCRIPT_NAME) + log.info "Writing kill script to ${scriptPath}" + flushScript(scriptPath, new SimpleTemplateEngine().createTemplate(KILL_SCRIPT_TEMPLATE).make().toString()) + } +} |