703a90964e55083643a29aef844ee1fbff76ea06
[m6w6/btr] / share / btr / btrc.sh
1 #!/bin/bash
2
3 function btrc-help {
4 btr-banner
5 echo
6 echo "Usage: $(basename $0) [-hyvq] [<options>] [action] <build>"
7 echo
8 echo " -h, --help Display this help"
9 echo " -y, --yes Always assume yes"
10 echo " -v, --verbose Be more verbose"
11 echo " -q, --quiet Be more quiet"
12 echo
13 echo " Options:"
14 echo " -D, --directory=<directory>"
15 echo " Use this directory as work root"
16 echo
17 echo " Actions:"
18 echo " s[tatus] Show the status of the build"
19 echo " r[un] Make a BTR run"
20 echo " c[ancel] Cancel any currently running BTR job"
21 echo " t[erminate] Terminate the BTR daemon"
22 echo
23 echo " Arguments:"
24 echo " <build> The build id of the \`btrd\` daemon, usually"
25 echo " something like \$repository@\$branch[-\$suffix]."
26 echo
27 exit
28 }
29 export -f btrc-help
30
31 function btrc-parseargs {
32 while test $# -gt 0
33 do
34 case "$1" in
35 s|st|sta|stat|statu|status)
36 BTR_ACTION=status
37 ;;
38 r|ru|run)
39 BTR_ACTION=run
40 ;;
41 c|ca|can|cancel)
42 BTR_ACTION=cancel
43 ;;
44 t|te|ter|term|termi|termin|termina|terminat|terminate)
45 BTR_ACTION=term
46 ;;
47 *)
48 if test -z "$BTR_BUILD"
49 then
50 BTR_BUILD="$1"
51 else
52 error "Unknown action: '$BTR_BUILD' for build id '$1'!"
53 fi
54 ;;
55 esac
56 shift
57 done
58 }
59
60 function btrc-parseopts {
61 local shortoptions="hvqyD:"
62 local longoptions="help,verbose,quiet,yes,directory:"
63 local options=$(getopt \
64 --options "$shortoptions" \
65 --longoptions "$longoptions" \
66 -- "$@" \
67 )
68
69 if test $? -ne 0 ; then
70 btrc-help
71 fi
72
73 eval set -- "$options"
74
75 while test $# -gt 1
76 do
77 case "$1" in
78 -h|--help)
79 btrc-help
80 ;;
81 -y|--yes)
82 BTR_FORCEYES=true
83 ;;
84 -v|--verbose)
85 BTR_VERBOSE=true
86 BTR_QUIET=false
87 ;;
88 -q|--quiet)
89 BTR_QUIET=true
90 BTR_VERBOSE=false
91 ;;
92 #
93 -d|--directory)
94 BTR_RUNDIR="$2"
95 shift
96 ;;
97 #
98 --)
99 shift
100 btrc-parseargs "$@"
101 esac
102 shift
103 done
104 }
105 export -f btrc-parseopts
106
107 function btrc-setup {
108 if test -z "$BTR_BUILD"
109 then
110 btrc-help
111 fi
112
113 if test -z "$BTR_ACTION"
114 then
115 BTR_ACTION=status
116 fi
117
118 btr-setup-rundir
119 btr-setup-verbosity
120
121 BTR_PIDFILE="$BTR_RUNDIR/$BTR_BUILD.pid"
122 BTR_LOGFILE="$BTR_RUNDIR/$BTR_BUILD.log"
123 BTR_COMFILE="$BTR_RUNDIR/$BTR_BUILD.socket"
124
125 if test -r "$BTR_PIDFILE"
126 then
127 export BTR_PIDFILE BTR_LOGFILE BTR_COMFILE
128 else
129 if test -e "$BTR_LOGFILE"
130 then
131 cat "$BTR_LOGFILE"
132 echo
133 fi
134 error "Could not find pid file of btr daemon for '$BTR_BUILD' in $BTR_RUNDIR."
135 fi
136 }
137 export -f btrc-setup
138
139 function btrc-signal {
140 local sig=$1
141 local pid=$(cat "$BTR_PIDFILE")
142 kill -s $sig $pid
143 kill -s CONT $pid
144 case "$sig" in
145 TERM|SIGTERM|15)
146 $SAY -n "Waiting for the daemon to shutdown..."
147 while kill -s 0 $pid &>/dev/null
148 do
149 $SAY -n "."
150 sleep .1
151 done
152 $SAY " Done, bye."
153 ;;
154 esac
155 }
156 export -f btrc-signal
157
158
159 # vim: noet