Grails External configuration Config System.getenv - grails

i have code in config.groovy
def ENV_NAME = "/home/myFolder"
if(System.getenv("ENV_NAME")) {
println( "Including configuration file: " + System.getenv("ENV_NAME"));
grails.config.locations << "file:" + System.getenv("ENV_NAME")
} else {
println "No external configuration file defined."
}
When I run, the result is "No external configuration file defined."
What do I miss?

Presumably that there isn't an environment variable named ENV_NAME defined at the point when you ran the app.

Related

Jenkins Error after upgrade - "Not all environment variables could be successfully injected."

After the upgrade from Jenkins "1.651.3" to "2.346.3 LTS".
My job log contains this message many times:
ERROR: Not all environment variables could be successfully injected. Check for similarly-named environment variables.
There is no clue. How to find which variables could not be successfully injected?
I found that message goes from Environment Injector plugin
It goes from class EnvInjectEnvVarsContributor
And it seems that it is not an error. It is just a warning, that some property, already exists in environment variables.
Responsilbe commit from history
Map<String, String> result = jobPropertyInfo.getPropertiesContentMap(env);
if (result != null) {
int expectedEnvSize = env.size() + result.size();
env.putAll(result);
if (env.size() != expectedEnvSize) {
listener.error("Not all environment variables could be successfully injected. " +
"Check for similarly-named environment variables.");
}
}
To find which properties are confilicting I used this groovy script executed in the job like "Execute system Groovy script"
import hudson.model.*
import org.jenkinsci.plugins.envinject.EnvInjectJobProperty
import jenkins.model.Jenkins
println "FIND CONFLICTING PROPERTIES GROOVY SCRIPT---------------------------- START"
def job = Hudson.instance.getJob('my_job')
println job
EnvInjectJobProperty jobProperty = (EnvInjectJobProperty) job.getProperty(EnvInjectJobProperty.class);
def jobPropertyInfo = jobProperty.getInfo();
def env = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars()
Map<String, String> result = jobPropertyInfo.getPropertiesContentMap(env);
println "RESULT"
println result
println result.size()
println "ENV"
println env
println env.size()
println "FIND CONFLICTING PROPERTIES GROOVY SCRIPT---------------------------- END"
Maybe it will help to someone.

Jenkins pipeline not able to open a file

I am trying a pipeline script in which I need to open a file and change some text in . So my script goes like this :import java.io.File
node {
stage('File settings') {
dir ('gitfile') {
dir('config') {
sh 'dir'
sh 'pwd > outFile'
curPath = readFile 'outFile'
echo "The current date is ${curPath}"
def file = new File("${curPath}/"+"const.js")
def lines = file.readLines()
println "${file} has ${lines.size()} lines of text"
println "Here is the first line: ${lines[0]}"
println "Here is the last line: ${lines[lines.size()-1]}"
}
}
}
}
But I get error like :
java.io.FileNotFoundException: /var/lib/jenkins/workspace/Daily/smoke/config
/const.js (No such file or directory)
But the file is present in that location. Please let me know why this error happens.
You should use the readFile() and writeFile() Jenkins pipeline steps to manipulate file contents on the workspace directory. See https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/

How to set Set Vm Arguements for Grails 2.2.4

I am using Grail 2.2.4,
in conf/spring/resources.xml, I try to import a file as ${realm}-config.properties, where realm is a vm arguement, I tried to set vm arguements in BuildConfig.groovy as below
grails.tomcat.jvmArgs = ["-Dstage=dev","-Drealm=app"]
But it doesn't seem to pick up. I get below error,
class path resource [${realm}-config.properties] cannot be opened because it does not exist
What is the right way to set vm arguements in grails 2.2.4 ?
Maybe try to load your config via environment variable handled in conf/Config.groovy
def ENV_NAME = "MY_CONFIG"
grails.config.locations = []
if (System.getenv(ENV_NAME)) {
grails.config.locations << "file:" + System.getenv(ENV_NAME)
}
else if (System.getProperty(ENV_NAME)) {
grails.config.locations << "file:" + System.getProperty(ENV_NAME)
}

externalizing grails config into multiple properties files from environment variable

I have setup an environment variable like this:
APP_HOME = "c:\app\app-datasource.properties
in config.groovy I do
def ENV_NAME = "APP_HOME"
if(!grails.config.location || !(grails.config.location instanceof List)) {
grails.config.location = []
}
if(System.getenv(ENV_NAME)) {
println "Including configuration file specified in environment: " + System.getenv(ENV_NAME);
grails.config.location << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
println "Including configuration file specified on command line: " + System.getProperty(ENV_NAME);
grails.config.location << "file:" + System.getProperty(ENV_NAME)
} else {
println "No external configuration file defined."
}
I got this from a post online, I want to know if we need to use grails.config.location or grails.config.locations ?
Also instead of APP_HOME being set to the properties file directly, can I set it to a directory path (e.g.: c:\apps) and then can I have multiple properties files placed in that directory, then if I do the following multiple times will it work?:
grails.config.locations << "file:" + System.getProperty(ENV_NAME)+ "\app-datasource.properties"
grails.config.locations << "file:" + System.getProperty(ENV_NAME)+ "\app-reporting.properties"
and so on...
Thanks in advance
You need to modify grails.config.locations (plural). My (very limited) experience says that the external files are probably not loaded until after Config.groovy is completed.
You might want to consider looking for he additional configuration file on your classpath; then you can put your extra outside of the Grails project (e.g., in your web server's library) or within the grails-app/conf directory. I have written the instructions on how to do that here.
Here's a post on how to do it from a plugin: https://stackoverflow.com/a/9789506/1269312

grails configure log4j without rebuilding war?

This seems pretty strange, but when grails builds a war file it doesn't generate a log4j.properties or log4j.xml file.
Instead it has the following in WEB-INF/web.xml
web.xml:
<listener>
<listener-class>org.codehaus.groovy.grails.web.util.Log4jConfigListener</listener-class>
</listener>
and apparently "grails Log4j DSL configures logging in-memory". The problem here is - log4j isn't automatically exposed to JMX for us to dynamically change and there's no log4j file generated by grails. But Config.groovy is a compiled file.
There's got to be an easy way to manage this without rebuilding the war?
One option suggested is go through to spring and configure logging there:
resources.groovy:
beans = {
log4jConfigurer(org.springframework.beans.factory.config.MethodInvokingFactoryBean)
{
targetClass = "org.springframework.util.Log4jConfigurer"
targetMethod = "initLogging"
arguments = ["classpath:myapp/log4j.properties"]
}
}
then shift the configuration in the DSL to the configured file.
Can anyone advise the 'groovy' way to dynamically change logging configuration without rebuilding the WAR file each time. Using grails-1.3.7. Cutting the DSL out doesn't seem the right way.
Thanks
You may have an external config file that is searched for by your application at startup time.
You would have a MyExternalConfig.groovy file somewhere in your production environment. For example:
log4j = {
def catalinaBase = System.properties.getProperty('catalina.base')
if (!catalinaBase) catalinaBase = '.'
def logDirectory = "${catalinaBase}/logs"
appenders {
rollingFile name:"infoLog", maxFileSize:'900KB', file:"${logDirectory}/${appName}Info.log", maxBackupIndex:10, layout:pattern(conversionPattern: '%d{DATE} %p %c - %m%n'), threshold: org.apache.log4j.Level.INFO
rollingFile name:"erroLog", maxFileSize:'900KB', file:"${logDirectory}/${appName}Erro.log", maxBackupIndex:10, layout:pattern(conversionPattern: '%d{DATE} %p %c - %m%n'), threshold: org.apache.log4j.Level.ERROR
}
root {
info 'infoLog', 'erroLog'
additivity = false
}
error erroLog:"StackTrace"
error erroLog: 'org.codehaus.groovy.grails.web.servlet', // controllers
'org.codehaus.groovy.grails.web.pages', // GSP
'net.sf.ehcache.hibernate'
warn infoLog: 'org.mortbay.log'
info infoLog: "grails.app"
}
Then in your Config.groovy file, that belongs to your grails project in conf folder, you put this as the last thing of the file:
def ENV_NAME = "MY_EXTERNAL_CONFIG"
if(!grails.config.locations || !(grails.config.locations instanceof List)) {
grails.config.locations = []
}
if(System.getenv(ENV_NAME)) {
grails.config.locations << "file:" + System.getenv(ENV_NAME)
} else if(System.getProperty(ENV_NAME)) {
grails.config.locations << "file:" + System.getProperty(ENV_NAME)
} else {
println "No external configuration file defined."
}
This will look for external configurations files to add to your grails.config.locations attribute of your Config.groovy. First it looks for it as a System Environment variable (I use it this way), if it does not find, then it looks for a command line parameter (so you could add it when you start your tomcat app, as a parameter to startup.sh).
To configure your system environment variabble, just do this before starting tomcat:
MY_EXTERNAL_CONFIG="/home/tomcat/configs/MyExternalConfig.groovy"
export MY_EXTERNAL_CONFIG
--- start tomcat here ---
That's it.

Resources