-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathkubesolo-service.sh
More file actions
297 lines (284 loc) · 7.92 KB
/
kubesolo-service.sh
File metadata and controls
297 lines (284 loc) · 7.92 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/bin/sh
set -e
APP_NAME="kubesolo"
PIDFILE="/var/run/$APP_NAME.pid"
LOGFILE="/var/log/$APP_NAME.log"
# Function to detect init system
detect_init_system() {
if command -v systemctl >/dev/null 2>&1 && [ -d /etc/systemd/system ]; then
echo "systemd"
elif [ -f /sbin/init ] && /sbin/init --version 2>/dev/null | grep -q upstart; then
echo "upstart"
elif [ -d /etc/init.d ]; then
echo "sysvinit"
elif [ -d /etc/s6 ] || command -v s6-svc >/dev/null 2>&1; then
echo "s6"
elif command -v runit >/dev/null 2>&1 || [ -d /etc/runit ]; then
echo "runit"
elif command -v openrc >/dev/null 2>&1 || [ -f /sbin/openrc ]; then
echo "openrc"
else
echo "unknown"
fi
}
INIT_SYSTEM=$(detect_init_system)
# Function to start service
start_service() {
case "$INIT_SYSTEM" in
"systemd")
systemctl start "$APP_NAME"
;;
"sysvinit")
service "$APP_NAME" start
;;
"openrc")
rc-service "$APP_NAME" start
;;
"s6")
if [ -d "/etc/s6/sv/$APP_NAME" ]; then
s6-svc -u "/etc/s6/sv/$APP_NAME"
else
echo "❌ s6 service not found"
exit 1
fi
;;
"runit")
if [ -L "/var/service/$APP_NAME" ] || [ -L "/etc/runit/runsvdir/default/$APP_NAME" ]; then
echo "✅ runit service should start automatically"
else
echo "❌ runit service not found"
exit 1
fi
;;
"upstart")
initctl start "$APP_NAME"
;;
*)
echo "❌ Unknown init system or daemon mode"
echo "💡 Try: nohup kubesolo [args] > $LOGFILE 2>&1 &"
exit 1
;;
esac
echo "✅ $APP_NAME service started"
}
# Function to stop service
stop_service() {
case "$INIT_SYSTEM" in
"systemd")
systemctl stop "$APP_NAME"
;;
"sysvinit")
service "$APP_NAME" stop
;;
"openrc")
rc-service "$APP_NAME" stop
;;
"s6")
if [ -d "/etc/s6/sv/$APP_NAME" ]; then
s6-svc -d "/etc/s6/sv/$APP_NAME"
else
echo "❌ s6 service not found"
exit 1
fi
;;
"runit")
if [ -L "/var/service/$APP_NAME" ]; then
sv stop "$APP_NAME"
elif [ -L "/etc/runit/runsvdir/default/$APP_NAME" ]; then
sv stop "$APP_NAME"
else
echo "❌ runit service not found"
exit 1
fi
;;
"upstart")
initctl stop "$APP_NAME"
;;
*)
# Try to stop daemon mode
if [ -f "$PIDFILE" ]; then
PID=$(cat "$PIDFILE")
if kill -0 "$PID" 2>/dev/null; then
kill "$PID"
rm -f "$PIDFILE"
echo "✅ $APP_NAME daemon stopped"
else
echo "⚠️ PID file exists but process not running"
rm -f "$PIDFILE"
fi
else
echo "❌ No PID file found. Try: pkill kubesolo"
exit 1
fi
;;
esac
echo "✅ $APP_NAME service stopped"
}
# Function to restart service
restart_service() {
echo "🔄 Restarting $APP_NAME service..."
stop_service
sleep 2
start_service
}
# Function to check service status
status_service() {
case "$INIT_SYSTEM" in
"systemd")
systemctl status "$APP_NAME"
;;
"sysvinit")
service "$APP_NAME" status
;;
"openrc")
rc-service "$APP_NAME" status
;;
"s6")
if [ -d "/etc/s6/sv/$APP_NAME" ]; then
s6-svstat "/etc/s6/sv/$APP_NAME"
else
echo "❌ s6 service not found"
exit 1
fi
;;
"runit")
if [ -L "/var/service/$APP_NAME" ]; then
sv status "$APP_NAME"
elif [ -L "/etc/runit/runsvdir/default/$APP_NAME" ]; then
sv status "$APP_NAME"
else
echo "❌ runit service not found"
exit 1
fi
;;
"upstart")
initctl status "$APP_NAME"
;;
*)
# Check daemon mode
if [ -f "$PIDFILE" ]; then
PID=$(cat "$PIDFILE")
if kill -0 "$PID" 2>/dev/null; then
echo "✅ $APP_NAME daemon is running (PID: $PID)"
else
echo "❌ $APP_NAME daemon is not running (stale PID file)"
exit 1
fi
else
if pgrep kubesolo >/dev/null; then
echo "⚠️ $APP_NAME process found but no PID file"
pgrep -l kubesolo
else
echo "❌ $APP_NAME is not running"
exit 1
fi
fi
;;
esac
}
# Function to show logs
show_logs() {
case "$INIT_SYSTEM" in
"systemd")
journalctl -u "$APP_NAME" -f
;;
"sysvinit")
tail -f /var/log/syslog | grep "$APP_NAME"
;;
"openrc")
tail -f /var/log/messages | grep "$APP_NAME"
;;
*)
if [ -f "$LOGFILE" ]; then
tail -f "$LOGFILE"
else
echo "❌ Log file not found: $LOGFILE"
exit 1
fi
;;
esac
}
# Function to enable service
enable_service() {
case "$INIT_SYSTEM" in
"systemd")
systemctl enable "$APP_NAME"
;;
"sysvinit")
if command -v update-rc.d >/dev/null 2>&1; then
update-rc.d "$APP_NAME" defaults
elif command -v chkconfig >/dev/null 2>&1; then
chkconfig "$APP_NAME" on
fi
;;
"openrc")
rc-update add "$APP_NAME" default
;;
*)
echo "⚠️ Auto-enable not supported for $INIT_SYSTEM"
;;
esac
echo "✅ $APP_NAME service enabled"
}
# Function to disable service
disable_service() {
case "$INIT_SYSTEM" in
"systemd")
systemctl disable "$APP_NAME"
;;
"sysvinit")
if command -v update-rc.d >/dev/null 2>&1; then
update-rc.d "$APP_NAME" remove
elif command -v chkconfig >/dev/null 2>&1; then
chkconfig "$APP_NAME" off
fi
;;
"openrc")
rc-update del "$APP_NAME" default
;;
*)
echo "⚠️ Auto-disable not supported for $INIT_SYSTEM"
;;
esac
echo "✅ $APP_NAME service disabled"
}
# Main script logic
case "$1" in
start)
start_service
;;
stop)
stop_service
;;
restart)
restart_service
;;
status)
status_service
;;
logs)
show_logs
;;
enable)
enable_service
;;
disable)
disable_service
;;
*)
echo "Usage: $0 {start|stop|restart|status|logs|enable|disable}"
echo ""
echo "Commands:"
echo " start - Start the $APP_NAME service"
echo " stop - Stop the $APP_NAME service"
echo " restart - Restart the $APP_NAME service"
echo " status - Show service status"
echo " logs - Show service logs (follow mode)"
echo " enable - Enable service to start at boot"
echo " disable - Disable service from starting at boot"
echo ""
echo "Detected init system: $INIT_SYSTEM"
exit 1
;;
esac
exit 0