~loggerhead-team/loggerhead/trunk-rich

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
#!/bin/sh
### BEGIN INIT INFO
# Required-Start:       $local_fs $remote_fs $network
# Default-Start:        3 5
# Default-Stop:         0 1 2 6
# Short-Description:    Loggerhead
# Description:          Manage Loggerhead (a web viewer for projects in bazaar)
### END INIT INFO


#
# Configure this please:
# (Please stop loggerhead before changing the configuration, otherwise this
#   script might not be able to kill loggerhead)
#

LHUSER=loggerhead

if [ `whoami` = "$LHUSER" ]; then
    SUDO=""
else
    SUDO="sudo -H -u $LHUSER"
fi

# If serve-branches is not in your path, you will need to specify the full path:
SERVE_BRANCHES_CMD=serve-branches

LOG_FOLDER=/var/log/loggerhead
LOG_FILE=$LOG_FOLDER/loggerheadd.log
URL_PREFIX=/loggerhead
PORT=8080

#please specify the base directory to serve:
BZRROOT=/bzrroot

# You can add additional options to serve-branches here:
START_CMD="$SERVE_BRANCHES_CMD --prefix=$URL_PREFIX --log-folder=$LOG_FOLDER --port=$PORT $BZRROOT"


#
# main part
#

loggerhead_process(){
    $SUDO pgrep -fl "$START_CMD"
}

loggerhead_status(){
    proccess=`loggerhead_process`
    #echo "$proccess"
    listening=`netstat -nl |grep -e ":$PORT "`
    #echo "$listening"
    if [ -z "$proccess" ]; then
        echo "Loggerhead is *not* running."
    else
        echo "Loggerhead is running."
        if [ -z "$listening" ]; then
            echo "This server is *not* listening on port $PORT."
        else
            echo "This server is listening on port $PORT."
        fi
    fi
}

start_loggerhead(){
    echo "Starting loggerhead.   (See $LOG_FOLDER for details.)"

    # make sure the log folder is created
    if [ ! -d $LOG_FOLDER ]
    then
        $SUDO mkdir -p $LOG_FOLDER
    fi
    echo "" > $LOG_FILE
    $SUDO python $START_CMD > $LOG_FILE 2>&1 &

    #wait a little while of some logging to appear
    log=""
    for i in $(seq 1 3 30); do
        log=`cat $LOG_FILE`
        if [ -n "$log" ]; then
            break
        fi
        sleep 0.3
    done
    tail $LOG_FILE
    loggerhead_status
}

stop_loggerhead(){
    echo "Stopping loggerhead."
    $SUDO pkill -f "$START_CMD"
    loggerhead_status
}

case "$1" in
    start)
        start_loggerhead
    ;;
    stop)
        stop_loggerhead
    ;;
    status)
        loggerhead_status
    ;;
    restart)
        stop_loggerhead
        start_loggerhead
    ;;
    *)
        echo "Usage: loggerheadd { start | stop | status | restart }"
        exit 1
esac