How to check if commands inside the deployment.yaml are working? - bash

I'm trying to write some bash commands inside my deployment.yaml file. I want to execute this commands at the postStart.
How can I check if the commands are working well? And if possible, how to get the node IP address?
lifecycle:
postStart:
exec:
command:
- "sh"
- "-c"
- |
GATEWAY_HTTPS_NAME="${GATEWAY_SERVICE_NAME}_SERVICE_PORT_HTTPS"
GATEWAY_HTTPS_PORT=$(eval "echo \$$GATEWAY_HTTPS_NAME")
cat /app/gateway/ip
BOOL=true
while [ $BOOL ]; do
if [ "$GATEWAY_HTTPS_PORT" != '' ]; then
nohup sh -c "sleep 30; cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k https://[]/actuator/refresh" &
ROUTES=$(nohup sh -c "cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k https://[]/actuator/gateway/routes")
else
nohup sh -c "sleep 30; cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k http://[]/actuator/refresh" &
ROUTES=$(nohup sh -c "cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k http://[]/actuator/gateway/routes")
fi
NODE_IP=???
echo $ROUTES
if [ $ROUTES[*] =~ $NODE_IP ]; then
BOOL=$false
fi
BOOL=$false
sleep 10
done

You can validate the command using exit status, I usually do that by adding a double pipe which will output the exit status if something went wrong, here is an example:
ls /unexistant/dir &>/dev/null && echo Success, Code=$? || echo Something went wrong, Code=$?
This will obviously throw an error and run the commands after double pipe since the status code not equals 0 will return Something went wrong, Code=2
Instead, if we run this:
ls /existant/dir &>/dev/null && echo Success, Code=$? || echo Something went wrong, Code=$?
This will run the first command with success and exit status will be 0 which means success, this is the output: Success, Code=0
You can of course play around with this and read more about double pipe

Related

How to get Http Status Code and Vourbose logs by Curl function

We try to check the API connection and response by several curl commands as follows, we have two issue though.
-- function
function exec_cmd() {
cmd=$#
echo "curl command:" ${cmd}
response=$(${cmd} --max-time ${timeout_sec} -o ${LogFile} -w '%{http_code}\n' -S)
echo statud_code:${response}
}
-- curl command#1 ...
exec_cmd curl -v -x put <LBFrontIP/ApiEndpoint1> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"SystemId":"XXXX", "AppNo":"99999999999"}'
-- curl command#2
exec_cmd curl -v -x put <LBFrontIP/ApiEndpoint2> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"sbSystemId":"XXXX", "email":"XXX#example.com",
"PhoneNo":"99999999999"}'
-- curl command#3
exec_cmd curl -v -x put <LBFrontIP/ApiEndpoint3> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '
{
"ID1": "XXXXX",
"ID2": "1q2w3e4r5t",
"applyNo": "00db182faef74e779ca958681127bcec", ...
"docLiveScore": "0.1391"
}'
Issue #1 : cmd=$# can get the curl command but if that curl command contains # or return code, it can't work as expected.
curl command#1 is working fine, but curl command#2 and #3 is not, since bash misunderstands # and return code in data option as terminate of the command line. Curl command can work by itself though.
Also tried cmd=$* or cmd="$#" but not working so far.
Issue #2 : We'd like to get status_code for our quick check, but on the same time, we'd like to get the detail response in the log.
Is there any better way to get both of status_code and verbose log by appropriate function?
Above commands over write the verbose logs...
Given the complexity of the commands involved, I am tempted to say that you should avoid command parsing and read the command lines from a job configuration file.
#!/bin/bash
START=`pwd`
echo ${START}
BASE=`basename "$0" ".sh" `
ThisDATE=`date '+%Y%m%d_%H%M%S' `
LogPrefix="${START}/${BASE}.${ThisDATE}" ; rm -f "${LogPrefix}"*
BatchFile="${START}/${BASE}.${ThisDATE}.config" ; rm -f "${BatchFile}"
cat >$BatchFile <<-!EnDoFbAtCh
curl -v -x put <LBFrontIP/ApiEndpoint1> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"SystemId":"XXXX", "AppNo":"99999999999"}'
curl -v -x put <LBFrontIP/ApiEndpoint2> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{"sbSystemId":"XXXX", "email":"XXX#example.com", "PhoneNo":"99999999999"}'
curl -v -x put <LBFrontIP/ApiEndpoint3> -H 'Content-Type:application/json; charset=utf-8' -H 'Host:<HostName>' -d '{ "ID1": "XXXXX", "ID2": "1q2w3e4r5t", "applyNo": "00db182faef74e779ca958681127bcec", ... "docLiveScore": "0.1391" }'
!EnDoFbAtCh
function exec_cmd() {
echo "curl command: ${cmd}"
${cmd} --max-time ${timeout_sec} -o ${LogFile} -w '%{http_code}\n' -S
RC=$?
echo "statud_code:${RC}"
}
index=1
while read cmd
do
if [ -z "${cmd}" ] ; then break ; fi
LogFile="${LogPrefix}.${index}.log"
exec_cmd
index=`expr ${index} + 1 `
done < "${BatchFile}"

Cookie is saved, but when I send a request, I get an un-logged-in status

I am writing a script to save a website locally. Specifically, I am trying to download articles from a website while logged in. However, when I run the following command, it saves the website without logging in. cookies.txt has the proper information, probably "JSON=$(curl -b "cookies.txt" -c "cookies.txt" -s "${API}&page =$i")" seems to be the problem, does anyone know why this happens?
#!/bin/bash
curl -X POST -H "Content-Type: application/json" -c "cookies.txt" -d '{"login":"user_name","password":"password"}' https://note.com/api/v1/sessions/sign_in --cookie-jar cookies.txt
#info
NAME=somebody
API="https://note.com/api/v2/creators/${NAME}/contents?kind=note"
TOTAL_COUNT=$(curl -s $API | jq ".data.totalCount")
END=$(( $TOTAL_COUNT / 6 + 1 ))
echo "we will get ${NAME}'s articles"
for i in $(seq 1 $END); do
JSON=$(curl -b "cookies.txt" -c "cookies.txt" -s "${API}&page=$i")
IDS=($(echo $JSON | jq '.data.contents[].id'))
URLS=($(echo $JSON | jq -r '.data.contents[].noteUrl'))
IDS_COUNT=$(( ${#IDS[#]} - 1 ))
for j in $(seq 0 $IDS_COUNT); do
ID=${IDS[$j]}
URL=${URLS[$j]}
mkdir -p "note/${NAME}"
echo "ID: ${ID}"
monolith.exe $URL -s -o "note/${NAME}/${ID}.html"
done
done
echo "end"

How to get the result of a command in a HERE_doc in Bash

I'm using the at command to schedule a job in the future.
DoCurlAt () {
if [ -n "${AuthToken:-}" ] ; then
$4 << 'EOF'
curl -s -H "${AuthHeader:-}" -H "$1" --data-urlencode "$2" "$3"
EOF
Exitcode=$?
fi
WriteLog Output Info "AT Output: $AtOutput Exitcode: $Exitcode"
}
How can I capture the result of the at in a variable called $AtOutput?
I tried with
AtOutput=$(bash $4 << EOF
curl -s -H "${AuthHeader:-}" -H "$1" --data-urlencode "$2" "$3"
EOF
)
But that does't really give any result.
Also tried with:
AtOutput=$(curl -s -H "${AuthHeader:-}" -H "$1" --data-urlencode "$2" "$3" | at "$4")
But I would prefer to use the HERE-doc.
The function is called with
DoCurlAt "$AcceptJson" "argString=$ArgString" "$ApiUrl/$ApiVersion/job/$JobUid/run" "$OneTime"
$OneTime ($4) could be for example "at 15:19 today" The output is mostly something like this:
job 7 at 2016-08-16 15:30
at writes to standard error, not standard output. Use the 2>&1 redirection to copy standard error to standard output first.
$ at_output=$( echo "cmd" | at "$when" 2>&1 )

if condition in shell scripting based on 404 unauthorized error

Here is my shell script. I want to put a condition to my curl commands based on the status i get. I need help in grepping "Http/1.1 401 Unauthorized" from the first curl command. After that i need to put it in a condition if status is 401, execute 2nd and 3rd curl command. Pls help
STATUS="HTTP/1.1 401 Unauthorized"
read $STATUS
for ((i=1; i<=2000; i++)); do
curl -i -vs -X POST -D post.txt -H "$SESSION_TOKEN" -H "$AUTH_TOKEN" -H "Accept:$ACCEPT_HEADER" -H "Content-Type:text/plain" "http://$BASE_URI/api/$PLATFORM//$CHANNEL_ID/subscription" | grep -e "*Unauth*" >> post.txt
if [$STATUS]
then
curl -i -vs -X POST -D tmp.txt -H "Content-Type:text/plain" --data "$SECRET" -H "Accept:$ACCEPT_HEADER" -H "Connection:close" http://$BASE_URI/api/${PLATFORM}/authenticate >> tmp1.txt
SESSION_TOKEN=`grep SessionToken tmp.txt`
curl -i -vs -X POST -D tmp2.txt -H "$SESSION_TOKEN" -H "Content-Type:text/plain" -H "Content-Type:application/json" --data "{ \"username\":\"$USER_NAME\",\"password\":\"$PASSWORD\", \"rememberMe\":\"false\"}" http://$BASE_URI/api/web/users/authenticate >> data.json
AUTH_TOKEN=`grep Authorization tmp2.txt`
continue
fi
done
The proper solution :
res=$(curl -s -o /dev/null -w '%{http_code}\n' http://google.fr)
if ((res == 404)); then
echo "404 spotted"
fi
or
res=$(curl -s -o /dev/null -w '%{http_code}\n' http://google.fr)
if [[ $res == 404 ]]; then
echo "404 spotted"
fi
Check
man curl | less +/'^ *-w'
You have to capture the curl output and examine the status line:
output=$( curl -i ... )
if [[ $output == "$STATUS"* ]]; then
# I was unauthorized
else
# ...
fi
Be aware that [ (and [[ I use in my answer)
is not mere syntax, it is a command, and as such it requires a space between it and its arguments

How do i write a shell script for checking website live or not

I am writing a shell script to monitor website live or not and send an email alert below is my code
#!/bin/bash
if [[ curl -s --head --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null] && [ curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null ]]
then echo "The HTTP server on opx.com and oss.com is up!" #> /dev/null
else
msg="The HTTP server opx.com Or oss.com is down "
email="opx-noc#opx.com"
curl --data "body=$msg &to=$email &subject=$msg" https://opx.com/email/send
fi;
if i run this code i got
./Monitoring_Opx_Oss: line 2: conditional binary operator expected
./Monitoring_Opx_Oss: line 2: syntax error near `-s'
./Monitoring_Opx_Oss: line 2: `if [[ curl -s --head --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null] && [ curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null ]] '
Please correct me...
Change it do this:
if [ $(curl -s --head --request GET http://opx.opera.com/opx/version | grep "200 OK" > /dev/null) ] && [ $(curl -s --head --request GET http://oss.opera.com/version | grep "200 OK" > /dev/null) ]
To check the status of a command inside an if, you have to do it like
if [ $(command) ]
while you were using
if [ command]
note also the need of spaces around [ ]: if [_space_ command _space_ ]
Update
Based on Ansgar Wiechers's comment, you can also use the following:
if curl -s --head --request GET http://opx.com/opx/version | grep "200 OK" > /dev/null && curl -s --head --request GET http://oss.com/version | grep "200 OK" > /dev/null;
That is,
if command && command

Resources