File: ecflow_ui.in

package info (click to toggle)
ecflow 5.13.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 51,304 kB
  • sloc: cpp: 259,208; python: 22,228; sh: 3,216; perl: 770; xml: 333; f90: 199; ansic: 141; makefile: 71
file content (447 lines) | stat: -rwxr-xr-x 15,248 bytes parent folder | download | duplicates (2)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/bin/bash

#============================================================================
# Copyright 2009- ECMWF.
# This software is licensed under the terms of the Apache Licence version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
#============================================================================

# Environment variables from the configuration step
ECFLOWUI_BT=@UI_BACKTRACE@
ECFLOWUI_BT_EMAIL_ADDRESS_FILE=@UI_BACKTRACE_EMAIL_ADDRESS_FILE@
ECFLOWUI_VERSION=@ECFLOW_VERSION_STR@
ECFLOWUI_INSTALLDIR=@CMAKE_INSTALL_PREFIX@
ECFLOWUI_USAGE_LOG=@UI_LOG_FILE@
ECFLOWUI_SITE_TAG=@UI_LOG_SITE_TAG@
PREDEF_SYSTEM_SERVERS_LIST=@UI_SYSTEM_SERVERS_LIST@

#-----------------------------------------------------------
# The system server list
#-----------------------------------------------------------

export ECFLOW_SYSTEM_SERVERS_LIST=${ECFLOW_SYSTEM_SERVERS_LIST:=$PREDEF_SYSTEM_SERVERS_LIST}

#-----------------------------------------------------------
# Figure out the directory of the exe
#-----------------------------------------------------------

EXE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
exe=${EXE_DIR}/ecflow_ui.x


function failed() {
    local msg="Error: $1"
    echo $msg >&2
    exit 1
}

# ----------------------------------------------------------
# Clean up the tmp directory at the end or on error
#-----------------------------------------------------------
cleanup(){
    status=$?
    trap "" EXIT ERR
    set +e  # do not exit on errors during cleanup

    # kill all the child processes
    pkill -P $$ || true

    # restore stdout/stderr
    if [ x$ECFLOWUI_SLOG != xyes ]
    then
        exec 1>&5
        exec 2>&6
    fi

    if [ "$status" -eq "0" ]
    then
        echo "$(basename $0): $1 (OK) (line $2), exit status $status, starting 'cleanup'"
    else
        status_message="$(basename $0): $1 on ERROR (line $2), exit status $status, starting 'cleanup'"
        echo $status_message

        if [ -e $ECFLOWUI_LOGFILE ] ; then

            # if the error message is something that's not really a crash, then we should
            # not send a crash report
            hhh=$(grep "The X11 connection broke" $ECFLOWUI_LOGFILE)  # 0 if x11 broke, 1 if not
            x11notbroke=$?

            if [ "$x11notbroke" = "1" ] ; then

                TAILNUM=50
                UI_TAILNUM=50

                if [ $ECFLOWUI_BT != "no" ] ; then
                    # find the backtrace tag if there
                    btline=$(grep -n "Backtrace:" $ECFLOWUI_LOGFILE | cut -f1 --delimiter=':')
                    gret=$?
                    if [ "$gret" = "0" -a "x$btline" != "x" ] ; then
                        nlines=$(wc -l $ECFLOWUI_LOGFILE | cut -f1 --delimiter=' ')
                        TAILNUM=$(expr $nlines - $btline + 100)
                    fi
                fi


                # Logic: ECFLOWUI_BT_EMAIL_ADDRESS_FILE is not set, then the log/backtrace
                # will go to stdout; otherwise it will be e-mailed to the
                # addresses instead (the user will not see the backtrace).

                if [ $ECFLOWUI_BT_EMAIL_ADDRESS_FILE != "x" ] ; then
                    if [ -e $ECFLOWUI_BT_EMAIL_ADDRESS_FILE ] ; then
                        EFLOWUI_LOGTAIL="${ECFLOWUI_LOGFILE}.tail"
                        ecfuimsg="ecFlowUI backtrace from user $USER"
                        echo "Sending crash report by e-mail..."
                        echo "user/host          : $USER/$HOST"          >  $EFLOWUI_LOGTAIL
                        echo "ecflow version     : $ECFLOWUI_VERSION"    >> $EFLOWUI_LOGTAIL
                        echo "ecflow install dir : $ECFLOWUI_INSTALLDIR" >> $EFLOWUI_LOGTAIL
                        echo "start time         : $ECFLOWUI_STARTTIME"  >> $EFLOWUI_LOGTAIL
                        echo "stop time          : $(date)"              >> $EFLOWUI_LOGTAIL
                        echo ""                                          >> $EFLOWUI_LOGTAIL
                        echo $status_message                             >> $EFLOWUI_LOGTAIL
                        tail -$TAILNUM $ECFLOWUI_LOGFILE                 >> $EFLOWUI_LOGTAIL

                        #Add the last lines from the user interaction log file
                        if [ -e $ECFLOWUI_LOGFILE ] ; then
                            echo ""                                      >> $EFLOWUI_LOGTAIL
                            echo "Last $UI_TAILNUM lines of user interaction log file:" >> $EFLOWUI_LOGTAIL
                            tail -${UI_TAILNUM} $ECFLOWUI_LOGFILE     >> $EFLOWUI_LOGTAIL
                        fi

                        addr=$(head -1 $ECFLOWUI_BT_EMAIL_ADDRESS_FILE)
                        mail -s "$ecfuimsg"  $addr < $EFLOWUI_LOGTAIL
                        rm -f $EFLOWUI_LOGTAIL
                        echo "Report sent"
                    else
                        echo "No report sent because e-mail address file does not exist: $ECFLOWUI_BT_EMAIL_ADDRESS_FILE"
                    fi
                else
                    echo "Last $TAILNUM lines of log file:"
                    tail -$TAILNUM $ECFLOWUI_LOGFILE

                    #Add the last lines from the user interaction log file
                    if [ -e $ECFLOWUI_LOGFILE ] ; then
                        echo ""
                        echo "Last $UI_TAILNUM lines of user interaction log file:"
                        tail -${UI_TAILNUM} $ECFLOWUI_LOGFILE
                    fi
                fi
            else
                echo "X11 Connection broke - no report sent to developers"
            fi # $x11notbroke
        fi
    fi

    # remove temporary directory
    if [ $(echo $ECFLOWUI_TMPDIR | grep -c "/ecflow_ui.") -ge 1 ] ; then
        rm -Rf $ECFLOWUI_TMPDIR
    fi

    exit $status
}


#-----------------------------------------------------------
# checkoption()
# Checks the command-line options
#-----------------------------------------------------------

checkoption() {

    local USE_PC4=0
    local USE_CMD=0
    local CMD_MSG=""

    while [ "$1" != "" ]
    do
        case "$1" in

        -log)
            ECFLOWUI_SLOG=yes
            ;;

        -slog)
            ECFLOWUI_SLOG=yes
            ;;

        -s)
            export ECFLOWUI_SESSION_MANAGER=yes
            ;;

        -ts)
            export ECFLOWUI_TEMP_SESSION_HOST=$2
            export ECFLOWUI_TEMP_SESSION_PORT=$3
            if [ "x$ECFLOWUI_TEMP_SESSION_HOST" == x -o "x$ECFLOWUI_TEMP_SESSION_PORT" == x ] ; then
                failed "Please supply a host and port: ecflow_ui -ts <host> <port>"
            fi
            echo "Starting a temporary session: host=$ECFLOWUI_TEMP_SESSION_HOST, port=$ECFLOWUI_TEMP_SESSION_PORT"
            shift 2
            ;;

        -title)
            export ECFLOWUI_TITLE=$2
            shift
            ;;

        -fs)
            export ECFLOWUI_FONT_SIZE=$2
            shift
            ;;

        -pc4)
            ECFLOWUI_USER_START_CMD="proxychains4"
            CMD_MSG="Starting ecFlowUI with the following command: ${ECFLOWUI_USER_START_CMD}"
            USE_PC4=1
            ;;

        -cmd)
            ECFLOWUI_USER_START_CMD="$2"
            if [ "x${ECFLOWUI_USER_START_CMD}" == "x" ] ; then
                failed "No command is specified for option -cmd!"
            fi
            CMD_MSG="Starting ecFlowUI with user specified command: ${ECFLOWUI_USER_START_CMD}"
            USE_CMD=1
            shift
            ;;

        #Specify the location of the config dir
        -confd)
            export ECFLOWUI_CONFIG_DIR=$2
            if [[ $ECFLOWUI_CONFIG_DIR  != /* ]] ; then
                failed "Please specify an absolute path for flag -confd!"
            fi
            echo "Starting ecFlowUI with user specified config dir: ${ECFLOWUI_CONFIG_DIR}"
            shift
            ;;

        -h)
            ECFLOWUI_HELP=yes
            ;;

        esac
        shift
    done

    if [[ $USE_PC4 -eq 1 && $USE_CMD -eq 1 ]] ; then
        failed "The -pc4 and -cmd flags cannot be used together"
    elif [[  $USE_PC4 -eq 1 || $USE_CMD -eq 1 ]] ; then
        echo $CMD_MSG
    fi
}

#-----------------------------------------------------------
# Help message
#-----------------------------------------------------------

printhelp() { 

    version=$("$exe" --version 2>/dev/null)

    cat <<HEND
ecFlowUI $version
  Command-line flags:
    -fs               : set fontsize (in points) for the user interface
    -log              : write log messages to standard output
    -s                : start via the session manager
    -ts <host> <port> : start a temporary session viewing the given server
    -pc4              : start the executable via the proxychains4 command
    -cmd <cmd>        : start the executable via the specified command
    -confd <dir>      : start with the specified configuration directory
    -h                : print this help message
HEND

cat << HEND

  Environment variables that have an effect on ecFlowUI:
  [current value in brackets]

  ECFLOW_SYSTEM_SERVERS_LIST   Pre-defined system server list files. User settings in
                               ecFlowUI (Preferences > Server > System server list) will overwrite it.
                               [$ECFLOW_SYSTEM_SERVERS_LIST]
HEND

}

checkoption "$@"

if [ "x$ECFLOWUI_HELP" != x ]
then
    printhelp
    exit 0
fi

#-----------------------------------------------------------
# usage log
#-----------------------------------------------------------

set +e  # failing to write to the log file should not cause a fatal error
if [ $ECFLOWUI_USAGE_LOG != x ]
then
	# checks on file existence and size
	if [ -f $ECFLOWUI_USAGE_LOG ]  # log file exists?
	then
		if [ ! -s $ECFLOWUI_USAGE_LOG ]  # file is empty? - write the header line
		then
			echo "date,time,version,user,host,site" >> $ECFLOWUI_USAGE_LOG
		fi

		echo "`date -u '+%Y.%m.%d,%R'`,$ECFLOWUI_VERSION,$USER,`hostname`,$ECFLOWUI_SITE_TAG" >> $ECFLOWUI_USAGE_LOG
	else
		echo "Log file $ECFLOWUI_USAGE_LOG does not exist."
	fi
fi
set -e


#Define tmp root dir
TMPDIR_ROOT=${TMPDIR:=/tmp}

# check that TMPDIR_ROOT actually exists - we can't run without it
if [ ! -d $TMPDIR_ROOT ]
then
    echo ""
    echo "  Temporary directory '$TMPDIR_ROOT' does not exist"
    echo "  ecFlowUI needs this in order to run. Check that environment"
    echo "  variable \$TMPDIR is set to a valid directory and try again."
    exit 1
fi

#Define and create tmp dir
export ECFLOWUI_TMPDIR=${TMPDIR_ROOT}/ecflow_ui.$$.$LOGNAME
mkdir -p $ECFLOWUI_TMPDIR

#Check if we could create it
if [ $? -ne 0 ]
then
    echo ""
    echo "  Temporary directory '$ECFLOWUI_TMPDIR' could not be created."
    echo "  ecFlowUI needs this in order to run. Check that environment"
    echo "  variable \$TMPDIR is set to a valid directory and try again."
    exit 1
fi


# Where to write the log file to
export git=$ECFLOWUI_TMPDIR/ecflowui_log_$$.$LOGNAME.txt

# Where to write the user interaction log file to
export ECFLOWUI_LOGFILE=$ECFLOWUI_TMPDIR/ecflowui_uilog_$$.$LOGNAME.txt

#---------------------------------------------------------------
# Create socket dir for local domain socket based communication
#---------------------------------------------------------------

export ECFLOWUI_SOCKETDIR=$ECFLOWUI_TMPDIR/sockets
mkdir -p $ECFLOWUI_SOCKETDIR

#---------------------------------------------------------------
# Manage config dir.
# From version 5.3.0 onwards the default config
# dir is: ${HOME}/.ecflow_ui_v5.
# Previously it was: ${HOME}/.ecflow_ui
#---------------------------------------------------------------

confDirDefault="${HOME}/.ecflow_ui_v5"
confDir=${confDirDefault}
confDir_v4="${HOME}/.ecflow_ui"

# if default the config dir does not exist we try to copy it
# from an existing one
if [[ ! -d "${confDir}" ]] ; then
    export ECFLOWUI_CONFIG_DIR_COPIED=1
    # the user did not start ecFlowUI with the -confd option and the
    # old confdir exists
    if  [[ x"${ECFLOWUI_CONFIG_DIR}" == x && -d "${confDir_v4}" ]] ; then
        cp -Rf "${confDir_v4}" "${confDir}"
        #  the session description is not copied over
        session_json="${confDir}/default.session/session.json"
        if [[ -f "${session_json}" ]] ; then
            rm -f "${session_json}"
        fi
    fi
fi

#---------------------------------------------------------------
# Remove lock files created more than 3 s ago (see ECFLOW-980)
#---------------------------------------------------------------

confDir=${ECFLOWUI_CONFIG_DIR:=${confDirDefault}}
#We are extra careful an delete the files in a loop instead of using exec for find
if [[ -d $confDir ]] ; then
    for fn in $(find $confDir -type f -mmin +1 -name "*.lock") ; do
        if [[ $fn == *.lock ]] ; then
            echo "Remove lock file: ${fn}"
            rm -f $fn
        fi
    done
fi
#-----------------------------------------------------------
# Close tty and redirect stdout to a file?
#-----------------------------------------------------------

if [ x$ECFLOWUI_SLOG != xyes ]
then
    exec  < /dev/null
    exec 5>&1                 # store for later
    exec 6>&2                 # store for later 
    exec 1> $ECFLOWUI_LOGFILE # (restored in cleanup())
    exec 2>&1                 # redirect stderr to stdout, (restored in cleanup())
    # note that it seems to be better to do
    #   exec 2>&1
    # rather than 
    #   exec 2> $ECFLOWUI_LOGFILE
    # because this way we get the stderr at the end rather than the start
    # due to the difference in flushing behaviours
fi



# =============================================================
# tmp directory cleaned automatically at end or at error.
# on new platforms check that these signals are valid:

ECFLOWUI_SIGLIST="QUIT TERM XCPU  XFSZ"
#              3    15   30/24 31/25

trap 'cleanup "EXIT" $LINENO' EXIT
trap 'cleanup "SIGNAL trap" $LINENO' $ECFLOWUI_SIGLIST

# note when we started it up
ECFLOWUI_STARTTIME=$(date)

#-----------------------------------------------------------
# The log viewer exe
#-----------------------------------------------------------

ECFLOWUI_LOG_VIEWER=${EXE_DIR}/ecflow_ui_log.x

#-----------------------------------------------------------
# Set up environment on mac
#-----------------------------------------------------------

if [[  $(uname | tr '[:upper:]' '[:lower:]') == "darwin" ]]; then
    export QT_MAC_WANTS_LAYER=1
fi

#-----------------------------------------------------------
# Run the executable
#-----------------------------------------------------------

if [[ ${ECFLOWUI_USER_START_CMD} = "proxychains" || ${ECFLOWUI_USER_START_CMD} = "proxychains"[4-9] ]] ; then
    export ECFLOWUI_USE_PROXYCHAINS=1
fi

#-----------------------------------------------------------
# Backtrace report?
#-----------------------------------------------------------

if [ $ECFLOWUI_BT != "no" ]
then
    catchsegv ${ECFLOWUI_USER_START_CMD} "$exe"
else
    ${ECFLOWUI_USER_START_CMD} "$exe"
fi