-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
executable file
·357 lines (305 loc) · 9.48 KB
/
bootstrap
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
#!/usr/bin/env bash
unset CDPATH
set -eu
set -o pipefail
if [ -t 1 ] ; then export termout=true; else export termout=false; fi
this="$(echo $(basename "$0"))"
here="$(cd "$(dirname "$BASH_SOURCE")" && echo $PWD)"
fncreds="$HOME/.config/axc/.creds"
_usage='
## Usage
'$this' <specfile> [-t token] [<-u user> [-p password]] [-C] [-N]
'
_doc='
# AXC Bootstrapping On a New Host
'$_usage'
## Purpose
- Creates initial environ
For non interactive use either provide in spec or command line:
- axchange_token
or
- AXCHANGE_USER and _AXCHANGE_PASSWORD
-C prevents us from adding interactively entered creds into '$fncreds'
'
# can be overridden in spec:
CONDA_VER="3-4.5.12"
CONDA_URL="https://repo.continuum.io/miniconda/Miniconda$CONDA_VER-Linux-x86_64.sh"
DIR="/opt/axc"
LOCAL_AXC_DIR=
axchange_token=
AXCHANGE_USER=
_AXCHANGE_PASSWORD=
NO_ENTER=false
enter_env=
add_to_fn_creds=true
AXC_THEME_CODE='733.3399'
R="\e[1;38;5;124m"
M="\e[38;5;187m"
I="\e[38;5;46m"
L="\e[38;5;244m"
L2="\e[38;5;240m"
O="\e[0m" #$;38;5;251;40m"
$termout || { R=; M=; I=; L=; L2=; O=; }
out () { echo -e $* >&2; }
warn () { out "$R█ $O$*$O"; }
die () { termout=true; warn "$*"; exit 1; }
hl () { out "$I█ $I$*$O"; }
dbg () { out "$L█ $L$*$O"; }
nfo () { out "$M█ $O$*$O"; }
show () {
test "${1:-}x" == "-expx" && {
set -a
shift
show -m "exporting: " source $*
local ret=$?
set +a
return $ret
}
test "${1:-}x" == "-mx" && {
shift; dbg "$*"; shift
} || dbg "$*"
run_cmd $*
}
run_cmd () {
$termout && echo -e "$L2"
local ret=0
$* || ret=$?
$termout && echo -e "$O"
return $ret
}
# To make set -x more usable
export PS4='(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:-} : '
parse_cli () {
shift # spec is first
while getopts "Ct:u:p:hN" opt; do
case "$opt" in
h) { echo -e "$_doc"; exit 0; };;
t) axchange_token="$OPTARG";;
u) AXCHANGE_USER="$OPTARG";;
p) _AXCHANGE_PASSWORD="$OPTARG";;
C) add_to_fn_creds=false;;
N) NO_ENTER=true;;
esac
done
shift $((OPTIND-1))
}
axc_url () {
echo "$AXCHANGE_BASE_URL/scm/hg/xc3/axc/#tip"
}
isroot () { [ "$UID" != "0" -o "$HOME" != "/root" ] && return 1 || return 0; }
deact_any_other_current_env () {
test "${CONDA_SHLVL:-0}x" == "0x" && return 0
show source "${CONDA_PREFIX:-x}/bin/deactivate"
return 0
}
source_spec () {
SPECFILE="${1:-}"
test -e "$SPECFILE" || { echo "$_doc"; die "Require spec file"; }
set -a; show source "$SPECFILE"; set +a
# for conda:
PKGS="$PKGS bzip2 patch"
}
web_get() {
url="$1";u="${3:-x}"; p="${4:-x}"
_via_wget () { wget -q --http-user="$3" --http-password="$4" "$1" -O "$2"; }
_via_curl () { curl -s -u "$3:$4" "$1" > "$2"; }
builtin type wget 2>/dev/null && {
_via_wget "$url" "$2" "$u" "$p" || true; } || {
_via_curl "$url" "$2" "$u" "$p" || die "Could not download $1"
}
}
install_system_packages () {
system_pkg_install $PKGS
}
system_pkg_install () {
builtin type yum 2>/dev/null && yum install -y $* && return 0
builtin type apt-get 2>/dev/null && apt-get install -y $* && return 0
die "Please Install: $I"$*"$O"
}
download_conda () {
conda_fn="/tmp/`basename "$CONDA_URL"`"
test -e "$conda_fn" && { nfo "Already local: $conda_fn"; return 0; }
web_get "$CONDA_URL" "$conda_fn"
}
copy_dir () { (cd "$1" && tar -cf - .) | (cd "$2" && tar -xpf -); }
install_conda () {
# we make the folder clear to install:
unshare -m ls /tmp >/dev/null || die "Required: unshare -m"
md="`mktemp -t axc_install_dir_XXXXXXXX -d`"
test -z "$LOCAL_AXC_DIR" && {
cp "$here/.token" "$md/"
}
cp "$SPECFILE" "$md/SPEC"
# calling ourselves with unshared_install_conda:
show unshare -m "$here/$this" unshared_install_conda "$DIR" "$conda_fn" "$md"
show mv "$md/`basename "$DIR"`" "$INST_DEST_DIR"
show cp "$SPECFILE" "$INST_DEST_DIR/settings"
}
verify_token () {
test -z "$axchange_token" && {
export axchange_token="`cat $here/.token`"
test -z "$axchange_token" && return 1
}
local AX="$AXCHANGE_BASE_URL/accounts/auth_request/verify"
rm -f "$here/.lifetime"
web_get "$AX/$axchange_token" "$here/.lifetime"
local lt="`cat "$here/.lifetime"`"
rm -f "$here/.lifetime"
test -z "$lt" && { warn "verification failed"; return 1; }
echo "$axchange_token" > "$here/.token"
export axchange_token
}
fetch_axchange_creds_if_required () {
# If we can't use a local clone we have to fetch - with creds required.
# We will on success at least generate a .token file for later clone
# when we have merc.
cat "$SPECFILE" | grep "^[^#;]" | grep "\$AXCHANGE_BASE_URL" || {
test -e "${LOCAL_AXC_DIR:-xx}/bash" && {
nfo "Not required to fetch token"
return 0
}
}
eval `cat "$fncreds"` 2>/dev/null || true
verify_token && { nfo "axchange_token verified"; return 0; }
test -z "$AXCHANGE_USER" && {
read -p ">>> AXCHANGE_USER? " AXCHANGE_USER
}
test -z "$_AXCHANGE_PASSWORD" && {
read -p ">>> _AXCHANGE_PASSWORD? " _AXCHANGE_PASSWORD
}
local AX="$AXCHANGE_BASE_URL/accounts/auth_request"
rm -f "$here/.token"
web_get "$AX/token" "$here/.token" "$AXCHANGE_USER" "$_AXCHANGE_PASSWORD"
test -e "$here/.token" || die "Failed to get token"
export axchange_token="`cat "$here/.token"`"
$add_to_fn_creds || return 0
mkdir -p "`dirname "$fncreds"`"
touch "$fncreds"
cat "$fncreds" | \
grep "AXCHANGE_USER" | \
grep "$AXCHANGE_USER" >/dev/null || {
nfo "Adding user into "$fncreds""
echo "AXCHANGE_USER=\"$AXCHANGE_USER\"" >> "$fncreds"
}
cat "$fncreds" | grep "_AXCHANGE_PASSWORD" |\
grep ''$_AXCHANGE_PASSWORD'' >/dev/null || {
nfo "Adding password into "$fncreds""
echo "_AXCHANGE_PASSWORD=\"$_AXCHANGE_PASSWORD\"" >> "$fncreds"
}
}
bootstrap_inside () {
hl "Building inside"
show "$INST_DEST_DIR/axc/enter "$DIR/axc/bin/\#build""
$NO_ENTER && exit
echo
hl "Entering the built axc environment. Have fun."
show "$INST_DEST_DIR/axc/enter"
}
# ---------------------------------------------------------------- Run unshared
unshared_install_conda_and_axc_into_it () {
DIR="$1"
conda_fn="$2"
d_into="$3"
d="`dirname "$DIR"`"
test -e "$d" || mkdir -p "$d"
show mount -o bind "$d_into" "$d"
chmod +x "$conda_fn"
"$conda_fn" -b -p "$DIR"
source "$DIR/etc/profile.d/conda.sh"
show conda activate root
show conda create -q -y -n py2 python=2.7 mercurial=4.8.2
show unshared_add_axc_into_conda
show unshared_add_env_configs_into_conda
}
unshared_add_axc_into_conda () {
# source also for env installs:
test -z "$axchange_token" && \
export axchange_token="`cat "$d_into/.token"`" || true
test -e "${LOCAL_AXC_DIR:-}/bash" && {
mkdir "$DIR/axc"
show copy_dir "$LOCAL_AXC_DIR" "$DIR/axc"
return $?
}
show conda activate py2
show cd "$DIR"
local ret=0
show hg_clone "`axc_url`"
}
unshared_add_env_configs_into_conda () {
testmode=false
test "${1:-}x" == "testmodex" && testmode=true
$testmode || mkdir -p "$DIR/conf"
IFS=' '; for _env in $ENVS; do
nfo "Installing $I$_env$O"
defi="${!_env:-}"
test -z "$defi" && {
local rec="`${_env}_environment`"
test -z "$rec" && die "Env not defined: $_env"
$testmode && continue
nfo "$_env defined inline"
local d="$DIR/conf/$_env/recipe"
mkdir -p "$d"
echo -e "$rec" > "$d/environment.yml"
rec=
rec="`${_env}_repos`" 2>/dev/null || dbg "$_env: No repos"
test -z "$rec" || {
nfo "$_env has repos inline"
echo -e "$rec" > "$d/repos.yml"
}
continue
}
$testmode && continue
show hg_clone "$defi" "$DIR/conf/$_env"
done
}
hg_clone () {
local md="`mktemp -t axc_install_hg_home_dir_XXXXXXXX -d`"
local h="$HOME"
export HOME="$md"
echo -e '
[auth]
top.prefix = '$AXCHANGE_BASE_URL'
top.username = $axchange_token
top.password = AXC
' > "$md/.hgrc"
local ret=0
show hg clone $* || ret=$?
export HOME="$h"
return $ret
}
sanity_checks () {
test -e "${INST_DEST_DIR:-}" && die "${INST_DEST_DIR:-} exists"
test -z "$ENVS" && die "No \$ENVS."
test -z "$AXCHANGE_BASE_URL" && die "Set \$AXCHANGE_BASE_URL"
test -z "$axchange_token" && local h="No" || local h="Have"
test -z "$axchange_token" || echo "$axchange_token" > "$here/.token"
nfo "$h axchange_token"
nfo "Will install $ENVS"
unshared_add_env_configs_into_conda testmode
}
cleanup () {
# temporay files
rm -f "/tmp/axc_env.$$.txt"
rm -f "/tmp/axc_env.$$.yml"
echo -e "\e[0m"
}
trap cleanup EXIT
main () {
deact_any_other_current_env
test "x ${1:-}" == "x unshared_install_conda" && {
d_inst_tmp="$4"
show source_spec "$d_inst_tmp/SPEC"
shift
show unshared_install_conda_and_axc_into_it "$@"
exit $?
}
show source_spec "$@"
parse_cli "$@"
show install_system_packages
sanity_checks
fetch_axchange_creds_if_required
show download_conda
show install_conda
show bootstrap_inside
}
main $*