-
Notifications
You must be signed in to change notification settings - Fork 5
/
all.bash
executable file
·149 lines (125 loc) · 2.88 KB
/
all.bash
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
#!/usr/bin/env bash
#
# author: s. rannou <[email protected]>
# contributor: Manfred Touron <[email protected]>
PROJECT="gorobot"
# user to run as
USER="mxs"
# edit these to your needs if you know what you are doing
CHDIR="root" # directory where we chdir/chroot
PID="$PROJECT.pid" # relative to $CHDIR
BIN="$PROJECT" # relative to $CHDIR
LOG="gorobot.log" # relative to $CHDIR
CONFIG="gorobot.json" # relative to $CHDIR
LOGS="logs" # relative to $CHDIR
cd $(dirname $0)
function ok { # <msg>
echo -e "\033[0;32;49m$@\\033[0m"
return 0
}
function ko { # <msg>
echo -e "\033[0;31;49mError: $@\\033[0m"
return 1
}
function title { # <msg>
echo -e "\033[0;32;40m$@\\033[0m"
return 0
}
function warn-upon-failure { # <return_value> <msg>
local ret=$1
local msg=$2
if [ $ret -ne 0 ]
then
ko "$msg"
fi
return $ret
}
function be-quiet { # <command1>
$@ 2>&1 > /dev/null
return $?
}
function get-uid {
return $(id -u)
}
function usage {
echo -e "usage: $0 [build|stop|start|restart|status]"
}
function check-env {
# make sure $DEPLOY_DIR exists
if ! [ -d $DEPLOY ]
then
warn-upon-failure 1 "$DEPLOY does not exist, maybe you should edit \$DEPLOY" || return 1
fi
# make sure $USER exists
be-quiet id $USER
warn-upon-failure $? "User $USER does not exist, maybe you should edit \$USER"
return 0
}
check-env || exit 1
case $1 in
"stop")
ok "stopping $PROJECT"
if [ -f $CHDIR/$PID ]
then
kill -9 $(cat $CHDIR/$PID)
rm -f $CHDIR/$PID
fi
ok "daemon stopped"
exit 0
;;
"start")
ok "starting $PROJECT"
mkdir -p $CHDIR/$LOGS
touch $CHDIR/$LOG
./daemonize -p $PID -u $USER -c $CHDIR -- ./$PROJECT -c $CONFIG
warn-upon-failure $? "Can't start the daemon, check your config" || exit 1
ok "daemon started"
exit 0
;;
"restart")
$0 stop
$0 start
exit 0
;;
"status")
pid=$(ps -o pid= --pid $(cat $CHDIR/$PID 2>/dev/null) 2>/dev/null)
if [ "$pid" != "" ]
then
ok "$PROJECT is running, with pid $pid"
else
ko "$PROJECT is down"
fi
exit 0
;;
"log")
if [ -f $CHDIR/$LOG ]
then
ok "last 25 log entries:"
tail -n 25 $CHDIR/$LOG
else
ko "can't find log file ($CHDIR/$LOG)"
fi
exit 0
;;
"build")
# build the daemonizer
if [ ! -f daemonize ] || [ $(stat -c %Y daemonize) -lt $(stat -c %Y daemonizer/daemonizer.c) ]
then
ok "building daemonize..."
gcc -W -Wall -pedantic -ansi -O3 -Wno-unused-result daemonizer/daemonizer.c -o daemonize
warn-upon-failure $? "can't build daemonize"
ok "daemonize built (or not)"
else
ok "daemonize is already up-to-date (skipped)"
fi
# build the website
ok "building $PROJECT..."
go build -o $PROJECT
cp $PROJECT $CHDIR/$PROJECT
warn-upon-failure $? "unable to build $PROJECT"
ok "$PROJECT built (or not)..."
exit 0
;;
esac
usage
exit 1