Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Running groovy from python file

Jakub Cieplinski June 30, 2022

In our project we have one workflow which relies on python numpy matrices to assign appropriate user. There is no issue with calling python from scriptrunner script and getting its value. However, the numpy matrix needs some calculated value to determine which user it will return, and that value is calculated by another script

 

It goes like this

The -> means "calls" in this context 

groovy script -> python User matrix -> python Value calculation script

If it's called like that everything works. But we need to move that value calculation script to groovy and need to call that groovy script from within python matrix script.

 

So it will go like this

groovy script -> python User matrix -> groovy Value calculation script

The issue is, python matrix cannot call groovy script. It just returns an error stating it doesn't have necessary file permissions. All the groovy scripts, and groovy executable on the server were chmod 777 to see if it will help. It didn't. 

 

How can we do this?

 

Code for calling groovy from within python looks like this

amount = subprocess.Popen(["/root/.sdkman/candidates/groovy/4.0.3/bin/groovy",
"/home/jira-home/scripts/valueCalcScript.groovy",
sys.argv[1],
sys.argv[5],
"additionalArg"
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate()

 

2 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 1, 2022

I'm not sure this is the best forum for your question.

But it got me curious even though I'm not much of a python guy and I had never used groovy command line.

I installed groovy and used the same subprocess.Popen to start a simple script.

While documentation says it should be possible for a groovy script executed with the .../bin/groovy should be able to return a final value, I wasn't able to get that behaviour either from a shell command line of from python Popen.

So I would say your best bet is to make sure that your script doesn't output anything other than your final value.

Here is my groovy script:

println "my return value with ${args.size()} arguments: ${args.join(',')}"
return "not printed"

Here is my python script:

import subprocess
p = subprocess.Popen(["/snap/groovy/current/bin/groovy", "/path/to/script.groovy", "arg1", "arg2"],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
print out

Here is the output

>>> my return value with 2 arguments: arg1,arg2

 Before looking all this up, my instinct would have been to tell you that you can call the scriptrunner groovy engine running inside jira using curl command:

$ curl -X POST "http://127.0.0.1:8080/rest/scriptrunner/latest/user/exec/ -H "X-Atlassian-token: no-check" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: application/json" --data-urlencode "scriptText@/path/to/script.groovy" -u <user>:<password>

Hope this helps.

Jakub Cieplinski July 3, 2022

Hi, I have no issue running groovy script through python locally.

What fails is doing the same thing in scriptrunner workflow because of the aforementioned "lack of permissions" to (i think) groovy excutable.

 

We don't want to run it through REST. Although you say that scriptrunner groovy engine can be accessed through it. Which means it has to be installed with plugin itself, is there any way to call it locally on jira itself? Any specific path to it maybe?

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 4, 2022

Groovy in jira/scriptrunner is embedded into the scriptrunner plugin via the inclusions of libraries.

There are no executable that you can reach via the command line.

Once you start execution of a python script in a shell, you can't access the JVM components anymove other than through an externalized endpoint. And the only one I am aware of is the rest endpoint.

Here is a quick POC I did (expanding on the example before).

groovy script stored in /apps/tmp/test.groovy: 

println "this output is from groovy script. ${args.size()} arguments: ${args.join(',')}"
return "not printed"

 Python file stored in/apps/tmp/test.py

import subprocess
print "This output is from the python script before calling groovy"
p = subprocess.Popen(["/snap/groovy/current/bin/groovy", "/apps/tmp/test.groovy", "groovy arg1", "groovy arg2"],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print "This output is from the python script after calling groovy"
out, err = p.communicate()
print out

 Code in scriptrunner groovy console:

def proc = """python2 /apps/tmp/test.py """.execute()
def sout = new StringBuilder()
def serr = new StringBuilder()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(10000)
if(serr) log.error serr
log.info sout

Output

2022-07-04 13:36:23,652 INFO [runner.ScriptBindingsManager]: This output is from the python script before calling groovy 
2022-07-04 13:36:23,653 INFO [runner.ScriptBindingsManager]: This output is from the python script after calling groovy
2022-07-04 13:36:23,653 INFO [runner.ScriptBindingsManager]: this output is from groovy script. 2 arguments: groovy arg1,groovy arg2
Jakub Cieplinski July 4, 2022

Hmm, what's the 'snap' directory? Did you install groovy on jira server via snap package?

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 4, 2022

Like I said. I had never used groovy from the command line. That was just the first result I found when I searched for how to install groovy on RHEL. 
https://snapcraft.io/install/groovy/rhel

Jakub Cieplinski July 18, 2022

Unfortunately we do not use systemd and apparently snap needs it(the solution in provided link for OS without it does not work). Thanks tho

TAGS
AUG Leaders

Atlassian Community Events