chef knife environment upload
We use Chef environments to control the DSC versions of our various systems.
Often times when we have confirmed a new version is ready to be rolled out, we will update many environment files and want to upload them all to Chef.
Since we want to ensure the changes are always tracked and the versions in Chef are always accurate, we have the following
bash
function sourced in our shell.
knife-environment-upload () {
git pull
if [[ "$?" -gt 0 ]]; then
echo Git pull failed.
exit 1
fi
CHANGED_FILES=($(git status -s | awk '{print $2}'))
if [[ "${#CHANGED_FILES[@]}" -eq 0 ]]; then
echo No files changed, exiting.
exit 0
fi
knife environment from file "${CHANGED_FILES[@]}"
git add "${CHANGED_FILES[@]}"
if [[ ! -z "$1" ]]; then
# Commit with message then push
git commit -m "$@" && git push
else
# Open commit window and push
git commit && git push
fi
}
This function pulls all the updates with git to ensure we are up to date.
It then checks if there are any changed files and if so, uploads them to Chef.
Finally, it adds the changes to git and commits and pushes.
If an argument is supplied to the function, it is used as the commit message.
Otherwise, it will open a new commit window before pushing.
last updated 2024-03-18