Statistics
| Branch: | Tag: | Revision:

root / daemons / daemon-util.in @ 10c5c1c3

History | View | Annotate | Download (3.3 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 the exit code of a daemon
55
check_exitcode() {
56
  if [[ "$#" -lt 1 ]]; then
57
    echo 'Missing exit code.' >&2
58
    return 1
59
  fi
60

    
61
  local rc="$1"; shift
62

    
63
  case "$rc" in
64
    0) ;;
65
    11)
66
      echo "not master"
67
    ;;
68
    *)
69
      echo "exit code $rc"
70
      return 1
71
    ;;
72
  esac
73

    
74
  return 0
75
}
76

    
77
# Checks whether daemon is running
78
check() {
79
  if [[ "$#" -lt 1 ]]; then
80
    echo 'Missing daemon name.' >&2
81
    exit 1
82
  fi
83

    
84
  local name="$1"; shift
85

    
86
  start-stop-daemon --stop --signal 0 --quiet \
87
    --pidfile $(_daemon_pidfile $name)
88
}
89

    
90
# Starts a daemon
91
start() {
92
  if [[ "$#" -lt 1 ]]; then
93
    echo 'Missing daemon name.' >&2
94
    exit 1
95
  fi
96

    
97
  local name="$1"; shift
98

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

    
102
  # Read $<daemon>_ARGS and $EXTRA_<daemon>_ARGS
103
  eval local args="\"\$${ucname}_ARGS \$EXTRA_${ucname}_ARGS\""
104

    
105
  start-stop-daemon --start --quiet --oknodo \
106
    --pidfile $(_daemon_pidfile $name) \
107
    --startas "@PREFIX@/sbin/$name" \
108
    -- $args "$@"
109
}
110

    
111
# Stops a daemon
112
stop() {
113
  if [[ "$#" -lt 1 ]]; then
114
    echo 'Missing daemon name.' >&2
115
    exit 1
116
  fi
117

    
118
  local name="$1"; shift
119

    
120
  start-stop-daemon --stop --quiet --oknodo --retry 30 \
121
    --pidfile $(_daemon_pidfile $name)
122
}
123

    
124
# Starts a daemon if it's not yet running
125
check_and_start() {
126
  local name="$1"
127

    
128
  if ! check $name; then
129
    start $name
130
  fi
131
}
132

    
133
# Starts the master role
134
start_master() {
135
  start ganeti-masterd
136
  start ganeti-rapi
137
}
138

    
139
# Stops the master role
140
stop_master() {
141
  stop ganeti-rapi
142
  stop ganeti-masterd
143
}
144

    
145
# Reloads the SSH keys
146
reload_ssh_keys() {
147
  @RPL_SSH_INITD_SCRIPT@ restart
148
}
149

    
150
if [[ "$#" -lt 1 ]]; then
151
  echo "Usage: $0 <action>" >&2
152
  exit 1
153
fi
154

    
155
orig_action=$1; shift
156

    
157
if [[ "$orig_action" == *_* ]]; then
158
  echo "Command must not contain underscores" >&2
159
  exit 1
160
fi
161

    
162
# Replace all dashes (-) with underlines (_)
163
action=${orig_action//-/_}
164

    
165
# Is it a known function?
166
if ! declare -F "$action" >/dev/null 2>&1; then
167
  echo "Unknown command: $orig_action" >&2
168
  exit 1
169
fi
170

    
171
# Call handler function
172
$action "$@"