forked from kwindrem/SetupHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceResources
executable file
·177 lines (152 loc) · 4.7 KB
/
ServiceResources
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
# ServiceManager for SetupHelper
# contains a functions to install, remove, start and stop a package's service
# managing a normal package's service is straight forward
#
# normally, services are connected via a symbolic link, but to avoid issues with
# updating package files, the service directory is COPIED to the /service directory instead.
#
# If the active copy of the service already exists, the run and log/run files are updated
# ONLY if there are changes. This leaves other files managed by supervise untouched.
#
# For all functions, $1 specifies the package name
#
# functions that begin with _ skip checks and do not log activity
# starting with v2.80~10, services are stored in this directory which is overlayed onto /service
# all services need to be added there rather than /service
# Note: service calls (eg svc) are still made on /service/...
# there is an unknown interaction between /service and the overlay source
# so code here operates on both directories
victronServicesDir="/opt/victronenergy/service"
overlayWorkDir="/run/overlays/service"
if [ -d "$victronServicesDir" ]; then
serviceDir="$victronServicesDir"
serviceOverlay=true
else
serviceDir="/service"
serviceOverlay=false
fi
# startService and stopService start and stop the service, respectively
# the 'down' flag is also cleared/set to control service runs in the future
# startService will cause the service to stop then start again !!!
_startService ()
{
local pkg=$1
rm -f "$serviceDir/$pkg/down"
if $serviceOverlay ; then
rm -f "/service/$pkg/down"
svc -u "/service/$pkg"
fi
svc -u "/service/$pkg"
if [ -e "$serviceDir/$pkg/log" ]; then
rm -f "$serviceDir/$pkg/log/down"
svc -u "/service/$pkg/log"
fi
}
startService ()
{
# no package specified
if [ $# -lt 1 ]; then
return
fi
local pkg=$1
if [ -e "$serviceDir/$pkg" ]; then
logMessage "starting $pkg service"
_startService $pkg
fi
}
_stopService ()
{
local pkg=$1
touch "$serviceDir/$pkg/down"
svc -d "/service/$pkg"
if [ -e "$serviceDir/$pkg/log" ]; then
touch "$serviceDir/$pkg/log/down"
svc -d "/service/$pkg/log"
fi
}
stopService ()
{
# no package specified
if [ $# -lt 1 ]; then
return
fi
local pkg=$1
if [ -e "$serviceDir/$pkg" ]; then
logMessage "stopping $pkg service"
_stopService $pkg
fi
}
#
# removeService cleanly removes the service
#
_removeService ()
{
local pkg=$1
# stop the service
_stopService $pkg
# remove the service directory
# removing the service in the overlayed service directory doesn't remove it from /service
# it needs to be removed from the overlay work directory also
rm -rf "$serviceDir/$pkg"
if $serviceOverlay ; then
rm -rf "$overlayWorkDir/$pkg"
fi
}
removeService ()
{
# no package specified
if [ $# -lt 1 ]; then
return
fi
local pkg=$1
if [ -e "$serviceDir/$pkg" ]; then
logMessage "removing $pkg service"
_removeService $pkg
fi
}
# installService adds the service to the /service directory or updates an existing one
#
# If the service does not yet exist, it will start immediately unless
# it includes the 'down' flag file. This behavior is up to the service writer.
#
# If the service already exists, installService will stop it,
# update the service files and stop all child processes
# Then restart the service unless the down flag is set
#
installService ()
{
# no package specified
if [ $# -lt 1 ]; then
return
fi
# no service to install
if [ ! -e "$scriptDir/service" ]; then
return
fi
local pkg=$1
if [ -L "$serviceDir/$pkg" ]; then
logMessage "removing old $pkg service (was symbolic link)"
rm -f "$serviceDir/$pkg"
fi
# service not yet installed, COPY service directory to the active locaiton
if [ ! -e "$serviceDir/$pkg" ]; then
logMessage "installing $pkg service"
cp -R "$scriptDir/service" "$serviceDir/$pkg"
# service already installed - only copy changed files, then restart service
else
logMessage "restarting $pkg service"
if [ -f "$scriptDir/service/run" ]; then
cmp -s "$scriptDir/service/run" "$serviceDir/$pkg/run" > /dev/null
if [ $? != 0 ]; then
cp "$scriptDir/service/run" "$serviceDir/$pkg/run"
fi
fi
if [ -f "$scriptDir/service/log/run" ]; then
cmp -s "$scriptDir/service/log/run" "$serviceDir/$pkg/log/run" > /dev/null
if [ $? != 0 ]; then
cp "$scriptDir/service/log/run" "$serviceDir/$pkg/log/run"
fi
fi
fi
_startService $pkg
}