-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmanage-iocs
More file actions
executable file
·255 lines (223 loc) · 7.59 KB
/
manage-iocs
File metadata and controls
executable file
·255 lines (223 loc) · 7.59 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash
VER="1-10"
PATH="${PATH}:/sbin:/bin:/usr/sbin:/usr/bin"
function die {
echo "$1" >&2
exit 1
}
# the command 'manage-iocs' is a symlink; find the source directory;
SRCDIR="$(dirname "$(readlink -f "$0")")"
# variable configuration: IOCPATH, PIDDIR, LOGDIR, PROCSERV, SYSTEMDDIR
CONF="$SRCDIR/epics-softioc.conf"
if [ -f "$CONF" ]; then
. "$CONF"
else
die "Aborted: missing $CONF"
fi
# function library: usage, vist, reportone, requireroot, installioc
LIBRARY="$SRCDIR/library.sh"
[ -f "$LIBRARY" ] || die "Aborted: missing $LIBRARY"
. "$LIBRARY"
VERB=
while getopts hvx arg
do
case $arg in
v) VERB=1;;
x) set -x;;
h) usage;;
esac
done
shift $(($OPTIND - 1))
cmd="$1"
shift
[ -n "$VERB" ] && { echo "Searching in: $IOCPATH"; echo "Command: $cmd"; }
case "$cmd" in
help)
usage
;;
report)
visit reportone "$@"
# installioc() also checks duplicated ports, so the following seems unnecessary
if [ $# -eq 0 ]; then
# report duplicated ports
#ports="`visit reportone "" | awk '{print $7}' ORS=' ' | sort -n`"
ports="`visit reportone "" | awk '{print $7}' | sort -n`"
#echo "${ports[@]}"
d_ports="$(printf '%s\n' "${ports[@]}" | awk '!($0 in seen){seen[$0];next} 1')"
if [ -n "$d_ports" ]; then
printf "\nWARNING: duplicated telnet ports are being used: "
echo $d_ports
printf "\tYou have to use a unique port for each IOC instance\n"
fi
fi
;;
status)
ls -1 $SYSTEMDDIR/softioc-*.service | while read ff
do
unitfile="`basename "$ff"`"
# On Debian: Failed to retrieve unit: Access denied
result="$(systemctl is-active $unitfile)" &> /dev/null
# one has to use 'sudo ...' on old CentOS (7.8)
#if [ "$result" = "unknown" -o -z "$result" ]; then
# die "Please type 'sudo manage-iocs status'"
#fi
printf "$unitfile\t\t"
#systemctl -q is-active $unitfile
#if [ $? -eq 0 ]; then
if [ "$result" = "active" ]; then
printf "Running"
elif [ "$result" = "inactive" ]; then
printf "Stopped"
else
printf "Strange status for $unitfile: $result. Please try 'sudo manage-iocs status'"
fi
# status of auto-start at boot
if [ ! -h "$SYSTEMDDIR/multi-user.target.wants/$unitfile" ]; then
printf ". Not registered"
fi
printf "\n"
# consistency checking in case something changed on the IOC
IOC=${unitfile#softioc-}
IOC=${IOC%.service}
IOCBASE="`findbase "$IOC"`"
[ $? -ne 0 -o -z "$IOCBASE" ] &&
{ printf "Warning: failed to read '$IOC' or '$IOC/config' in '$IOCPATH'; please\
verify file permission, confirm the IOC setup and uninstall $IOC if needed\n\n"; continue; }
. "$IOCBASE/$IOC/config"
[ "$HOST" != "$(hostname -s)" -a "$HOST" != "$(hostname -f)" ] &&
printf "Warning: HOST in $IOCBASE/$IOC/config is changed to $HOST;\
please confirm the IOC setup and uninstall $IOC if needed\n\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`"
#[ -n "$LAST" -a "$LAST" -ne 0 ] && expr "$LAST" '+' 1 || echo 4050
# could "echo 4050" fail?
[ -z "$LAST" ] && echo 4050 || expr "$LAST" '+' 1
;;
install)
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
installioc "$1"
# ask to start the IOC if it is not running? NO, otherwise infinite loop
;;
uninstall)
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
systemctl -q is-active softioc-$1.service
[ $? -eq 0 ] && die "Aborted: the IOC '$1' is running. Please stop it first"
unitfile=$SYSTEMDDIR/softioc-$1.service
if [ -f $unitfile ]; then
rm -f $SYSTEMDDIR/softioc-$1.service || die "Failed to remove $unitfile"
echo "$unitfile is removed successfully"
else
die "Aborted: $unitfile is already removed "
fi
# ask to disable auto-start IOC at boot if it is enabled
if [ -h $SYSTEMDDIR/multi-user.target.wants/softioc-$1.service ]; then
read -p "Do you want to disable auto-start '$1' at boot? Type 'yes' if you do. " answer
[ "$answer" = "yes" ] && manage-iocs disable $1
fi
;;
start)
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
systemctl -q is-active softioc-$1.service
[ $? -eq 0 ] && die "The IOC '$1' is already running"
echo "Starting the IOC '$1' ..."
# update the unit file just in case the IOC's config file is changed
manage-iocs install $1 2>&1 >/dev/null
systemctl daemon-reload
systemctl start softioc-$1.service
sleep 1
systemctl -q is-active softioc-$1.service
if [ $? -eq 0 ]; then
echo "The IOC '$1' has been started successfully"
else
die "Failed to start the IOC '$1'; type 'sudo journalctl -xe' for details."
fi
# ask to enable auto-start IOC at boot if it is not enabled
if [ ! -h $SYSTEMDDIR/multi-user.target.wants/softioc-$1.service ]; then
read -p "Do you want to enable auto-start '$1' at boot? Type 'yes' if you do. " answer
[ "$answer" = "yes" ] && manage-iocs enable $1
fi
;;
stop)
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
systemctl -q is-active softioc-$1.service
[ $? -ne 0 ] && die "The IOC $1 is already stopped"
echo "Stopping the IOC $1 ..."
systemctl stop softioc-$1.service
sleep 1
systemctl -q is-active softioc-$1.service
if [ $? -ne 0 ]; then
echo "The IOC $1 has been stopped successfully"
else
die "Failed to stop the IOC $1; type 'sudo journalctl -xe' for details."
fi
;;
restart)
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
systemctl -q is-active softioc-$1.service
[ $? -eq 0 ] && manage-iocs stop $1
manage-iocs start $1
;;
startall)
requireroot
ls -1 $SYSTEMDDIR/softioc-*.service | while read ff
do
unitfile="`basename "$ff"`"
IOC=${unitfile#softioc-}
IOC=${IOC%.service}
manage-iocs start $IOC
done
;;
stopall)
requireroot
ls -1 $SYSTEMDDIR/softioc-*.service | while read ff
do
unitfile="`basename "$ff"`"
IOC=${unitfile#softioc-}
IOC=${IOC%.service}
manage-iocs stop $IOC
done
;;
enable)
#enable IOC auto-start at boot
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
if [ -h $SYSTEMDDIR/multi-user.target.wants/softioc-$1.service ]; then
die "auto-start '$1' at boot is already enabled"
fi
systemctl enable softioc-$1 #&> /dev/null
if [ -h $SYSTEMDDIR/multi-user.target.wants/softioc-$1.service ]; then
echo "auto-start the IOC '$1' at boot has been enabled successfully"
fi
;;
disable)
#disable IOC auto-start at boot
requireroot
[ -z "$1" ] && die "Aborted: missing one argument -- an IOC name"
if [ ! -h $SYSTEMDDIR/multi-user.target.wants/softioc-$1.service ]; then
die "auto-start '$1' at boot is already disabled"
fi
systemctl disable softioc-$1
if [ ! -h $SYSTEMDDIR/multi-user.target.wants/softioc-$1.service ]; then
echo "auto-start the IOC '$1' at boot has been disabled successfully"
fi
;;
list)
visit echo "$1"
;;
version)
echo "Version: $VER"
;;
*)
[ "$cmd" ] && echo "Unknown command '$cmd'"
usage
exit 1
;;
esac