Skip to content

Commit

Permalink
Fixes #12614: Update the technique migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdall committed May 15, 2018
1 parent 4f8e5f3 commit 1946d1e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 1 deletion.
84 changes: 84 additions & 0 deletions share/commands/directive-replace
@@ -0,0 +1,84 @@
#!/bin/bash

. "${BASEDIR}/../lib/api_call.sh"
. "${BASEDIR}/../lib/common.sh"

TECHNIQUES_DIRECTORY="${CONFIGURATION_DIRECTORY}/technique"
TOKEN="$(cat /var/rudder/run/api-token)"
DISPLAY_COMMAND=false

while getopts "iIvdqc:o:n:" opt; do
case $opt in
i|I|v|d)
DISPLAY_COMMAND=true
;;
o)
OLD_DIRECTIVE="${OPTARG}"
;;
n)
NEW_DIRECTIVE="${OPTARG}"
;;
c)
clear_colors
;;
esac
done
# to keep the argument as $1
shift $((OPTIND-1))

if [[ -z ${OLD_DIRECTIVE} || -z ${NEW_DIRECTIVE} ]]
then
echo "Usage: rudder directive replace [-iIvdq] [-c] -o <old-directive> -n <new-directive>"
exit 1
fi

echo "DIRECTIVE ${OLD_DIRECTIVE} will be replaced by ${NEW_DIRECTIVE}"
echo ""

# Find the rules using the old directive
RULES=$(complete_api_call "${API_URL}/api/latest/rules" "${TOKEN}" "GET" "| jq -r ' .data.rules[] | select(.directives[] | contains(\"${OLD_DIRECTIVE}\")) | {\"id\":.id, \"displayName\":.displayName, \"directives\":.directives}'" "${DISPLAY_COMMAND}")

echo "Directives ${OLD_DIRECTIVE} will be replaced by ${NEW_DIRECTIVE} in the following rules:"
printf "${RULES}\n\n"

# Adding the new directive in the local JSON with jq
RULES=$(echo "${RULES}" | jq '.directives |= (. +["'${NEW_DIRECTIVE}'"] |unique)')
# Removing the old directive in the local JSON with jq
RULES=$(echo "${RULES}" | jq '.directives |= (. -["'${OLD_DIRECTIVE}'"] |unique)')
echo ""
if [ -z "${RULES}" ];
then
printf "${RED} No rules found using the directive ${BLUE}${OLD_DIRECTIVE}${RED}, aborting.${NORMAL}\n\n"
else
while IFS= read
do
echo "Treating $REPLY"
# Adding the new directive
DIRECTIVES=$( echo ${RULES} | jq -r 'select(.id=="'${REPLY}'") |.directives')
result=$(complete_api_call "${API_URL}/api/latest/rules/$REPLY" "${TOKEN}" "POST" "-H \"Content-Type: application/json\" -d '{\"directives\": $DIRECTIVES}'" "${DISPLAY_COMMAND}")
code=$?

#Looking for old directive
old_result=$(complete_api_call "${API_URL}/api/latest/rules/$REPLY" "${TOKEN}" "GET" " | jq '.data.rules[] | select(.directives[] | contains(\"${OLD_DIRECTIVE}\")) | {\"id\":.id, \"displayName\":.displayName, \"directives\":.directives}'" "${DISPLAY_COMMAND}")
if ! [ -z "${old_result}" ];
then
printf "${RED} Unexpected old directive found ${old_result}${NORMAL}\n\n"
fi

#Looking for new directive
new_result=$(complete_api_call "${API_URL}/api/latest/rules/$REPLY" "${TOKEN}" "GET" " | jq '.data.rules[] | select(.directives[] | contains(\"${NEW_DIRECTIVE}\")) | {\"id\":.id, \"displayName\":.displayName, \"directives\":.directives}'" "${DISPLAY_COMMAND}")
if $DISPLAY_COMMAND ;
then
printf "${GREEN} New directive found ${new_result}${NORMAL}\n\n"
fi

if [ ${code} -eq 0 ] && [ -z "${old_result}" ]
then
printf " ${GREEN}ok${NORMAL}: Add new directive ${NEW_DIRECTIVE} and removing old directive ${OLD_DIRECTIVE} to rule ${REPLY}\n"
else
printf " ${RED}error${NORMAL}: Could not add new directive ${NEW_DIRECTIVE} and remove ${OLD_DIRECTIVE} to rule ${REPLY} \n"
echo " ${result}"
exit 1
fi
done <<< "$(echo ${RULES} | jq -r '.id')"
fi
78 changes: 78 additions & 0 deletions share/commands/technique-migrate
@@ -0,0 +1,78 @@
#!/bin/bash

. "${BASEDIR}/../lib/api_call.sh"
. "${BASEDIR}/../lib/common.sh"

TOKEN="$(cat /var/rudder/run/api-token)"
DISPLAY_COMMAND=false

while getopts "iIvdqcn:" opt; do
case $opt in
i|I|v|d)
DISPLAY_COMMAND=true
;;
n)
TECHNIQUE_NAME="${OPTARG}"
;;
c)
clear_colors
;;
esac
done
# to keep the argument as $1
shift $((OPTIND-1))

if [ -z "${TECHNIQUE_NAME}" ]
then
echo "Usage: rudder technique migrate [-iIcvd] -n technique_name [target_technique_version]"
exit 1
fi

TECHNIQUE_VERSION="$1"
if [ -z "${TECHNIQUE_VERSION}" ]
then
TECHNIQUE_VERSION="latest"
fi

if [ "${TECHNIQUE_VERSION}" == "latest" ]
then
# Find the newest technique version
TECHNIQUE_VERSION=$(complete_api_call "${API_URL}/api/latest/techniques" "${TOKEN}" "GET" "| jq -r '.data | .techniques[] | select( .name == \"${TECHNIQUE_NAME}\") | .versions[]' | tail -n1" "${DISPLAY_COMMAND}")
if [ -z "${TECHNIQUE_VERSION}" ];
then
printf "${RED}No version found for the technique ${TECHNIQUE_NAME}${NORMAL}\n"
exit 1
fi
fi

echo "TECHNIQUE ${TECHNIQUE_NAME} will migrate to ${TECHNIQUE_VERSION}"
echo ""

# Find the directives using the given technique
DIRECTIVES=$(complete_api_call "${API_URL}/api/latest/directives" "${TOKEN}" "GET" "| jq -r '.data.directives[] | select(.techniqueName==\"${TECHNIQUE_NAME}\") | .id' | sed 's/\"//g'" "${DISPLAY_COMMAND}" )

echo "Directives using the '${TECHNIQUE_NAME}' technique:"
printf "${BLUE}${DIRECTIVES}${NORMAL}\n\n"

if [ -z "${DIRECTIVES}" ];
then
printf " ${RED}No directive found using the technique ${NORMAL}${TECHNIQUE_NAME}\n"
else
while IFS= read
do
# Change the version parameter in the previously found directives (ie 1 directive = 1 technique)
echo "Treating $REPLY"
result=$(complete_api_call "${API_URL}/api/latest/directives/$REPLY" "${TOKEN}" "POST" "-d \"techniqueVersion=${TECHNIQUE_VERSION}\"" "${DISPLAY_COMMAND}")
code1=$?
post_result=$(complete_api_call "${API_URL}/api/latest/directives/$REPLY" "${TOKEN}" "GET" "| jq -r '.data.directives[].techniqueVersion'" "${DISPLAY_COMMAND}" )

if [ ${code1} -eq 0 ] && [ "${post_result}" = "${TECHNIQUE_VERSION}" ]
then
printf " ${GREEN}ok${NORMAL}: Changed technique version to ${GREEN}${post_result}${NORMAL} for directive: '$REPLY'.\n\n"
else
printf " ${RED}error${NORMAL}: Could not change technique version for directive: '$REPLY'.\n"
echo " ${result}"
exit 1
fi
done <<< "${DIRECTIVES}"
fi
22 changes: 21 additions & 1 deletion share/lib/api_call.sh
@@ -1,8 +1,10 @@
# This file provide helpers to make API call from Rudder commands
API_URL="https://127.0.0.1/rudder"
TECHNIQUES_DIRECTORY="${CONFIGURATION_DIRECTORY}/technique"

if type curl >/dev/null 2>/dev/null
then
DOWNLOAD_COMMAND="curl --silent --show-error --insecure --location --proxy ''"
DOWNLOAD_COMMAND="curl --silent --show-error --insecure --location --proxy '' --globoff"
HEADER_OPT="--header"
else
DOWNLOAD_COMMAND="wget --quiet --no-check-certificate --no-proxy -O -"
Expand Down Expand Up @@ -66,6 +68,24 @@ full_api_call() {
echo "A token is mandatory to query the server"
exit 1
fi

eval ${DOWNLOAD_COMMAND} ${HEADER_OPT} "\"X-API-Token: ${token}\"" ${HEADER_OPT} "\"Content-Type: application/json;charset=utf-8\"" ${HEADER_OPT} "\"X-API-Version: latest\"" \"${url}${api}\"

}

# This function make an API call
complete_api_call() {
#set -x
url="$1"
token="$2"
action="$3"
filter="$4"
display_command="$5"
curl_command="${DOWNLOAD_COMMAND} -H \"X-API-Token: ${token}\" -X ${action} \"${url}\" ${filter}"
if ${display_command};
then
printf "${WHITE}${curl_command}${NORMAL}\n\n" >&2
fi
eval ${curl_command}
}

0 comments on commit 1946d1e

Please sign in to comment.