-
Notifications
You must be signed in to change notification settings - Fork 7
/
github.sh
executable file
·158 lines (139 loc) · 4.08 KB
/
github.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
#!/bin/bash
set -e
usage() {
cat << EOF
Install or upgrade Helm charts from Github
https://docs.helm.sh/helm/#helm-install
https://docs.helm.sh/helm/#helm-upgrade
Available Commands:
helm github install Install a Helm chart from Github
helm github upgrade <release> Upgrades the release to a new version of the Helm chart from Github
helm github fetch Fetch Helm chart from Github (e.g git clone)
Available Flags:
--repo, -r (Required) Specify the repo to install
--ref, -b (Optional) Specify the branch, commit, or reference to checkout before installing
--path, -p (Optional) Specify a path within repo where helm chart is stored. Must be relative to base of repo
--debug (Optional) Verbosity intensifies... ...
--help (Optional) This text.
--update, -u (Optional) Update this plugin to the latest version from the master branch
Example Usage:
helm github install --repo [email protected]:coreos/alb-ingress-controller.git --ref master --path alb-ingress-controller-helm
helm github upgrade happy-panda --repo [email protected]:kubernetes/charts.git --path stable/external-dns
EOF
}
# Create the passthru array
PASSTHRU=()
while [[ $# -gt 0 ]]
do
key="$1"
# Parse arguments
case $key in
-r|--repo)
REPO="$2"
shift # past argument
shift # past value
;;
-b|--ref)
BRANCH="$2"
shift # past argument
shift # past value
;;
-p|--path)
CHARTPATH="$2"
shift # past argument
shift # past value
;;
--debug)
DEBUG=TRUE
shift # past argument
;;
--help)
HELP=TRUE
shift # past argument
;;
--update)
UPDATE=TRUE
shift # past argument
;;
*) # unknown option
PASSTHRU+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
# Restore PASSTHRU parameters
set -- "${PASSTHRU[@]}"
# Show help if flagged
if [ "$HELP" == "TRUE" ]; then
usage
exit 0
fi
# Update this Helm plugin
if [ "$UPDATE" == "TRUE" ]; then
cd "$HELM_HOME/plugins/helm-github"
git pull
PLUGIN_VERSION=$(git log --pretty=format:'%h' -n 1)
cd -
echo "Success! Updated this plugin to: $PLUGIN_VERSION"
exit 0
fi
if [ -z "$REPO" ]; then
echo "Error: No Repo provided. Please provide --repo flag"
usage
exit 1
fi
if [ -z "$BRANCH" ]; then
BRANCH="master"
fi
if [ -z "$CHARTPATH" ]; then
CHARTPATH="."
fi
REPO_BASE_NAME="$(basename $REPO .git)"
REPO_LOCATION="$HELM_HOME/plugins/helm-github/repos/$REPO_BASE_NAME"
# Print params for debugging
if [ "$DEBUG" == "TRUE" ]; then
echo "PARAMS";
echo $*;
echo " ......................... ";
echo Helm home = "$HELM_HOME";
echo REPO = "${REPO}";
echo BRANCH = "${BRANCH}";
echo CHARTPATH = "${CHARTPATH}";
echo PASSTHRU = "${PASSTHRU[@]}";
echo REPO_BASE_NAME = "$REPO_BASE_NAME";
echo REPO_LOCATION = "$REPO_LOCATION";
fi
# Checkout the repo & enter it
if [ -d "${REPO_LOCATION}" ]; then
cd $REPO_LOCATION
git checkout master;
git pull;
else
git clone $REPO $REPO_LOCATION;
cd $REPO_LOCATION;
fi
# Checkout the correct branch
git checkout $BRANCH
# Exit the repo
cd -
# COMMAND must be either 'install' or 'upgrade'
COMMAND=${PASSTHRU[0]}
if [ "$COMMAND" == "install" ]; then
echo "Installing the Helm chart from the GitHub repo"
# Take out the first variable in passthru (install) so that helm install works
helm install "${PASSTHRU[@]:1}" $REPO_LOCATION/$CHARTPATH
exit 0
elif [ "$COMMAND" == "upgrade" ]; then
RELEASE=${PASSTHRU[1]}
echo "Upgrading ${RELEASE} release to a new version of the chart from the GitHub repo"
# Take out the first two variables in passthru (install <release>) so that helm upgrade works
helm upgrade "${PASSTHRU[@]:2}" $RELEASE $REPO_LOCATION/$CHARTPATH
exit 0
elif [ "$COMMAND" == "fetch" ]; then
echo "GitHub repo has been fetched to ${REPO_LOCATION}"
exit 0
else
echo "Error: Invalid command, must be one of 'install' or 'upgrade'"
usage
exit 1
fi