-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-chart.sh
executable file
·106 lines (91 loc) · 2.56 KB
/
install-chart.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
set -euo pipefail
# Stolen from Docker - https://get.docker.com/
command_exists() {
command -v "$@" > /dev/null 2>&1
}
if ! command_exists yq; then
echo "This script relies on yq. Install with 'brew install yq' (assuming you're on Mac)"
exit 1
fi
addRepo() {
echo "Adding repo for $1"
# https://stackoverflow.com/a/315113/1040915
{
pushd "$1" > /dev/null
if [[ -f "chart-info.yaml" ]]; then
yq '.chartRepos[] | [.name, .url] | join(" ")' chart-info.yaml | xargs -I {} sh -c "helm repo add {}"
fi
# TODO - error checking for incomplete chart-info
popd > /dev/null
} >"out/$1.out" 2>"out/$1.err"
}
process () {
echo "Processing $1"
# https://stackoverflow.com/a/315113/1040915
{
pushd "$1" > /dev/null
dir_name=$(basename $1)
if [[ ! -f "chart-info.yaml" ]]; then
args="$dir_name $dir_name helm/"
else
args=$(yq "[.namespace // \"$dir_name\", .chartName // \"$dir_name\", .chartReference // \"helm/\"] | join(\" \")" chart-info.yaml)
extraArgs=$(yq ".extraArgs // \"\"" chart-info.yaml)
if [ -n "$extraArgs" ]; then
args+=" $extraArgs"
fi
fi
if [[ -f "values.yaml" ]]; then
args+=" --values values.yaml"
fi
helm_command="helm upgrade --install --create-namespace -n $args"
if [ -n "${debug+x}" ]; then
>&3 echo "DEBUG - helm_command is $helm_command"
fi
if ! eval $helm_command; then
# The following line will print to (actual) stdout, even though (regular)
# stdout is being captured into `out/$1.out` (see end of braced-expression)
>&3 echo "=== ERROR while processing $1 - check the logs! ==="
fi
popd > /dev/null
} 3>&1 >"out/$1.out" 2>"out/$1.err"
}
# https://unix.stackexchange.com/a/50695/30828
export -f addRepo
export -f process
if [[ -d "out" ]]; then
rm -rf out-old/
mv out/ out-old/
fi
mkdir -p out/
while getopts n:a:d flag
do
case "${flag}" in
n) name=${OPTARG};;
a) all=1;;
d) debug=1;;
*) echo "Unknown flag detected"; exit 1;;
esac
done
if [ -n "${name+x}" ] && [ -n "${all+x}" ]; then
echo "You cannot set both -n and -a. Try again";
exit 1
fi
if [ -z ${name+x} ] && [ -z ${all+x} ]; then
echo "You must set one of -n [name] or -a.";
exit 1
fi
if [ -n "${name+x}" ]; then
names="./$name"
fi
if [ -n "${all+x}" ]; then
names=$(find . -type d -depth 1 -not -name 'out' -not -name 'out-old' | tr '\n' ':')
fi
IFS=':' read -ra ARR <<< "$names"
for dir in "${ARR[@]}"; do
addRepo "$dir"
done
helm repo update
IFS=':' read -ra ARR <<< "$names"
for dir in "${ARR[@]}"; do
process "$dir"
done