Executing CURL in Shared Library Jenkins Pipeline Using `Process` - jenkins

I have a method in a shared library in my Jenkins pipeline. The idea is to use this library and upload files to a remote host. The library is imported in a singleton library.
import com.package.jobutil.UploadFile
def uploadFunc() {
def uploader = new UploadFile(this)
withCredentials ([usernamePassword(credentialsId: 'user', userNameVariable: 'username', passwordVariable:'password)]) {
uploader.uploadArtifact("${username}", "${password}", file.txt, location)
}
}
def call() {
uploadFunc()
}
The class that is instantiated looks like this:
class UploadFile {
def steps
UploadFile (steps) {
this.steps = steps
}
pulic uploadArtifct (String user, String password, String file, String location) {
Process proc
def cred = "${user}:${pass}"
def cmd = ["curl", "-v", "-u", cred, "--upload-file", file, location]
steps.println "CURL: ${cmd}"
proc = cmd.execute()
}
}
Even though I see the println line in the logs. I do not see the curl command being executed.
Is there something I am missing that does not invoke the cmd.execute to work?
EDIT
When I use the curl directly in the library, it works.
pulic uploadArtifct (String user, String password, String file, String
location) {
def cred = "${user}:${password}"
def cmd = "curl -v -u ${cred} --upload-file ${file} ${nexusLocation}/${file}"
try {
steps.sh cmd
} catch (Exception e) {
throw new RuntimeExceptipon("Cannot execute curl, exception: [${e.getClass().getName()} - '${e.getMessage()}']")
}
}
However, when trying to use the Process it does not work.
pulic uploadArtifct (String user, String password, String file, String
location) {
def cred = "${user}:${password}"
def cmd = ["curl", "-v", "-u", cred, "--upload-file", ${file}, ${location}]
try {
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println sout
} catch (Exception e) {
throw new RuntimeExceptipon("Cannot execute curl, exception: [${e.getClass().getName()} - '${e.getMessage()}']")
}
}
The exception I get is:
java.lang.RuntimeException: Cannot execute curl, exception: [groovy.lang.MissingMethodException - 'No signature of method: java.lang.String.div() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [file.txt]

As explained here, you need to capture stdout/stderr to see anything.
At the very least:
def outputStream = new StringBuffer();
proc.waitForProcessOutput(outputStream, System.err)
//proc.waitForProcessOutput(System.out, System.err)
Or, as in this gist:
def sout = new StringBuffer(), serr = new StringBuffer()
def proc = cmd.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println sout
An example of blocking call would be:
println new ProcessBuilder( 'sh', '-c', 'du -h --max-depth=1 /var/foo/bar/folder\\ with\\ spaces | sort -hr').redirectErrorStream(true).start().text
def cmd = ["curl", "-v", "-u", cred, "--upload-file", ${file}, ${location}/${file}]
No signature of method: java.lang.String.div() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [file.txt]
The '/' in '${location}/${file}' is interpreted as an '/ (div) operation instead of a string.
Try instead for that curl command argument:
${location}+"/"+${file}
As noted in your subsequent question, the all path needs to be between double-quotes.

Related

How to pass parameter to groovy post build in jenkins?

I have the following Groovy Postbuild script in a jenkins job:
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
def sendPostRequest(urlString, paramString) {
def url = new URL(urlString)
def conn = url.openConnection()
conn.setDoOutput(true)
def writer = new OutputStreamWriter(conn.getOutputStream())
writer.write(paramString)
writer.flush()
String line
def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
while ((line = reader.readLine()) != null) {
println line
}
writer.close()
reader.close()
}
def result =" Project: *Autotests* \n Version *manager.build.buildVariables.get("VERSION_CURR")* \n"
sendPostRequest("https://api.telegram.org/bot{token}/sendMessage", "parse_mode=Markdown&chat_id={id_Chat}&reply_to_message_id=&text=${result}")
But I am getting an error: "groovy.lang.MissingPropertyException: No such property: VERSION_CURR for class: Script1"
However, I have a string parameter:
i also tried: def result =' Project: *Autotests* \n Version *${VERSION_CURR}* \n'

How to use `StaplerResponse rsp` in `AsyncResourceDisposer.doStopTracking()`

I'm trying to remove (stop tracking) trackig item from Jenkins AsyncResourceDisposer (${JENKINS_URL}/administrativeMonitor/AsyncResourceDisposer) via groovy scripts (${JENKINS_URL}/script).
According to the Javadoc and source code
// Javadoc
#Restricted(value=org.kohsuke.accmod.restrictions.DoNotUse.class)
public org.kohsuke.stapler.HttpResponse doStopTracking(#QueryParameter
int id,
org.kohsuke.stapler.StaplerResponse rsp
)
// source code
#Restricted(DoNotUse.class)
#RequirePOST
public HttpResponse doStopTracking(#QueryParameter int id, StaplerResponse rsp) {
...
}
I'd like to know how to add org.kohsuke.stapler.StaplerResponse rsp in doStopTracking(int id, org.kohsuke.stapler.StaplerResponse rsp):
import org.jenkinsci.plugins.resourcedisposer.AsyncResourceDisposer
AsyncResourceDisposer disposer = AsyncResourceDisposer.get()
disposer.backlog.each {
disposer.doStopTracking( it.id, <what should I put here> )
}
Current I can get the item id, and the other informaitons like below:
import org.jenkinsci.plugins.resourcedisposer.AsyncResourceDisposer
AsyncResourceDisposer disposer = AsyncResourceDisposer.get()
String url = Jenkins.instance.rootUrl + disposer.url
disposer.getBacklog().each { item ->
println "\n${item.id} : \t${url}/stopTracking/?id=${item.id} : \t${item.class.simpleName} : \n" +
"\t${item.getLastState().getDisplayName()} : \n" +
"\t${item.getDisposable().node} : ${item.getDisposable().path}\n" +
"\t${item.toString()}"
}
If I'm go to the url "${url}/stopTracking/?id=${item.id}" in browser (login first), the item can be removed after click RETRY USING POST (as below)
So... I'm using the API call curl -H <crumbIssues> -X POST <url> by passed the disposer.doStopTracking(int, org.kohsuke.stapler.StaplerResponse) (still really wants know how to use it)
Before running the following script, Strict Crumb Issuers Plugin is necessary to be installed and configured (or setup -Dhudson.security.csrf.DefaultCrumbIssuer.EXCLUDE_SESSION_ID=true) due to SECURITY-626 : Improved CSRF protection since:
obtain a crumb using the /crumbIssuer/api URL will now fail to perform actions protected from CSRF unless the scripts retain the web session ID in subsequent requests.
Here is details:
import org.jenkinsci.plugins.resourcedisposer.AsyncResourceDisposer
import org.jenkinsci.plugins.strictcrumbissuer.StrictCrumbIssuer
AsyncResourceDisposer disposer = AsyncResourceDisposer.get()
StrictCrumbIssuer issuer = jenkins.model.Jenkins.instance.crumbIssuer
String jenkinsCrumb = "${issuer.crumbRequestField}:${issuer.crumb}"
String url = Jenkins.instance.rootUrl + disposer.url
disposer.getBacklog().each { item ->
println "\n ~~> removeing ${item.id} : "
[ 'bash', '-c', 'curl -s ' +
'-u <user>:<token> ' +
'-X POST ' +
"-H \"Content-Type: application/json\" " +
"-H \"Accept: application/json\" " +
"-H \"${jenkinsCrumb}\" " +
"${url}/stopTracking/?id=${item.id} "
].execute().with{
def stdout = new StringBuffer()
def stderr = new StringBuffer()
it.waitForProcessOutput( stdout, stderr )
println "EXIT CODE: ${it.exitValue()}"
println "ERROR: ${stderr}"
println "OUTPUT: ${stdout}"
}
}
Although, I still have a question... As we know that if the groovy script running in ${JENKINS_URL}/script, which means the "runner" is the administrator, so, how I can remove the specific user authorication '-u <user>:<token>' (by using the jenkins administrator authorication) in curl ?

issues with groovy script

I have the following groovy script that does the following
- checks all the builds for a specific job to see if it has the same build parameter as the current build
- stops the build with the same parameter as the current build
import hudson.tasks.Ant
def myBuildNumber = build.getEnvVars()['BUILD_NUMBER'].toInteger();
def myFractureNO = build.getEnvVars()['FRACTURE_NO'].toInteger();
def projectXml = new XmlSlurper().parseText("curl http://test.corp.test.com:8080/job/FractureAUT/api/xml".execute().text);
projectXml.build.each {
if(it.number.toInteger() < myBuildNumber)
{
def jobXml = new XmlSlurper().parseText(("curl http://test.corp.test.com:8080/job/FractureAUT/" + it.number + "/api/xml").execute().text);
def myparams = jobXml.getAction(hudson.model.ParametersAction.class);
for( p in myparams ) {
printlin p.name.toString();
if (p.name.toString == "FRACTURE_NO") {
if (p.value.toString() == myFractureNO.toString()) {
"curl http://test.corp.adobe.com:8080/job/FractureAUT/" + it.number + "/stop".execute().waitFor();
break;
}
}
}
}
}
the script outputs a bunch of urls to the screen because of the line:
def projectXml = new XmlSlurper().parseText("curl http://test.corp.test.com:8080/job/FractureAUT/api/xml".execute().text);
and it doesn't do what it suppose to do. How can I make it so that the line
def projectXml = new XmlSlurper().parseText("curl http://test.corp.test.com:8080/job/FractureAUT/api/xml".execute().text);
doesn't output the urls to the screen?
I get the following output
189http://test.corp.test.com:8080/job/FractureAUT/189/188http://test.corp.test.com:8080/job/FractureAUT/188/187http://test.corp.test.com:8080/job/FractureAUT/187/186http://test.corp.test.com:8080/job/FractureAUT/186/185http://test.corp.test.com:8080/job/FractureAUT/185/184http://test.corp.test.com:8080/job/FractureAUT/184/183http://test.corp.test.com:8080/job/FractureAUT/183/182http://test.corp.test.com:8080/job/FractureAUT/182/181http://test.corp.test.com:8080/job/FractureAUT/181/179http://test.corp.test.com:8080/job/FractureAUT/179/178http://test.corp.test.com:8080/job/FractureAUT/178/177http://test.corp.test.com:8080/job/FractureAUT/177/176http://test.corp.test.com:8080/job/FractureAUT/176/174http://test.corp.test.com:8080/job/FractureAUT/174/173http://test.corp.test.com:8080/job/FractureAUT/173/172http://test.corp.test.com:8080/job/FractureAUT/172/171http://test.corp.test.com:8080/job/FractureAUT/171/170http://test.corp.test.com:8080/job/FractureAUT/170/169http://test.corp.test.com:8080/job/FractureAUT/169/168http://test.corp.test.com:8080/job/FractureAUT/168/167http://test.corp.test.com:8080/job/FractureAUT/167/166http://test.corp.test.com:8080/job/FractureAUT/166/165http://test.corp.test.com:8080/job/FractureAUT/165/164http://test.corp.test.com:8080/job/FractureAUT/164/163http://test.corp.test.com:8080/job/FractureAUT/163/162http://test.corp.test.com:8080/job/FractureAUT/162/161http://test.corp.test.com:8080/job/FractureAUT/161/160http://test.corp.test.com:8080/job/FractureAUT/160/159http://test.corp.test.com:8080/job/FractureAUT/159/158http://test.corp.test.com:8080/job/FractureAUT/158/157http://test.corp.test.com:8080/job/FractureAUT/157/156http://test.corp.test.com:8080/job/FractureAUT/156/155http://test.corp.test.com:8080/job/FractureAUT/155/154http://test.corp.test.com:8080/job/FractureAUT/154/153http://test.corp.test.com:8080/job/FractureAUT/153/152http://test.corp.test.com:8080/job/FractureAUT/152/151http://test.corp.test.com:8080/job/FractureAUT/151/150http://test.corp.test.com:8080/job/FractureAUT/150/148http://test.corp.test.com:8080/job/FractureAUT/148/146http://test.corp.test.com:8080/job/FractureAUT/146/144http://test.corp.test.com:8080/job/FractureAUT/144/143http://test.corp.test.com:8080/job/FractureAUT/143/142http://test.corp.test.com:8080/job/FractureAUT/142/141http://test.corp.test.com:8080/job/FractureAUT/141/140http://test.corp.test.com:8080/job/FractureAUT/140/139http://test.corp.test.com:8080/job/FractureAUT/139/138http://test.corp.test.com:8080/job/FractureAUT/138/137http://test.corp.test.com:8080/job/FractureAUT/137/136http://test.corp.test.com:8080/job/FractureAUT/136/135http://test.corp.test.com:8080/job/FractureAUT/135/134http://test.corp.test.com:8080/job/FractureAUT/134/133http://test.corp.test.com:8080/job/FractureAUT/133/132http://test.corp.test.com:8080/job/FractureAUT/132/131http://test.corp.test.com:8080/job/FractureAUT/131/130http://test.corp.test.com:8080/job/FractureAUT/130/129http://test.corp.test.com:8080/job/FractureAUT/129/128http://test.corp.test.com:8080/job/FractureAUT/128/127http://test.corp.test.com:8080/job/FractureAUT/127/126http://test.corp.test.com:8080/job/FractureAUT/126/125http://test.corp.test.com:8080/job/FractureAUT/125/124http://test.corp.test.com:8080/job/FractureAUT/124/123http://test.corp.test.com:8080/job/FractureAUT/123/122http://test.corp.test.com:8080/job/FractureAUT/122/121http://test.corp.test.com:8080/job/FractureAUT/121/120http://test.corp.test.com:8080/job/FractureAUT/120/119http://test.corp.test.com:8080/job/FractureAUT/119/118http://test.corp.test.com:8080/job/FractureAUT/118/117http://test.corp.test.com:8080/job/FractureAUT/117/116http://test.corp.test.com:8080/job/FractureAUT/116/115http://test.corp.test.com:8080/job/FractureAUT/115/114http://test.corp.test.com:8080/job/FractureAUT/114/113http://test.corp.test.com:8080/job/FractureAUT/113/112http://test.corp.test.com:8080/job/FractureAUT/112/111http://test.corp.test.com:8080/job/FractureAUT/111/110http://test.corp.test.com:8080/job/FractureAUT/110/109http://test.corp.test.com:8080/job/FractureAUT/109/108http://test.corp.test.com:8080/job/FractureAUT/108/107http://test.corp.test.com:8080/job/FractureAUT/107/106http://test.corp.test.com:8080/job/FractureAUT/106/105http://test.corp.test.com:8080/job/FractureAUT/105/104http://test.corp.test.com:8080/job/FractureAUT/104/103http://test.corp.test.com:8080/job/FractureAUT/103/102http://test.corp.test.com:8080/job/FractureAUT/102/101http://test.corp.test.com:8080/job/FractureAUT/101/100http://test.corp.test.com:8080/job/FractureAUT/100/99http://test.corp.test.com:8080/job/FractureAUT/99/98http://test.corp.test.com:8080/job/FractureAUT/98/97http://test.corp.test.com:8080/job/FractureAUT/97/96http://test.corp.test.com:8080/job/FractureAUT/96/95http://test.corp.test.com:8080/job/FractureAUT/95/94http://test.corp.test.com:8080/job/FractureAUT/94/93http://test.corp.test.com:8080/job/FractureAUT/93/92http://test.corp.test.com:8080/job/FractureAUT/92/91http://test.corp.test.com:8080/job/FractureAUT/91/90http://test.corp.test.com:8080/job/FractureAUT/90/89http://test.corp.test.com:8080/job/FractureAUT/89/88http://test.corp.test.com:8080/job/FractureAUT/88/87http://test.corp.test.com:8080/job/FractureAUT/87/86http://test.corp.test.com:8080/job/FractureAUT/86/85http://test.corp.test.com:8080/job/FractureAUT/85/
Why do I get the output above?
Why are you invoking curl at all? Just retrieve the contents of the URL from groovy:
def xmlString = new URL("http://test.corp.test.com:8080/job/FractureAUT/api/xml").getText()
def projectXML = new XMLSlurper().parseText(xmlString)
XmlSlurper can parse the URL directly:
def projectXml = new XmlSlurper().parse('http://test.corp.test.com:8080/job/FractureAUT/api/xml')
I think the output on screen is because of curl. So can try to silence it with -s or --silent option:
def projectXml = new XmlSlurper().parseText("curl -s 'http://test.corp.test.com:8080/job/FractureAUT/api/xml'".execute().text);

Got NoClassDefFound when running jcraft sshexec from Grails controller

I've got groovy class which is calling AntBuilder.sshexec method to execute remote command. This is working fine when I'm running this Groovy class as Java application. But, when I'm trying to call this class/method from controller, I've got error "Could not create type sshexec due to java.lang.NoClassDefFoundError: com/jcraft/jsch/UserInfo".
Groovy class:
package filemanager
import com.jcraft.jsch.*
import com.jcraft.jsch.ChannelSftp.*
class FileWork {
String servName
String servUser
String servPassword
String servFolder
String localFolder
int servPort
String fileName
FileWork (String p_servName, String p_servUser, String p_servPassword, String p_servFolder, String p_localFolder, int p_servPort,String p_fileName) {
println 'Exec constructor'
this.servName = p_servName
this.servUser = p_servUser
this.servPassword = p_servPassword
this.servFolder = p_servFolder
this.localFolder = p_localFolder
this.servPort = p_servPort
this.fileName = p_fileName
}
.....
String runRemoteCommand () {//(Session p_ses) {
try {
def result = ''
def ant = new AntBuilder()
ant.sshexec(
host: servName,
port: servPort,
trust: true,
username: servUser,
password: servPassword,
command: "unzip -o ${servFolder}${fileName} -d ${servFolder}",
outputproperty: 'result',
verbose: false
)
return result
} catch (Exception e) {
println 'This is filemanager.FileWork.runRemoteCommandException'
e.printStackTrace();
}
}
}
Controller:
package filemanager
import com.jcraft.jsch.*
import com.jcraft.jsch.ChannelSftp.*
class ChooseToController {
def index(params) {
params.max = Math.min(max ?: 10, 100)
//render params
//model:[destServ: DestServ, fileName:fileName]
}
def copyToRemote(params) {
def destServ = DestServ.get(params.id)
//FileWork fileWork = new FileWork (destServ.getDestServName(), destServ.getDestServUser(), destServ.getDestServPassword(), destServ.getDestServFolder(), "C:/tmp/", destServ.getDestServPort(), params.fileName)
//Session ses = fileWork.connect()
//fileWork.copyToRemoteServ(ses)
//ses.disconnect()
FileWork fileWork3 = new FileWork ("###########", "test", "Test123", "/home/test/IN/", "C:/tmp/", 22, "1.zip")
String result = fileWork3.runRemoteCommand()
println(result)
}
}
Dependencies:
runtime "com.jcraft:jsch:0.1.51"
runtime "org.apache.ant:ant-jsch:1.8.1"
Error:
Could not create type sshexec due to java.lang.NoClassDefFoundError: com/jcraft/jsch/UserInfo
at org.apache.tools.ant.AntTypeDefinition.createAndSet(AntTypeDefinition.java:278)
at org.apache.tools.ant.AntTypeDefinition.icreate(AntTypeDefinition.java:219)
at org.apache.tools.ant.AntTypeDefinition.create(AntTypeDefinition.java:206)fro.....
Seems that not all classes are visible from grails runtime context...

commonsMultipartFile trouble

Hi I have am trying to implement a file upload in my application where the file uploaded is parsed and an entry is created in the database using that information.
def save = {
def file = request.getFile("file");
def filename = file.getOriginalFilename();
def type = filename.split('\\.');
if(!file.isEmpty()){
if(type[1] == "properties"){
redirect(action:"parsePropertyFile", params:params);
}
}
}
def parsePropertyFile = {
println "\n"
println params.file;
println "\n";
def f = params.file;
println f;
def filename = f.getOriginalFilename();
println filename;
}
when I print out f this is output:
org.springframework.web.multipart.commons.CommonsMultipartFile#29d32df9
but when I try to call getOriginalFilename() on f I get the following error:
groovy.lang.MissingMethodException: No signature of method:
java.lang.String.getOriginalFilename() is applicable for argument types: () values: []
I also printed out file from the save function and the output of that is also:
org.springframework.web.multipart.commons.CommonsMultipartFile#29d32df9
so why am I getting the error?
Instead of redirecting, can you just call your another function? Redirect will issue an http redirect with the file as param with no need.
if(type[1] == "properties") {
parsePropertyFile(file)
}
And then:
private def parsePropertyFile(def file) {
String filename = file.getOriginalFilename();
...
}
In your parsePropertyFile action you aren't getting a File object, you're getting the String from params. Just like in your save action, you need to do
def f = request.getFile('file')
println f.getOriginalFilename()

Resources