Accessing config.groovy variables from Quartz Job - grails

This is grails-app/conf/Config.groovy from which i am trying to access
variables in Quartz Job. This is the variable i am trying to access in the quartz job below:
ais.mediquery.TrialVariable="My Variable";
Quartz Job :
import com.projectname.*;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import grails.util.Holders
class TrialJob {
GrailsApplication grailsApplication;
static triggers = {
simple repeatInterval: 10000l // execute job once in 5 seconds
}
def execute() {
//log.info(grailsApplication.ais.mediquery.TrialVariable)
println (Holders.config.ais.mediquery.TrialVariable)
}
}
I have tried using both GrailsApplication and ais.mediquery.TrialVariable
but none of them seem to access variables and print them

The config is definitely available from GrailsApplication and that's the best option:
println grailsApplication.config.ais.mediquery.TrialVariable
If that doesn't seem to work, try printing the whole config and see if you have a typo:
println grailsApplication.config
or
println grailsApplication.config.flatten()
As with any behavior that seems strange, run grails clean and grails compile to force a clean compile.

Related

How do you modify Jenkins configuration from a shared-library?

Jenkins allows us to grab a Singelton of the running instance with jenkins.get(). I'm trying to build a class in my shared-library that CRUD's the cloud providers. My code looks like this.
#NonCPS
def create(){
Jenkins jenkins = Jenkins.getInstance()
// logic to create cloud
jenkins.clouds.add(tmpCloud)
jenkins.save()
}
#NonCPS
def delete(){
Jenkins jenkins = Jenkins.getInstance()
def newlist = jenkins.clouds.findAll{ it.getDisplayName() != cloud }
if(newlist){
jenkins.clouds.clear()
}
for ( int i = 0; i < newlist.size; i++ ) {
jenkins.clouds.add(newlist[i])
}
jenkins.save()
}
If I run just the create() function the code works as expected, same if I run just the delete(), but if I run both in the same job like this.
cloud.create()
cloud.delete()
Then only the create will work, the delete wont error but it wont do anything either. Is it possible to reinitialize a singleton? I feel like that is what I need to do. I have tried saving the jenkins instance as a field of my class, but that leads to a hudson serializable error. I dont see a way to pass the jenkins singleton around because of CPS issues.

Inject variable in jenkins pipeline with groovy script

I am building a jenkins pipeline and the job can be triggered by remote. I have the requirement to know which IP triggered the job. So I have a little groovy script, which returns the remote IP. With the EnvInject-plugin I can easily use this variable in a normal freestyle job, but how can I use this in the pipeline scirpt? I can't use the EnvInject-plugin with the pipeline-plugin :(
Here is the little script for getting the IP:
import hudson.model.*
import static hudson.model.Cause.RemoteCause
def ipaddress=""
for (CauseAction action : currentBuild.getActions(CauseAction.class)) {
for (Cause cause : action.getCauses()) {
if(cause instanceof RemoteCause){
ipaddress=cause.addr
break;
}
}
}
return ["ip":ipaddress]
You can create a shared library function (see here for examples and the directory structure). This is one of the undocumented (or really hard to find any documentation) features of Jenkins.
If you would put a file triggerIp.groovy in the directory vars, which is in the directory workflow-libs at the root level of JENKINS_HOME and put your code in that file.
The full filename then will be $JENKINS_HOME/workflow-libs/vars/ipTrigger.groovy
(You can even make a git repo for your shared libraries and clone it in that directory)
// workflow-libs/vars/ipTrigger.groovy
import hudson.model.*
import static hudson.model.Cause.RemoteCause
#com.cloudbees.groovy.cps.NonCPS
def call(currentBuild) {
def ipaddress=""
for (CauseAction action : currentBuild.getActions(CauseAction.class)) {
for (Cause cause : action.getCauses()) {
if(cause instanceof RemoteCause){
ipaddress=cause.addr
break;
}
}
}
return ["ip":ipaddress]
}
After a restart of Jenkins, from your pipeline script, you can call the method by the filename you gave it.
So from your pipeline just call def trigger = ipTrigger(currentBuild)
The the ipaddress will be, trigger.ip (sorry for the bad naming, couldn't come up with something original)

jenkinsfile use traits and other groovy synax

I would like to use a slightly more complex pipeline build via jenkinsfiles, with some reusable steps as I have a lot or similar projects. I'm using jenkins 2.0 with the pipeline plugins. I know that you can load groovy scripts which contain can contain some generic pieces of code but I was wondering if these scripts can use some of the Object oriented features of groovy like traits. For example say I had a trait called Step:
package com.foo.something.ci
trait Step {
void execute(){ echo 'Null execution'}
}
And a class that then implemented the trait in another file:
class Lint implements Step {
def execute() {
stage('lint')
node {
echo 'Do Stuff'
}
}
}
And then another class that contained the 'main' function:
class foo {
def f = new Lint()
f.execute()
}
How would I load and use all these classes in a Jenkinsfile, especially since I may have multiple classes each defining a step? Is this even possible?
Have a look at Shared Libaries. These enable the use of native groovy code in Jenkins.
Your Jenkinsfile would include your shared libary, and the use the classes you defined. Be aware, that you have to pass the steps variable of Jenkins, if you want to use stage or the other variables defined in the Jenkins Pipeline plugin.
Excerpt from the documentation:
This is the class, which would define your stages
package org.foo
class Utilities implements Serializable {
def steps
Utilities(steps) {this.steps = steps}
def mvn(args) {
steps.sh "${steps.tool 'Maven'}/bin/mvn -o ${args}"
}
}
You would use it like this:
#Library('utils') import org.foo.Utilities
def utils = new Utilities(steps)
node {
utils.mvn 'clean package'
}

Service not injecting properly into quartz job class With quartz scheduler plugin (v1.0.1) for grails 2.3.6

I've implemented my own quartz job classes that take in a service method . However, the service doesn't seem to be injecting properly into my job class as i always get a nullpointer exception when accessing a service.
How do i get the services to be injected during the trigger of the job .
Any suggestion on how to invoke the method in the service from the Job class
Update 1 : Added the job code and log messages
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobDataMap;
import com.unitrac.app.reportcentre.report.ReportService;
// Logging
import org.apache.commons.logging.LogFactory;
class ReportJob implements Job {
def reportService;
def grailsApplication;
// Logging
private static final log = LogFactory.getLog(this);
void execute(JobExecutionContext context) throws JobExecutionException {
// execute job
JobDataMap jobMapData = context.getMergedJobDataMap();
try
{
log.debug("In Report Job - executing its JOB at "
+ new Date() + " by " + context.getTrigger().getName());
String groupName = context.getTrigger().getJobKey().getName();
log.debug("group Name : " + groupName);
reportService.invokeMethod();
}
catch( Exception e )
{
log.error("Exception: $e");
}
}
}
|
|
Server running. Browse to http://localhost:8080/ReportCentre
2014-04-22 14:50:00,087 [UnitracJobScheduler_Worker-1] DEBUG reportcentre.ReportJob - In Report Job - executing its JOB at Tue Apr 22 14:50:00 CAT 2014 by CCtrigger
2014-04-22 14:50:00,104 [UnitracJobScheduler_Worker-1] DEBUG reportcentre.ReportJob - group Name : DevGroup
2014-04-22 14:50:00,106 [UnitracJobScheduler_Worker-1] ERROR reportcentre.ReportJob - Exception: java.lang.NullPointerException: Cannot invoke method invokeMethod() on null object
For what it's worth. I tried all the suggestions above, and it' didn't work for me. I eventually settled for Injecting the service manually from the Grails ApplicationContext.
def ss = Holders.grailsApplication.mainContext.getBean(SampleService.class)
This is better than creating a service using new()
Make sure your job class in in grails-app/jobs and try removing implements Job.
You actually don't need to remove "implements Job". Ran into the same sort of problem...
I am guessing that you are creating the job with JobBuilder.newJob(...)
Using newJob breaks the injection (it is bypassing standard object creation patterns). You can, usually, just put the things that you were loading into JobDetail into the trigger.
Then use: ReportJob.schedule(trigger)
Doesn't even need an instance to work. All injection is happy. Yay, Reports!
I added #Autowired() annotation to the fields I need wired in and this worked for me.

How to execute a Groovy Script from my Grails app?

Well, it seems a simple task but I didn't manage to make it run.
I have a groovy script that runs fine under Windows Vista when calling from prompt:
> cd MY_GAILS_PROJECT_DIR
> groovy cp src/groovy scripts/myscript.groovy
Now, I want to execute this script (and passing to it some input arguments) through my my Maintenance Service Class (called from a controller) as below,
class MaintenanceService {
def executeMyScript() {
"groovy cp src/groovy scripts/myscript.groovy".execute()
}
}
It does not work at all! I don't even manage to have the execute() method recognizing any command (like "cd .".execute()) throwing exception:
Error 500: java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified
1- How can I execute a groovy script from my grails application?
2- What are the best practices here? For instance, should I use the QuartzPlugin and then the triggerNow method for executing a script? should I use a Gant Task? If yes, how to do it?
Thank you.
If you don't mind your script running asynchronously (in a separate process to the service method), the following should work assuming groovy is on your PATH variable:
def cmd = ['groovy.bat', 'cp', 'src/groovy scripts/myscript.groovy']
cmd.execute()
If you want to view the output of the process in the application console, you should try something like this instead
// Helper class for redirecting output of process
class StreamPrinter extends Thread {
InputStream inputStream
StreamPrinter(InputStream is) {
this.inputStream = is
}
public void run() {
new BufferedReader(new InputStreamReader(inputStream)).withReader {reader ->
String line
while ((line = reader.readLine()) != null) {
println(line)
}
}
}
}
// Execute the script
def cmd = ['groovy', 'cp', 'src/groovy scripts/myscript.groovy']
Process executingProcess = cmd.execute()
// Read process output and print on console
def errorStreamPrinter = new StreamPrinter(executingProcess.err)
def outputStreamPrinter = new StreamPrinter(executingProcess.in)
[errorStreamPrinter, outputStreamPrinter]*.start()
Update:
In response to your comment below, try the following (which assumes you're on Windows):
1: Create the file C:\tmp\foo.groovy. The content of this file should be simply:
println 'it works!'
2: In the groovy console, run the following:
cmd = ['groovy.bat', 'C:\\tmp\\foo.groovy']
cmd.execute().text
3: You should see the result of the script (the text 'it works!') shown in the Groovy console
If you can't get this simple example working, there's something wrong with your environment, e.g. 'groovy.bat' is not on your PATH. If you can get this example working, then you should be able to work forward from it to achieve your objective.
As of grails 1.3.6 the run-script command is built in to let you run
grails run-script myScript.groovy
For earlier versions of grails, check out my updated blog post from what Carlos posted above.
Easiest Way:
Generate an Groovy Class and place at in your /src/groovy Folder of your Grails Project.
Import that Class in your Domain Class and use the Functions you defined.
My 2 Cents...
This might help as well:
http://naleid.com/blog/2008/03/31/using-gant-to-execute-a-groovy-script-within-the-grails-context-updated/
Carlos
Another decision you can use GroovyScriptEngine for example:
file MyScript.groovy:
static String showMessage() {
println("Message from showMessage")
}
file BootStrap.groovy:
class BootStrap {
def init = { servletContext ->
new GroovyScriptEngine("scripts")
.loadScriptByName("MyScript.groovy")
.showMessage()
}
def destroy = {
}
}

Resources