-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·218 lines (183 loc) · 6.66 KB
/
setup.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
#!/usr/bin/env bash
application_root="/srv"
application_data="$application_root/.application_data"
container="/cvmfs/unpacked.cern.ch/registry.hub.docker.com/cmsml/cmsml:3.10"
env_extras=""
virtual_env_path="$application_data/venv"
function box_out()
{
local box_h="#"
local box_v="#"
local s=("$@") b w
for l in "${s[@]}"; do
((w<${#l})) && { b="$l"; w="${#l}"; }
done
echo " ${box_h}${b//?/${box_h}}${box_h}
${box_v} ${b//?/ } ${box_v}"
for l in "${s[@]}"; do
printf "${box_v} %*s ${box_v}\n" "-$w" "$l"
done
echo "${box_v} ${b//?/ } ${box_v}
${box_h}${b//?/${box_h}}${box_h}"
}
function activate_venv(){
source $virtual_env_path/bin/activate
local localpath="$VIRTUAL_ENV$(python3 -c 'import sys; print(f"/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages")')"
export PYTHONPATH=${localpath}:$PYTHONPATH
}
function version_info(){
local packages_to_show=("gpytorch" "numpy" "pyro-ppl" "torch" "numpyro")
local package_info="$(python3 -m pip show "${packages_to_show[@]}")"
for package in "${packages_to_show[@]}"; do
awk -v package="$package" 'BEGIN{pat="Name: " package } a==1{printf("%s: %s\n", package, $2); exit} $0~pat{a++}' \
<<< "$package_info"
done >&2
}
function create_venv(){
export TMPDIR=$(mktemp -d -p .)
export PIP_DOWNLOAD_CACHE=".pipcache"
trap 'rm -rf -- "$TMPDIR"' EXIT
python3 -m venv --system-site-packages "$virtual_env_path"
activate_venv
printf "Created virtual environment %s\n" "$env_path"
printf "Upgrading installation tools\n"
python3 -m pip install pip --upgrade
python3 -m pip install setuptools pip wheel --upgrade
printf "Installing project\n"
if [[ -z $env_extras ]]; then
python3 -m pip install -U -e .
else
python3 -m pip install -U -e ".[$env_extras]"
fi
# pip3 install ipython --upgrade
python3 -m ipykernel install --name "$env_name" --prefix "$application_data/local/"
rm -rf $TMPDIR && unset TMPDIR
sed -i "/PS1=/d" "$virtual_env_path"/bin/activate
trap - EXIT
}
function rcmode(){
K="\[\033[0;30m\]" # black
R="\[\033[0;31m\]" # red
G="\[\033[0;32m\]" # green
Y="\[\033[0;33m\]" # yellow
B="\[\033[0;34m\]" # blue
M="\[\033[0;35m\]" # magenta
C="\[\033[0;36m\]" # cyan
W="\[\033[0;37m\]" # white
EMK="\[\033[1;30m\]"
EMR="\[\033[1;31m\]"
EMG="\[\033[1;32m\]"
EMY="\[\033[1;33m\]"
EMB="\[\033[1;34m\]"
EMM="\[\033[1;35m\]"
EMC="\[\033[1;36m\]"
EMW="\[\033[1;37m\]"
BGK="\[\033[40m\]"
BGR="\[\033[41m\]"
BGG="\[\033[42m\]"
BGY="\[\033[43m\]"
BGB="\[\033[44m\]"
BGM="\[\033[45m\]"
BGC="\[\033[46m\]"
BGW="\[\033[47m\]"
NONE="\[\033[0m\]" # unsets color to term's fg color
mkdir -p $application_data/local
HISTSIZE=50000
HISTFILESIZE=20000
export HISTCONTROL="erasedups:ignoreboth"
export HISTTIMEFORMAT='%F %T '
export HISTIGNORE=:"&:[ ]*:exit:ls:bg:fg:history:clear"
shopt -s histappend
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
shopt -s cmdhist &>/dev/null
export HISTFILE=/srv/.bash_history
export CONDOR_CONFIG="$application_data/.condor_config"
export JUPYTER_PATH=$application_data/local/share/jupyter
export JUPYTER_RUNTIME_DIR=$application_data/local/share/jupyter/runtime
export JUPYTER_DATA_DIR=$application_data/local/share/jupyter
export IPYTHONDIR=$application_data/local/ipython
export MPLCONFIGDIR=$application_data/local/mpl
export MPLBACKEND="Agg"
#export LD_LIBRARY_PATH=/opt/conda/lib/:$LD_LIBRARY_PATH
#export POETRY_HOME=/srv/.local/poetry
#if [[ ! -d $POETRY_HOME ]]; then
# curl -sSL https://install.python-poetry.org | python3 -
#fi
if [[ ! -d $virtual_env_path ]]; then
printf "Virtual environment does not exist, creating virtual environment in $virtual_env_path\n"
create_venv
fi
activate_venv
PS1="${R}[APPTAINER\$( [[ ! -z \${VIRTUAL_ENV} ]] && echo "/\${VIRTUAL_ENV##*/}")]${M}[\t]${W}\u@${C}\h:${G}[\w]> ${NONE}"
unset PROMPT_COMMAND
local localpath="$VIRTUAL_ENV$(python3 -c 'import sys; print(f"/lib/python{sys.version_info.major}.{sys.version_info.minor}/site-packages")')"
if [[ -d $localpath/argcomplete ]]; then
source $localpath/argcomplete/bash_completion.d/_python-argcomplete
eval "$(register-python-argcomplete analyzer)"
fi
welcome_message="
Single Stop Statistical Framework
........................................................
Python version is $(python3 --version)
Using environment $VIRTUAL_ENV
........................................................
"
IFS=$'\n' read -rd '' -a split_welcome_message <<<"$welcome_message"
mkdir -p .private
box_out "${split_welcome_message[@]}"
}
function startup_with_container(){
local rel_data=${application_data#$application_root/}
mkdir -p $rel_data
local in_apptainer=${APPTAINER_COMMAND:-false}
local apptainer_flags="--nv"
if [ "$in_apptainer" = false ]; then
if command -v condor_config_val &> /dev/null; then
condor_config_val -summary > .condor_config
fi
if [[ -e $HISTFILE ]]; then
apptainer_flags="$apptainer_flags --bind $HISTFILE:/srv/.bash_history"
fi
if [[ $(hostname) =~ "fnal" ]]; then
apptainer_flags="$apptainer_flags --bind /uscmst1b_scratch/"
fi
if [[ $(hostname) =~ "umn" ]]; then
apptainer_flags="$apptainer_flags --bind /local/cms/user/"
fi
if [[ ! -z "${X509_USER_PROXY}" ]]; then
apptainer_flags="$apptainer_flags --bind ${X509_USER_PROXY%/*}"
fi
if [[ -d "$HOME/.globus" ]]; then
apptainer_flags="$apptainer_flags --bind $HOME/.globus" # --bind $HOME/.rnd"
fi
apptainer exec \
--env "APPTAINER_WORKING_DIR=$PWD" \
--env "APPTAINER_IMAGE=$container" $apptainer_flags \
--bind /cvmfs \
--bind ${PWD}:/srv \
--pwd /srv "$container" /bin/bash \
--rcfile <(printf "source setup.sh bashrc")
else
printf "Already in apptainer, nothing to do.\n"
fi
}
function start_jupyter(){
local port=${1:-8999}
python3 -m jupyter lab --no-browser --port "$port" --allow-root
}
function main(){
local mode="${1:-apptainer}"
case "$mode" in
apptainer )
startup_with_container
;;
bashrc )
rcmode
;;
* )
printf "Unknown mode\n"
return 1
;;
esac
}
main "$@"