-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathinstall_rancher
367 lines (319 loc) · 12.1 KB
/
install_rancher
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
##
## system configurations
##
## Note: the `rancher/agent` must be coupled with the `rancher/server`, which
## similarly constrains the docker-engine version:
##
## - https://github.com/rancher/rancher/issues/14182#issuecomment-399698574
## - https://github.com/rancher/rancher/releases/tag/v1.6.18
##
CWD=$(pwd)
RANCHER_CLI_VERSION='v0.6.9'
RANCHER_AGENT_VERSION='v1.2.10'
RANCHER_SERVER_VERSION='v1.6.18'
RANCHER_REPO='https://github.com/rancher/cli'
BOOT2DOCKER_REPO='https://github.com/boot2docker/boot2docker'
OPERATING_SYSTEM=$(uname -s)
RANCHER_CONTAINER='rancher'
RANCHER_PORT='8080'
## system configuration: initialize for windows case
PYTHON_VERSION_FULL='3.6.4'
WINDOWS_VERSION='10'
## stack configurations
RANCHER_STACK='MLStack'
DOCKER_COMPOSE='docker-compose.rancher.yml'
RANCHER_COMPOSE='rancher-compose.yml'
RANCHER_COMPOSE_TEMPLATE='rancher-template.yml'
## cleanup docker volumes
rm -rf interface/static/{js,css}
## environment variables used by docker
##
## @CWD, environment variable, needed to build absolute path for docker-compose:
##
## - https://github.com/jeff1evesque/machine-learning/issues/2935#issuecomment-373184049
##
export CWD
case "${OPERATING_SYSTEM}" in
Linux*)
DISTRO_TYPE='linux'
RANCHER_DISTRO='rancher-linux-amd64';;
Darwin*)
DISTRO_TYPE='unix'
RANCHER_DISTRO='rancher-darwin-amd64';;
CYGWIN*)
DISTRO_TYPE='windows'
RANCHER_DISTRO='rancher-windows-amd64';;
MINGW*)
DISTRO_TYPE='windows'
RANCHER_DISTRO='rancher-windows-amd64';;
*)
echo ''
echo 'Error: operating system not known, please open an issue:'
echo ''
echo 'https://github.com/jeff1evesque/machine-learning/issues/new'
echo ''
exit 1
esac
## download rancher-cli
if [ "$DISTRO_TYPE" = 'unix' ]; then
curl -OL "$RANCHER_REPO/releases/download/$RANCHER_CLI_VERSION/$RANCHER_DISTRO-$RANCHER_CLI_VERSION.tar.gz"
tar zxf "$RANCHER_DISTRO-$RANCHER_CLI_VERSION.tar.gz"
rm "$RANCHER_DISTRO-$RANCHER_CLI_VERSION.tar.gz"
sudo mv rancher-"$RANCHER_CLI_VERSION"/rancher /usr/local/bin/rancher
sudo chmod +x /usr/local/bin/rancher
elif [ "$DISTRO_TYPE" = 'linux' ]; then
if ! type curl &>/dev/null; then
sudo apt-get -y install curl || sudo yum -y install curl || (echo 'curl cannot install' && exit 1)
fi
curl -OL "$RANCHER_REPO/releases/download/$RANCHER_CLI_VERSION/$RANCHER_DISTRO-$RANCHER_CLI_VERSION.tar.gz"
tar zxf "$RANCHER_DISTRO-$RANCHER_CLI_VERSION.tar.gz"
rm "$RANCHER_DISTRO-$RANCHER_CLI_VERSION.tar.gz"
sudo mv rancher-"$RANCHER_CLI_VERSION"/rancher /usr/local/bin/rancher
sudo chmod +x /usr/local/bin/rancher
elif [ "$DISTRO_TYPE" = 'windows' ]; then
WINDOWS_CURRENT_VERSION=$(
powershell.exe \
-NoProfile -InputFormat None -ExecutionPolicy Bypass \
-Command "((
Get-ItemProperty -path 'HKLM:\software\microsoft\Windows NT\CurrentVersion'
).ProductName).split(' ')[1]"
)
curl -OL "$RANCHER_REPO/releases/download/$RANCHER_CLI_VERSION/$RANCHER_DISTRO-$RANCHER_CLI_VERSION.zip"
unzip -j "$RANCHER_DISTRO-$RANCHER_CLI_VERSION.zip"
mkdir -p /c/programdata/rancher/bin
mv -f rancher.exe /c/programdata/rancher/bin
rm "$RANCHER_DISTRO-$RANCHER_CLI_VERSION.zip"
## install python
command -v python >/dev/null 2>&1 || INSTALL_PYTHON=true
if [[ $OPERATING_SYSTEM == MINGW* ]] && [[ $INSTALL_PYTHON == true ]]; then
PYTHON_VERSION=$(echo ${PYTHON_VERSION_FULL%.*} | tr --delete .)
while [[ "$INSTALL_PYTHON" != 'y' ]] && [[ "$INSTALL_PYTHON" != 'n' ]]
do
echo ''
echo "Windows (MINGW*) installation for rancher-cli,"
echo "depends on 'choco', and 'python'."
echo ''
echo '[Y]: install'
echo '[N]: do not install'
echo ''
read -rp 'Proceed with installation: ' INSTALL_PYTHON
done
if [ "${INSTALL_PYTHON,,}" = 'y' ]; then
powershell.exe -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
"
## current process
PATH="$PATH:/c/programdata/chocolatey/bin:/c/programdata/rancher/bin:/c/python$PYTHON_VERSION"
## successive processes
setx PATH "%PATH%;C:\programdata\chocolatey\bin;C:\programdata\rancher\bin;C:\python$PYTHON_VERSION"
## install python
choco install -f python --version "$PYTHON_VERSION_FULL"
else
echo 'terminating rancher installation...'
exit 0
fi
fi
else
echo ''
echo 'Error: distro not known, please open an issue:'
echo ''
echo 'https://github.com/jeff1evesque/machine-learning/issues/new'
echo ''
exit 1
fi
## install rancher ecosystem
if docker -v >/dev/null 2>&1; then
## docker toolbox provides 'default' container
if [ "$DISTRO_TYPE" = 'windows' ] && \
[ "$RANCHER_CONTAINER" != 'default' ] && \
docker ps -a | grep "$RANCHER_CONTAINER";
then
docker kill "$RANCHER_CONTAINER"
docker rm "$RANCHER_CONTAINER"
elif [ "$DISTRO_TYPE" != 'windows' ] && \
docker ps -a | grep "$RANCHER_CONTAINER";
then
docker kill "$RANCHER_CONTAINER"
docker rm "$RANCHER_CONTAINER"
fi
## start rancher server
docker run -d \
--name "$RANCHER_CONTAINER" \
--restart=unless-stopped \
-p "$RANCHER_PORT":8080 \
rancher/server:"$RANCHER_SERVER_VERSION"
mkdir -p ~/.rancher
##
## error detection: capture error then exit
##
## Note: this was intended to target the below 'ACCESS=$(...) && break'.
##
set -e
SLEEPER=0
while true
do
## legacy windows uses docker toolbox, which requires 'default' container
if [ "$DISTRO_TYPE" = 'windows' ] && [ "$WINDOWS_CURRENT_VERSION" -le "$WINDOWS_VERSION" ]; then
echo ''
echo 'Windows docker implementation, requires DockerToolbox, which'
echo "creates a 'default' container to manage docker. To proceed,"
echo "rancher configurations will reflect port '8080'."
echo ''
RANCHER_CONTAINER='default'
SERVER_IP=$(docker-machine ip 'default')
else
SERVER_IP=$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" "$RANCHER_CONTAINER")
fi
## generate access + secret key:
##
## Note: exporting environment eliminates the need to explicitly provide
## environment variable to the 'rancher' command.
##
## Note: 'curl' response for ACCESS is json:
##
## https://github.com/rancher/rancher/issues/4961#issuecomment-222598633
##
export RANCHER_URL="http://$SERVER_IP:$RANCHER_PORT"
## access rancher server
ACCESS=$(
curl -v -XPOST -H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"type":"apikey",
"accountId":"1a1",
"name":"admin",
"description":null,
"created":null,
"kind":null,
"removed":null,
"uuid":null
}' \
"$RANCHER_URL"/v2-beta/apikeys
) && break
if [ "$SLEEPER" = 300 ]; then
echo ''
echo 'Attempted to request access + secret key,'
echo 'from rancher for over 5 minutes.'
echo ''
echo '[Y]: yes, keep trying'
echo '[N]: no, exit installation'
echo ''
read -rp 'Choice: ' API_KEYS
if [ "${API_KEYS,,}" = 'n' ]; then
exit 1
else
SLEEPER=$((SLEEPER+30))
echo 'sleeping 30s'
sleep 30
fi
else
echo ''
echo 'Rancher server has not started. Attempting to obtain'
echo 'access + secret key, from rancher in 30s'.
echo ''
SLEEPER=$((SLEEPER+30))
sleep 30
fi
done
##
## discontinue error detection
##
set +e
if [ "$ACCESS" ]; then
SLEEPER=0
STATE='registering'
## post method creates new resource
curl -v -XPOST -H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"$RANCHER_URL"/v2-beta/projects/1a5/registrationTokens
while true
do
REGISTER=$(
curl -v -H 'Accept: application/json' \
-H 'Content-Type: application/json' \
"$RANCHER_URL"/v2-beta/projects/1a5/registrationTokens
)
STATE=$(echo "$REGISTER" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["data"][0]["state"])')
if [ "${STATE,,}" = 'active' ]; then
TOKEN=$(echo "$REGISTER" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["data"][0]["token"])')
break
elif [ "$SLEEPER" = 60 ]; then
echo ''
echo 'Waited over 60s for active state.'
echo ''
echo '[Y]: yes, keep waiting'
echo '[N]: no, exit installation'
echo ''
read -rp 'Choice: ' TOKEN_STATUS
if [ "${TOKEN_STATUS,,}" = 'n' ]; then
exit 1
else
SLEEPER=$((SLEEPER+2))
sleep 2
fi
else
SLEEPER=$((SLEEPER+2))
sleep 2
fi
done
fi
RANCHER_ACCESS_KEY=$(echo "$ACCESS" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["name"])')
RANCHER_SECRET_KEY=$(echo "$ACCESS" | python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["secretValue"])')
## @environment: must contain user account id:
##
## - 1a1, administrator
## - 1a5, default environment
##
## Note: rancher host registration can "also" be validated against:
##
## - $RANCHER_URL/env/1a5/infra/hosts/add?driver=custom
##
echo "{
\"accessKey\":\"$RANCHER_ACCESS_KEY\",
\"secretKey\":\"$RANCHER_SECRET_KEY\",
\"url\":\"$RANCHER_URL\",
\"environment\":\"1a5\"
}" > ~/.rancher/cli.json
if [ "$TOKEN" ]; then
## rancher agent: legacy docker toolbox requires older 'boot2docker'.
##
## - https://github.com/rancher/rancher/issues/10970#issuecomment-372061736
## - https://github.com/jeff1evesque/machine-learning/issues/2935#issuecomment-372060078
## - https://github.com/jeff1evesque/machine-learning/issues/2935#issuecomment-372061391
##
if [ "$DISTRO_TYPE" = 'windows' ] && [ "$WINDOWS_CURRENT_VERSION" -le "$WINDOWS_VERSION" ]; then
docker-machine \
create \
-d virtualbox \
--virtualbox-boot2docker-url="$BOOT2DOCKER_REPO"/releases/download/v17.09.1-ce/boot2docker.iso \
rancher
docker-machine.exe env rancher
eval "$('C:\Program Files\Docker Toolbox\docker-machine.exe' env rancher)"
fi
## create docker volumes
mkdir -p ./interface/static/{js,css}
## register host with rancher
docker run \
--rm \
--privileged \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /var/lib/rancher:/var/lib/rancher \
rancher/agent:"$RANCHER_AGENT_VERSION" \
"$RANCHER_URL/v1/scripts/$TOKEN"
## create rancher-compose
sed "s|PREPATH|$PWD|g" "$RANCHER_COMPOSE_TEMPLATE" > "$RANCHER_COMPOSE"
## create rancher stack
rancher stacks \
create "$RANCHER_STACK" \
-f "$DOCKER_COMPOSE" \
-r "$RANCHER_COMPOSE" \
--start
else
echo 'rancher registration token not generated'
exit 1
fi
else
echo 'please install docker per https://github.com/jeff1evesque/machine-learning#installation'
exit 1
fi