NBash

Форк
0
297 строк · 7.6 Кб
1
#!/usr/bin/env bash
2
#
3
# Copyright (C) 2012-2013, 2016, 2020, 2021, 2023  Etersoft
4
# Copyright (C) 2012-2013, 2016, 2020, 2021, 2023  Vitaly Lipatov <lav@etersoft.ru>
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
15
#
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
#
19

20
PROGDIR=$(dirname "$0")
21
PROGNAME=$(basename "$0")
22
[ -n "$EPMCURDIR" ] || export EPMCURDIR="$(pwd)"
23
CMDSHELL="/bin/bash"
24
# TODO: pwd for ./epm and which for epm
25
[ "$PROGDIR" = "." ] && PROGDIR="$EPMCURDIR"
26
if [ "$0" = "/dev/stdin" ] || [ "$0" = "sh" ] ; then
27
    PROGDIR=""
28
    PROGNAME=""
29
fi
30

31
# will replaced with /usr/share/eepm during install
32
SHAREDIR=$PROGDIR
33
# will replaced with /etc/eepm during install
34
CONFIGDIR=$PROGDIR/../etc
35

36
EPMVERSION="@VERSION@"
37

38
# package, single (file), pipe, git
39
EPMMODE="package"
40
[ "$SHAREDIR" = "$PROGDIR" ] && EPMMODE="single"
41
[ "$EPMVERSION" = "@""VERSION""@" ] && EPMMODE="git"
42
[ "$PROGNAME" = "" ] && EPMMODE="pipe"
43

44
if [ "$EPMMODE" = "git" ] ; then
45
    EPMVERSION=$(head $PROGDIR/../eepm.spec | grep "^Version: " | sed -e 's|Version: ||' )
46
fi
47

48

49
load_helper()
50
{
51
    local shieldname="loaded$(echo "$1" | sed -e 's|-||g')"
52
    # already loaded
53
    eval "test -n \"\$$shieldname\"" && debug "Already loaded $1" && return
54

55
    local CMD="$SHAREDIR/$1"
56
    # do not use fatal() here, it can be initial state
57
    [ -r "$CMD" ] || { echo "FATAL: Have no $CMD helper file" ; exit 1; }
58
    eval "$shieldname=1"
59
    # shellcheck disable=SC1090
60
    . $CMD
61
}
62

63

64
load_helper epm-sh-functions
65

66
INITDIR=/etc/init.d
67

68
PATH=$PATH:/sbin:/usr/sbin
69

70
set_sudo
71

72
check_tty
73

74
#############################
75

76
# FIXME: detect by real init system
77
# FIXME: add upstart support (Ubuntu?)
78
set_service_type()
79
{
80
    local CMD
81

82
    set_distro_info
83

84
case "$DISTRCONTROL" in
85
    sysvinit)
86
        CMD="service-chkconfig"
87
        ;;
88
    systemd)
89
        CMD="systemd"
90
        ;;
91
esac
92

93
# override system control detection result
94
[ -n "$SERV_BACKEND" ] && CMD="$SERV_BACKEND"
95
# obsoleted
96
[ -n "$FORCESERVICE" ] && CMD="$FORCESERVICE"
97

98
SERVICETYPE="$CMD"
99

100
ANYSERVICE=$(print_command_path anyservice)
101

102
}
103

104
# TODO: done it on anyservice part
105
is_anyservice()
106
{
107
    [ -n "$ANYSERVICE" ] || return
108
    [ -n "$1" ] || return
109
    # check if anyservice is exists and checkd returns true
110
    $ANYSERVICE "$1" checkd 2>/dev/null
111
}
112

113

114
phelp()
115
{
116
    echo "$Descr
117
$Usage
118
 Commands:
119
$(get_help HELPCMD)
120

121
 Options:
122
$(get_help HELPOPT)
123
"
124
}
125

126
print_version()
127
{
128
        local on_text="(host system)"
129
        local virt="$($DISTRVENDOR -i)"
130
        [ "$virt" = "(unknown)" ] || [ "$virt" = "(host system)" ] || on_text="(under $virt)"
131
        echo "Service manager version $EPMVERSION  https://wiki.etersoft.ru/Epm"
132
        echo "Running on $DISTRNAME/$DISTRVERSION $on_text with $SERVICETYPE"
133
        echo "Copyright (c) Etersoft 2012-2023"
134
        echo "This program may be freely redistributed under the terms of the GNU AGPLv3."
135
}
136

137
progname="${0##*/}"
138

139
Usage="Usage: $progname [options] [<service>] [<command>] [params]..."
140
Descr="serv - Service manager"
141

142
set_service_type
143

144
verbose=
145
quiet=
146
short=
147
non_interactive=
148
show_command_only=
149
serv_cmd=
150
service_name=
151
params=
152
withoutservicename=
153

154
# load system wide config
155
[ -f /etc/eepm/serv.conf ] && . /etc/eepm/serv.conf
156

157
check_command()
158
{
159
    # do not override command
160
    [ -z "$serv_cmd" ] || return
161

162
    case $1 in
163
    status)                   # HELPCMD: show service status
164
        serv_cmd=status
165
        ;;
166
    restart)                 # HELPCMD: restart service
167
        serv_cmd=restart
168
        ;;
169
    reload)                  # HELPCMD: reload service
170
        serv_cmd=reload
171
        ;;
172
    start)                    # HELPCMD: start service
173
        serv_cmd=start
174
        ;;
175
    stop)                     # HELPCMD: stop service
176
        serv_cmd=stop
177
        ;;
178
    on)                       # HELPCMD: add service to run on startup and start it now
179
        serv_cmd=on
180
        ;;
181
    off)                      # HELPCMD: remove service to run on startup and stop it now
182
        serv_cmd=off
183
        ;;
184
    enable)                   # HELPCMD: add service to run on startup (see 'on' also)
185
        serv_cmd=enable
186
        ;;
187
    disable)                 # HELPCMD: remove service to run on startup (see 'off' also)
188
        serv_cmd=disable
189
        ;;
190
    log|journal)              # HELPCMD: print log for the service (-f - follow,  -r - reverse order)
191
        serv_cmd=log
192
        ;;
193
    cat)                      # HELPCMD: print out service file for the service
194
        serv_cmd=cat
195
        ;;
196
    exists)                   # HELPCMD: check if the service is installed on the system
197
        serv_cmd=exists
198
        ;;
199
    edit)                     # HELPCMD: edit service file overload (use --full to edit full file)
200
        serv_cmd=edit
201
        ;;
202
    test|-t)                  # HELPCMD: test a config file of the service
203
        serv_cmd=test
204
        ;;
205
    list)                     # HELPCMD: list running services
206
        serv_cmd=list
207
        withoutservicename=1
208
        ;;
209
    list-all)                 # HELPCMD: list all available services
210
        serv_cmd=list_all
211
        withoutservicename=1
212
        ;;
213
    list-startup)             # HELPCMD: list all services to run on startup
214
        serv_cmd=list_startup
215
        withoutservicename=1
216
        ;;
217
    list-failed|--failed)       # HELPCMD: list services failed on startup
218
        serv_cmd=list_failed
219
        withoutservicename=1
220
        ;;
221
    print)                    # HELPCMD: print some info
222
        serv_cmd=print
223
        withoutservicename=1
224
        ;;
225
    try-restart|condrestart)  # HELPCMD: Restart service if running
226
        serv_cmd=try_restart
227
        ;;
228
    usage)                    # HELPCMD: print out usage of the service
229
        serv_cmd=usage
230
        withoutservicename=1
231
        ;;
232
    *)
233
        return 1
234
        ;;
235
    esac
236
    return 0
237
}
238

239
check_option()
240
{
241
    case $1 in
242
    -h|--help|help)       # HELPOPT: this help
243
        phelp
244
        exit 0
245
        ;;
246
    -v|--version)         # HELPOPT: print version
247
        print_version
248
        exit 0
249
        ;;
250
    --verbose)            # HELPOPT: verbose mode
251
        verbose=1
252
        ;;
253
    --short)              # HELPOPT: short mode
254
        short=1
255
        ;;
256
    --show-command-only)  # HELPOPT: show command only, do not any action
257
        show_command_only=1
258
        ;;
259
    --quiet)              # HELPOPT: quiet mode (do not print commands before exec)
260
        quiet=1
261
        ;;
262
    --auto)               # HELPOPT: non interactive mode
263
        non_interactive=1
264
        ;;
265
    *)
266
        return 1
267
        ;;
268
    esac
269
    return 0
270
}
271

272
for opt in "$@" ; do
273
    check_command $opt && continue
274
    check_option $opt && continue
275
    [ -z "$service_name" ] && service_name=$opt && continue
276
    params="$params $opt"
277
done
278

279
echover "service: $service_name"
280
echover "command: $serv_cmd"
281

282
# Just printout help if run without args
283
if [ -z "$withoutservicename" ] && [ -z "$service_name" ] ; then
284
    print_version
285
    echo
286
    fatal "Run $ $progname --help for get help"
287
fi
288

289
# use common way if the command is unknown
290
if [ -z "$serv_cmd" ] ; then
291
    serv_cmd=common
292
fi
293

294
# Run helper for command
295
load_helper serv-$serv_cmd
296
serv_$serv_cmd $service_name $params
297
# return last error code (from subroutine)
298

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.