1
1
#! /bin/bash
2
2
3
- ALTPYTHON=/home/exacloud/software/spack/opt/spack/linux-centos7-ivybridge/gcc-8.3.1/python-3.10.8-lgmermnb2kmzendjw24fftlpxwpaokdm/bin/python3
4
- PMIN= " 8 " # python3 minor version = 3.8
3
+ # Make sure script ends as soon as an error arises
4
+ set -e
5
5
6
- script_path= " $( readlink -f " $0 " ) "
7
- script_dir= " $( dirname " $script_path " ) "
8
- umask 0002
6
+ # ################
7
+ # ## VARIABLES ###
8
+ # ################
9
9
10
- if [[ $1 == " update" ]]; then
11
- curl -s https://raw.githubusercontent.com/dirkpetersen/froster/main/install.sh? token=$( date +%s) | bash -s -- update
12
- exit 0
13
- fi
14
- if [[ -d ~ /.local/share/froster/bin ]]; then
15
- export VIRTUAL_ENV_DISABLE_PROMPT=1
16
- source ~ /.local/share/froster/bin/activate
17
- PY3=$( which python3)
18
- PY3=$( readlink -f ${PY3} )
19
- unset LIBRARY_PATH PYTHONPATH
20
- export LD_LIBRARY_PATH=${PY3%/ bin/ python3* } /lib:${LD_LIBRARY_PATH}
21
- LD_LIBRARY_PATH=${LD_LIBRARY_PATH%: }
22
- python3 ${script_dir} /froster.py " $@ "
10
+ froster_path=${HOME} /.froster
11
+ froster_backup_path=${HOME} /.froster.bak
12
+ froster_bin_path=${froster_path} /bin
13
+
14
+ froster_old_path=${HOME} /.local/share/froster
15
+ froster_backup_old_path=${HOME} /.local/share/froster.bak
16
+
17
+ # TODO: change this path to correct one before merging
18
+ repository_path=https://raw.githubusercontent.com/HPCNow/froster/main
19
+
20
+ # ####################
21
+ # ## ERROR HANDLER ###
22
+ # ####################
23
+
24
+ # Define error handler function
25
+ set -e
26
+
27
+ trap ' catch $? $BASH_COMMAND' EXIT
28
+
29
+ catch () {
30
+ if [ " $1 " != " 0" ]; then
31
+ # error handling goes here
32
+ echo " Error: $2 : exit code $1 "
33
+ fi
34
+ }
35
+
36
+ # ################
37
+ # ## FUNCTIONS ###
38
+ # ################
39
+
40
+ # Check all needed dependencies to install froster
41
+ check_dependencies () {
42
+
43
+ # Check if curl is installed
44
+ if [[ -z $( command -v curl) ]]; then
45
+ echo " Error: curl is not installed."
46
+ echo
47
+ echo " In most linux distros you can install curl by running the following commands:"
48
+ echo " sudo apt-get update"
49
+ echo " sudo apt-get install curl"
50
+ echo
51
+ exit 1
52
+ fi
53
+
54
+ # Check if python3 is installed
55
+ if [[ -z $( command -v python3) ]]; then
56
+ echo " Error: python3 is not installed."
57
+ echo
58
+ echo " Please install python v3.8 or greater."
59
+ echo " In most linux distros you can install the latest version of python3 by running the following commands:"
60
+ echo " sudo apt-get update"
61
+ echo " sudo apt-get install python3"
62
+ echo
63
+ exit 1
64
+ fi
65
+
66
+ # Check if version of python is equal or greater than python v3.8
67
+ if [[ $( python3 -c " import sys; print(sys.version_info >= (3,8))" ) == " False" ]]; then
68
+ echo " Error: python3 installed version is lower than python v3.8"
69
+ echo
70
+ echo " Please install python v3.8 or greater."
71
+ echo " In most linux distros you can install the latest version of python3 by running the following commands:"
72
+ echo " sudo apt-get update"
73
+ echo " sudo apt-get install python3"
74
+ echo
75
+ exit 1
76
+ fi
77
+
78
+ # Check if pip3 is installed
79
+ if [[ -z $( command -v pip3) ]]; then
80
+ echo " Error: pip3 is not installed."
81
+ echo
82
+ echo " Please install pip3."
83
+ echo " In most linux distros you can install the latest version of pip3 by running the following commands:"
84
+ echo " sudo apt-get update"
85
+ echo " sudo apt-get install python3-pip"
86
+ echo
87
+ exit 1
88
+ fi
89
+ }
90
+
91
+ # Backup older installations (if any)
92
+ backup_old_installation () {
93
+
94
+ echo
95
+ echo " Backing up older froster installation..."
96
+
97
+ # Remove older installation backup (old path)
98
+ rm -rf ${froster_backup_old_path}
99
+
100
+ # Remove older installation backup (new path)
101
+ rm -rf ${froster_backup_path}
102
+
103
+ # Back up (if any) older froster installations (old path)
104
+ if [[ -d ${HOME} /.local/share/froster ]]; then
105
+ mv -f ${froster_old_path} ${froster_backup_path}
106
+ fi
107
+
108
+ # Remove old files (old path)
109
+ rm -f ${HOME} /.local/bin/froster
110
+ rm -f ${HOME} /.local/bin/froster.py
111
+ rm -f ${HOME} /.local/bin/s3-restore.py
112
+
113
+ # Back up (if any) older froster installations (new path)
114
+ if [[ -d ${froster_path} ]]; then
115
+ mv -f ${froster_path} ${froster_backup_path}
116
+ fi
117
+
118
+ echo " ...older installation backup (if any): ${froster_backup_path} "
119
+ }
120
+
121
+ # Donwload froster files from repository
122
+ download_froster () {
123
+
124
+ echo
125
+ echo " Downloading froster files..."
126
+
127
+ # Get the latest version of froster.py
128
+ curl -Ls ${repository_path} /froster.py? token=$( date +%s) \
129
+ -o ${froster_bin_path} /froster.py --create-dirs
130
+
131
+ # Get the latest version of froster.sh
132
+ curl -Ls ${repository_path} /froster? token=$( date +%s) \
133
+ -o ${froster_bin_path} /froster --create-dirs
134
+
135
+ # Get the latest version of restore.py
136
+ curl -Ls ${repository_path} /s3-restore.sh? token=$( date +%s) \
137
+ -o ${froster_bin_path} /s3-restore.sh --create-dirs
138
+
139
+ # Get the latest version of requirements.txt
140
+ curl -Ls ${repository_path} /requirements.txt? token=$( date +%s) \
141
+ -o ${froster_path} /requirements.txt --create-dirs
142
+
143
+ chmod +x ${froster_bin_path} /froster
144
+ chmod +x ${froster_bin_path} /s3-restore.sh
145
+
146
+ echo " ...files downloaded"
147
+ }
148
+
149
+ # Install froster
150
+ install_froster () {
151
+
152
+ # Create froster virtual environment
153
+ echo
154
+ echo " Creating froster's virtual environment... "
155
+ python3 -m venv ${froster_path}
156
+ echo " ...virtual environment created"
157
+
158
+ # Activate froster virtual environment
159
+ echo
160
+ echo " Activating froster's virtual environment... "
161
+ source ${froster_bin_path} /activate
162
+ echo " ...virtual environment activated"
163
+
164
+ echo
165
+ echo " Installing froster's requirements... "
166
+ python3 -m pip --disable-pip-version-check \
167
+ install --upgrade -r ${froster_path} /requirements.txt > /dev/null 2>&1
168
+ echo " ...requirements installed"
169
+
170
+ # Deactivate virtual environment
171
+ echo
172
+ echo " Deactivating froster's virtual environment... "
23
173
deactivate
24
- else
25
- if [[ -e ${ALTPYTHON} ]]; then
26
- ${ALTPYTHON} ${script_dir} /froster.py " $@ "
174
+ echo " ...virtual environment deactivated"
175
+ }
176
+
177
+ # Make sure froster binary files are in path
178
+ froster_in_path () {
179
+
180
+ echo
181
+ echo " Adding ${froster_bin_path} folder to PATH..."
182
+
183
+ if [[ " :$PATH :" == * " :${froster_bin_path} :" * ]]; then
184
+ echo " ...already in PATH."
27
185
else
28
- # Check if we have at least the minimum Python version
29
- if [[ $( /usr/bin/python3 -c " import sys; print(sys.version_info >= (3,${PMIN} ))" ) == " True" ]]; then
30
- /usr/bin/python3 ${script_dir} /froster.py " $@ "
186
+ # Create ${HOME}/.bashrc file if it does not exist
187
+ touch ${HOME} /.bashrc
188
+
189
+ # Add ${froster_bin_path} to PATH only when it's not already there
190
+ if ! grep -q froster " ${HOME} /.bashrc" ; then
191
+ echo " export PATH=\$ PATH:${froster_bin_path} " >> " ${HOME} /.bashrc"
192
+ echo " ...${froster_bin_path} added to PATH in .bashrc"
31
193
else
32
- echo " Python version must be greater than or equal to 3. ${PMIN} "
194
+ echo " ... ${froster_bin_path} already added in .bashrc "
33
195
fi
34
196
fi
35
- fi
197
+ }
198
+
199
+ # ###########
200
+ # ## CODE ###
201
+ # ###########
202
+
203
+ # Always first thing to do is to check the dependencies
204
+ check_dependencies
205
+
206
+ # Check if argument is "install" or "update".
207
+ # If so, download and install latest version of froster
208
+ # Otherwise, execute the main script (froster.py)
209
+ if [[ $1 == " install" || $1 == " update" ]]; then
210
+
211
+ echo
212
+ echo " Installing last version of froster..."
213
+
214
+ # Set rw permissions on anyone in file's group
215
+ umask 0002
216
+
217
+ # Backup old installation (if any)
218
+ backup_old_installation
219
+
220
+ # Download froster files
221
+ download_froster
222
+
223
+ # Installing froster
224
+ install_froster
225
+
226
+ # Make sure binary folder is in path
227
+ froster_in_path
228
+
229
+ echo
230
+ echo " Installation complete!"
231
+
232
+ echo
233
+ echo " Check out froster by running command:"
234
+ echo " froster --help"
235
+ echo
236
+
237
+ else
238
+
239
+ # TODO: Before execution check if python version has changed (do to updates/downgrades)
240
+ # and reinstall virtual environment
241
+
242
+ # Pass any command to main script except "install" and "update"
243
+ source ${froster_bin_path} /activate
244
+ python3 ${froster_bin_path} /froster.py " $@ "
245
+ deactivate
246
+
247
+ fi
0 commit comments