summaryrefslogtreecommitdiffstats
path: root/heartbeat/mysql-common.sh
blob: d6b4e3cdf418eb426e22e7fb92287a8d4a347d11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/sh

#######################################################################

# Use runuser if available for SELinux.
if [ -x /sbin/runuser ]; then
	SU=runuser
else
	SU=su
fi

# Attempt to detect a default binary
OCF_RESKEY_binary_default=$(which mysqld_safe 2> /dev/null)
if [ "$OCF_RESKEY_binary_default" = "" ]; then
	OCF_RESKEY_binary_default=$(which safe_mysqld 2> /dev/null)
fi

# Fill in some defaults if no values are specified
HOSTOS=`uname`
if [ "X${HOSTOS}" = "XOpenBSD" ];then
	if [ "$OCF_RESKEY_binary_default" = "" ]; then
		OCF_RESKEY_binary_default="/usr/local/bin/mysqld_safe"
	fi
	OCF_RESKEY_config_default="/etc/my.cnf"
	OCF_RESKEY_datadir_default="/var/mysql"
	OCF_RESKEY_user_default="_mysql"
	OCF_RESKEY_group_default="_mysql"
	OCF_RESKEY_log_default="/var/log/mysqld.log"
	OCF_RESKEY_pid_default="/var/mysql/mysqld.pid"
	OCF_RESKEY_socket_default="/var/run/mysql/mysql.sock"
else
	if [ "$OCF_RESKEY_binary_default" = "" ]; then
		OCF_RESKEY_binary_default="/usr/bin/safe_mysqld"
	fi
	OCF_RESKEY_config_default="/etc/my.cnf"
	OCF_RESKEY_datadir_default="/var/lib/mysql"
	OCF_RESKEY_user_default="mysql"
	OCF_RESKEY_group_default="mysql"
	OCF_RESKEY_log_default="/var/log/mysqld.log"
	OCF_RESKEY_pid_default="/var/run/mysql/mysqld.pid"
	OCF_RESKEY_socket_default="/var/lib/mysql/mysql.sock"
fi
OCF_RESKEY_client_binary_default="mysql"
OCF_RESKEY_test_user_default="root"
OCF_RESKEY_test_table_default="mysql.user"
OCF_RESKEY_test_passwd_default=""
OCF_RESKEY_enable_creation_default=0
OCF_RESKEY_additional_parameters_default=""
OCF_RESKEY_replication_user_default="root"
OCF_RESKEY_replication_passwd_default=""
OCF_RESKEY_replication_port_default="3306"
OCF_RESKEY_replication_require_ssl_default="false"
OCF_RESKEY_replication_master_ssl_ca_default=""
OCF_RESKEY_replication_master_ssl_cert_default=""
OCF_RESKEY_replication_master_ssl_key_default=""
OCF_RESKEY_max_slave_lag_default="3600"
OCF_RESKEY_evict_outdated_slaves_default="false"
OCF_RESKEY_reader_attribute_default="readable"

: ${OCF_RESKEY_binary=${OCF_RESKEY_binary_default}}
MYSQL_BINDIR=`dirname ${OCF_RESKEY_binary}`

: ${OCF_RESKEY_client_binary=${OCF_RESKEY_client_binary_default}}

: ${OCF_RESKEY_config=${OCF_RESKEY_config_default}}
: ${OCF_RESKEY_datadir=${OCF_RESKEY_datadir_default}}

: ${OCF_RESKEY_user=${OCF_RESKEY_user_default}}
: ${OCF_RESKEY_group=${OCF_RESKEY_group_default}}

: ${OCF_RESKEY_log=${OCF_RESKEY_log_default}}
: ${OCF_RESKEY_pid=${OCF_RESKEY_pid_default}}
: ${OCF_RESKEY_socket=${OCF_RESKEY_socket_default}}

: ${OCF_RESKEY_test_user=${OCF_RESKEY_test_user_default}}
: ${OCF_RESKEY_test_table=${OCF_RESKEY_test_table_default}}
: ${OCF_RESKEY_test_passwd=${OCF_RESKEY_test_passwd_default}}

: ${OCF_RESKEY_enable_creation=${OCF_RESKEY_enable_creation_default}}
: ${OCF_RESKEY_additional_parameters=${OCF_RESKEY_additional_parameters_default}}

: ${OCF_RESKEY_replication_user=${OCF_RESKEY_replication_user_default}}
: ${OCF_RESKEY_replication_passwd=${OCF_RESKEY_replication_passwd_default}}
: ${OCF_RESKEY_replication_port=${OCF_RESKEY_replication_port_default}}
: ${OCF_RESKEY_replication_require_ssl=${OCF_RESKEY_replication_require_ssl_default}}
: ${OCF_RESKEY_replication_master_ssl_ca=${OCF_RESKEY_replication_master_ssl_ca_default}}
: ${OCF_RESKEY_replication_master_ssl_cert=${OCF_RESKEY_replication_master_ssl_cert_default}}
: ${OCF_RESKEY_replication_master_ssl_key=${OCF_RESKEY_replication_master_ssl_key_default}}

: ${OCF_RESKEY_max_slave_lag=${OCF_RESKEY_max_slave_lag_default}}
: ${OCF_RESKEY_evict_outdated_slaves=${OCF_RESKEY_evict_outdated_slaves_default}}

: ${OCF_RESKEY_reader_attribute=${OCF_RESKEY_reader_attribute_default}}

#######################################################################
# Convenience variables

MYSQL=$OCF_RESKEY_client_binary
if ocf_is_true "$OCF_RESKEY_replication_require_ssl"; then
  MYSQL_OPTIONS_LOCAL_SSL_OPTIONS="--ssl-mode=REQUIRED"
else
  MYSQL_OPTIONS_LOCAL_SSL_OPTIONS=""
fi
MYSQL_OPTIONS_LOCAL="-S $OCF_RESKEY_socket"
MYSQL_OPTIONS_REPL="$MYSQL_OPTIONS_LOCAL_SSL_OPTIONS $MYSQL_OPTIONS_LOCAL --user=$OCF_RESKEY_replication_user --password=$OCF_RESKEY_replication_passwd"
MYSQL_OPTIONS_TEST="$MYSQL_OPTIONS_LOCAL --user=$OCF_RESKEY_test_user --password=$OCF_RESKEY_test_passwd"
MYSQL_TOO_MANY_CONN_ERR=1040

NODENAME=$(ocf_local_nodename)
CRM_ATTR="${HA_SBIN_DIR}/crm_attribute -N $NODENAME "
INSTANCE_ATTR_NAME=`echo ${OCF_RESOURCE_INSTANCE}| awk -F : '{print $1}'`
CRM_ATTR_REPL_INFO="${HA_SBIN_DIR}/crm_attribute --type crm_config --name ${INSTANCE_ATTR_NAME}_REPL_INFO -s mysql_replication"

#######################################################################

mysql_common_validate()
{

    if ! have_binary "$OCF_RESKEY_binary"; then
        ocf_exit_reason "Setup problem: couldn't find command: $OCF_RESKEY_binary"
        return $OCF_ERR_INSTALLED;
    fi

    if ! have_binary "$OCF_RESKEY_client_binary"; then
        ocf_exit_reason "Setup problem: couldn't find command: $OCF_RESKEY_client_binary"
        return $OCF_ERR_INSTALLED;
    fi

    if [ ! -f $OCF_RESKEY_config ]; then
        ocf_exit_reason "Config $OCF_RESKEY_config doesn't exist";
        return $OCF_ERR_INSTALLED;
    fi

    if [ ! -d $OCF_RESKEY_datadir ]; then
        ocf_exit_reason "Datadir $OCF_RESKEY_datadir doesn't exist";
        return $OCF_ERR_INSTALLED;
    fi

    getent passwd $OCF_RESKEY_user >/dev/null 2>&1
    if [ ! $? -eq 0 ]; then
        ocf_exit_reason "User $OCF_RESKEY_user doesn't exit";
        return $OCF_ERR_INSTALLED;
    fi

    getent group $OCF_RESKEY_group >/dev/null 2>&1
    if [ ! $? -eq 0 ]; then
        ocf_exit_reason "Group $OCF_RESKEY_group doesn't exist";
        return $OCF_ERR_INSTALLED;
    fi

    return $OCF_SUCCESS
}

mysql_common_check_pid() {
    local pid=$1

    if [ -d /proc -a -d /proc/1 ]; then
        [ "u$pid" != "u" -a -d /proc/$pid ]
    else
        kill -s 0 $pid >/dev/null 2>&1
    fi
    return $?
}

mysql_common_status() {
    local loglevel=$1
    local pid=$2
    if [ -z "$pid" ]; then
        if [ ! -e $OCF_RESKEY_pid ]; then
            ocf_log $loglevel "MySQL is not running"
            return $OCF_NOT_RUNNING;
        fi

        pid=`cat $OCF_RESKEY_pid`;
    fi

    mysql_common_check_pid $pid


    if [ $? -eq 0 ]; then
        return $OCF_SUCCESS;
    else
        if [ -e $OCF_RESKEY_pid ]; then
            ocf_log $loglevel "MySQL not running: removing old PID file"
            rm -f $OCF_RESKEY_pid
        fi
        return $OCF_NOT_RUNNING;
    fi
}

mysql_common_prepare_dirs()
{
    local rc

    touch $OCF_RESKEY_log
    chown $OCF_RESKEY_user:$OCF_RESKEY_group $OCF_RESKEY_log
    chmod 0640 $OCF_RESKEY_log
    [ -x /sbin/restorecon ] && /sbin/restorecon $OCF_RESKEY_log

    if ocf_is_true "$OCF_RESKEY_enable_creation" && [ ! -d $OCF_RESKEY_datadir/mysql ] ; then
        ocf_log info "Initializing MySQL database: "
        $MYSQL_BINDIR/mysql_install_db --datadir=$OCF_RESKEY_datadir
        rc=$?
        if [ $rc -ne 0 ] ; then
            ocf_exit_reason "Initialization failed: $rc";
            exit $OCF_ERR_GENERIC
        fi
        chown -R $OCF_RESKEY_user:$OCF_RESKEY_group $OCF_RESKEY_datadir
    fi

    pid_dir=`dirname $OCF_RESKEY_pid`
    if [ ! -d $pid_dir ] ; then
        ocf_log info "Creating PID dir: $pid_dir"
        mkdir -p $pid_dir
        chown $OCF_RESKEY_user:$OCF_RESKEY_group $pid_dir
    fi

    socket_dir=`dirname $OCF_RESKEY_socket`
    if [ ! -d $socket_dir ] ; then
        ocf_log info "Creating socket dir: $socket_dir"
        mkdir -p $socket_dir
        chown $OCF_RESKEY_user:$OCF_RESKEY_group $socket_dir
    fi

    # Regardless of whether we just created the directory or it
    # already existed, check whether it is writable by the configured
    # user
    for dir in $pid_dir $socket_dir $OCF_RESKEY_datadir; do
        if ! $SU -s /bin/sh - $OCF_RESKEY_user -c "test -w $dir"; then
            ocf_exit_reason "Directory $dir is not writable by $OCF_RESKEY_user"
            exit $OCF_ERR_PERM;
        fi
    done
}

mysql_common_start()
{
    local mysql_extra_params="$1"
    local pid

    $SU - $OCF_RESKEY_user -s /bin/sh -c \
    "${OCF_RESKEY_binary} --defaults-file=$OCF_RESKEY_config \
    --pid-file=$OCF_RESKEY_pid \
    --socket=$OCF_RESKEY_socket \
    --datadir=$OCF_RESKEY_datadir \
    --log-error=$OCF_RESKEY_log \
    $OCF_RESKEY_additional_parameters \
    $mysql_extra_params >/dev/null 2>&1" &
    pid=$!

    # Spin waiting for the server to come up.
    # Let the CRM/LRM time us out if required.
    start_wait=1
    while [ $start_wait = 1 ]; do
        if ! ps $pid > /dev/null 2>&1; then
            wait $pid
            ocf_exit_reason "MySQL server failed to start (pid=$pid) (rc=$?). Check $OCF_RESKEY_log for details"
            return $OCF_ERR_GENERIC
        fi
        mysql_common_status info
        rc=$?
        if [ $rc = $OCF_SUCCESS ]; then
            start_wait=0
        elif [ $rc != $OCF_NOT_RUNNING ]; then
            ocf_log info "MySQL start failed: $rc"
            return $rc
        fi
        sleep 2
    done

    return $OCF_SUCCESS
}

mysql_common_stop()
{
    local pid
    local rc

    if [ ! -f $OCF_RESKEY_pid ]; then
        ocf_log info "MySQL is not running"
        return $OCF_SUCCESS
    fi

    pid=`cat $OCF_RESKEY_pid 2> /dev/null `

    mysql_common_check_pid $pid
    if [ $? -ne 0 ]; then
        rm -f $OCF_RESKEY_pid
        ocf_log info "MySQL is already stopped"
        return $OCF_SUCCESS;
    fi

    /bin/kill $pid > /dev/null
    rc=$?
    if [ $rc != 0 ]; then
        ocf_exit_reason "MySQL couldn't be stopped"
        return $OCF_ERR_GENERIC
    fi
    # stop waiting
    shutdown_timeout=15
    if [ -n "$OCF_RESKEY_CRM_meta_timeout" ]; then
        shutdown_timeout=$((($OCF_RESKEY_CRM_meta_timeout/1000)-5))
    fi
    count=0
    while [ $count -lt $shutdown_timeout ]
    do
        mysql_common_status info $pid
        rc=$?
        if [ $rc = $OCF_NOT_RUNNING ]; then
            break
        fi
        count=`expr $count + 1`
        sleep 1
        ocf_log debug "MySQL still hasn't stopped yet. Waiting..."
    done

    mysql_common_status info $pid
    if [ $? != $OCF_NOT_RUNNING ]; then
        ocf_log info "MySQL failed to stop after ${shutdown_timeout}s using SIGTERM. Trying SIGKILL..."
        /bin/kill -KILL $pid > /dev/null
        mysql_common_status info $pid
        if [ $? != $OCF_NOT_RUNNING ]; then
            return $OCF_ERR_GENERIC
        fi
    fi

    ocf_log info "MySQL stopped";
    rm -f /var/lock/subsys/mysqld
    rm -f $OCF_RESKEY_socket
    return $OCF_SUCCESS

}