Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ 8e2ed2e8

History | View | Annotate | Download (5.4 kB)

1
#!/bin/bash
2
#
3

    
4
# Copyright (C) 2009, 2012 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
# This is a list of all daemons and the order in which they're started. The
26
# order is important as there are dependencies between them. On shutdown,
27
# they're stopped in reverse order.
28
DAEMONS=(
29
  ganeti-noded
30
  ganeti-masterd
31
  ganeti-rapi
32
  ganeti-confd
33
  )
34

    
35
NODED_ARGS=
36
MASTERD_ARGS=
37
CONFD_ARGS=
38
RAPI_ARGS=
39

    
40
# Read defaults file if it exists
41
if [[ -s $defaults_file ]]; then
42
  . $defaults_file
43
fi
44

    
45
_daemon_pidfile() {
46
  echo "@LOCALSTATEDIR@/run/ganeti/$1.pid"
47
}
48

    
49
_daemon_executable() {
50
  echo "@PREFIX@/sbin/$1"
51
}
52

    
53
_daemon_usergroup() {
54
  case "$1" in
55
    masterd)
56
      echo "@GNTMASTERUSER@:@GNTMASTERDGROUP@"
57
      ;;
58
    confd)
59
      echo "@GNTCONFDUSER@:@GNTCONFDGROUP@"
60
      ;;
61
    rapi)
62
      echo "@GNTRAPIUSER@:@GNTRAPIGROUP@"
63
      ;;
64
    noded)
65
      echo "@GNTNODEDUSER@:@GNTDAEMONSGROUP@"
66
      ;;
67
    *)
68
      echo "root:@GNTDAEMONSGROUP@"
69
      ;;
70
  esac
71
}
72

    
73
# Checks whether the local machine is part of a cluster
74
check_config() {
75
  local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem
76
  local fname
77

    
78
  for fname in $server_pem; do
79
    if [[ ! -f $fname ]]; then
80
      echo "Missing configuration file $fname" >&2
81
      return 1
82
    fi
83
  done
84

    
85
  return 0
86
}
87

    
88
# Checks the exit code of a daemon
89
check_exitcode() {
90
  if [[ "$#" -lt 1 ]]; then
91
    echo 'Missing exit code.' >&2
92
    return 1
93
  fi
94

    
95
  local rc="$1"; shift
96

    
97
  case "$rc" in
98
    0) ;;
99
    11)
100
      echo "not master"
101
    ;;
102
    *)
103
      echo "exit code $rc"
104
      return 1
105
    ;;
106
  esac
107

    
108
  return 0
109
}
110

    
111
# Prints path to PID file for a daemon.
112
daemon_pidfile() {
113
  if [[ "$#" -lt 1 ]]; then
114
    echo 'Missing daemon name.' >&2
115
    return 1
116
  fi
117

    
118
  local name="$1"; shift
119

    
120
  _daemon_pidfile $name
121
}
122

    
123
# Prints path to daemon executable.
124
daemon_executable() {
125
  if [[ "$#" -lt 1 ]]; then
126
    echo 'Missing daemon name.' >&2
127
    return 1
128
  fi
129

    
130
  local name="$1"; shift
131

    
132
  _daemon_executable $name
133
}
134

    
135
# Prints a list of all daemons in the order in which they should be started
136
list_start_daemons() {
137
  local name
138

    
139
  for name in "${DAEMONS[@]}"; do
140
    echo "$name"
141
  done
142
}
143

    
144
# Prints a list of all daemons in the order in which they should be stopped
145
list_stop_daemons() {
146
  list_start_daemons | tac
147
}
148

    
149
# Checks whether a daemon name is known
150
is_daemon_name() {
151
  if [[ "$#" -lt 1 ]]; then
152
    echo 'Missing daemon name.' >&2
153
    return 1
154
  fi
155

    
156
  local name="$1"; shift
157

    
158
  for i in "${DAEMONS[@]}"; do
159
    if [[ "$i" == "$name" ]]; then
160
      return 0
161
    fi
162
  done
163

    
164
  echo "Unknown daemon name '$name'" >&2
165
  return 1
166
}
167

    
168
# Checks whether daemon is running
169
check() {
170
  if [[ "$#" -lt 1 ]]; then
171
    echo 'Missing daemon name.' >&2
172
    return 1
173
  fi
174

    
175
  local name="$1"; shift
176

    
177
  start-stop-daemon --stop --signal 0 --quiet \
178
    --pidfile $(_daemon_pidfile $name)
179
}
180

    
181
# Starts a daemon
182
start() {
183
  if [[ "$#" -lt 1 ]]; then
184
    echo 'Missing daemon name.' >&2
185
    return 1
186
  fi
187

    
188
  local name="$1"; shift
189

    
190
  # Convert daemon name to uppercase after removing "ganeti-" prefix
191
  local plain_name=${name#ganeti-}
192
  local ucname=$(tr a-z A-Z <<<$plain_name)
193

    
194
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
195
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
196

    
197
  @PKGLIBDIR@/ensure-dirs
198

    
199
  start-stop-daemon --start --quiet --oknodo \
200
    --pidfile $(_daemon_pidfile $name) \
201
    --startas $(_daemon_executable $name) \
202
    --chuid $(_daemon_usergroup $plain_name) \
203
    -- $args "$@"
204
}
205

    
206
# Stops a daemon
207
stop() {
208
  if [[ "$#" -lt 1 ]]; then
209
    echo 'Missing daemon name.' >&2
210
    return 1
211
  fi
212

    
213
  local name="$1"; shift
214

    
215
  start-stop-daemon --stop --quiet --oknodo --retry 30 \
216
    --pidfile $(_daemon_pidfile $name)
217
}
218

    
219
# Starts a daemon if it's not yet running
220
check_and_start() {
221
  local name="$1"
222

    
223
  if ! check $name; then
224
    start $name
225
  fi
226
}
227

    
228
# Starts the master role
229
start_master() {
230
  start ganeti-masterd
231
  start ganeti-rapi
232
}
233

    
234
# Stops the master role
235
stop_master() {
236
  stop ganeti-rapi
237
  stop ganeti-masterd
238
}
239

    
240
# Start all daemons
241
start_all() {
242
  for i in $(list_start_daemons); do
243
    local rc=0
244

    
245
    # Try to start daemon
246
    start $i || rc=$?
247

    
248
    if ! errmsg=$(check_exitcode $rc); then
249
      echo "$errmsg" >&2
250
      return 1
251
    fi
252
  done
253

    
254
  return 0
255
}
256

    
257
# Stop all daemons
258
stop_all() {
259
  for i in $(list_stop_daemons); do
260
    stop $i
261
  done
262
}
263

    
264
# Reloads the SSH keys
265
reload_ssh_keys() {
266
  @RPL_SSH_INITD_SCRIPT@ restart
267
}
268

    
269
if [[ "$#" -lt 1 ]]; then
270
  echo "Usage: $0 <action>" >&2
271
  exit 1
272
fi
273

    
274
orig_action=$1; shift
275

    
276
if [[ "$orig_action" == *_* ]]; then
277
  echo "Command must not contain underscores" >&2
278
  exit 1
279
fi
280

    
281
# Replace all dashes (-) with underlines (_)
282
action=${orig_action//-/_}
283

    
284
# Is it a known function?
285
if ! declare -F "$action" >/dev/null 2>&1; then
286
  echo "Unknown command: $orig_action" >&2
287
  exit 1
288
fi
289

    
290
# Call handler function
291
$action "$@"