How to get the current Nodes where the Job is running - rundeck

I'm developing a job and the user can choose in which nodes can run, so the Node Filter is open to the convenient of the user.
When the job is starting I need to do a calculation based in the number of nodes chosed by the user, exist a way to get this number?
Regards,
Alejandro L

By design, that information is only available after the execution, so, a good approach is to call the job via API (in a script step) and with the execution ID number (available in the API call output) you can list and count the nodes, e.g:
#!/bin/bash
nodes=$(curl -s -X GET "http://localhost:4440/api/41/execution/16" \
--header "Accept: application/json" \
--header "X-Rundeck-Auth-Token: your_user_token" \
| jq -r '.successfulNodes | . []')
number_of_nodes=$(echo $nodes | wc -w)
echo "Number of nodes: $number_of_nodes"
This example needs jq to extract the nodes from the API response.
Anyway, your request sounds good for an enhancement request, please suggest that here.

a workaround to take would be using job.filter variable
so if you do #job.filter#
it returns a string with the list of nodes like us-east-1-0,us-east-1-1,us-east-1-2
if you save it as a string, and then split the string on ',' then you get an array of nodes:
IFS=',' read -r -a array <<< "$string"
and then you can get the number of nodes by
echo ${#array[#]}
Note
as #MegaDrive68k mentioned this won't work if use select all node with the use of filter .*

Related

gitlab api equivalent to git log (compare branch with tag) and list commit message json

i need some equivalent command to the following command:
git log --pretty="* %s%n%b" BRANCH2...TAGNAME1
i tried already the compare feature of the api but sadly without the result i expected:
curl -s --header "PRIVATE-TOKEN: XXXXXXX" "https://gitlab.localhost/api/v4/projects/3/repository/compare?from=BRANCH2&to=TAGNAME1&straight=true" | jq .
what i get is:
"commit": null,
"commits": [],
but git log gives me the following:
* commit 1 description
* commit 2 description
so what i search and need is to compare BRANCH2 with TAGNAME1 and list the different commits.
this is later on needed in order to generate a changelog file which is pushed to a new tag.
This would be the compare API
Using the gitlab-org/gitlab repo as an example, to compare the tags/branches v14.8.0-ee to v14.9.0-ee:
curl https://gitlab.com/api/v4/projects/278964/repository/compare?from=v14.8.0-ee&to=v14.9.0-ee
If you're not getting the response you're expecting, try swapping the from and to arguments and/or omitting the straight argument.
thank you!
the solution in the end was change the from and to so in the end it works like that:
curl -s --header "PRIVATE-TOKEN: XXXXXXX" "https://gitlab.localhost/api/v4/projects/3/repository/compare?from=TAGNAME1&to=BRANCH2&straight=true" | jq .

how to use filter parameters and Grep command together for listing all GITHUB pull request from a repository

I am looking for an option to get all pull requests with ID, Name, Date, and count of files committed from GitHub.
I managed to get all pull requests using API, but listing only the required parameter is not working. Can anyone help?
curl -X GET -u :https://api.github.com/repos/XXXX/XXXX/pulls?state=all&page=1&per_page=100| egrep -w 'title|number|state|created_at|updated_at'
Grep is working if the query is like below, without multiple filter parameters (state=all&page=1&per_page=100)
curl -X GET -u :https://api.github.com/repos/XXXX/XXXX/pulls?state=all| egrep -w 'title|number|state|created_at|updated_at'
Can anyone help how to list only the required data with multipe filter parameter.
try grep with something like this:
grep -Pa 'state'

Powershell: loop for equivalent

Currently, I'm performing this command on linux shell:
for binary in $(curl -s -X GET "${FHIR_SERVER}/\$export-poll-status?_jobId=${JOB_ID}" -H "Authorization: Bearer ${ACCESS_TOKEN}" | jq -r ".output[].url"); \
do wget --header="Authorization: Bearer ${ACCESS_TOKEN}" ${binary} -O ->>patients-pre.json;
done
Is there any way to get this on powershell?
While not knowing exactly the format of your incoming json, I'd use something like this in powershell / pwsh:
curl ... | % { wget (convertto-json $_).output.url } >> patients-pre.json
The % is an alias for ForEach-Object which will iterate over objects (or lines of text) sent from the left side of the pipe. Powershell is interesting because when using the pipe symbol, there's an implicit for / do / done operation going on.
curl is on all modern versions of windows, so you don't need to change that. wget isn't, but you could install it, or use curl again.
If you want to go full powershell, look at invoke-restmethod ( https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.2 ). This can do the jobs of both curl and wget in your example, and also automatically handle json returns to give you a structured object instead of text (which is what convertto-json is doing above.)

Filtering on labels in Docker API not working (possible bug?)

I'm using the Docker API to get info on containers in JSON format. Basically, I want to do a filter based on label values, but it is not working (just returns all containers). This filter query DOES work if you just use the command line docker, i.e.:
docker ps -a -f label=owner=fred -f label=speccont=true
However, if I try to do the equivalent filter query using the API, it just returns ALL containers (no filtering done), i.e.:
curl -s --unix-socket /var/run/docker.sock http:/containers/json?all=true&filters={"label":["speccont=true","owner=fred"]}
Note that I do uri escape the filters param when I execute it, but am just showing it here unescaped for readability.
Am I doing something wrong here? Or does this seem to be a bug in the Docker API? Thanks for any help you can give!
The correct syntax for filtering containers by label as of Docker API v1.41 is
curl -s -G -X GET --unix-socket /var/run/docker.sock http://localhost/containers/json" \
--data 'all=true' \
--data-urlencode 'filters={"label":["speccont=true","owner=fred"]}'
Note the automatic URL encoding as mentioned in this stackexchange post.
I felt there was a bug with API too. But turns out there is none. I am on API version 1.30.
I get desired results with this call:
curl -sS localhost:4243/containers/json?filters=%7B%22ancestor%22%3A%20%5B%222bab985010c3%22%5D%7D
I got the url escaped string using used above with:
python -c 'import urllib; print urllib.quote("""{"ancestor": ["2bab985010c3"]}""")'

ServiceM8 api email - how to relate to job diary

I can send an email from a ServiceM8 account through the ServiceM8 API 'message services' (http://developer.servicem8.com/docs/platform-services/message-services/), and read the resulting ServiceM8 message-id.
But I would like to relate that message to a specific job within ServiceM8, so that it will appear as an email item in that job's diary in the ServiceM8 web application. (Emails sent from within the ServiceM8 web application are related to the diary and appear there - my question is about how to do this from the API).
Worst case, I could create a new 'Note' containing the email text and add that to the job in the hope that it would show up in the diary in the web application as a note.
But I want to check there isn't an easier way since sending the email results in there already being a relatable message-id available within ServiceM8.
Thanks
Using the messaging services API, can't be done. Using the web API, you can do just that.
There's an authorisation code required, which is specific to your account and to this function, you only need to retrieve it once, and then you can integrate that specific URL into your code. It's contained within the ClientSidePlatform_PerSessionSetup URL.
Here is a script that will grab the E-mail URL specific to your login:
Syntax: ./getsm8emailurl.sh "email#address.com" "password"
#!/usr/bin/env bash
#getsm8emailurl.sh
#Create Basic auth
user="$1"
pass="$2"
pass="$(echo -n "${pass}" | md5sum | cut -f1 -d' ')"
auth="$(echo -n "${user}:${pass}" | base64)"
#Get Account specific e-mail url
email_url="https://go.servicem8.com/$(curl --compressed -s -L "https://go.servicem8.com/$(curl --compressed -s -L "https://go.servicem8.com/" -H "Authorization: Basic $auth" | grep -o 'ClientSidePlatform_PerSessionSetup.[^"]*' | grep -v "s_boolFailover")" -H "Authorization: Basic $auth" | grep -o "PluginEmailClient_SendEmail.[^']*")"
#Output base e-mail URL
echo "$email_url"
Once you have the email url, (will start with https://go.servicem8.com/PluginEmailClient_SendEmail and will end with the s_auth code), you can use it like any other rest endpoint.
Required Header Values:
Authorization (same as regular API)
Required Post Params:
s_form_values="guid-to-cc-subject-msg-job_id-attachedFiles-attachedContacts-strRegardingObjectUUID-strRegardingObject-boolAllowDirectReply"
(these have to stay just as they are)
s_auth="your_account_s_auth_code"
to="recipient#domain.com"
Optional Post Params:
subject="subject"
msg="html message body"
boolAllowDirectReply="true|false" (Can recipient reply directly to job diary)
strRegardingObject="job|company"
strRegardingObjectUUID="job|company uuid"
DEMO
#!/usr/bin/env bash
#sendemail.sh
#demo here using random auth codes and uuids
curl --compressed -s "https://go.servicem8.com/PluginEmailClient_SendEmail" \
-H "Authorization: Basic dGVzdHVzZXJAdGVzdGRvbWFpbi5jb206dGVzdHBhc3M=" \
-d s_form_values=guid-to-cc-subject-msg-job_id-attachedFiles-attachedContacts-strRegardingObjectUUID-strRegardingObject-boolAllowDirectReply \
-d s_auth="6akj209db12bikbs01hbobi3r0fws7j2" \
-d boolAllowDirectReply=true \
-d strRegardingObject=job \
-d strRegardingObjectUUID="512b3b2a-007e-431b-be23-4bd812f2aeaf" \
-d to="test#testdomain.com" \
-d subject="Job Diary E-mail" \
-d msg="hello"
Edit/Update/Disclaimer:
This information is for convenience and efficiency - memos, quick tasks, notifications, updates, etc. This isn't to be relied upon for critical business operations as it is undocumented, and since it does not process JS like a browser would, it could stop working if the inner workings of the service changed.