Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ 98028e5d

History | View | Annotate | Download (5.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
# 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 a list of all daemons in the order in which they should be started
112
list_start_daemons() {
113
  local name
114

    
115
  for name in "${DAEMONS[@]}"; do
116
    echo "$name"
117
  done
118
}
119

    
120
# Prints a list of all daemons in the order in which they should be stopped
121
list_stop_daemons() {
122
  list_start_daemons | tac
123
}
124

    
125
# Checks whether a daemon name is known
126
is_daemon_name() {
127
  if [[ "$#" -lt 1 ]]; then
128
    echo 'Missing daemon name.' >&2
129
    return 1
130
  fi
131

    
132
  local name="$1"; shift
133

    
134
  for i in "${DAEMONS[@]}"; do
135
    if [[ "$i" == "$name" ]]; then
136
      return 0
137
    fi
138
  done
139

    
140
  echo "Unknown daemon name '$name'" >&2
141
  return 1
142
}
143

    
144
# Checks whether daemon is running
145
check() {
146
  if [[ "$#" -lt 1 ]]; then
147
    echo 'Missing daemon name.' >&2
148
    return 1
149
  fi
150

    
151
  local name="$1"; shift
152

    
153
  start-stop-daemon --stop --signal 0 --quiet \
154
    --pidfile $(_daemon_pidfile $name)
155
}
156

    
157
# Starts a daemon
158
start() {
159
  if [[ "$#" -lt 1 ]]; then
160
    echo 'Missing daemon name.' >&2
161
    return 1
162
  fi
163

    
164
  local name="$1"; shift
165

    
166
  # Convert daemon name to uppercase after removing "ganeti-" prefix
167
  local plain_name=${name#ganeti-}
168
  local ucname=$(tr a-z A-Z <<<$plain_name)
169

    
170
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
171
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
172

    
173
  @PKGLIBDIR@/ensure-dirs
174

    
175
  start-stop-daemon --start --quiet --oknodo \
176
    --pidfile $(_daemon_pidfile $name) \
177
    --startas $(_daemon_executable $name) \
178
    --chuid $(_daemon_usergroup $plain_name) \
179
    -- $args "$@"
180
}
181

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

    
189
  local name="$1"; shift
190

    
191
  start-stop-daemon --stop --quiet --oknodo --retry 30 \
192
    --pidfile $(_daemon_pidfile $name)
193
}
194

    
195
# Starts a daemon if it's not yet running
196
check_and_start() {
197
  local name="$1"
198

    
199
  if ! check $name; then
200
    start $name
201
  fi
202
}
203

    
204
# Starts the master role
205
start_master() {
206
  start ganeti-masterd
207
  start ganeti-rapi
208
}
209

    
210
# Stops the master role
211
stop_master() {
212
  stop ganeti-rapi
213
  stop ganeti-masterd
214
}
215

    
216
# Start all daemons
217
start_all() {
218
  for i in $(list_start_daemons); do
219
    local rc=0
220

    
221
    # Try to start daemon
222
    start $i || rc=$?
223

    
224
    if ! errmsg=$(check_exitcode $rc); then
225
      echo "$errmsg" >&2
226
      return 1
227
    fi
228
  done
229

    
230
  return 0
231
}
232

    
233
# Stop all daemons
234
stop_all() {
235
  for i in $(list_stop_daemons); do
236
    stop $i
237
  done
238
}
239

    
240
# Reloads the SSH keys
241
reload_ssh_keys() {
242
  @RPL_SSH_INITD_SCRIPT@ restart
243
}
244

    
245
if [[ "$#" -lt 1 ]]; then
246
  echo "Usage: $0 <action>" >&2
247
  exit 1
248
fi
249

    
250
orig_action=$1; shift
251

    
252
if [[ "$orig_action" == *_* ]]; then
253
  echo "Command must not contain underscores" >&2
254
  exit 1
255
fi
256

    
257
# Replace all dashes (-) with underlines (_)
258
action=${orig_action//-/_}
259

    
260
# Is it a known function?
261
if ! declare -F "$action" >/dev/null 2>&1; then
262
  echo "Unknown command: $orig_action" >&2
263
  exit 1
264
fi
265

    
266
# Call handler function
267
$action "$@"