forked from arras-energy/gridlabd-old
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·361 lines (336 loc) · 8.47 KB
/
install.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
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
#!/bin/bash
# set the path to use during installation
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
# check sudo
if [ "$(whoami)" == "root" ]; then
function sudo ()
{
$*
}
else
sudo --version >/dev/null 2>&1 || (echo "$0: sudo is required"; exit 1)
fi
# local folder
VAR="/usr/local/var/gridlabd"
if [ ! -d "$VAR" ]; then
mkdir -p $VAR || ( sudo mkdir -p $VAR && sudo chown ${USER:-root} $VAR )
fi
# setup logging
LOG="$VAR/install.log"
function log()
{
case "$*" in
("clear")
rm -f $LOG
shift 1
;;
(*)
echo "$*" >> $LOG
return
;;
esac
}
# setup exit handling
function on_exit()
{
log "STOP: $(date)"
log "STATUS: ${STATUS:-ok}"
sleep 1
if [ "$VERSION" == "yes" ]; then
kill %1
fi
}
trap on_exit EXIT
# setup error handling
function error()
{
[ "${SILENT:-no}" == "no" -a "${VERBOSE:-no}" == "no" ] && echo "ERROR: $*" > /dev/stderr
echo "ERROR: $*" >> $LOG
STATUS="error"
exit 1
}
# check for commands that absolutely necessary to proceed
function require()
{
$1 ${2:---version} > /dev/null 2>&1 || error "$1 is required"
}
# check workdir
require dirname -
cd $(dirname $0)
if [ ! -f "install.sh" -o ! -d "build-aux" ]; then
error "$0 must be run from the source root"
fi
# load the configuration
if [ ! -f "install.conf-default" ]; then
error "missing install.conf-default"
fi
source "install.conf-default"
if [ -f "install.conf" ]; then
source "install.conf"
fi
# define commands that used by command line options
function info()
{
echo "CHECK=$CHECK"
echo "DOCS=$DOCS"
echo "FORCE=$FORCE"
echo "INDEX=$INDEX"
echo "LINK=$LINK"
echo "PARALLEL=$PARALLEL"
echo "PREFIX=$PREFIX"
echo "QUICK=$QUICK"
echo "SETUP=$SETUP"
echo "SILENT=$SILENT"
echo "TEST=$TEST"
echo "UPDATE=$UPDATE"
echo "VERBOSE=$VERBOSE"
}
function help()
{
cat <<-END
Syntax: $0 [<install-options>] [<configure-options>]
Install options:
-h --help Print this helpful output
--info Print information about this install
-c --no-check Do not check system for requirements
-d --no-docs Do not install documentation
-f --force Force install into existing target folder
-i --no-index Do not index data archives
-l --no-link Do not link new install to activate it
-t --no-test Do not run validation tests
-u --no-update Do not update system to meet requirements
-p --parallel Enable parallelism when possible
--prefix <path> Set install prefix
--save Save the current configuration as default
--no-setup Perform system setup
--reset Reset the configuration to default
-q --quick Run only updates instead of a clean install
-s --silent Run without showing commands
-v --verbose Run showing log output
--validate Run validation tests
--version <name> Override the default version name
END
}
# process command line options
while [ $# -gt 0 ]; do
case "$1" in
(-q|--quick)
QUICK="yes"
;;
(--validate)
TEST="yes"
;;
(-i|--no-index)
INDEX="no"
;;
(-c|--no-check)
CHECK="no"
;;
(-t|--no-test)
TEST="no"
;;
(-l|--no-link)
LINK="no"
;;
(-d|--no-docs)
DOCS="no"
;;
(-u|--no-update)
UPDATE="no"
;;
(-s|--silent)
SILENT="yes"
;;
(-v|--verbose)
VERBOSE="yes"
;;
(-p|--parallel)
PARALLEL="yes"
;;
(--prefix)
PREFIX="$2"
if [ ! -d "$PREFIX" ]; then
error "$PREFIX does not exist"
fi
INSTALL="$PREFIX/$VERSION"
shift 1
;;
(--no-setup)
SETUP="no"
;;
(-f|--force)
FORCE="yes"
;;
(--save)
info > "install.conf"
exit 0
;;
(--reset)
rm -f "install.conf"
exit 0
;;
(-h|--help)
help
exit 0
;;
(--info)
info
exit 0
;;
(--version)
VERSION="$2"
shift 1
;;
(--install)
INSTALL="$2"
shift 1
;;
(*)
error "'$1' is not a valid option"
exit 1
;;
esac
shift 1
done
# start logging
log clear
log "START: $(date)"
log "COMMAND: $0 $*"
log "CONTEXT: ${USER:-unknown}@${HOSTNAME:-localhost}:$PWD"
log "SYSTEM: $(uname -a)"
log "$(info | sed -e '1,$s/=/: /')"
# define functions used during install processing
function run ()
{
if [ "$SILENT" == "no" -a "$VERBOSE" == "no" ]; then
echo $*
fi
NAME=${USER:-nobody}
CMD=($(ps -ocommand $$ | tail -n 1))
CMD=$(basename ${CMD[0]})
if [ $(whoami) == "root" ]; then
NAME="root"
fi
echo "RUN: [$CMD://$NAME@$HOSTNAME$PWD] $*" >> $LOG
T0=$(date '+%s')
$* >> $LOG 2>&1 || error "$0 failed -- see $LOG for details "
T1=$(date '+%s')
log "OK: done in $((T1-T0)) seconds"
}
# enable verbose logging
if [ "$VERBOSE" == "yes" ]; then
tail -f -n 10000 $LOG 2>/dev/null &
fi
# run setup
if [ "$SETUP" == "yes" ]; then
if [ ! -f "build-aux/setup.sh" ]; then
error "build-aux/setup.sh not found"
fi
SOK="$VAR/setup.ok"
if [ ! -f "$SOK" -o "$FORCE" == "yes" ]; then
run build-aux/setup.sh
date > "$SOK"
elif [ -f "$SOK" ]; then
log "SETUP: already completed at $(cat $SOK)"
else
log "SETUP: skipping"
fi
fi
# dynamic variables
require git
VERSION=${VERSION:-`build-aux/version.sh --name`}
INSTALL=${INSTALL:-$PREFIX/opt/gridlabd/$VERSION}
# run checks
if [ "$LINK" == "yes" -a -f "$PREFIX/bin/gridlabd" -a ! -L "$PREFIX/bin/gridlabd" ]; then
OLDVER="$(gridlabd --version | cut -f2 -d' ')-saved_$(date '+%y%m%d')"
VDIR=$OLDVER
while [ -e $PREFIX/opt/gridlabd/$VDIR ]; do
VDIR=${OLDVER}_${VNUM:-0}
VNUM=$((${VNUM:-0}+1))
done
log "BACKUP: saving $OLDVER to $PREFIX/opt/gridlabd/$VDIR and linking it back to current version"
run mkdir -p $PREFIX/opt/gridlabd/$VDIR || error "unable to create $VDIR to save current version"
run ln -sf $PREFIX/opt/gridlabd/$VDIR $PREFIX/opt/gridlabd/current
for item in bin include lib share; do
[ ! -d $PREFIX/opt/gridlabd/$VDIR/$item ] && run mkdir -p $PREFIX/opt/gridlabd/$VDIR/$item
[ ! -d $PREFIX/opt/gridlabd/$VDIR/$item ] && run mv $PREFIX/$item/gridlabd* $PREFIX/opt/gridlabd/$VDIR/$item/
run ln -sf $PREFIX/opt/gridlabd/$VDIR/$item/gridlabd $PREFIX/opt/gridlabd/current/$item/gridlabd
run ln -sf $PREFIX/opt/gridlabd/current/$item /$PREFIX/$item/gridlabd
done
run ln -s $PREFIX/opt/gridlabd/current/bin/gridlabd.bin $PREFIX/bin/gridlabd.bin
error "stopping here for debugging reasons -- this error message should be deleted"
fi
if [ "$CHECK" == "yes" ]; then
if [ "$LINK" == "yes" -a -d "$PREFIX/gridlabd" -a ! -L "$PREFIX/gridlabd" ]; then
error "$PREFIX/gridlabd exists but it is not a symbolic link"
fi
if [ "$DOCS" == "yes" ]; then
require doxygen
require mono
require natural_docs
fi
fi
# run autoconf
if [ "$QUICK" == "no" ]; then
if [ ! -f "build-aux/ltmain.sh" ]; then
require libtoolize
run libtoolize
fi
require autoscan
run autoscan
fi
if [ ! -d "autom4te.cache" -o "$QUICK" == "no" ]; then
require autoreconf
run autoreconf -isf
fi
# prep install dir
log "VERSION: $VERSION"
log "INSTALL: $INSTALL"
if [ -e "$INSTALL" -a "$FORCE" == "no" ]; then
error "$INSTALL already exists, please delete it first"
fi
mkdir -p "$INSTALL" || ( run sudo mkdir -p "$INSTALL" && run sudo chown -R "${USER:-root}" "$INSTALL" )
if [ ! -f "configure" -o "$QUICK" == "no" ]; then
run ./configure --prefix="$INSTALL" $*
fi
NPROC=1
SYSTEM=$(uname -s)
if [ "$PARALLEL" == "yes" ]; then
if [ "$SYSTEM" == "Linux" ]; then
NPROC=$(lscpu | grep '^CPU(s):' | cut -f2- -d' ')
elif [ "$SYSTEM" == "Darwin" ]; then
NPROC=$(sysctl -n machdep.cpu.thread_count)
fi
fi
# build everything
export PATH=/usr/local/bin:/usr/bin:/bin
run make -j$((3*$NPROC)) system
if [ "$DOCS" == "yes" ]; then
run make html
run cp -r "documents/html" "$INSTALL"
run cp "documents/index.html" "$INSTALL/html"
fi
if [ "$INDEX" == "yes" ]; then
run make index
fi
if [ "$TEST" == "yes" ]; then
export PATH=$INSTALL/bin:$PATH
export GLPATH=$INSTALL/share/gridlabd/weather/US
export LD_LIBRARY_PATH=.:${LD_LIBRARY_PATH:-.}
run $INSTALL/bin/gridlabd -T $NPROC --validate
fi
# activate this version
if [ -x "$INSTALL/bin/gridlabd-version" -a "$TEST" == "yes" ]; then
log "ACTIVATE: automatic"
run $INSTALL/bin/gridlabd version set "$VERSION"
elif [ "$LINK" == "yes" ]; then
log "ACTIVATE: manual"
[ ! -L "$PREFIX/opt/gridlabd/current" ] && run sudo rm -f "$PREFIX/opt/gridlabd/current"
run sudo ln -sf "$INSTALL" "$PREFIX/opt/gridlabd/current"
for dir in bin lib include share; do
run sudo ln -sf $PREFIX/opt/gridlabd/current/$dir/gridlabd $PREFIX/$dir/gridlabd
done
run sudo ln -sf $PREFIX/opt/gridlabd/current/bin/gridlabd.bin $PREFIX/bin/gridlabd.bin
fi
# all done :-)
exit 0