Powershell - 'PUT' files to URL that require parameters - bash

I've been trying for a while now to find a way in Powershell to take a list of files and upload them to a URL. (ive been able to do it in Unix Bash no problem).
However, I think there are issues with the parameters passing in and the URI building - causing a 500 internal server Error.
What I currently have is this:
$logfiles = "\\location\trace\logs"
$buildName = "NewBuild"
$EngineName = "DefaultEngine"
$request = "http://localhost:9005/parser/singleUploadTracerLib.do?
blockingUpload=true&autocommit=true&tracerSetName=$buildName&$EngineName"
foreach($f in $logfiles | Get-ChildItem) {
Invoke-WebRequest -Uri $Url -Verbose -Method PUT `
-InfileĀ $f.FullName -ContentType 'application/octet-stream'
}
When I do this in bash - i have no issues (see below for comparison) so i assume I need to pass in the prams another way?
In Bash(works):
ls -N ${pathToLogs} | grep clean | sort | xargs -I {} echo "-F
inputs[]=#${pathToLogs}{}" | xargs curl -X POST
"${Url}/parser/singleUploadTracerLib.do?
blockingUpload=true&autocommit=true&
tracerSetName=${buildName}&EngineName=${engineName}"
any kind person got any ideas? it would be much appreciated.

Related

Jenkins get status of last build with a specific parameter

I have a Jenkins freestyle job (yeah I know..) that sometimes needs to wait for another job with the same parameter to finish if it's running (or not run at all if it failed).
Using curl and the Jenkins API, is it possible to query a certain job and get the status of the last build where certainParam=certainValue?
(The reason I'm asking how to do that with curl is because it doesn't seem to be possible to do in freestyle job and the job can't be migrated to pipeilnes yet.. It seems like curl would be the east way..)
Thanks ahead!
So far I know there's no direct way to achieve it.
I wrote recursive script that search by the values on each build from the very last until match the information.
It prints every build url and the result of the query.
Dependencies
jq - install "jq.x86_64", Command-line JSON processor
Script
#!/bin/bash
user="admin"
pwd="11966e4dd8c33a730abf88e98fb952ebc3"
builds=$(curl -s --user $user:$pwd localhost:8080/job/test/api/json)
urls=$(echo $builds | jq '.builds[] | .url')
while read -r url
do
url=$(echo $url | sed -nr 's/"([^|]*)"/\1/p')
# get the build log
build=$(curl -s --user $user:$pwd "${url}api/json")
# transform the log in a simple structure
build=$(echo $build | jq '.result as $result | .actions[].parameters[]? | select(.name == "certainParam") | {(.name): .value, "result": $result}')
# check if the parameter value and the build result are the expected
result=$(echo $build | jq 'if .certainParam == "certainValue" and .result == "SUCCESS" then true else false end')
# print the result of each build
echo "url=${url}api/json;result=$result"
if [[ $result == true ]]
then
break
fi
done <<< $urls
Result
sh jenkins.sh
url=http://localhost:8080/job/test/12/api/json;result=false
url=http://localhost:8080/job/test/11/api/json;result=true

Use data from variable in bash

This is what I am trying to do in bash. Here is a PowerShell example:
$metaData = Invoke-RestMethod -Headers #{"Metadata"="true"} -URI http://169.254.169.254/metadata/instance?api-version=2019-04-30 -Method GET
$azEnv = $metadata.compute.azEnvironment
The above command gives me the value of azEnviroment from the Json output and saves that as a variable called $azEnv.
How can I do this in bash?
You can do something like this,
PC_VMSIZE=$(curl -s -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2018-10-01" | jq -r '.compute.vmSize')
export AZHPC_VMSIZE=${AZHPC_VMSIZE,,}
#echo "Running on $PC_VMSIZE"

Symfony command finding folder

I am trying to call command using symfony Process and command looks like:
$data = escapeshellarg("public\uploads\post_data.txt");
$url = "www.example.com/";
$process = new Process(['ab -n 10 -p '.$data.' -T application/x-www-form-urlencoded -w '. $url . 'test/send']);
$process->run();
When i call this command from terminal it works fine but when i try to call it using symfony command then i am getting:
The filename, directory name, or volume label syntax is incorrect. This error is windows terminal error as i know and i think the problem is with \ but i have tried to change it to \\, // and / but not helped.
In my point of view, your approach doesnt work, because new Process needs to be defined in a array, where each piece of the shellcmd is a entry in the array.
So for your case, you should have to write
$process = new Process(['ab', '-n', 10, '-p', $data, '-T', 'application/x-www-form-urlencoded', '-w', $url.'test/send']);
Or try with the fromShellCommandline static function
$process = Process::fromShellCommandline('ab -n 10 -p '.$data.' -T application/x-www-form-urlencoded -w '. $url . 'test/send');

Bash : Adding input file names in output results

I'm using cURL API to submit files on a API service and It returns back with something called taks_id for the files submitted.
#submitter.sh
creation_date=$(date +"%m_%d_%Y")
task_id_file="/home/results/$creation_date"_task_ids".csv"
for i in $(find $1 -type f);do
task_id="$(curl -s -F file=#$i http://X.X.X.X:XXXX/api/tiscale/v1/upload)"
final_task_id=$(echo $task_id | grep -o 'http\?://[^"]\+')
echo "$final_task_id" >> $task_id_file
done
#Result ( 10_13_2016_task_ids.csv )
http://192.168.122.24:8080/api/tiscale/v1/task/17
http://192.168.122.24:8080/api/tiscale/v1/task/18
http://192.168.122.24:8080/api/tiscale/v1/task/19
Run Method :
$./submitter.sh /home/files/pdf/
Now, Using[find $1 -type f] logic will get the full path with file name mentioned.
#find /home/files/pdf -type f
/home/files/pdf/country.pdf
/home/files/pdf/summary.pdf
/home/files/pdf/age.pdf
How can i add the file names along with cURL API response result. For example , When submitting "/home/files/country.pdf", The API might give the task_id with http://192.168.122.24:8080/api/tiscale/v1/task/17'`.
Expecting Result :
country.pdf,http://192.168.122.24:8080/api/tiscale/v1/task/17
summary.pdf,http://192.168.122.24:8080/api/tiscale/v1/task/18
age.pdf,http://192.168.122.24:8080/api/tiscale/v1/task/19
I'm beginner in Bash, Any suggestions on how to achieve this ?

Posting to Twitter account using Windows .bat file code

I'm trying to setup an automatic tweet script that runs after a git commit. I'm using Windows 7 have curl available in the command line.
I'm not sure how to set variables with the language windows scripts run and also am not positive about the oauth process.
I have the api key and secret, and also the consumer key and secret but I'm just not sure how to wrap it all together.
Here is a paper clipped mashup of code I'm trying to use as a foundation:
#!/bin/sh
# PATH modification needed for http_post and oauth_sign
export PATH=$PATH:/usr/local/bin
toplevel_path=`git rev-parse --show-toplevel`
toplevel_dir=`basename "$toplevel_path"`
branch=`git rev-parse --abbrev-ref HEAD`
subject=`git log --pretty=format:%s -n1`
hashtags="#code #$toplevel_dir"
tweet=$hashtags' ['$branch']: "'$subject'"'
# truncate tweets that are longer than 140 characters
if [ ${#tweet} -gt 140 ]
then
tweet_trunc=$(echo $tweet | cut -c1-137)
tweet=${tweet_trunc}...
fi
//set vars
consumer_key="mPijnvYpD0sHAY8r*******"
consumer_secret="OWuvuyQeYrT3ToJgyvNdR6baNuDldmTDF5IIJCI************"
access_token="2476143012-ld78CrgnNY3kUmD0QRdvIchXeDC13nO3********"
access_secret="3HTdOlf8jCVzPi5I9usV7rIbGFtM5f****************"
//build oauth
//post data
//example curl code found during research
curl --request 'POST' 'https://api.twitter.com/1.1/statuses/update.json' --header 'Authorization: OAuth oauth_consumer_key="mPijnvYpD0sHAY8r6fkox0KBj", oauth_nonce="OWuvuyQeYrT3ToJgyvNdR6baNuDldmTDF5IIJCIablQbyHA2PS", oauth_signature="Ba6IB8uH2SjtrK8a%2FgZnqCgvIKs%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1346207448", oauth_token="14814762-vvYtBOLX8hBAQ0i0f1k4wxrioG1jOk49MJrqn3myE", oauth_version="1.0"' --verbose -F "media[]=#mack.jpg" -F "status=Test from cURL" --header "Expect: "
Any help at all is appreciated.
Bro, on Windows you should use PowerShell now. .bat is lame!
$toplevel_path = git rev-parse --show-toplevel
$toplevel_dir = Split-Path $toplevel_path -Leaf
$branch = git rev-parse --abbrev-ref HEAD
$subject = git log --pretty=format:%s -n1
$hashtags = "#code #$toplevel_dir"
$tweet = '{0} [{1}]: "{2}"' -f $hashtags, $branch, $subject
if ($tweet.length -gt 140) {
$tweet = $tweet.substring(0,137)
}
$oauths =
'oauth_consumer_key="mPijnvYpD0sHAY8r6fkox0KBj"',
'oauth_nonce="OWuvuyQeYrT3ToJgyvNdR6baNuDldmTDF5IIJCIablQbyHA2PS"',
'oauth_signature="Ba6IB8uH2SjtrK8a%2FgZnqCgvIKs%3D"',
'oauth_signature_method="HMAC-SHA1"',
'oauth_timestamp="1346207448"',
'oauth_token="14814762-vvYtBOLX8hBAQ0i0f1k4wxrioG1jOk49MJrqn3myE"',
'oauth_version="1.0"'
$header = 'Authorization: OAuth {0}' -f ($oauths -join ',')
curl --verbose --request POST -F 'media[]=#mack.jpg' `
-F 'status=Test from cURL' --header 'Expect: ' `
--header $header https://api.twitter.com/1.1/statuses/update.json
command substitution is actually better than with Bash
PowerShell "printf" is cool
arrays are cool
the continuation character ` is too small though, hard to see

Resources