How to add def variable into echo commnad inside a shell script in jenkins? - jenkins-pipeline

script {
now = new Date()
time = now.format("%D", TimeZone.getTimeZone('IST'))
CREATE = sh (
script: ' echo "curl -v -u Jbdi00QTpDnrEJ37IUy9:X -H \\"Content-Type: application/json\\" -d \' { \\"description\\": \\" Descrption of ticket\\", \\"subject\\": \\"Ticket from Jenkins ${time}\\", \\"email\\": \\" tom#outerspace.com \\", \\"priority\\": 1, \\"status\\": 2}\' -X POST \'https://srikartest1.freshpo.com/api/v2/tickets\' | python -m json.tool"',
returnStdout: true
)
echo "${CREATE}"
}
in the output the time is not inserting the echo command
output:
echo 'curl -v -u Jbdi00QTpDnrEJ37IUy9:X -H "Content-Type: application/json" -d '\'' { "description": " Descrption of ticket", "subject": "Ticket from Jenkins ", "email": " tom#outerspace.com ", "priority": 1, "status": 2}'\'' -X POST '\''https://srikartest1.freshpo.com/api/v2/tickets'\'' | python -m json.tool'

you have to escape the $ in groovy -
script {
now = new Date()
time = now.format("dd", TimeZone.getTimeZone('IST'))
CREATE = $/ sh (
script: ' echo "curl -v -u Jbdi00QTpDnrEJ37IUy9:X -H "Content-Type: application/json" -d ' { "description": " Descrption of ticket", "subject": "Ticket from Jenkins ${time} ", "email": " tom#outerspace.com ", "priority": 1, "status": 2}' -X POST 'https://srikartest1.freshpo.com/api/v2/tickets' | python -m json.tool"',
returnStdout: true
) /$
echo "${CREATE}"
}

Related

Not able to pass argument to curl command in shell script

I am trying to run this shell script but the value of val1 is not being replaced in the CURL command
gcs_ip="172.16.0.81:8080"
run(){
val1=$1
echo "Migration with clickhouse worker thread: $1, batchsize: , consumer thread: , pagesize:" | tee -a log_migr.log result_migr.log
echo "curl -X PUT "http://$gcs_ip/gcs/api/v1/config""
curl -X PUT "http://$gcs_ip/gcs/api/v1/config" -H "accept: application/json" -H "Content-Type: application/json" -d '[ { "application": "pm", "instance": "-", "partition": "clickhouse", "name": "migration.clickhouse.workers", "value": "$val1" }]'
}
run 10

Jenkins shell hiding stacktrace loses curl output

I am trying to hide stacktrace from the following script:
def status = "false"
while (status.equals("false")) {
sleep 5
status = sh(
script: "curl -s -H 'Accept: application/json' http://my.ip | jq \'.completed\' ",
returnStdout: true
).trim()
echo "status: ${status}"
}
Output:
+ curl -s -H 'Accept: application/json' 'http://my.ip'
+ jq .completed
status: true
If I only want to see the output message I have to write "set +x" in the scripts body. But this results in status returning as NULL.
status = sh(
script:'''
set +x
script: "curl -s -H 'Accept: application/json' http://my.ip | jq \'.completed\'
''',
returnStdout: true
).trim()
Output:
status: NULL
Why does the output get lost and is there any other way to remove the stack trace?
The fix here is that I declared multi line script wrong. For this case it should use double quotes not single. So the solution:
script: """
my
script
here
""",

jq shell script: aggregate iteration content into json body

I'm creating a shell script.
I need to create a curl json body according the a key-value list content. This content is splitted by way of a awk which generate a two column table:
KVS_VARIABLES=$(awk -F= '!($1 && $2 && NF==2) { print "File failed validation on line " NR | "cat 1>&2"; next } { print $1, $2 }' $f)
Example output:
VAR1 VAL1
VAR2 VAL2
VAR3 VAL3
So, this table is iterated on a while iteration and each key and value are splitted:
echo "$KVS_VARIABLES" | while read -r kv
do
key=$(echo $kv | awk '{print $1}')
value=$(echo $kv | awk '{print $2}')
done
So, I need some way to aggregate this content into a json document in order to send it out using curl:
curl -k \
-X PUT \
-d #- \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
"$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_ID" <<-EOF
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
"$key": "$value" <<<<<<<<<<<<<(1)>>>>>>>>>>>>>>
}
}
EOF
So, on <<<<<<<<<<<<<(1)>>>>>>>>>>>>>> I need to aggregate each key and value propagation.`
So, in this case I'd need to generate:
"VAR1": "VAL1",
"VAR2": "VAL2",
"VAR3": "VAL3"
and then insert it inside "stringData":
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
<<<<<<<<<<<<<(1)>>>>>>>>>>>>>>
}
}
So, after all:
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
"VAR1": "VAL1",
"VAR2": "VAL2",
"VAR3": "VAL3"
}
}
jq is installed.
Any ideas?
You don't need an awk statement inside the while loop, but just read the key value pairs inside the read command itself.
Also storing awk output in a variable and later parsing is an anti-pattern. You could use the process substitution feature provided by the shell, the < <() part will slurp the output of a command as if it were appearing on a file (or) use the here-strings
json=$(cat <<-EOF
{
"kind": "Secret",
"apiVersion": "v1",
"metadata": {
"name": "$SECRET_ID"
},
"stringData": {
}
}
EOF
)
while read -r key value; do
json=$(echo "$json" | jq ".stringData += { \"$key\" : \"$value\" }")
done< <(awk -F= '!($1 && $2 && NF==2) { print "File failed validation on line " NR | "cat 1>&2"; next } { print $1, $2 }' $f)
You could now use the variable "$json" in the curl as
curl -k \
-X PUT \
-d #- \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
"$SERVER_URL/api/v1/namespaces/$NAMESPACE/secrets/$SECRET_ID" <<<"$json"

Unable to send large files to elasticsearch using curl: argument too long

This is the script i used to export some documents to elasticsearch but no luck
#!/bin/ksh
set -v
trap read debug
date=$(date +%Y-%m-%d);
echo $date;
config_file="/home/p.sshanm/reports_elastic.cfg";
echo $config_file;
URL="http://p-acqpes-app01.wirecard.sys:9200/reports-"$date"";
echo $URL;
find /transfers/documents/*/done/ -type f -name "ABC-Record*_${date}*.csv"|
while IFS='' read -r -d '' filename
do
echo "filename : ${filename}"
var=$(base64 "$filename"| perl -pe 's/\n//g');
#if i use below it will fail as argument too long , so i used with curl option #
# var1= $(curl -XPUT 'http://localhost:9200/reports-'$date'/document/reports?pipeline=attachment&pretty' -d' { "data" : "'$var'" }')
var1=$(curl -X PUT -H "Content-Type: application/json" -d #- "$URL" >>CURLDATA
{ "data": "$var" }
CURL_DATA)
done;
If i use below it as
var1= $(curl -XPUT 'http://localhost:9200/reports-'$date'/document/reports?pipeline=attachment&pretty' -d' { "data" : "'$var'" }')
will fail as below, so i used with curl option #
argument too long
Your syntax to read from stdin is wrong, the here-doc string should have been (<<) and the de-limiters are mis-matching use CURL_DATA at both places.
curl -X PUT -H "Content-Type: application/json" -d #- "$URL" <<CURL_DATA
{ "data": "$var" }
CURL_DATA

Bash store output in variable, parse, and store a few values for later use

So, i'm trying to write a script to create a server on Rackspace Cloud. I've been able to get the script to successfully create a server. Although, after the server is created i need to get a couple of pieces of data out of the output and store them in a variable for later use in the same script (going to do a couple things on the new server after creation).
Here is the script i'm using to create the server:
#!/bin/bash
# Ask user to continue or not
read -p "You are about to setup a new Rackspace cloud server. Would you like to continue (y/n)? " cont
if [ "$cont" == "y" ]; then
echo "Starting server setup script..."
# Ask questions to get login creds
read -p "Rackspace Username? " username
read -p "Rackspace API Key? " apikey
# Get Rackspace token
echo "Getting token..."
token=$(curl -s -X POST https://auth.api.rackspacecloud.com/v2.0/tokens \
-d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'"$username"'", "apiKey":"'"$apikey"'" } } }' \
-H "Content-type: application/json" \
| python -m json.tool \
| python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["id"]')
echo "...done!"
# Get Rackspace account id
echo "Getting account id..."
account=$(curl -s -X POST https://auth.api.rackspacecloud.com/v2.0/tokens \
-d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'"$username"'", "apiKey":"'"$apikey"'" } } }' \
-H "Content-type: application/json" \
| python -m json.tool \
| python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["tenant"]["id"]')
echo "...done!"
# Create a new Rackspace cloud server
echo "Creating a new Rackspace cloud server..."
serverpassword=$(curl -s -X POST https://dfw.servers.api.rackspacecloud.com/v2/$account/servers \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-Auth-Project-Id: test-project" \
-T server_build.json \
| python -m json.tool \
| python -c 'import sys, json; print json.load(sys.stdin)["server"]["adminPass"]')
echo "...done!"
echo "Your new server has been created!"
read -p "Would you like to continue on to setting up your new server (y/n)? " cont2
if [ "$cont2" == "y" ]; then
exit
else
echo "Here is your root password for this server (you must write this down now): $serverpassword"
exit
fi
else
echo "You have chosen not to setup a new Rackspace server."
exit
fi
You can see that the token and account variables are being set and used in the creation curl command. The last curl command outputs something like this: (removed real values)
{
"server": {
"OS-DCF:diskConfig": "AUTO",
"adminPass": "************",
"id": "************",
"links": [
{
"href": "https://dfw.servers.api.rackspacecloud.com/v2/...",
"rel": "self"
},
{
"href": "https://dfw.servers.api.rackspacecloud.com/...",
"rel": "bookmark"
}
]
}
}
I'd like to be able to store both the adminPass and id in two different variables. How can i do this? I also know that by solving this issue i will be able to refactor the script when getting token and account values by using one curl command instead of two different ones.
Thanks for any help!
I appreciate all the help. I was able to figure out what i needed to do. Maybe my original question wasn't worded in a way to get the best answers but hopefully this resolution will explain better what i was trying to accomplish.
I ended up using these commands for the 3rd curl command and to set the variables i needed:
serverinfo=$(curl -s https://dfw.servers.api.rackspacecloud.com/v2/731174/servers \
-X POST \
-H "Content-Type: application/json" \
-H "X-Auth-Token: $token" \
-H "X-Auth-Project-Id: test-project" \
-T server_build.json | python -m json.tool)
serverpass=$(echo $serverinfo | python -c 'import sys, json; print json.load(sys.stdin)["server"]["adminPass"]')
This allowed me to run the curl command to setup the server and then grab the adminPass from the output of that command and set it to the serverpass variable for later use.
Using this concept i was also able to remove the 2nd curl command since it was a duplicate command just to set a variable. I can now use these commands to rub the curl once and set both variables from its output:
tokensinfo=$(curl -s -X POST https://auth.api.rackspacecloud.com/v2.0/tokens \
-d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'"$username"'", "apiKey":"'"$apikey"'" } } }' \
-H "Content-type: application/json")
token=$(echo $tokensinfo | python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["id"]')
account=$(echo $tokensinfo | python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["tenant"]["id"]')
Hope that better explains what i was going for. Thanks for the help, it did help me arrive at this solution.
you can grab adminpass and id from the output and store in new variable like
var1=$(grep "adminPass" output.txt | cut -d: --complement -f1)
var2=$(grep "id" output.txt | cut -d: --complement -f1)
echo -e $var1
echo -e $var2
for here i assume the third command output you redirected to output.txt file.

Resources