Skip to content

Commit

Permalink
Work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ncharles committed Sep 5, 2018
1 parent d155ac3 commit 551eaa9
Showing 1 changed file with 126 additions and 24 deletions.
150 changes: 126 additions & 24 deletions share/commands/server-upgrade-techniques
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @man This command will replace the techniques in /var/rudder/configuration-repository/techniques
# @man by the techniques found in /opt/rudder/share/techniques which is installed by rudder-technique package.
# @man The upgrade can take care of user defined changes.
# @man This command creates an update branch "rudder_update" with the content of current techniques first time is it run
# @man +
# @man *Options*:
# @man +
Expand All @@ -14,7 +15,7 @@
# @man +
# @man *-f*: suppress any warning and run without prompting for input
# @man +
# @man *-c*: use the givent commit id as the update branch origin
# @man *-c*: use the given commit id as the update branch origin

. "${BASEDIR}/../lib/common.sh"
. "${BASEDIR}/../lib/api_call.sh"
Expand All @@ -24,39 +25,132 @@ INITIAL=false
OVERRIDE=false
FORCE=false
PARENT_COMMIT=""
SET_OPTION=false
KEY_OPTION=""
VALUE_OPTION=""
CONFIGURATION_FILE=/opt/rudder/etc/rudder.conf

while getopts "uiofc:" opt; do
case $opt in
u)
# Output usage
function usage()
{
echo "rudder server upgrade-techniques"
echo "upgrade techniques in the configuration repository from the packaged ones"
echo "This command will replace the techniques in /var/rudder/configuration-repository/techniques"
echo "by the techniques found in /opt/rudder/share/techniques which is installed by rudder-technique package."
echo "The upgrade can take care of user defined changes."
echo "This command creates an update branch rudder_update with the content of current techniques first time is it run"
echo ""
echo "It can also control behaviour of automatic Techniques upgrades in Rudder 5.0 and later"
echo "By default, instance upgraded from Rudder 4 to 5 will not get automatic technique upgrade,"
echo "unless option autoupdate-technique-library as been set to true"

echo "Options:"
echo "--upgrade -u : merge updated techniques into the configuration repository"
echo "--initial -i : create the initial version of the update branch"
echo "--override -o : override existing technique without looking for local changes"
echo "--force -f : suppress any warning and run without prompting for input"
echo "--commit -c COMMITID : use the given commit id as the update branch origin"
echo "--autoupdate-technique-library -a true/false: set the auto update technique library at upgrade to true or false (for Rudder 5+)"
echo "--show -s: Display the currently defined options"
}

function check_configuration_file_exists()
{
if [ ! -f ${CONFIGURATION_FILE} ]; then
cat <<EOF > ${CONFIGURATION_FILE}
[server]
autoupdate-technique-library=false
EOF
fi
}

OPTIONS=uiofc:a:s
LONGOPTS=upgrade,initial,override,force,commit:,autoupdate-technique-library:,show


! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")

if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
# Wrong arguments
echo "Wrong parameters for command rudder server upgrade-techniques"
usage
exit 1
fi

eval set -- "$PARSED"


while true; do
case "$1" in
-u |--upgrade)
UPGRADE=true
shift
;;
i)
-i |--initial)
INITIAL=true
shift
;;
o)
-o |--override)
OVERRIDE=true
shift
;;
f)
-f |--force)
FORCE=true
shift
;;
-c|--commit)
PARENT_COMMIT="$2"
shift 2
;;
-a|--autoupdate-technique-library)
SET_OPTION=true
KEY_OPTION=autoupdate-technique-library
VALUE_OPTION="$2"
if [ "${VALUE_OPTION}" != "true" ] && [ "${VALUE_OPTION}" != "false" ]; then echo "--autoupdate-technique-library requires 'true' or 'false' parameter, ${VALUE_OPTION} given"; exit 2; fi
shift 2
;;
c)
PARENT_COMMIT="${OPTARG}"
-s|--show)
echo "Show values"
shift
;;
--)
shift
break
;;
*)
echo "Programming error - option not correctly handled"
exit 2
;;
esac
done

# Exactly one option is allowed
[ "${UPGRADE}" = "false" ] && [ "${INITIAL}" = "false" ] && [ "${OVERRIDE}" = "false" ] && err="error"
# Exactly one option is allowed for upgrade of technique
#[ "${UPGRADE}" = "false" ] && [ "${INITIAL}" = "false" ] && [ "${OVERRIDE}" = "false" ] && err="error"
[ "${UPGRADE}" = "true" ] && [ "${INITIAL}" = "true" ] && err="error"
[ "${UPGRADE}" = "true" ] && [ "${OVERRIDE}" = "true" ] && err="error"
[ "${INITIAL}" = "true" ] && [ "${OVERRIDE}" = "true" ] && err="error"

if [ "${err}" != "" ]
then
echo "I need exactly one option. Available options: -u -i -o"
echo "To automaticaly upgrade Technique, this command needs exactly one choice. Available choice are:"
echo " -u: merge updated techniques into the configuration repository"
echo " -i: create the initial version of the update branch"
echo " -o: override existing technique without looking for local changes"
echo ""
echo "Additionnal option are :"
echo " -f: suppress any warning and run without prompting for input"
echo " -c COMMITID: use the given commit id as the update branch origin"

exit 1
fi

if [ "${SET_OPTION}" = "true" ]
then
echo "Option is set ${KEY_OPTION} = ${VALUE_OPTION}"
check_configuration_file_exists
sed -i "/^\[server\]/,/^\[/{s/^${KEY_OPTION}=.*/${KEY_OPTION}=${VALUE_OPTION}/}" ${CONFIGURATION_FILE}
fi

REPO="/var/rudder/configuration-repository"
BASE="/opt/rudder/share/techniques"
UPDATE_BRANCH="rudder_update"
Expand All @@ -67,16 +161,23 @@ then
# Upgrading needs the upgrade branch
if git rev-parse --verify --quiet "${UPDATE_BRANCH}" > /dev/null
then
git checkout "${UPDATE_BRANCH}"
cp -a "${BASE}"/* techniques/
git add techniques/
git commit -q -m "${TAG_MESSAGE} Standard technique upgrade from version ${package_version} on $(date)"
git checkout master
git merge "${UPDATE_BRANCH}"
# TODO Now's a god time for a user shell
rudder server reload-techniques
# Checkout can fail because of unstaged change
if git checkout "${UPDATE_BRANCH}"
then
cp -a "${BASE}"/* techniques/
git add techniques/
git commit -q -m "${TAG_MESSAGE} Standard technique upgrade from version ${package_version} on $(date)"
git checkout master
git merge "${UPDATE_BRANCH}"
# TODO Now's a good time for a user shell
rudder server reload-techniques
else
echo "You have unstagged changes in current branch"
echo "Aborting"
exit 1
fi
else
echo "You need and update branch."
echo "You need an update branch."
echo "- If you have not made any change in the techniques, you can override techniques with the updated ones using the -o option."
echo "- If you have made changes you would like to keep, you should first create the initial branch with the -i option."
exit 1
Expand All @@ -86,13 +187,13 @@ fi

if [ "${INITIAL}" = "true" ]
then
echo "Creating initial branch from an early commit"
echo "Creating initial version of the branch for Techniques update"
if git rev-parse --verify --quiet "${UPDATE_BRANCH}" > /dev/null
then
echo "The upgrade branch '${UPDATE_BRANCH}' already exists, aborting"
exit 1
fi
# find the first commit with the techniques directory in it to minimize merging difficulties
# find the first commit with the Techniques directory in it to minimize merging difficulties
if [ "${PARENT_COMMIT}" = "" ]
then
if [ "${FORCE}" != "true" ]
Expand Down Expand Up @@ -128,9 +229,10 @@ then
if git rev-parse --verify --quiet "${UPDATE_BRANCH}" > /dev/null
then
# it's easier to remove then create the branch
git branch -D "${UPDATE_BRANCH}"
git branch -D "${UPDATE_BRANCH}" > /dev/null
fi
git branch "${UPDATE_BRANCH}"
rudder server reload-techniques
echo "Techniques have been updated, and update branch set to current state of the Techniques"
fi

0 comments on commit 551eaa9

Please sign in to comment.