Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdall committed May 30, 2018
1 parent 1946d1e commit 5162232
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
12 changes: 6 additions & 6 deletions share/commands/directive-replace
Expand Up @@ -7,7 +7,7 @@ TECHNIQUES_DIRECTORY="${CONFIGURATION_DIRECTORY}/technique"
TOKEN="$(cat /var/rudder/run/api-token)"
DISPLAY_COMMAND=false

while getopts "iIvdqc:o:n:" opt; do
while getopts "iIvdc:o:n:" opt; do
case $opt in
i|I|v|d)
DISPLAY_COMMAND=true
Expand All @@ -28,15 +28,15 @@ shift $((OPTIND-1))

if [[ -z ${OLD_DIRECTIVE} || -z ${NEW_DIRECTIVE} ]]
then
echo "Usage: rudder directive replace [-iIvdq] [-c] -o <old-directive> -n <new-directive>"
echo "Usage: rudder directive replace [-iIvd] [-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}")
RULES=$(filtered_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"
Expand All @@ -55,18 +55,18 @@ else
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}")
result=$(filtered_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}")
old_result=$(filtered_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}")
new_result=$(filtered_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"
Expand Down
8 changes: 4 additions & 4 deletions share/commands/technique-migrate
Expand Up @@ -37,7 +37,7 @@ 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}")
TECHNIQUE_VERSION=$(filtered_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"
Expand All @@ -49,7 +49,7 @@ 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}" )
DIRECTIVES=$(filtered_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"
Expand All @@ -62,9 +62,9 @@ else
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}")
result=$(filtered_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}" )
post_result=$(filtered_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
Expand Down
5 changes: 3 additions & 2 deletions share/lib/api_call.sh
Expand Up @@ -73,8 +73,9 @@ full_api_call() {

}

# This function make an API call
complete_api_call() {
# This function makes an API call and will add the filter param after the bash call to the api.
# Also if display_command is set to "true", the executed command will be printed
filtered_api_call() {
#set -x
url="$1"
token="$2"
Expand Down

0 comments on commit 5162232

Please sign in to comment.