rancher-charts/scripts/generate-patch

76 lines
2.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
set -x
function remove_timestamp_from_diff {
prefix="[-\+]\{3\}"
filename_format="packages\/$(basename -- ${f})\/[^[:space:]]*"
# https://www.gnu.org/software/diffutils/manual/html_node/Example-Unified.html#Example-Unified
# Example timestamp: 2002-02-21 23:30:39.942229878 -0800
date="[[:digit:]]\{4\}-[[:digit:]]\{2\}-[[:digit:]]\{2\}"
time="[[:digit:]]\{2\}:[[:digit:]]\{2\}:[[:digit:]]\{2\}.[[:digit:]]\{9\}"
timezone="[-\+][[:digit:]]\{4\}"
timestamp_format="${date}[[:space:]]${time}[[:space:]]${timezone}"
sed -i.bak "s/\(${prefix} ${filename_format}\)[[:space:]]${timestamp_format}/\1/g" ${f}/$(basename -- ${f}).patch
rm ${f}/$(basename -- ${f}).patch.bak
}
function revert_crd_changes {
if [[ -z $1 ]]; then
echo "No directory provided to revert charts-crd changes within"
exit 1
fi
# Move charts-crd/templates back into charts/crd/
mkdir -p ${f}/charts/crds/
mv ${f}/charts-crd/templates/* ${f}/charts/crds/
# Remove the validate-install-${name}-crd.yaml
name=$(cat ${f}/charts/Chart.yaml | yq r - 'name')
rm ${f}/charts/templates/validate-install-${name}-crd.yaml
# Remove additional annotations added to original chart
yq d -i ${f}/charts/Chart.yaml "annotations[catalog.cattle.io/auto-install-gvr]"
# Remove charts-crd
rm -rf ${f}/charts-crd
}
for f in packages/*; do
if [[ -f ${f}/package.yaml ]]; then
if [[ -z $CHART || $CHART == $(basename -- ${f}) ]]; then
url=$(cat ${f}/package.yaml | yq r - url)
subdirectory=$(cat ${f}/package.yaml | yq r - subdirectory)
type=$(cat ${f}/package.yaml | yq r - type)
commit=$(cat ${f}/package.yaml | yq r - commit)
fields=$(echo ${subdirectory} | awk -F'/' '{print NF}')
if [[ $fields -eq '0' ]]; then
fields='1'
fi
if [[ $type == 'git' ]]; then
mkdir -p /tmp/tmp-charts
git clone --depth=1 --no-tags $url /tmp/tmp-charts
pwd=$(pwd)
cd /tmp/tmp-charts
git fetch origin $commit
git checkout $commit
cd $pwd
cp -r /tmp/tmp-charts/${subdirectory} ${f}/charts-original
rm -rf /tmp/tmp-charts
else
mkdir -p ${f}/charts-original
curl -sLf ${url} | tar xvzf - -C ${f}/charts-original --strip ${fields} ${subdirectory} > /dev/null 2>&1
fi
if [[ -d ${f}/charts ]]; then
split_crds=$(cat ${f}/package.yaml | yq r - generateCRDChart.enabled)
if [[ "${split_crds}" == "true" ]]; then
revert_crd_changes ${f}
fi
diff -x *.tgz -x *.lock -uNr ${f}/charts-original ${f}/charts > ${f}/$(basename -- ${f}).patch || true
if [[ "${split_crds}" == "true" ]]; then
./scripts/prepare-crds ${f}
fi
remove_timestamp_from_diff
fi
rm -rf ${f}/charts-original
fi
fi
done