-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsync
executable file
·207 lines (169 loc) · 5.92 KB
/
sync
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
BASE_DIR="$( cd $SCRIPT_DIR && cd .. & pwd)"
CYAN="\033[96m"
YELLOW="\033[93m"
RESET="\033[0m"
BOLD="\033[1m"
NORMAL="\033[22m"
CHECK="\xE2\x9C\x94"
ORG=rancher
BRANCH=main
CLEAN=false
usage() {
echo "Usage: $0 [<options>]"
echo " options:"
echo " [-c | --clean] Clean the current extension assets including: ./{charts, assets, extensions, icons}"
echo " [-o | --org] <name> Specify the organization of the current repository (defaults to 'rancher')"
echo " [-b | --branch] <name> Specify the destination branch of the extension assets within the current repository (defaults to 'main')"
exit 1
}
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
;;
-o|--org)
if [[ -z $2 || $2 == -* ]]; then
echo "Error: Missing argument for $1 option"
usage
fi
ORG="${2}"
shift 2
;;
-c|--clean)
CLEAN=true
shift
;;
-b|--branch)
if [[ -z $2 || $2 == -* ]]; then
echo "Error: Missing argument for $1 option"
usage
fi
BRANCH="${2}"
shift 2
;;
*)
echo "Error: Unknown option $1"
usage
;;
esac
done
shift $((OPTIND-1))
if [[ $CLEAN == true ]]; then
rm -rf ./charts
rm -rf ./assets
rm -rf ./extensions
rm -rf ./icons
fi
echo -e "${CYAN}${BOLD}Syncing Extensions${RESET}"
EXTS=$(jq -r ".extensions | keys[]" manifest.json)
TMP=${BASE_DIR}/tmp
rm -rf ${TMP}
mkdir -p ${TMP}
HELM_INDEX=${BASE_DIR}/index.yaml
CHART_TMP=${TMP}/_charts
REPOSITORY=${ORG}/partner-extensions
echo "GitHub Repository: ${REPOSITORY}"
echo "GitHub Branch: ${BRANCH}"
for NAME in ${EXTS}
do
echo -e "${CYAN} + Syncing: ${BOLD}${NAME}${RESET}"
# Make diretories for assets, charts, and extensions
mkdir -p ./assets/${NAME}
mkdir -p ./charts/${NAME}
mkdir -p ./extensions/${NAME}
# Get repository name, branch, and versions
REPO=$(jq -r ".extensions.\"${NAME}\".repo" manifest.json)
EXT_BRANCH=$(jq -r ".extensions.\"${NAME}\".branch" manifest.json)
VERSIONS=$(jq -r ".extensions.\"${NAME}\".versions[]" manifest.json)
VFORMAT=$(echo $VERSIONS | tr '\n' ' ')
echo -e " Repository: ${REPO}"
echo -e " Branch: ${EXT_BRANCH}"
echo -e " Versions : ${VFORMAT}"
echo ""
echo -e " .. Cloning repository"
rm -rf ./tmp/${NAME}
pushd tmp > /dev/null
git clone https://github.com/${REPO}.git ${NAME}
cd ${NAME}
git checkout ${EXT_BRANCH}
pwd
popd > /dev/null
for VERSION in ${VERSIONS}
do
echo -e "${CYAN} Syncing: ${BOLD}${NAME}@${VERSION}${RESET}"
if [ -d "./charts/${NAME}/${VERSION}" ]; then
echo " Version already synced"
else
echo " + Copying version ${VERSION}"
# cp ./tmp/${NAME}/extensions/${NAME}/${NAME}-${VERSION}.tgz ./assets/${NAME}
cp -R ./tmp/${NAME}/extensions/${NAME}/${VERSION} ./extensions/${NAME}/${VERSION}
cp -R ./tmp/${NAME}/charts/${NAME}/${VERSION} ./charts/${NAME}/${VERSION}
# Need to patch the charts
echo " + Patching Helm chart"
CR_FILE=./charts/${NAME}/${VERSION}/templates/cr.yaml
ENDPOINT=https://raw.githubusercontent.com/${REPOSITORY}/${BRANCH}/extensions/${NAME}/${VERSION}
sed -i.bak -e 's@endpoint:.*@endpoint: '"$ENDPOINT"'@' ${CR_FILE}
rm -f ${CR_FILE}.bak
echo " + Fetching and patching icon"
# Check the chart file for an icon
CHART_FILE=./charts/${NAME}/${VERSION}/Chart.yaml
ICON=$(yq eval '.icon' ${CHART_FILE})
if [ -n "${ICON}" ]; then
# Downloading icon
ICON_FILE=$(basename $ICON)
echo " + Fetching icon: ${ICON}"
ICON_REL=icons/${NAME}/${VERSION}-${ICON_FILE}
mkdir -p ${BASE_DIR}/icons/${NAME}
rm -f ${BASE_DIR}/${ICON_REL}
# Download the icon - ignore errors, we will handle those
set +e
wget --quiet -O ${BASE_DIR}/${ICON_REL} ${ICON}
ERR=$?
set -1
# Icon downloaded okay
if [ ${ERR} -eq 0 ]; then
NEW_ICON=https://raw.githubusercontent.com/${REPOSITORY}/${BRANCH}/${ICON_REL}
# Check if '>-' exists in the icon property to remove the second line
if grep -q 'icon: >-' "${CHART_FILE}"; then
SECOND_LINE=$(sed -n '/^icon: >-/{n;p;q}' "${CHART_FILE}")
sed -i.bak '/^icon: >-/{n;d;}' "${CHART_FILE}"
sed -i -e 's@icon:.*@icon: '"${NEW_ICON}"'@' ${CHART_FILE}
else
sed -i.bak -e 's@icon:.*@icon: '"${NEW_ICON}"'@' ${CHART_FILE}
fi
rm -rf ${CHART_FILE}.bak
PKG_FILE=${BASE_DIR}/extensions/${NAME}/${VERSION}/plugin/package.json
sed -i.bak -e 's@\"icon\": \".*\"@\"icon\": \"'"$NEW_ICON"'\"@' ${PKG_FILE}
rm -rf ${PKG_FILE}.bak
else
echo -e "${YELLOW}${BOLD}Warning: Could not download icon - check URL${RESET}"
# Rename the 'icon' property to 'ignored' so the icon is not used
sed -i.bak -e 's@icon:@ignored:@' ${CHART_FILE}
fi
fi
echo " + Packaging Helm chart"
helm package ./charts/${NAME}/${VERSION} -d ./assets/${NAME}
echo " + Updating Helm index"
# --------------------------------------------------------------------------------
# Update the helm index just for this chart
# --------------------------------------------------------------------------------
if [ -f "${HELM_INDEX}" ]; then
UPDATE="--merge ${HELM_INDEX}"
fi
# Base URL referencing assets directly from GitHub
BASE_URL="assets/${NAME}"
rm -rf ${CHART_TMP}
mkdir -p ${CHART_TMP}
cp ${BASE_DIR}/assets/${NAME}/${NAME}-${VERSION}.tgz ${CHART_TMP}
helm repo index ${CHART_TMP} --url ${BASE_URL} ${UPDATE}
cp ${CHART_TMP}/index.yaml ${HELM_INDEX}
fi
done
echo ""
done
# Clean up
rm -rf ${CHART_TMP}
rm -rf ${TMP}