root / daemons / daemon-util.in @ d2baa21d
History | View | Annotate | Download (3.1 kB)
1 |
#!/bin/bash |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2009 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 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, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
set -e |
22 |
|
23 |
defaults_file=@SYSCONFDIR@/default/ganeti |
24 |
|
25 |
NODED_ARGS= |
26 |
MASTERD_ARGS= |
27 |
CONFD_ARGS= |
28 |
RAPI_ARGS= |
29 |
|
30 |
# Read defaults file if it exists |
31 |
if [[ -s $defaults_file ]]; then |
32 |
. $defaults_file |
33 |
fi |
34 |
|
35 |
_daemon_pidfile() { |
36 |
echo "@LOCALSTATEDIR@/run/ganeti/$1.pid" |
37 |
} |
38 |
|
39 |
# Checks whether the local machine is part of a cluster |
40 |
check_config() { |
41 |
local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem |
42 |
local fname |
43 |
|
44 |
for fname in $server_pem; do |
45 |
if [[ ! -f $fname ]]; then |
46 |
echo "Missing configuration file $fname" >&2 |
47 |
return 1 |
48 |
fi |
49 |
done |
50 |
|
51 |
return 0 |
52 |
} |
53 |
|
54 |
# Checks whether daemon is running |
55 |
check() { |
56 |
if [[ "$#" -lt 1 ]]; then |
57 |
echo 'Missing daemon name.' >&2 |
58 |
exit 1 |
59 |
fi |
60 |
|
61 |
local name="$1"; shift |
62 |
|
63 |
start-stop-daemon --stop --signal 0 --quiet \ |
64 |
--pidfile $(_daemon_pidfile $name) |
65 |
} |
66 |
|
67 |
# Starts a daemon |
68 |
start() { |
69 |
if [[ "$#" -lt 1 ]]; then |
70 |
echo 'Missing daemon name.' >&2 |
71 |
exit 1 |
72 |
fi |
73 |
|
74 |
local name="$1"; shift |
75 |
|
76 |
# Convert daemon name to uppercase after removing "ganeti-" prefix |
77 |
local ucname=$(echo ${name#ganeti-} | tr a-z A-Z) |
78 |
|
79 |
# Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS |
80 |
eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\"" |
81 |
|
82 |
start-stop-daemon --start --quiet --oknodo \ |
83 |
--pidfile $(_daemon_pidfile $name) \ |
84 |
--startas "@PREFIX@/sbin/$name" \ |
85 |
-- $args "$@" |
86 |
} |
87 |
|
88 |
# Stops a daemon |
89 |
stop() { |
90 |
if [[ "$#" -lt 1 ]]; then |
91 |
echo 'Missing daemon name.' >&2 |
92 |
exit 1 |
93 |
fi |
94 |
|
95 |
local name="$1"; shift |
96 |
|
97 |
start-stop-daemon --stop --quiet --oknodo --retry 30 \ |
98 |
--pidfile $(_daemon_pidfile $name) |
99 |
} |
100 |
|
101 |
# Starts a daemon if it's not yet running |
102 |
check_and_start() { |
103 |
local name="$1" |
104 |
|
105 |
if ! check $name; then |
106 |
start $name |
107 |
fi |
108 |
} |
109 |
|
110 |
# Starts the master role |
111 |
start_master() { |
112 |
start ganeti-masterd |
113 |
start ganeti-rapi |
114 |
} |
115 |
|
116 |
# Stops the master role |
117 |
stop_master() { |
118 |
stop ganeti-rapi |
119 |
stop ganeti-masterd |
120 |
} |
121 |
|
122 |
# Reloads the SSH keys |
123 |
reload_ssh_keys() { |
124 |
@RPL_SSH_INITD_SCRIPT@ restart |
125 |
} |
126 |
|
127 |
if [[ "$#" -lt 1 ]]; then |
128 |
echo "Usage: $0 <action>" >&2 |
129 |
exit 1 |
130 |
fi |
131 |
|
132 |
orig_action=$1; shift |
133 |
|
134 |
if [[ "$orig_action" == *_* ]]; then |
135 |
echo "Command must not contain underscores" >&2 |
136 |
exit 1 |
137 |
fi |
138 |
|
139 |
# Replace all dashes (-) with underlines (_) |
140 |
action=${orig_action//-/_} |
141 |
|
142 |
# Is it a known function? |
143 |
if ! declare -F "$action" >/dev/null 2>&1; then |
144 |
echo "Unknown command: $orig_action" >&2 |
145 |
exit 1 |
146 |
fi |
147 |
|
148 |
# Call handler function |
149 |
$action "$@" |