forked from Normation/rudder-agent
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #12614: Update the technique migration script
- Loading branch information
Showing
3 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters