-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrun_inv
executable file
·73 lines (62 loc) · 2.22 KB
/
run_inv
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
#!/bin/bash
# bash (only) shell script to run the Django-based inventory system
# General assumptions:
# - The virtual environment is located in a subdirectory called "venv".
# - This script is located at the root level of the inventory system directory.
#- manage.py is also located at the root level of the inventory system.
# Script Assumptions:
ROOT=$(pwd)
MANAGE_LOCATION=$ROOT
MANAGE_CMD=$MANAGE_LOCATION/manage.py
VIRTUAL_BIN_LOCATION=$ROOT/venv/bin
PYTHON=$VIRTUAL_BIN_LOCATION/python
HOST_IP=localhost
SERVER_PORT=8765
# Python environment variables
export PYTHONUNBUFFERED=1
export DJANGO_SETTINGS_MODULE=FPIDjango.settings
export PYTHONPATH=$PYTHONPATH:$ROOT
# Activate the virtual environment if needed
if [ X = X"$(echo deactivate)" ]; then
# shellcheck disable=SC1090
source $VIRTUAL_BIN_LOCATION/activate
fi
# defins a task to run the Django server
# task is needed so we can tell which process to kill when done
run_server() {
cd $ROOT/log
# echo "My PID is $$ "
$PYTHON $MANAGE_CMD runserver $HOST_IP:$SERVER_PORT > root.log 2>&1
}
# start Django server as a background task
# Note that this task will also be killed if the terminal session is closed
run_server &
# capture pid of the background tasks
ROOT_PID=$!
SERVER_PID=$(ps -f | grep runserver | awk "\$3 = $ROOT_PID { print \$2 }") >/dev/null 2>&1
# echo "root: $ROOT_PID, task: $SERVER_PID"
# wait a moment for the server to start up
sleep 2s
# build client URL
CLIENT_URL=http://$HOST_IP:$SERVER_PORT
# start a browser
if [ "$(uname)" = 'Darwin' ]; then
# assume this is a Macintosh
open $CLIENT_URL
elif [ "$X" = "Y" ]; then
# identify Windows Subsystem for Linux (WSL) and run command to start URL
cmd.exe /c start $URL
elif [ "$SHELL" = '/bin/bash' ]; then
# Assume some form of Linux with xdg-open command
xdg-open $CLIENT_URL
else
echo -n "\n\tUnable to open the URL $CLIENT_URL for you. "
echo "Please open manually.\n"
echo -n "\tIf/when you get tired of opening this URL manually, Please "
echo -n "\tfix this script for your OS and submit a pull request. :)"
fi
# tell user how to kill the server
echo "\nInstead of typing a CONTROL-C to kill the server, type"
echo "\tkill $SERVER_PID"
echo "instead when you are done with the server."
# EOF