aboutsummaryrefslogtreecommitdiffstats
path: root/bin/srt
blob: ddc3b6a78eebfef1019211eb5e2b57480e4b0d3c (plain)
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
#!/bin/bash

# SRTool - shell script to start "Security Response Tool"

# Copyright (C) 2013-2015 Intel Corp.
# Copyright (C) 2018-2023 Wind River Systems

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

HELP="
Usage 1: bin/srt start|stop [webport=<address:port>]
    Optional arguments:
        [webport] Set the SRTool server port (default: localhost:8000)
        [noautoupdate] Disable the auto update server
        [start_update] Start only the update server (when main server is service/SSL)
Usage 2: bin/srt manage [createsuperuser|lsupdates|migrate|makemigrations|checksettings|collectstatic|...]
"

preset_basic_directories()
{
    # create working directories for srtool
    mkdir -p $SRT_BASE_DIR/backups
    mkdir -p $SRT_BASE_DIR/data
    mkdir -p $SRT_BASE_DIR/data/cache
    mkdir -p $SRT_BASE_DIR/logs
    mkdir -p $SRT_BASE_DIR/reports
    mkdir -p $SRT_BASE_DIR/update_logs
    touch $SRT_BASE_DIR/update_logs/master_log.txt
}

databaseCheck()
{
    retval=0
    # you can always add a superuser later via
    # ../srt/manage.py createsuperuser --username=<ME>
    $MANAGE migrate --noinput || retval=1

    if [ $retval -eq 1 ] ; then
        echo "Failed migrations, aborting system start" 1>&2
        return $retval
    fi
    $MANAGE checksettings --traceback || retval=1

    if [ $retval -eq 1 ]; then
        printf "\nError while checking settings; aborting\n"
        return $retval
    fi

    return $retval
}

get_srt_env_settings() {
    echo "## Inherited SRT environment settings" > $ENV_FILE
    echo "SRT_BASE_DIR=$SRT_BASE_DIR" >> $ENV_FILE
    echo "SRTDBG_LOG=$SRTDBG_LOG" >> $ENV_FILE
    if [ -z "$TZ" ] ; then
        export TZ=America/Chicago
        echo "TZ=America/Chicago" >> $ENV_FILE
    fi

    mainapp="yp"
    # Apply all shell settings except default app 'yp'
    # Only look in directories with proper 'datasource.json' files
    for envscript in $(find $SRT_BASE_DIR/bin -name "datasource.json") ; do
        envscript="${envscript/datasource.json/srtool_env.sh}"
        if [ -f "$envscript" -a "$envscript" = "${envscript/bin\/yp/}" ] ; then
            . $envscript
            echo "## Inherit: $envscript" >> $ENV_FILE
            cat $envscript >> $ENV_FILE
        fi
    done
    # if no main app, default to 'yp'
    if [ -z "$SRT_MAIN_APP" ] ; then
        envscript="$SRT_BASE_DIR/bin/yp/srtool_env.sh"
        . $envscript
		echo "## Inherit: $envscript" >> $ENV_FILE
		cat $envscript >> $ENV_FILE
    fi
    echo "SRT_MAIN_APP=$SRT_MAIN_APP"
}

webserverKillAll()
{
    local pidfile
    for pidfile in ${SRT_BASE_DIR}/.srtmain.pid ; do
        if [ -f ${pidfile} ] ; then
            pid=`cat ${pidfile}`
            while kill -0 $pid 2>/dev/null; do
                echo "KILL:$pid"
                kill -SIGTERM -9 $pid 2>/dev/null
                sleep 1
            done
            rm  ${pidfile}
        fi
    done

    # Stop the Update app (even if start was disabled)
    ${SRT_BASE_DIR}/bin/common/srtool_update.py --cron-stop
}

webserverStartAll()
{
    # do not start if srtmain points to a valid process
    if ! cat "${SRT_BASE_DIR}/.srtmain.pid" 2>/dev/null | xargs -I{} kill -0 {} ; then
        retval=1
        rm "${SRT_BASE_DIR}/.srtmain.pid"
    fi

    retval=0

    # check the database
    databaseCheck || return 1

    echo "Starting SRTool webserver..."
    echo "RUN: $MANAGE runserver --noreload $ADDR_PORT </dev/null >> ${SRT_BASE_DIR}/srt_web.log 2>&1 & echo \$! >${SRT_BASE_DIR}/.srtmain.pid"

    $MANAGE runserver --noreload "$ADDR_PORT" \
           </dev/null >> ${SRT_BASE_DIR}/srt_web.log 2>&1 \
           & echo $! >${SRT_BASE_DIR}/.srtmain.pid

    sleep 1

    if ! cat "${SRT_BASE_DIR}/.srtmain.pid" | xargs -I{} kill -0 {} ; then
        retval=1
        rm "${SRT_BASE_DIR}/.srtmain.pid"
        echo "SRTool webserver NOT STARTED"
    else
        echo "SRTool webserver started at http://$ADDR_PORT"
    fi

    # Start the Update app
    if [ 0 -eq $no_auto_update ] ; then
        ${SRT_BASE_DIR}/bin/common/srtool_update.py --cron-start $UPDATE_FOLLOW_PID_FILE >> ${SRT_BASE_DIR}/srt_update.log 2>&1 &
        echo "SRTool update service started at PID $!"
    fi
    return $retval
}

update_start_all()
{
    # Start the Update app
    if [ 0 -eq $no_auto_update ] ; then
		echo " First stop any running updater"
        ${SRT_BASE_DIR}/bin/common/srtool_update.py --cron-stop
		echo " Now (re)start updater"
        ${SRT_BASE_DIR}/bin/common/srtool_update.py --cron-start $UPDATE_FOLLOW_PID_FILE -v >> ${SRT_BASE_DIR}/srt_update.log 2>&1 &
        echo " SRTool update service started at PID $!"
    fi
}

INSTOPSYSTEM=0

# define the stop command
stop_system()
{
    # prevent reentry
    if [ $INSTOPSYSTEM -eq 1 ] ; then return; fi
    INSTOPSYSTEM=1
    webserverKillAll
    # unset exported variables
    unset SRT_BASE_DIR
    trap - SIGHUP
    #trap - SIGCHLD
    INSTOPSYSTEM=0
}

verify_prereq() {
    # Quick check for Python3
    if [ -z "$(which python3)" ] ; then
        echo "ERROR: missing 'python3' host package"
        return 2
    fi
    if [ -z "$(which sqlite3)" ] ; then
        echo "ERROR: missing 'sqlite3' host package"
        return 2
    fi

    # Verify Django version
    reqfile=$(python3 -c "import os; print(os.path.realpath('$SRT_BASE_DIR/bin/srtool-requirements.txt'))")
    exp='s/Django\([><=]\+\)\([^,]\+\),\([><=]\+\)\(.\+\)/'
    # expand version parts to 2 digits to support 1.10.x > 1.8
    # (note:helper functions hard to insert in-line)
    exp=$exp'import sys,django;'
    # Allow for development versions like '2.2.dev20181217100344'
    exp=$exp'version=["%02d" % int(n) for n in django.get_version().replace("dev","").split(".")];'
    exp=$exp'vmin=["%02d" % int(n) for n in "\2".split(".")];'
    exp=$exp'vmax=["%02d" % int(n) for n in "\4".split(".")];'
    exp=$exp'sys.exit(not (version \1 vmin and version \3 vmax))'
    exp=$exp'/p'
    if ! sed -n "$exp" $reqfile | python3 - ; then
        req=`grep ^Django $reqfile`
        echo "This program needs $req"
        echo "Please install with pip3 install -r $reqfile"
        return 2
    fi

    return 0
}

create_restart() {
	cat > $SRT_BASE_DIR/bin/srt_start.sh << endmsg
#!/bin/bash
# Restart the SRTool using the last start options
if [ -f $SRT_BASE_DIR/.srtmain.pid ] ; then
    $SRT_BASE_DIR/bin/srt_stop.sh
fi
$SRT_BASE_DIR/bin/srt $*
endmsg
	chmod +x $SRT_BASE_DIR/bin/srt_start.sh
	cat > $SRT_BASE_DIR/bin/srt_stop.sh << endmsg
#!/bin/bash
# Stop the SRTool based on the the last start options
if [ 1 -eq $no_auto_update ] ; then
    $SRT_BASE_DIR/bin/srt stop noautoupdate
else
    $SRT_BASE_DIR/bin/srt stop
fi
endmsg
	chmod +x $SRT_BASE_DIR/bin/srt_stop.sh
}

# read command line parameters
if [ -n "$BASH_SOURCE" ] ; then
    SRT=${BASH_SOURCE}
elif [ -n "$ZSH_NAME" ] ; then
    SRT=${(%):-%x}
else
    SRT=$0
fi

# read command line parameters
if [ -n "$BASH_SOURCE" ] ; then
    SRT=${BASH_SOURCE}
elif [ -n "$ZSH_NAME" ] ; then
    SRT=${(%):-%x}
else
    SRT=$0
fi

# set up base paths and definitions
export SRT_BASE_DIR=$(dirname $SRT)
SRT_BASE_DIR=$(readlink -f $SRT_BASE_DIR)
SRT_BASE_DIR=$(dirname $SRT_BASE_DIR)
MANAGE="python3 $SRT_BASE_DIR/lib/manage.py"
ENV_FILE=$SRT_BASE_DIR/.env_vars.env

# Pre-set the local database configuration file if not yet done
if [ ! -f "$SRT_BASE_DIR/srt_dbconfig.yml" ] ; then
    cp "$SRT_BASE_DIR/bin/srt_dbconfig.yml" "$SRT_BASE_DIR/srt_dbconfig.yml"
fi

# Fetch the datasource environent settings and copy current environment variables to txt for wsgi.py to read 
get_srt_env_settings

# insure basic directories are present
preset_basic_directories

ADDR_PORT="localhost:8000"
unset CMD
manage_cmd=""
UPDATE_FOLLOW_PID_FILE=""
if [ "1" = "$SRT_SKIP_AUTOUPDATE" ] ; then
    no_auto_update=1
else
    no_auto_update=0
fi
for param in $*; do
    case $param in
    start )
            CMD=$param
    ;;
    stop )
            CMD=$param
    ;;
    manage )
            CMD=$param
    ;;
    export_env )
            CMD=$param
    ;;
    start_update )
            CMD=$param
    ;;
    update_follow_pid=*)
			UPDATE_FOLLOW_PID_FILE="--follow-pid-file=${param#*=}"
    ;;
    webport=*)
            ADDR_PORT="${param#*=}"
            # Split the addr:port string
            ADDR=`echo $ADDR_PORT | cut -f 1 -d ':'`
            PORT=`echo $ADDR_PORT | cut -f 2 -d ':'`
            # If only a port has been specified then set address to localhost.
            if [ $ADDR = $PORT ] ; then
                ADDR_PORT="localhost:$PORT"
            fi
    ;;
    noautoupdate )
            no_auto_update=1
    ;;
    --help)
            echo "$HELP"
            exit 0
    ;;
    *)
            if [ "manage" == "$CMD" ] ; then
                cd $SRT_BASE_DIR/lib
                manage_cmd="$manage_cmd $param"
            else
                echo "$HELP"
                exit 1
            fi
    ;;

    esac
done

verify_prereq || exit 1

# this defines the dir SRTool will use for
# 1) the sqlite db if that is being used.
# 2) pid's we need to clean up on exit/shutdown

# Determine the action. If specified by arguments, fine, if not, toggle it
if [ "$CMD" = "start" ] ; then
    if [ -n "$BBSERVER" ]; then
        echo " SRT is already running. Exiting..."
        exit 1
    fi
elif [ "$CMD" = "" ]; then
    echo "No command specified"
    echo "$HELP"
    exit 1
fi

echo "The system will $CMD."

# Execute the commands
case $CMD in
    start )
        # check if addr:port is not in use
        if [ "$CMD" == 'start' ] ; then
            $MANAGE checksocket "$ADDR_PORT" || exit 1
        fi

        if ! webserverStartAll; then
            echo "Failed ${CMD}."
            exit 4
        fi
        # set fail safe stop system on terminal exit
        trap stop_system SIGHUP
        echo "Successful ${CMD}."
        create_restart $*
        exit 0
    ;;
    stop )
        stop_system
        echo "Successful ${CMD}."
    ;;
    manage )
        $MANAGE $manage_cmd
    ;;

    export_env )
        echo "#export variables"
        cat $ENV_FILE
        exit 0
    ;;

    start_update )
        echo "start update service"
        update_start_all
        echo "update service started"
    ;;    

esac