upcmd / up

UP - Ultimate Provisioner CLI
https://upcmd.netlify.app/
MIT License
50 stars 4 forks source link

(question): how to debug error codes from curl ? #21

Closed lukasmrtvy closed 2 years ago

lukasmrtvy commented 3 years ago

Any idea, how to loop over curl result ( json ) and at the same check if curl reponse code = 200 or 201 ; else fail ?

Usually this is possible:

STATUSCODE=$(curl --silent --output /dev/stderr --write-out "%{http_code}" --request POST --header "PRIVATE-TOKEN: $MYTOKEN" "$URL)

if test $STATUSCODE -ne 201; then
  exit 1
fi

But I am not sure, if is possible to register stderr:

name: Main
task:
  - func: shell
    name: statuses
    do:
    - 'curl -sL --output /dev/stderr --write-out "%{http_code}"  -H "PRIVATE-TOKEN: {{ .gltoken }}" "{{ .glapiurl }"'
    reg: statuses_result
  - func: cmd
    do:
    - name: fail
    if: '{{eq .statuses_result.Code 200 or eq .statuses_result.Code 201 }}'
  - func: shell
    name: loop
    do:
    - 'echo {{ .loopitem.id }}'
    loop:
    - "{{ statuses_result.Stderr }}"
stephencheng commented 3 years ago

The basic ideas are:

  1. register using reg
  2. in the next step, use dvar to convert the output to a structured data and make sure you could reference to the key data
  3. use the keydata in template as condition then make condition using if or else, either in call to redirect to another task, or simply use block

see an example from: https://github.com/upcmd/up/blob/master/up.yml

image

lukasmrtvy commented 3 years ago

Problem is that curl in "enquire the latest release" task might fail due to:

Look at Ansible uri module for example: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html You can define response codes that signifies success of the request:

status_code:
- 200 
- 201

Its much more generic way how to check if request was successful, than parsing output body.

stephencheng commented 3 years ago

You will have to handle it yourself.

I have come up with an example: https://github.com/upcmd/up/blob/master/tests/functests/c0209.yml

result example:

~~SubStep1: [return:  ]
=Task2: [task ==> handle_exit_code:  ]
--Step1: [: prototyping the case to randomly get a http response either 200 or 201 ]
cmd( 1):
-
200

-
 .. ok
. ok
--Step2:
dvar> success:
"true"

-
true
~~SubStep1: [return:  ]
=Task2: [task ==> handle_exit_code:  ]
--Step1: [: prototyping the case to randomly get a http response either 200 or 201 ]
cmd( 1):
-
302

-
 .. ok
. ok
--Step2:
dvar> success:
"false"

-
false
~~SubStep1: [return:  ]
=Task2: [task ==> handle_exit_code:  ]
--Step1: [: prototyping the case to randomly get a http response either 200 or 201 ]
cmd( 1):
-
302

-
 .. ok
. ok
--Step2:
dvar> success:
"false"

-
false
~~SubStep1: [return:  ]
=Task2: [task ==> handle_exit_code:  ]
--Step1: [: prototyping the case to randomly get a http response either 200 or 201 ]
cmd( 1):
-
500

-
 .. ok
. ok
--Step2:
dvar> success:
"false"

-
false
~~SubStep1: [return:  ]
=Task2: [task ==> handle_exit_code:  ]
--Step1: [: prototyping the case to randomly get a http response either 200 or 201 ]
cmd( 1):
-
200

-
 .. ok
. ok
--Step2:
dvar> success:
"true"

-
true
~~SubStep1: [return:  ]
=Task2: [task ==> handle_exit_code:  ]
--Step1: [: prototyping the case to randomly get a http response either 200 or 201 ]
cmd( 1):
-
301

-
 .. ok
. ok
--Step2:
dvar> success:
"false"

-
false
~~SubStep1: [return:  ]
-Step3: [: show if the above http request is successful ]
~SubStep1: [print:  ]
?success: false