-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup-helper.sh
305 lines (232 loc) · 7.77 KB
/
setup-helper.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
DOC_BASE="
It will do the following:
- Print this message and wait for confirmation
"
DOC_TOOLS="
- Install the following tools if not already installed (might make changes to .zshrc/.bashrc/...)
- brew (if not available, will need sudo access to install)
- kubectl (Kubernetes Cli)
- helmfile
- werf
- devspace (v5.18.5 not latest)
- kubelogin (int128/kubelogin/kubelogin)
NOTE: If you don't have brew (homebrew) installed (i.e. running brew --version gives 'command not found'), use razorpay self-serve app to make yourself admin before running this script again.
"
DOC_ACCESS="
- Configure these tools with kubernetes cluster info
- Configure these tools to use your razorpay email to login to the kubernetes cluster
- [Needs VPN] [Spinnaker Pipeline Trigger] Provision access to the kubernetes cluster for your razorpay email
"
DEV_SHRC_FILE="${HOME}/.devstack/shrc"
BIN_DIR="${HOME}/.devstack/bin"
BIN_DIR_EXPR="\${HOME}/.devstack/bin"
OS="$(uname | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in
i?86) ARCH="386" ;;
x86_64) ARCH="amd64" ;;
esac
test_private_connection() {
declare url="$1"
echo "Checking if ${url} is reachable..."
declare status=$(curl -s -I -o /dev/null -w '%{http_code}' --connect-timeout 10 "$url")
declare errMsg="Please check if you're connected to VPN and ${url} is reachable in browser"
[[ $status > 199 && $status < 400 ]] && echo 'ok' || abort "$errMsg"
}
add_dev_shrc_to_user_shrc() {
declare shellType="$(printf '%s' "$SHELL" | rev | cut -d'/' -f1 | rev)"
declare userShrcFile="${HOME}/.${shellType}rc"
declare sourceCmd="source ${DEV_SHRC_FILE}"
idempotent_file_append "$sourceCmd" "$userShrcFile"
}
append_line_to_file() {
declare line="$1"
declare file="$2"
mkdir -p "$(dirname $file)"
[[ -e $file ]] || touch "$file"
echo "$line" >> "$file"
}
idempotent_file_append() {
declare line="$1"
declare file="$2"
grep -qsxF -- "$line" "$file" || append_line_to_file "$line" "$file"
}
add_cmd_to_shrc() {
declare cmd="$1"
idempotent_file_append "$cmd" "$DEV_SHRC_FILE"
source "$DEV_SHRC_FILE"
}
check_path_contains() {
declare dir="$1"
[[ "$PATH" = *":${dir}:"* ]] || [[ "$PATH" = *":${dir}" ]] || [[ "$PATH" = "${dir}:"* ]]
}
add_dir_to_path() {
declare pathExpression="$1"
declare exepectedPathComponent="$2"
declare pathAppendCmd="export PATH=\"${pathExpression}:\${PATH}\""
check_path_contains "$exepectedPathComponent" || add_cmd_to_shrc "$pathAppendCmd"
}
install_binary() {
declare url="$1"
declare dir="$2"
declare bin="$3"
mkdir -p "$dir"
curl -L "$url" > "${dir}/${bin}"
chmod +x "${dir}/${bin}"
}
install() {
declare cmdName="$1"
declare installCmd="${2-}"
declare versionCmd="${3-}"
echo "looking for ${cmdName}"
declare path="$(which $cmdName)" || true
if [[ -z "$path" ]]; then
echo "couldn't find ${cmdName}. installing..."
if [[ -z "$installCmd" ]]; then
# default for installation
brew install "$cmdName"
else
"$installCmd"
fi
else
echo "found ${cmdName} at ${path}"
fi
if [[ -z "$versionCmd" ]]; then
# default for version check
"$cmdName" --version
else
"$versionCmd"
fi
}
abort() {
declare message="$1"
echo "$message"
exit 1
}
read_email() {
declare target="$1"
read -p "Enter your (razorpay) email address:" "$target"
is_rzp_email ${!target} || abort "Not a valid razorpay email address"
}
confirm() {
declare prompt="$1"
read -p "${prompt}Press enter to continue. Press any other key to stop." -n 1
[[ -z $REPLY ]]
}
spinnaker_webhook() {
declare spinnaker="$1"
declare webhook="$2"
declare parameters="$3"
curl -X POST "https://${spinnaker}/webhooks/webhook/${webhook}" \
-H "content-type: application/json" \
-d "{\"parameters\":${parameters}}"
}
is_rzp_email() {
declare input="$1"
[[ "$input" =~ ^[a-zA-Z0-9.!\#$%\&\'*+/=?^_\`{|}~-]+@razorpay\.com$ ]]
}
oidc_exists() {
declare email="$1"
declare template="{{\$res := 0}}{{if .users}}{{range .users}}{{if eq .name \"${email}\" }}{{\$res = 1}}{{end}}{{end}}{{end}}{{\$res}}"
[[ $(kubectl config view -o=go-template --template="${template}") == 1 ]]
}
install_devspace() {
declare tag="${OS}-${ARCH}"
declare version="v5.18.5"
declare url="https://github.com/loft-sh/devspace/releases/download/${version}/devspace-${tag}"
install_binary "$url" "${BIN_DIR}" "devspace"
}
install_brew() {
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
add_cmd_to_shrc 'eval "$(/opt/homebrew/bin/brew shellenv)"'
}
version_brew() {
brew config
}
version_kubectl() {
kubectl version --client --output yaml
}
install_werf() {
declare tag="${OS}-${ARCH}"
declare version="1.2.174"
declare url="https://tuf.werf.io/targets/releases/${version}/${tag}/bin/werf"
install_binary "$url" "${BIN_DIR}" "werf"
}
version_werf() {
echo "werf version: $(werf version)"
}
configure_helmfile_for_werf() {
add_cmd_to_shrc "export WERF_HELM3_MODE=1"
add_cmd_to_shrc "alias helmfile='helmfile --enable-live-output -b werf'"
}
install_kubelogin() {
brew install int128/kubelogin/kubelogin
}
cluster_config() {
declare contextName="$1"
declare clusterName="$2"
declare server="$3"
declare cadata="$4"
declare user="$5"
kubectl config set-cluster "$clusterName" --server="$server"
kubectl config set "clusters.${clusterName}.certificate-authority-data" "$cadata"
kubectl config set-context "$contextName" --cluster="$clusterName" --user="$user"
kubectl config use-context "$contextName"
echo "kubectl config current-context : $(kubectl config current-context)"
}
oidc_config() {
declare email="$1"
declare oidcIssuerUrl="$2"
declare oidcClientId="$3"
declare oidcClientSecret="$4"
oidc_exists "$email" && return 0
kubectl config set-credentials "$email" \
--exec-api-version=client.authentication.k8s.io/v1beta1 \
--exec-command=kubectl \
--exec-arg=oidc-login \
--exec-arg=get-token \
--exec-arg=--oidc-issuer-url="$oidcIssuerUrl" \
--exec-arg=--oidc-client-id="$oidcClientId" \
--exec-arg=--oidc-client-secret="$oidcClientSecret" \
--exec-arg=--oidc-extra-scope=email
}
setup_tools() {
install "brew" "install_brew" "version_brew"
install "kubectl" "" "version_kubectl"
install "helmfile"
add_dir_to_path "${BIN_DIR_EXPR}" "${BIN_DIR}"
install "werf" "install_werf" "version_werf"
install "devspace" "install_devspace"
# updates the helmfile version to the latest
brew install helmfile
configure_helmfile_for_werf
install "kubelogin" "install_kubelogin"
add_dev_shrc_to_user_shrc
}
final() {
echo "Done. Now, remember to open new terminal for these commands to be available"
}
setup_tools_only() {
confirm "Starting setup for devstack tools:${DOC_BASE}${DOC_TOOLS}"
setup_tools
final
}
e2e() {
declare oidcIssuerUrl="$1"
declare oidcClientId="$2"
declare oidcClientSecret="$3"
declare contextName="$4"
declare clusterName="$5"
declare clusterUrl="$6"
declare cadata="$7"
declare spinnakerHost="$8"
declare accessWebhook="$9"
confirm "Starting setup for devstack:${DOC_BASE}${DOC_TOOLS}${DOC_ACCESS}"
test_private_connection "https://${spinnakerHost}"
read_email email
setup_tools
oidc_config "$email" "$oidcIssuerUrl" "$oidcClientId" "$oidcClientSecret"
cluster_config "$contextName" "$clusterName" "$clusterUrl" "$cadata" "$email"
spinnaker_webhook "$spinnakerHost" "$accessWebhook" "{\"user_email\": \"${email}\"}"
final
}