forked from aslafy-z/helm-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-git-plugin.sh
executable file
·283 lines (229 loc) · 7.9 KB
/
helm-git-plugin.sh
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env sh
# See Helm plugins documentation: https://docs.helm.sh/using_helm/#downloader-plugins
set -e
readonly bin_name="helm-git"
readonly allowed_protocols="https http file ssh"
readonly url_prefix="git+"
readonly error_invalid_prefix="Git url should start with '$url_prefix'. Please check helm-git usage."
readonly error_invalid_protocol="Protocol not allowed, it should match one of theses: $allowed_protocols."
debug=0
if [ "$HELM_GIT_DEBUG" = "1" ]; then
debug=1
fi
export TMPDIR=${TMPDIR:-/tmp}
## Tooling
string_starts() { [ "$(echo "$1" | cut -c 1-${#2})" = "$2" ]; }
string_ends() { [ "$(echo "$1" | cut -c $((${#1} - ${#2} + 1))-${#1})" = "$2" ]; }
string_contains() { echo "$1" | grep -q "$2"; }
path_join() { echo "${1:+$1/}$2" | sed 's#//#/#g'; }
## Logging
debug() {
[ $debug = 1 ] && echo "Debug in plugin '$bin_name': $*" >&2
return 0
}
error() {
echo "Error in plugin '$bin_name': $*" >&2
exit 1
}
warning() {
echo "Warning in plugin '$bin_name': $*" >&2
}
## Functions
# git_try(git_repo)
git_try() {
_git_repo=$1
GIT_TERMINAL_PROMPT=0 git ls-remote "$_git_repo" --refs >&2 || return 1
}
# git_checkout(sparse, target_path, git_repo, git_ref, git_path)
git_checkout() {
_sparse=$1
_target_path=$2
_git_repo=$3
_git_ref=$4
_git_path=$5
cd "$_target_path" >&2
git init --quiet
git config pull.ff only
git remote add origin "$_git_repo" >&2
if [ "$_sparse" = "1" ]; then
git config core.sparseCheckout true
[ -n "$_git_path" ] && echo "$_git_path/*" >.git/info/sparse-checkout
git pull --quiet --depth 1 origin "$_git_ref" >&2 || \
error "Unable to sparse-checkout. Check your Git ref ($git_ref) and path ($git_path)."
else
git fetch --quiet --tags origin >&2 || \
error "Unable to fetch remote. Check your Git url."
git checkout --quiet "$git_ref" >&2 || \
error "Unable to checkout ref. Check your Git ref ($git_ref)."
fi
# shellcheck disable=SC2010,SC2012
if [ "$(ls -A | grep -v '^.git$' -c)" = "0" ]; then
error "No files have been checked out. Check your Git ref ($git_ref) and path ($git_path)."
fi
}
# helm_v2()
helm_v2() {
"$HELM_BIN" version -c --short | grep -q v2
}
# helm_check()
helm_check() {
"$HELM_BIN" help | grep -qF "The Kubernetes package manager"
}
# helm_init(helm_home)
helm_init() {
if ! helm_check; then return 1; fi
if ! helm_v2; then return 0; fi
_helm_home=$1
"$HELM_BIN" init --client-only --home "$_helm_home" >/dev/null
HELM_HOME=$_helm_home
export HELM_HOME
}
# helm_package(target_path, source_path, chart_name)
helm_package() {
_target_path=$1
_source_path=$2
_chart_name=$3
tmp_target="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
cp -r "$_source_path" "$tmp_target/$_chart_name"
_source_path="$tmp_target/$_chart_name"
cd "$_target_path" >&2
package_args=$helm_args
helm_v2 && package_args="$package_args --save=false"
# shellcheck disable=SC2086
"$HELM_BIN" package $package_args "$_source_path" >/dev/null
ret=$?
rm -rf "$tmp_target"
# forward return code
return $ret
}
# helm_dependency_update(target_path)
helm_dependency_update() {
_target_path=$1
# shellcheck disable=SC2086
"$HELM_BIN" dependency update $helm_args --skip-refresh "$_target_path" >/dev/null
}
# helm_index(target_path, base_url)
helm_index() {
_target_path=$1
_base_url=$2
# shellcheck disable=SC2086
"$HELM_BIN" repo index $helm_args --url="$_base_url" "$_target_path" >/dev/null
}
# helm_inspect_name(source_path)
helm_inspect_name() {
_source_path=$1
# shellcheck disable=SC2086
output=$("$HELM_BIN" inspect chart $helm_args "$_source_path")
name=$(echo "$output" | grep -e '^name: ' | cut -d' ' -f2)
echo "$name"
[ -n "$name" ]
}
# main(raw_uri)
main() {
helm_args="" # "$1 $2 $3"
_raw_uri=$4 # eg: git+https://git.com/user/repo@path/to/charts/index.yaml?ref=master
# If defined, use $HELM_GIT_HELM_BIN as $HELM_BIN.
if [ -n "$HELM_GIT_HELM_BIN" ]
then
export HELM_BIN="${HELM_GIT_HELM_BIN}"
# If not, use $HELM_BIN after sanitizing it or default to 'helm'.
elif
[ -z "$HELM_BIN" ] ||
# terraform-provider-helm: https://github.com/aslafy-z/helm-git/issues/101
echo "$HELM_BIN" | grep -q "terraform-provider-helm" ||
# helm-diff plugin: https://github.com/aslafy-z/helm-git/issues/107
echo "$HELM_BIN" | grep -q "diff"
then
export HELM_BIN="helm"
fi
if ! helm_check; then
error "'$HELM_BIN' is not a valid helm binary path."
fi
# Parse URI
string_starts "$_raw_uri" "$url_prefix" ||
error "Invalid format, got '$_raw_uri'. $error_invalid_prefix"
_raw_uri=$(echo "$_raw_uri" | sed 's/^git+//')
git_proto=$(echo "$_raw_uri" | cut -d':' -f1)
readonly git_proto=$git_proto
string_contains "$allowed_protocols" "$git_proto" ||
error "$error_invalid_protocol"
git_repo=$(echo "$_raw_uri" | sed -E 's#^([^/]+//[^/]+[^@\?]+)@?[^@\?]+\??.*$#\1#')
readonly git_repo=$git_repo
# TODO: Validate git_repo
git_path=$(echo "$_raw_uri" | sed -E 's#.*@([^\?]+)\/([^\?]+).*(\?.*)?#\1#')
readonly git_path=$git_path
# TODO: Validate git_path
helm_file=$(echo "$_raw_uri" | sed -E 's#.*@([^\?]+)\/([^\?]+).*(\?.*)?#\2#')
readonly helm_file=$helm_file
git_ref=$(echo "$_raw_uri" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/')
# TODO: Validate git_ref
if [ -z "$git_ref" ]; then
warning "git_ref is empty, defaulted to 'master'. Prefer to pin GIT ref in URI."
git_ref="master"
fi
readonly git_ref=$git_ref
git_sparse=$(echo "$_raw_uri" | sed '/^.*sparse=\([^&#]*\).*$/!d;s//\1/')
[ -z "$git_sparse" ] && git_sparse=1
helm_depupdate=$(echo "$_raw_uri" | sed '/^.*depupdate=\([^&#]*\).*$/!d;s//\1/')
[ -z "$helm_depupdate" ] && helm_depupdate=1
debug "repo: $git_repo ref: $git_ref path: $git_path file: $helm_file sparse: $git_sparse depupdate: $helm_depupdate"
readonly helm_repo_uri="git+$git_repo@$git_path?ref=$git_ref&sparse=$git_sparse&depupdate=$helm_depupdate"
debug "helm_repo_uri: $helm_repo_uri"
# Setup cleanup trap
cleanup() {
rm -rf "$git_root_path" \
"$helm_home_target_path" \
"$helm_target_path"
}
trap cleanup EXIT
git_root_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
readonly git_root_path=$git_root_path
git_sub_path=$(path_join "$git_root_path" "$git_path")
readonly git_sub_path=$git_sub_path
git_checkout "$git_sparse" "$git_root_path" "$git_repo" "$git_ref" "$git_path" ||
error "Error while git_sparse_checkout"
case "$helm_file" in
index.yaml) ;;
*.tgz) ;;
*)
# value files
cat "$git_path/$helm_file"
return
;;
esac
helm_target_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
readonly helm_target_path=$helm_target_path
helm_target_file="$(path_join "$helm_target_path" "$helm_file")"
readonly helm_target_file=$helm_target_file
# Set helm home
if helm_v2; then
helm_home=$("$HELM_BIN" home)
if [ -z "$helm_home" ]; then
helm_home_target_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
readonly helm_home_target_path=$helm_home_target_path
helm_init "$helm_home_target_path" || error "Couldn't init helm"
helm_home=$helm_home_target_path
fi
helm_args="$helm_args --home=$helm_home"
fi
chart_search_root="$git_sub_path"
chart_search=$(find "$chart_search_root" -maxdepth 2 -name "Chart.yaml" -print)
chart_search_count=$(echo "$chart_search" | wc -l)
echo "$chart_search" | {
while IFS='' read -r chart_yaml_file; do
chart_path=$(dirname "$chart_yaml_file")
chart_name=$(helm_inspect_name "$chart_path")
if [ "$helm_depupdate" = "1" ]; then
helm_dependency_update "$chart_path" ||
error "Error while helm_dependency_update"
fi
helm_package "$helm_target_path" "$chart_path" "$chart_name" ||
error "Error while helm_package"
done
}
[ "$chart_search_count" -eq "0" ] &&
error "No charts have been found"
helm_index "$helm_target_path" "$helm_repo_uri" ||
error "Error while helm_index"
cat "$helm_target_file"
}