summaryrefslogtreecommitdiff
path: root/scripts/deploy/codeq_refresh_and_deploy.sh
blob: f0a0aeb4122a0e3d982ffeebcc23993d07e53ebc (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
#!/bin/dash -e

. /etc/default/codeq
export CODEQ_PROBLEMS CODEQ_WEB_OUTPUT CODEQ_DB_HOST CODEQ_DB_DATABASE CODEQ_DB_USER CODEQ_DB_PASS

# flags to restart daemons at the end of the script
# 0 means not to restart, anything else means to restart
RESTART_PROLOG=0
RESTART_SERVER=0
RESTART_WEB=0
BUILD_WEB_RESOURCES=0

init=$(cat /proc/1/comm)

cd $CODEQ_SERVER
git fetch -q origin $CODEQ_GIT_BRANCH
git checkout -q $CODEQ_GIT_BRANCH
FILES=$(mktemp)
if git diff --name-status origin/$CODEQ_GIT_BRANCH | cut -c3- | grep -v \\.gitignore >$FILES; then
    # some modifications happened
    git merge -q

    if grep -q ^prolog/runner/ $FILES; then
        RESTART_PROLOG=1
    fi

    if grep -q -v '^\(web\|prolog/runner\|scripts\)/' $FILES; then
        RESTART_SERVER=1
    fi

    if grep -q ^web/ $FILES; then
        RESTART_WEB=1
    fi

    if grep -q ^scripts/build_web_resources.py $FILES; then
        BUILD_WEB_RESOURCES=1
    fi

    # rebuild sandbox binaries if out of date
    for prog in python/runner/sandbox python/runner/terminator; do
        if ! make -q "${prog}"; then
            make -s "${prog}" && setcap cap_setuid,cap_setgid+ep "${prog}"
        fi
    done

    if grep -q ^web/ $FILES | grep -v ^web/main.js; then
        # node dependencies may have changed, run installation
        cd web && /usr/bin/npm install
    fi
fi

cd $CODEQ_PROBLEMS
git fetch -q origin $CODEQ_GIT_BRANCH
git checkout -q $CODEQ_GIT_BRANCH
if git diff --name-status origin/$CODEQ_GIT_BRANCH | cut -c3- | grep -v \\.gitignore >$FILES; then
    git merge -q
    BUILD_WEB_RESOURCES=1
    # restart the server if any other files than language files were modified
    if grep -q -v '\(^\|/\)..\.py' $FILES; then
        RESTART_SERVER=1
    fi
fi

cd $CODEQ_WEB
git fetch -q origin $CODEQ_GIT_BRANCH
git checkout -q $CODEQ_GIT_BRANCH
if git diff --name-status origin/$CODEQ_GIT_BRANCH | cut -c3- | grep -v \\.gitignore >$FILES; then
    git merge -q
    echo Redeploying web app
    /usr/bin/rsync -aq --delete -f '- .git' -f '- .gitignore' -f '- config.xml' -f 'P data' /var/local/codeq-web/ /var/www/html
fi

rm -f $FILES

# as a precaution switch to the tmp folder
cd /tmp
if [ $BUILD_WEB_RESOURCES -ne 0 ]; then
    echo Rebuilding web resources
    /usr/bin/python3 $CODEQ_SERVER/scripts/build_web_resources.py
fi
if [ $RESTART_PROLOG -ne 0 ]; then
    echo Restarting codeq-prolog
    if [ "$init" = systemd ]; then
        systemctl restart codeq-prolog
    else
        /etc/init.d/codeq-prolog restart
    fi
fi
if [ $RESTART_SERVER -ne 0 ]; then
    echo Restarting codeq-server
    if [ "$init" = systemd ]; then
        systemctl restart codeq-server
    else
        /etc/init.d/codeq-server restart
    fi
fi
if [ $RESTART_WEB -ne 0 ]; then
    echo Restarting codeq-web
    if [ "$init" = systemd ]; then
        systemctl restart codeq-web
    else
        /etc/init.d/codeq-web restart
    fi
fi

exit 0