Refactoring from bash to dash

Some errors
This commit is contained in:
Gerardo 2020-04-18 22:01:16 +02:00 committed by GitHub
parent bc36b9e89d
commit 936d6cf837
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,15 @@
#!/bin/bash #!/usr/bin/env sh
#Original Author: Gerardo Trotta <gerardo.trotta@euronet.aero> #Original Author: Gerardo Trotta <gerardo.trotta@euronet.aero>
#Application username #Application username
#ARUBA_AK="username" #ARUBA_AK="xxxxx"
# #
#Application password #Application password
#ARUBA_AS="password" #ARUBA_AS="xxxxxx"
# #
#API key #API key
#ARUBA_TK="token" #ARUBA_TK="xxxxxxxx"
# #
#Consumer Key #Consumer Key
#ARUBA_CK="sdfsdfsdfsdfsdfdsf" #ARUBA_CK="sdfsdfsdfsdfsdfdsf"
@ -30,6 +30,7 @@ _aruba_get_api() {
;; ;;
*) *)
_err "Unknown parameter : $1" _err "Unknown parameter : $1"
return 1 return 1
;; ;;
@ -45,7 +46,7 @@ _initAuth() {
ARUBA_AK="" ARUBA_AK=""
ARUBA_AS="" ARUBA_AS=""
ARUBA_TK="" ARUBA_TK=""
_err "You don't specify ARUBA application key or application secret yet." _err "You don't specify ARUBA application key and application secret yet."
_err "Please create you key and try again." _err "Please create you key and try again."
return 1 return 1
fi fi
@ -76,10 +77,10 @@ _initAuth() {
_info "ARUBA consumer key is empty, Let's get one:" _info "ARUBA consumer key is empty, Let's get one:"
if ! _aruba_authentication; then if ! _aruba_authentication; then
_err "Can not get consumer key." _err "Can not get consumer key."
fi
#return and wait for retry. #return and wait for retry.
return 1 return 1
fi fi
fi
_info "Checking authentication and get domain details" _info "Checking authentication and get domain details"
@ -93,9 +94,9 @@ _initAuth() {
domainData=$(echo "$response" | tr -d '\r' ) domainData=$(echo "$response" | tr -d '\r' )
# get all Ids and peek only values # get all Ids and peek only values
temp="$(echo "$domainData" | grep -oP "Id\": \d{1,}" | cut -d : -f 2)" temp="$(echo "$domainData" | grep -oP "Id\": \d{1,}" | cut -d : -f 2 | head -1)"
read -ra ADDR <<< "$temp" #put Ids into array #read -ra ADDR <<< "$temp" #put Ids into array
domain_id="${ADDR[0]}" # first element is zone Id domain_id=$temp # first element is zone Id
_info "DomainId is: $domain_id" _info "DomainId is: $domain_id"
_info "Consumer key is ok." _info "Consumer key is ok."
@ -113,14 +114,16 @@ dns_aruba_add() {
return 1 return 1
fi fi
_debug "Check if _acme-challenge record exists" _debug _domain "$_domain"
if ! _get_zone_id "$_domain"; then _sub_domain="_acme-challenge"
_debug "Check if _acme-challenge record exists in " "$_domain"
if ! _extract_record_id "$_sub_domain$_domain"; then
_err "invalid domain" _err "invalid domain"
return 1 return 1
fi fi
_debug _domain "$_domain"
_sub_domain="_acme-challenge"
_payload="{ \"IdDomain\": $domain_id, \"Type\": \"TXT\", \"Name\": \"$_sub_domain\", \"Content\": \"\\\"$txtvalue\\\"\" }" _payload="{ \"IdDomain\": $domain_id, \"Type\": \"TXT\", \"Name\": \"$_sub_domain\", \"Content\": \"\\\"$txtvalue\\\"\" }"
@ -155,7 +158,7 @@ dns_aruba_rm() {
return 1 return 1
fi fi
if ! _ovh_rest DELETE "api/domains/dns/record/$_recordId"; then if ! _aruba_rest DELETE "api/domains/dns/record/$_recordId"; then
return 1 return 1
fi fi
@ -169,20 +172,32 @@ _extract_record_id() {
subdomain=$1 subdomain=$1
_arrayid=0 _arrayid=0
_ids="$(echo $domainData | grep -oP '(?<="Id": )[^,]+')" _ids="$(echo $domainData | grep -oP '(?<="Id": )[^,]+')"
_temp="$(echo $domainData | grep -oP "\"DomainId\":\s\d{1,}," | tr -d ' ')"
_domainids="$(echo $_temp | tr -d ' ')"
_names="$(echo $domainData | grep -oP '(?<="Name": ")[^"]+')" _names="$(echo $domainData | grep -oP '(?<="Name": ")[^"]+')"
ARRAY_IDS=($(echo $_ids | tr ", " "\n")) ARRAY_IDS=$(echo $_ids | tr ", " "\n")
ARRAY_NAMES=($_names) ARRAY_NAMES=$_names
for i in "${!ARRAY_NAMES[@]}" j=0
for i in $ARRAY_NAMES;
do do
if [[ ${ARRAY_NAMES[$i]} = $subdomain ]]; then if [ "$i" = "$subdomain" ]; then
_debug printf "%s\t%s\n" "$i" "${ARRAY_NAMES[$i]}" _debug printf "%s\t%s\n" "$i"
_arrayid=$i _arrayname=$i
_debug"Found txt record id: ${ARRAY_IDS[$_arrayid]}" _arrayId=$j
_recordId=${ARRAY_IDS[$_arrayid} _debug "Found txt record id: $_arrayId"
#printf "%s" ${ARRAY_IDS[$_arrayid} fi
j=$(_math "$j" + 1)
done
n=0
for i in $ARRAY_IDS;
do
if [ "$n" = "$_arrayId" ]; then
_recordId=$i
return 0 return 0
fi fi
n=$(_math "$n" + 1)
done done
return 1 return 1
@ -198,7 +213,7 @@ _aruba_authentication() {
_arubadata="grant_type=password&username=$ARUBA_AK&password=$ARUBA_AS" _arubadata="grant_type=password&username=$ARUBA_AK&password=$ARUBA_AS"
response="$(_post "$_arubadata" "$ARUBA_API/auth/token")" response="$(_post "$_arubadata" "$ARUBA_API/auth/token")"
_debug "$(_post "$_arubadata" "$ARUBA_API/auth/token")"
_debug3 response "$response" _debug3 response "$response"
access_token="$(echo "$response" | _egrep_o "access_token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')" access_token="$(echo "$response" | _egrep_o "access_token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
@ -230,8 +245,8 @@ _aruba_rest() {
export _H4="Authorization-Key: $ARUBA_TK" export _H4="Authorization-Key: $ARUBA_TK"
export _H5="Accept: application/json" export _H5="Accept: application/json"
_debug3 _H3 "$_H3" _debug2 _H3 "$_H3"
_debug3 _H4 "$_H4" _debug2 _H4 "$_H4"
if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
_debug data "$data" _debug data "$data"
response="$(_post "$data" "$_aruba_url" "" "$m")" response="$(_post "$data" "$_aruba_url" "" "$m")"
@ -239,7 +254,7 @@ _aruba_rest() {
response="$(_get "$_aruba_url")" response="$(_get "$_aruba_url")"
fi fi
if [ "$?" != "0" ] || _contains "$response" "wrong credentials" || _contains "$response" "Unprocessable"; then if [ "$?" != "0" ] || _contains "$response" "wrong credentials" || _contains "$response" "Unprocessable" || _contains "$response" "denied"; then
_err "Response error $response" _err "Response error $response"
return 1 return 1
fi fi