X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=dbc-client%2Fmisc%2Fdbc-client;h=532ecfc097a89109bec138b9a69f399f88372b6c;hb=59d6bbff61e58e7ab08ca39acf758ab7d8b1a449;hp=1e839ecf3de1913baf68ae194b5fb19a9a3777e9;hpb=7686e1139c9a581ac6c84f1f3baee15dae859f19;p=dmaap%2Fbuscontroller.git diff --git a/dbc-client/misc/dbc-client b/dbc-client/misc/dbc-client index 1e839ec..532ecfc 100644 --- a/dbc-client/misc/dbc-client +++ b/dbc-client/misc/dbc-client @@ -1,10 +1,11 @@ #!/bin/sh # -# ============LICENSE_START========================================== +# ============LICENSE_START=============================================== # org.onap.dmaap -# =================================================================== +# ======================================================================== # Copyright © 2019 AT&T Intellectual Property. All rights reserved. -# =================================================================== +# Copyright (C) 2022 Nordix Foundation. +# ======================================================================== # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -16,155 +17,147 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -# ============LICENSE_END============================================ +# ============LICENSE_END================================================= # ECOMP is a trademark and service mark of AT&T Intellectual Property. -# -# umask 0022 -TZ=GMT0 -COMPONENT=dbc-client -APP_ROOT=${APP_ROOT:-/opt/app/$COMPONENT} -USER=root -export TZ -PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin -export PATH +set -uex -o pipefail +export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin + +# RESP_CACHE is (/opt/app/config/cache) empty-dir volume mount for K8s env +RESP_CACHE=${RESP_CACHE:-''} +RESP=${RESP:-'/dev/null'} +APP_ROOT=${APP_ROOT:-/opt/app/dbc-client} +IF_PUB_SUB_EXIST=${IF_PUB_SUB_EXIST:-false} CONFIGMAP_ROOT=${CONFIGMAP_ROOT:-/opt/app/config} -CONTAINER_CONFIG=$CONFIGMAP_ROOT/conf/dbc-client.env -REQUESTID=${REQUESTID:-dbc-client} + +PORT=${PORT:-8443} DBC=${DBC:-dmaap-bc} PROTO=${PROTO:-https} -PORT=${PORT:-8443} -WAIT_TO_EXIT=${WAIT_TO_EXIT:-N} -PEMDIR=${PEMDIR:-/opt/app/osaaf/local} -CAPEM=${CAPEM:-ca.pem} -CLIENTPEM=${CLIENTPEM:-client.pem} -KEYPEM=${KEYPEM:-key.pem} -CERTPWD=${CERTPWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'} - - +PARAM=${PARAM:-'useExisting=true'} +REQUESTID=${REQUESTID:-dbc-client} +URL=${URL:-"${PROTO}"://"${DBC}":"${PORT}"/webapi/} +CA_PEM=${CA_PEM:-ca.pem} +KEY_PEM=${KEY_PEM:-key.pem} +CLIENT_PEM=${CLIENT_PEM:-client.pem} +PEM_DIR=${PEM_DIR:-/opt/app/osaaf/local} +CERT_PWD=${CERT_PWD:-'2U[iOZzMHI:.#tdCwlBqc;}S'} -config() { - echo "ENTER config" - set -x - if [ ! -d $APP_ROOT ] - then - echo "Expected app root directory $APP_ROOT does not exist" - exit 1 - fi - cd $PEMDIR - pwd - ls -l - if [ -f $CAPEM -a -f $CLIENTPEM -a -f $KEYPEM ] - then - echo "--key $PEMDIR/$KEYPEM --cacert $PEMDIR/$CAPEM --cert $PEMDIR/${CLIENTPEM}:${CERTPWD}" > $PEMDIR/curl.cred - CURLCRED="-K $PEMDIR/curl.cred" - else - echo "Warning: PEM files for authorization not found!" - CURLCRED="" - fi - echo "CURLCRED=$CURLCRED" - cd $APP_ROOT - find . -type f -exec ls -l {} \; - find $CONFIGMAP_ROOT -type f -exec ls -l {} \; - if [ ! -f $CONTAINER_CONFIG ] - then - echo "WARNING: Expected env file $CONTAINER_CONFIG not found. Default behaviors in effect" - else - source $CONTAINER_CONFIG - fi +BA_PWD=${BA_PWD:-'demo123456!'} +AUTH_METHOD=${AUTH_METHOD:-basicAuth} +BA_IDENTITY=${BA_IDENTITY:-dmaap-bc@dmaap-bc.onap.org} - if [ "$WAIT_TO_EXIT" != "Y" ] - then - echo "Creating $APP_ROOT/ok_to_exit so no waiting..." - > $APP_ROOT/ok_to_exit - else - echo "Not creating $APP_ROOT/ok_to_exit" - fi - - set +x +function xcurl() { + curl -X POST \ + -s "$CURL_CRED" \ + -w "%{http_code}" \ + -H "X-ECOMP-RequestID: $REQUESTID" \ + -H "Content-Type: application/json" "$@" } - -init() { - set -x - echo "ENTER init" - if [ ! -d $CONFIGMAP_ROOT ] - then - echo $CONFIGMAP_ROOT does not exist - return - fi - - #loop on get /dmaap until we get a good response to indicate other provisioning can continue - rc=${RESP:-999} - while [ $rc != "200" ] - do - sleep 10 - rc=`curl $CURLCRED -s -o /dev/null -I -w "%{http_code}" -X GET -H "X-ECOMP-RequestID: $REQUESTID" -H "Content-Type: application/json" ${PROTO}://${DBC}:${PORT}/webapi/dmaap` - echo "get dmaap response=${rc}" - done - set +x +function init_config() { + if [ ! -d "$APP_ROOT" -a ! -d "$CONFIGMAP_ROOT" ]; then + echo "Expected either App root directory $APP_ROOT Or ConfigMap directory $CONFIGMAP_ROOT does not exist." + exit 1 + fi + cd "$PEM_DIR" + if [ "$AUTH_METHOD" = "basicAuth" ]; then + echo "-u ${BA_IDENTITY}:${BA_PWD}" >"$PEM_DIR"/curl.cred + CURL_CRED="-K $PEM_DIR/curl.cred" + elif [ -f "$CA_PEM" -a -f "$CLIENT_PEM" -a -f "$KEY_PEM" ]; then + printf "key \"$PEM_DIR/$KEY_PEM\"\n cacert \"$PEM_DIR/$CA_PEM\"\n cert \"$PEM_DIR/${CLIENT_PEM}:${CERT_PWD}\"" >$PEM_DIR/curl.cred + CURL_CRED="-K $PEM_DIR/curl.cred" + else + echo "PEM files for authorization not found..!" + fi } -dopost() { - set -x - RETRY_TIME=60 - - loop=true - - while [ $loop = true ] - do - rc=`curl $CURLCRED -s -X POST -w "%{http_code}" -H "X-ECOMP-RequestID: $REQUESTID" -H "Content-Type: application/json" -d @${1} ${PROTO}://${DBC}:${PORT}/webapi/${2} -o /dev/null` - case $rc in - 200 | 201 | 409 ) - echo "response=$rc" - loop=false - ;; - * ) - echo "`date`: http response=$rc. Will retry after $RETRY_TIME seconds" - sleep $RETRY_TIME - ;; - esac - done - set +x +function init_dbc_provisioning() { + cd "$CONFIGMAP_ROOT" + for dir in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs; do + if [ -d ${dir} ]; then + for file in $(ls ${dir}/*.json); do + do_http_post "$file" "$dir" + done + fi + done } -doprov() { - set -x - cd $CONFIGMAP_ROOT - pwd - # order is important in this next list - for uri in dmaap dcaeLocations mr_clusters topics mr_clients dr_nodes feeds dr_pubs dr_subs - do - if [ -d ${uri} ] - then - for j in `ls ${uri}/*.json` - do - echo "POST $j to $uri" - dopost $j $uri - done - fi - done - set +x -} -delay() { - echo "DELAY=$DELAY" - if [ ! -z "$DELAY" ] - then - sleep $DELAY - fi +function do_http_post() { + RETRY_TIME=60 + if [ -n "$RESP_CACHE" ]; then + RESP="$RESP_CACHE"/"$(echo "${1##*/}" | cut -d "." -f1)"-resp.json + fi + while true; do + if [ "$2" != "feeds" -a "$2" != "topics" ]; then + req_body=$(cat "${1}" | envsubst) + if [ -n "$RESP_CACHE" ] && [ "${2}" = "dr_pubs" -o "${2}" = "dr_subs" ]; then + IF_PUB_SUB_EXIST=false + check_pub_sub "${2}" "${req_body}" "${RESP}" + if [ "${IF_PUB_SUB_EXIST}" = true ]; then + echo "DR Publisher or Subscriber already exist.." + break + fi + fi + rc=$(xcurl -o "$RESP" -d "$req_body" "${URL}${2}") + if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then + echo "Http Post request is successful with response code=$rc" + break + fi + else + rc=$(xcurl -o "$RESP" -d @"${1}" "${URL}${2}"/?"${PARAM}") + if [ "$rc" = "200" -o "$rc" = "201" -o "$rc" = "409" ]; then + echo "Http Post request for feed creation is successful with response code=$rc" + break + fi + fi + echo "$(date): Http Response code=$rc. Will retry after $RETRY_TIME seconds.." + sleep "$RETRY_TIME" + done } -delay -config -init -doprov +function check_pub_sub() { + #Parameters: + #${1} Param - String representing dir either dr_pubs or dr_subs + #${2} Param - Request details (Dr Publisher/Subscriber creation input details) containing feedName, dr-userName, dr-Password, dr-LocationName + #${3} Param - String representing RESP_CACHE Env to '/opt/app/config/cache' or '/dev/null' + feed_config_resp="$RESP_CACHE"/"feedConfig-$(echo "${3//[!0-9]/}")-resp.json" + if [ -f "${feed_config_resp}" ]; then + echo "Checking if pub or sub exist.." + val=${1#*_} + dr_usr=$(echo "${2}" | jq -r '.username') + dr_pwd=$(echo "${2}" | jq -r '.userpwd') + pub_sub_cnt=$(jq ".${val} | length" "${feed_config_resp}") + local i=0 + while [ "$i" -lt "$pub_sub_cnt" ]; do + _dr_usr=$(jq -r ".${val}[$i].username" "${feed_config_resp}") + _dr_pwd=$(jq -r ".${val}[$i].userpwd" "${feed_config_resp}") + if [ "${dr_usr}" = "${_dr_usr}" -a "${dr_pwd}" = "${_dr_pwd}" ]; then + if [ "${1}" = "dr_pubs" ]; then + echo "Publisher exist with username: ${dr_usr}, password: ${dr_pwd}" + echo "$(jq -c ".${val}[$i]" "${feed_config_resp}")" >"${3}" + IF_PUB_SUB_EXIST=true + break + elif [ "${1}" = "dr_subs" ]; then + dr_loc=$(echo "${2}" | jq -r '.dcaeLocationName') + _dr_loc=$(jq -r ".${val}[$i].dcaeLocationName" "${feed_config_resp}") + dr_deliveryURL=$(echo "${2}" | jq -r '.deliveryURL') + _dr_deliveryURL=$(jq -r ".${val}[$i].deliveryURL" "${feed_config_resp}") + if [ "${dr_loc}" = "${_dr_loc}" -a "${dr_deliveryURL}" = "${_dr_deliveryURL}" ]; then + echo "Subscriber exist with username: ${dr_usr}, password: ${dr_pwd}, dcaeLocationName: ${dr_loc}, deliveryURL: ${dr_deliveryURL}" + echo "$(jq -c ".${val}[$i]" "${feed_config_resp}")" >"${3}" + IF_PUB_SUB_EXIST=true + break + fi + fi + fi + i=$((i + 1)) + done + else + echo "Feed configuration doesn't exist." + fi +} -echo "Check $APP_ROOT/ok_to_exit" -while [ ! -f $APP_ROOT/ok_to_exit ] -do - echo "$APP_ROOT/ok_to_exit does not exist. Sticking around for debugging..." - sleep 10 -done -exit 0 +init_config +init_dbc_provisioning