master_ip_failover

‘#!/usr/bin/env perl

‘# Copyright (C) 2011 DeNA Co.,Ltd.
‘#
‘# This program is free software; you can redistribute it and/or modify
‘# it under the terms of the GNU General Public License as published by:
‘# the Free Software Foundation; either version 2 of the License, or
‘# (at your option) any later version.
‘#
‘# This program is distributed in the hope that it will be useful,
‘# but WITHOUT ANY WARRANTY; without even the implied warranty of
‘# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
‘# GNU General Public License for more details.
‘# You should have received a copy of the GNU General Public License
‘# along with this program; if not, write to the Free Software
‘# Foundation, Inc.,
‘# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

‘## Note: This is a sample script and is not complete. Modify the script based on your environment.
use strict;
use warnings FATAL => ‘all’;

use Getopt::Long;
use MHA::DBHelper;

my (
c o m m a n d , ssh_user, o r i g m a s t e r h o s t , orig_master_ip, o r i g m a s t e r p o r t , new_master_host,
n e w m a s t e r i p , new_master_port, n e w m a s t e r u s e r , new_master_password
);

my vip = ‘192.168.4.60/24’;  # Virtual IP   my key = “1”;
my s s h s t a r t v i p =" / s b i n / i f c o n f i g e t h 0 : key v i p ; m y ssh_stop_vip = "/sbin/ifconfig eth0:$key down”;

GetOptions(
‘command=s’ => $command,
‘ssh_user=s’ => $ssh_user,
‘orig_master_host=s’ => $orig_master_host,
‘orig_master_ip=s’ => $orig_master_ip,
‘orig_master_port=i’ => $orig_master_port,
‘new_master_host=s’ => $new_master_host,
‘new_master_ip=s’ => $new_master_ip,
‘new_master_port=i’ => $new_master_port,
‘new_master_user=s’ => $new_master_user,
‘new_master_password=s’ => $new_master_password,
);

exit &main();

sub main {
if ( c o m m a n d e q " s t o p " | | command eq “stopssh” ) {

# $orig_master_host, $orig_master_ip, $orig_master_port are passed.
# If you manage master ip address at global catalog database,
# invalidate orig_master_ip here.
my $exit_code = 1;
eval {

  # updating global catalog, etc
  &stop_vip();
  $exit_code = 0;
};
if ($@) {
  warn "Got Error: $@\n";
  exit $exit_code;
}
exit $exit_code;

}
elsif ( $command eq “start” ) {

# all arguments are passed.
# If you manage master ip address at global catalog database,
# activate new_master_ip here.
# You can also grant write access (create user, set read_only=0, etc) here.
my $exit_code = 10;
eval {
  my $new_master_handler = new MHA::DBHelper();

  # args: hostname, port, user, password, raise_error_or_not
  $new_master_handler->connect( $new_master_ip, $new_master_port,
    $new_master_user, $new_master_password, 1 );

  ## Set read_only=0 on the new master
  $new_master_handler->disable_log_bin_local();
  print "Set read_only=0 on the new master.\n";
  $new_master_handler->disable_read_only();

  ## Creating an app user on the new master
  print "Creating app user on the new master..\n";
  $new_master_handler->enable_log_bin_local();
  $new_master_handler->disconnect();

  ## Update master ip on the catalog database, etc
  &start_vip();
  $exit_code = 0;
};
if ($@) {
  warn $@;

  # If you want to continue failover, exit 10.
  exit $exit_code;
}
exit $exit_code;

}
elsif ( $command eq “status” ) {

# do nothing
exit 0;

}
else {
&usage();
exit 1;
}
}
sub start_vip() {
ssh $ssh_user\@$new_master_host \" $ssh_start_vip \";
}
sub stop_vip() {
return 0 unless ($ssh_user);
ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \";
}

sub usage {
print
“Usage: master_ip_failover –command=start|stop|stopssh|status –orig_master_host=host –orig_master_ip=ip –orig_master_port=port –new_master_host=host –new_master_ip=ip –new_master_port=port\n”;
}

猜你喜欢

转载自blog.csdn.net/weixin_42972553/article/details/82498509
今日推荐