-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunit-service.sh
More file actions
executable file
·171 lines (162 loc) · 5.79 KB
/
runit-service.sh
File metadata and controls
executable file
·171 lines (162 loc) · 5.79 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
#!/bin/sh
# Directories
RUNIT_AVAILABLE_SERVICES="/etc/runit/sv"
RUNIT_LOADED_SERVICE_PATH="/run/runit/service"
RUNIT_DEFAULT_SERVICE_PATH="/etc/runit/runsvdir/default"
# Files
RUNIT_DISABLED_SERVICE="/etc/runit/runsvdir/default/$2/down"
help_function()
{
printf "runit-service: A custom tool to manage runit services.\n"
printf "\n"
printf "Usage: runit-service <mode> <service>\n"
printf "\n"
printf "Available modes:\n"
printf "\tlist - List the services located at '/etc/runit/sv'.\n"
printf "\tunlink - Unlink the service from '/etc/runit/runsvdir/default'.\n"
printf "\tlink - Link the service from '/etc/runit/sv' to '/etc/runit/runsvdir/default'.\n"
printf "\tenabled - List the enabled services located at '/etc/runit/runsvdir/default'.\n"
printf "\tdisable - Disable a service from activating at boot.\n"
printf "\tenable - Enable a service activation after boot.\n"
printf "\tstatus - Display the service information.\n"
printf "\tstart - Start the service.\n"
printf "\trestart - Stop and start the service.\n"
printf "\tstop - Stop the service.\n"
printf "\tpurge - Will stop, unlink, and remove the service permanently.\n"
return 0
}
version_function()
{
printf "runit-service a0.1.1:\n"
printf "\tGitHub: https://github.com/SimplyCEO/runit-service.git\n"
printf "\tGitLab: https://gitlab.com/SimplyCEO/runit-service.git\n"
return 0
}
OPTIONS=`getopt -o vh --long version:,help -- "$@"`
eval set -- "${OPTIONS}"
while true; do
case "$1" in
-h|--help) help_function; shift ;;
-v|--version) version_function; shift ;;
--) shift; break ;;
*) break ;;
esac
done
# Code might be messed, but intention is to put in C
case "$1" in
disable|enable|unlink|link|status|start|restart|stop|purge)
if [ "$(whoami)" != "root" ]; then printf "\033[31merror\033[0m: Must be root to use this command.\n"; exit 1; fi
if [ -z "$2" ]; then printf "\033[31merror\033[0m: A service is needed. No service found.\n"; exit 1; fi
if [ ! -d "${RUNIT_AVAILABLE_SERVICES}/$2" ]; then printf "\033[31merror\033[0m: No service found.\n"; exit 1; fi
case "$1" in
unlink) rm "${RUNIT_DEFAULT_SERVICE_PATH}/$2"; exit 0 ;;
link)
# Cannot link an already linked service
if [ -d "${RUNIT_DEFAULT_SERVICE_PATH}/$2" ]; then
printf "\033[31merror\033[0m: Service already linked.\n"
exit 1
fi
ln -s "${RUNIT_AVAILABLE_SERVICES}/$2" "${RUNIT_DEFAULT_SERVICE_PATH}/$2"
touch "${RUNIT_DISABLED_SERVICE}"
printf "\033[32mrunit-service\033[0m: Linked service \"$2\".\n"
exit 0
;;
disable)
# Cannot disable a service that is not linked
if [ ! -d "${RUNIT_DEFAULT_SERVICE_PATH}/$2" ]; then
printf "\033[31merror\033[0m: Service not linked.\n"
exit 1
fi
# Cannot disable a service if it is already down
if [ -f "${RUNIT_DISABLED_SERVICE}" ]; then
printf "\033[31merror\033[0m: Service already disabled.\n"
exit 1
fi
touch "${RUNIT_DISABLED_SERVICE}"
printf "\033[32mrunit-service\033[0m: Disabled service \"$2\" from boot.\n"
exit 0
;;
enable)
# Cannot enable a service that is not linked
if [ ! -d "${RUNIT_DEFAULT_SERVICE_PATH}/$2" ]; then
printf "\033[31merror\033[0m: Service not linked.\n"
exit 1
fi
# Cannot enable a service if it is already up
if [ ! -f "${RUNIT_DISABLED_SERVICE}" ]; then
printf "\033[31merror\033[0m: Service already enabled.\n"
exit 1
fi
rm -f "${RUNIT_DISABLED_SERVICE}"
printf "\033[32mrunit-service\033[0m: Enabled service \"$2\" to boot.\n"
exit 0
;;
status|start|restart|stop) sv "$1" "$2"; exit 0; shift ;;
purge)
printf "\033[31mFAIL SAFE SECURITY\033[0m: Are you sure you want to delete \"$2\" service? (N/y)\n> "
read purge_confirmation
purge_confirmation=$(echo "$purge_confirmation" | tr '[:upper:]' '[:lower:]')
case ${purge_confirmation} in
y|yes|1)
sv stop "$2"
rm -f "${RUNIT_LOADED_SERVICE_PATH}/$2"
rm -f "${RUNIT_DEFAULT_SERVICE_PATH}/$2"
rm -rf "${RUNIT_AVAILABLE_SERVICES}/$2"
exit 0
;;
*) break ;;
esac
exit 1
;;
*) break ;;
esac
;;
list)
printf "\033[34m"
ls "${RUNIT_AVAILABLE_SERVICES}"
printf "\033[0m"
exit 0
;;
enabled)
if [ -n "$2" ] && [ "$2" != "list" ]; then
# Cannot check a service that is not linked
if [ ! -d "${RUNIT_DEFAULT_SERVICE_PATH}/$2" ]; then
printf "\033[31merror\033[0m: Service not linked.\n"
exit 1
fi
if [ -f "${RUNIT_DISABLED_SERVICE}" ]; then
printf "\033[31merror\033[0m: Service not enabled.\n"
exit 1
fi
printf "\033[32mrunit-service\033[0m: Service is enabled.\n"
exit 0
fi
printf "\033[34m"
for service in $(ls "${RUNIT_DEFAULT_SERVICE_PATH}"); do
if [ ! -f "${RUNIT_DEFAULT_SERVICE_PATH}/$service/down" ]; then
SERVICES="${SERVICES} $service"
fi
done
printf "${SERVICES}\n"
printf "\033[0m"
shift
;;
# TODO: Fix loaded function to display only services that are running.
#printf "\tloaded - List the loaded services located at '/run/runit/service'.\n"
#loaded)
# if [ -n "$2" ] && [ "$2" != "list" ]; then
# if [ ! -d "${RUNIT_LOADED_SERVICE_PATH}/$2" ]; then
# printf "\033[31merror\033[0m: Service not loaded.\n"
# exit 1
# fi
# printf "\033[32mrunit-service\033[0m: Service is loaded.\n"
# exit 0
# fi
# printf "\033[34m"
# ls "${RUNIT_LOADED_SERVICE_PATH}"
# printf "\033[0m"
# exit 0
# shift
# ;;
*) break ;;
esac