Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ 46a8da3b

History | View | Annotate | Download (4.6 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
# Checks whether the local machine is part of a cluster
54
check_config() {
55
  local server_pem=@LOCALSTATEDIR@/lib/ganeti/server.pem
56
  local fname
57

    
58
  for fname in $server_pem; do
59
    if [[ ! -f $fname ]]; then
60
      echo "Missing configuration file $fname" >&2
61
      return 1
62
    fi
63
  done
64

    
65
  return 0
66
}
67

    
68
# Checks the exit code of a daemon
69
check_exitcode() {
70
  if [[ "$#" -lt 1 ]]; then
71
    echo 'Missing exit code.' >&2
72
    return 1
73
  fi
74

    
75
  local rc="$1"; shift
76

    
77
  case "$rc" in
78
    0) ;;
79
    11)
80
      echo "not master"
81
    ;;
82
    *)
83
      echo "exit code $rc"
84
      return 1
85
    ;;
86
  esac
87

    
88
  return 0
89
}
90

    
91
# Prints a list of all daemons in the order in which they should be started
92
list_start_daemons() {
93
  local name
94

    
95
  for name in "${DAEMONS[@]}"; do
96
    echo "$name"
97
  done
98
}
99

    
100
# Prints a list of all daemons in the order in which they should be stopped
101
list_stop_daemons() {
102
  list_start_daemons | tac
103
}
104

    
105
# Checks whether a daemon name is known
106
is_daemon_name() {
107
  if [[ "$#" -lt 1 ]]; then
108
    echo 'Missing daemon name.' >&2
109
    return 1
110
  fi
111

    
112
  local name="$1"; shift
113

    
114
  for i in "${DAEMONS[@]}"; do
115
    if [[ "$i" == "$name" ]]; then
116
      return 0
117
    fi
118
  done
119

    
120
  echo "Unknown daemon name '$name'" >&2
121
  return 1
122
}
123

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

    
131
  local name="$1"; shift
132

    
133
  start-stop-daemon --stop --signal 0 --quiet \
134
    --pidfile $(_daemon_pidfile $name)
135
}
136

    
137
# Starts a daemon
138
start() {
139
  if [[ "$#" -lt 1 ]]; then
140
    echo 'Missing daemon name.' >&2
141
    return 1
142
  fi
143

    
144
  local name="$1"; shift
145

    
146
  # Convert daemon name to uppercase after removing "ganeti-" prefix
147
  local ucname=$(echo ${name#ganeti-} | tr a-z A-Z)
148

    
149
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
150
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
151

    
152
  @PKGLIBDIR@/ensure-dirs
153

    
154
  start-stop-daemon --start --quiet --oknodo \
155
    --pidfile $(_daemon_pidfile $name) \
156
    --startas $(_daemon_executable $name) \
157
    -- $args "$@"
158
}
159

    
160
# Stops a daemon
161
stop() {
162
  if [[ "$#" -lt 1 ]]; then
163
    echo 'Missing daemon name.' >&2
164
    return 1
165
  fi
166

    
167
  local name="$1"; shift
168

    
169
  start-stop-daemon --stop --quiet --oknodo --retry 30 \
170
    --pidfile $(_daemon_pidfile $name)
171
}
172

    
173
# Starts a daemon if it's not yet running
174
check_and_start() {
175
  local name="$1"
176

    
177
  if ! check $name; then
178
    start $name
179
  fi
180
}
181

    
182
# Starts the master role
183
start_master() {
184
  start ganeti-masterd
185
  start ganeti-rapi
186
}
187

    
188
# Stops the master role
189
stop_master() {
190
  stop ganeti-rapi
191
  stop ganeti-masterd
192
}
193

    
194
# Start all daemons
195
start_all() {
196
  for i in $(list_start_daemons); do
197
    local rc=0
198

    
199
    # Try to start daemon
200
    start $i || rc=$?
201

    
202
    if ! errmsg=$(check_exitcode $rc); then
203
      echo "$errmsg" >&2
204
      return 1
205
    fi
206
  done
207

    
208
  return 0
209
}
210

    
211
# Stop all daemons
212
stop_all() {
213
  for i in $(list_stop_daemons); do
214
    stop $i
215
  done
216
}
217

    
218
# Reloads the SSH keys
219
reload_ssh_keys() {
220
  @RPL_SSH_INITD_SCRIPT@ restart
221
}
222

    
223
if [[ "$#" -lt 1 ]]; then
224
  echo "Usage: $0 <action>" >&2
225
  exit 1
226
fi
227

    
228
orig_action=$1; shift
229

    
230
if [[ "$orig_action" == *_* ]]; then
231
  echo "Command must not contain underscores" >&2
232
  exit 1
233
fi
234

    
235
# Replace all dashes (-) with underlines (_)
236
action=${orig_action//-/_}
237

    
238
# Is it a known function?
239
if ! declare -F "$action" >/dev/null 2>&1; then
240
  echo "Unknown command: $orig_action" >&2
241
  exit 1
242
fi
243

    
244
# Call handler function
245
$action "$@"