rancher-partner-charts/scripts/generate-patch

92 lines
3.7 KiB
Plaintext
Raw Normal View History

2020-06-30 18:08:43 +00:00
#!/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
}
2020-06-30 18:08:43 +00:00
for f in packages/*; do
if [[ -f ${f}/package.yaml ]]; then
if [[ -z $CHART || $CHART == $(basename -- ${f}) ]]; then
2020-06-30 18:08:43 +00:00
url=$(cat ${f}/package.yaml | yq r - url)
if [[ -z ${url} ]]; then
continue
fi
2020-06-30 18:08:43 +00:00
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
2020-08-02 17:27:46 +00:00
git clone --depth=1 --no-tags $url /tmp/tmp-charts
2020-06-30 18:08:43 +00:00
pwd=$(pwd)
cd /tmp/tmp-charts
2020-08-03 16:45:50 +00:00
git fetch origin $commit
2020-06-30 18:08:43 +00:00
git checkout $commit
cd $pwd
cp -r /tmp/tmp-charts/${subdirectory} ${f}/charts-original
2020-06-30 18:08:43 +00:00
rm -rf /tmp/tmp-charts
else
mkdir -p ${f}/charts-original
2020-06-30 18:08:43 +00:00
curl -sLf ${url} | tar xvzf - -C ${f}/charts-original --strip ${fields} ${subdirectory} > /dev/null 2>&1
fi
if [[ -f ${f}/charts/requirements.yaml ]]; then
i=0
while ! [[ -z $(yq r ${f}/charts/requirements.yaml "dependencies[${i}]") ]]; do
subchart=$(yq r ${f}/charts/requirements.yaml "dependencies[${i}].name" | head -n 1)
version=$(yq r ${f}/charts/requirements.yaml "dependencies[${i}].version" | head -n 1)
helm_repo=$(yq r ${f}/charts/requirements.yaml "dependencies[${i}].repository" | head -n 1)
# Remove trailing slash
[[ ${helm_repo} == */ ]] && helm_repo=${helm_repo%/}
if [[ ${helm_repo} == file://* ]] || [[ ! ${version} =~ ^[0-9\.]+$ ]]; then
# Subchart is tracked locally; patches will not be tracked
rm -rf ${f}/charts-original/charts/${subchart}
((i=i+1))
continue
fi
# Subchart is tracked in remote Helm repo with a fixed version number; we grab the download URL and get the local chart archive
download_url=$(curl -sLf ${helm_repo}/index.yaml | cat | yq r - "entries.${subchart}.(version == ${version}).urls[0]")
if [[ -z ${download_url} ]]; then
echo "Failed to get download URL for ${subchart} at version ${version} from index.yaml at ${helm_repo}"
exit 1
fi
echo "Comparing current changes to ${subchart}-${version} against upstream found at ${download_url}"
curl -sLf ${download_url} | tar xvzf - -C ${f}/charts-original/charts > /dev/null 2>&1
((i=i+1))
done
fi
2020-06-30 18:08:43 +00:00
if [[ -d ${f}/charts ]]; then
split_crds=$(cat ${f}/package.yaml | yq r - generateCRDChart.enabled)
if [[ "${split_crds}" == "true" ]]; then
./scripts/clean-crds ${f}
fi
diff -x *.tgz -x *.lock -uNr ${f}/charts-original ${f}/charts > ${f}/$(basename -- ${f}).patch || true
if ! [[ -s ${f}/$(basename -- ${f}).patch ]]; then
# If no changes exist in patch then remove it
rm ${f}/$(basename -- ${f}).patch
fi
if [[ -f ${f}/$(basename -- ${f}).patch ]]; then
remove_timestamp_from_diff
fi
if [[ "${split_crds}" == "true" ]]; then
./scripts/prepare-crds ${f}
fi
2020-06-30 18:08:43 +00:00
fi
rm -rf ${f}/charts-original
fi
fi
done