forked from epicsdeb/sysv-rc-softioc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage-iocs
executable file
·234 lines (207 loc) · 4.83 KB
/
manage-iocs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
PATH="${PATH}:/usr/sbin"
CONF=/etc/default/epics-softioc
LIBRARY=/usr/share/sysv-rc-softioc/library.sh
INITBASESCRIPT=/usr/share/sysv-rc-softioc/softioc
INITDDIR=/etc/init.d
warn () {
echo "$1" >&2
}
die () {
echo "$1" >&2
exit 1
}
. "$LIBRARY"
usage() {
printf "Usage: %s [-v] [-x] cmd\n" `basename $0`
echo "Available commands:"
echo " help - display this message"
echo " report [ioc] - Show config of all/an IOC"
echo " status - Check if IOCs are running"
echo " nextport - Find the next unused procServ port"
echo " install <ioc> - Create /etc/init.d/softioc-[ioc]"
echo " enable <ioc> - Register IOC to start during system boot"
echo " disable <ioc> - Un-register IOC"
echo " startall - Start all IOCs installed for this system"
echo " stopall - Stop all IOCs installed for this system"
exit 2
}
# Print IOC instance config
#
# BASEDIR IOCNAME USER PORT HOSTNAME CMD
#
# $1 - iocdir
reportone() {
if [ -z "$HEADER" ]; then
case "$2" in
conserver) # no header
;;
*)
printf "n%-15s | %-15s | %-15s | %5s | %s\n" BASE IOC USER PORT EXEC
;;
esac
export HEADER=1
fi
local IOC="`basename $1`"
local BASE="`dirname $1`"
if [ ! -r "$1/config" ]; then
echo "Missing config $1/config" >&2
return 1
fi
unset EXEC USER HOST
PORT=0
local INSTBASE="$1"
CHDIR="$1"
. "$1/config"
USER="${USER:-${IOC}}"
EXEC="${EXEC:-${INSTBASE}/st.cmd}"
case "$2" in
conserver)
# skip IOC which don't specify a host
[ -n "$HOST" -a -n "$PORT" ] || continue
# identify if this is the host system
[ "$HOST" = "$(hostname -s)" -o "$HOST" = "$(hostname -f)" ] && HOST=localhost
echo "console $IOC {include softioc; master $HOST; port $PORT;}"
;;
all)
[ -n "$HOST" ] || HOST="<anywhere>"
printf "%-15s | %-15s | %-15s | %-15s | %5s | %s\n" $BASE $HOST $IOC $USER $PORT $EXEC
;;
*)
[ "$HOST" != "$(hostname -s)" -a "$HOST" != "$(hostname -f)" -a -n "$HOST" ] && return 0
[ -n "$HOST" ] || HOST="<anywhere>"
printf "%-15s | %-15s | %-15s | %5s | %s\n" $BASE $IOC $USER $PORT $EXEC
;;
esac
}
installioc() {
IOC="$1"
BASE="`findbase "$IOC"`"
[ $? -ne 0 ] && die "Failed to find ioc $IOC"
echo "Installing IOC $BASE/$IOC"
SCRIPT="$INITDDIR/softioc-$IOC"
echo "As $SCRIPT"
if [ -h "$SCRIPT" ]
then
# old-style symlink
echo "Replacing existing symlink to `readlink "$SCRIPT"`"
rm -f "$SCRIPT" || die "Failed to remove symlink"
elif [ -f "$SCRIPT" ] && ! grep 'AUTOMATICALLYGENERATED' "$SCRIPT" &>/dev/null
then
# script is a file and isn't automatically managed
mv --backup=numbered "$SCRIPT" "$SCRIPT".old || die "failed to backup existing script"
echo "Backing up existing script"
fi
cat << EOF > "$SCRIPT"
## Notice
# This file was generated by `basename $0`
# on `date -R`
# If you edit this file then remove the following line
# to prevent automatic updates
## AUTOMATICALLYGENERATED
### BEGIN INIT INFO
# Provides: softioc-$IOC
# Required-Start: \$remote_fs \$local_fs \$network \$syslog \$time +autofs
# Required-Stop: \$remote_fs \$local_fs \$network \$syslog +autofs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: An EPICS Soft IOC
# Description: Control a single software IOC
### END INIT INFO
. "$INITBASESCRIPT"
EOF
[ -f "$SCRIPT" -a -s "$SCRIPT" ] || die "Failed to create $SCRIPT"
chmod +x "$SCRIPT"
echo "Complete"
echo "To add the IOC to the system boot order run:"
echo " `basename $0` enable $IOC"
}
VERB=
while getopts hvx arg
do
case $arg in
v) VERB=1;;
x) set -x;;
h) usage;;
esac
done
shift $(($OPTIND - 1))
iocinit
[ -n "$VERB" ] && echo "Searching in: $IOCPATH"
cmd="$1"
shift
[ -n "$VERB" ] && echo "Command: $cmd"
case "$cmd" in
report)
visit reportone "$@"
;;
list)
visit echo "$1"
;;
status)
ls -1 /etc/init.d/softioc-*[^~] | while read ff
do
printf "$ff\t\t"
IOC="`basename "$ff"`"
if $ff status &>/dev/null
then
printf "Running"
else
printf "Stopped"
fi
if ! ls /etc/rc*.d/*$IOC &>/dev/null
then
printf ". Not registered"
fi
printf "\n"
done
;;
nextport)
# Find the highest port in use and add one.
LAST="`visit reportone "$1" | tail -n '+2' | awk '{print $7}' | sort -n | tail -n1`"
if [ -n "$LAST" ] && [ "$LAST" -ne 0 ]
then
# print next port
expr "$LAST" '+' 1
else
echo 4050
fi
;;
install)
requireroot
installioc "$1"
;;
enable)
requireroot
[ -z "$1" ] && die "Missing argument"
echo "Enable: softioc-$1"
update-rc.d "softioc-$1" defaults 99
;;
disable)
requireroot
[ -z "$1" ] && die "Missing argument"
echo "Disable: softioc-$1"
update-rc.d -f "softioc-$1" remove
;;
stopall)
requireroot
for initfile in /etc/init.d/softioc-*[^~]
do
$initfile stop
done
;;
startall)
requireroot
for initfile in /etc/init.d/softioc-*[^~]
do
$initfile start
done
;;
help)
usage
;;
*)
die "Unknown command '$cmd'"
usage
;;
esac