Your IP : 18.118.154.104


Current Path : /opt/webdir/bin/
Upload File :
Current File : //opt/webdir/bin/bx-mysql

#!/usr/bin/perl
# start ansible playbook for:
# 1. add slave server
# 2. remove slave server
# 3. change master
# 4. view server status
# this script is wrapper for ansible playbook ( we need json output for web-interface)
use strict;
use warnings;

use lib "/opt/webdir/lib";
use bxMysql;
use Output;
use Pool;
use Data::Dumper;
use Getopt::Long;
use File::Basename qw( dirname basename );
use Pod::Usage;

# program options
my $prog_name = basename $0;
my $prog_dir  = dirname $0;

my $o_action = "list";    # type of action that script must do.
                          # status - status the server: role: options
                          # list   - serverList
                          # master - change master
                          # slave  - create slave
                          # delete - delete mysql
                          # create - create config file
my $o_config = '/etc/ansible/group_vars/bitrix-mysql.yml';
my $options  = {
    cluster_login    => 'bx_clusteruser',
    cluster_password => undef,
    replica_login    => 'bx_repluser',
    replica_password => undef,
    slave_server     => undef,
    password_file    => undef,
};
my $o_mysql_server = undef;      # ip address or hostname for mysql server
my $o_format       = 'plain';    # format of stdout message
my $o_verbose      = 0;
my $o_help         = undef;
my $o_man          = undef;

# get command line options
Getopt::Long::Configure("bundling");
my $result_option = GetOptions(
    'v'                  => \$o_verbose,
    'verbose'            => \$o_verbose,
    'h'                  => \$o_help,
    'help'               => \$o_help,
    "a:s"                => \$o_action,
    'action:s'           => \$o_action,
    "s:s"                => \$o_mysql_server,
    'server:s'           => \$o_mysql_server,
    "c:s"                => \$o_config,
    'config:s'           => \$o_config,
    'cluster_login:s'    => \$options->{'cluster_login'},
    'cluster_password:s' => \$options->{'cluster_password'},
    'replica_login:s'    => \$options->{'replica_login'},
    'replica_password:s' => \$options->{'replica_password'},
    'password_file:s'    => \$options->{'password_file'},
    "o:s"                => \$o_format,
    'output'             => \$o_format,
    'man'                => \$o_man,
    'm'                  => \$o_man,
) or pod2usage(2);

# help message
pod2usage(1) if ( $o_help || $o_format !~ /^(json|plain|te?xt)$/ );
# manual message
pod2usage(-exitval => 0, -verbose => 2) if $o_man;


if ( $o_format =~ /^te?xt$/ ) { $o_format = "plain" }

# process request
my $confPool = bxMysql->new( cmd => $options );
my $confMysql = undef;
if ( $o_action eq "status" ) {
    $confMysql = $confPool->serverOptions($o_mysql_server);
}
elsif ( $o_action eq "list" ) {
    $confMysql = $confPool->serverList();
}
elsif ( $o_action eq "slave" ) {
    $confMysql = $confPool->slave($o_mysql_server);
}
elsif ( $o_action eq "master" ) {
    $confMysql = $confPool->master($o_mysql_server);
}
elsif ( $o_action eq "remove" ) {
    $confMysql = $confPool->remove($o_mysql_server);
}
elsif ( $o_action eq "update" ) {
    $confMysql = $confPool->update();
}
elsif ( $o_action =~ /^(change_password|client_config)$/ ) {
    $confMysql = $confPool->password( $o_mysql_server, $1 );
}
elsif ( $o_action =~ /^(stop_service|start_service)$/ ) {
    $confMysql = $confPool->manage( $o_mysql_server, $1 );
}
else {
    $confMysql = Output->new(
        error   => 1,
        message => "Unknown action option. PLease use -h for help message."
    );
}
$confMysql->print($o_format);
exit;

__END__
=pod
=head1 NAME

    bx-mysql - Managing MySQL servers in the Bitrix pool

=head1 SYNOPSIS

    bx-mysql --help|-h
    bx-mysql [--verbose|-v] [--output|-o json|plain] [-a status|list] \
        [-s|--server pool_hostname]
    bx-mysql [--verbose|-v] [--output|-o json|plain] [-a master|slave|remove] \
        [-s|--server pool_hostname] \
        [--cluster_login cluster_login --cluster_password cluster_password] \
        [--replica_login replica_login --replica_password replica_password]
    bx-mysql [--verbose|-v] [--output|-o json|plain] [-a update|change_password|client_config] \
        [-s|--server pool_hostname] \
        [--password_file /path/to/file]
    bx-mysql [--verbose\-v] [--output|-o json|plain] [-a stop_service|start_service] \
        [-s|--server pool_hostname]

=head1 OPTIONS

=over 8

=item B<--help|-h>
    
    Print a brief help message and exits.

=item B<--man|-m>
    
    Prints the manual page and exits.

=item B<--verbose|-v>

    Enable verbose/debug output.

=item B<--server|-s>

    Server name in the Bitrix pool.

=item B<--action|-a>

    Available options for actions in the Bitrix pool.

=item B<--password_file>
    
    File with MySQL password. Mandatory option for changing root password.

=item B<--cluster_login and --cluster_password>

    MySQL login and password that used by Bitrix cluster module.

=item B<--replica_login and --replica_password>

    MySQL login and password that used by MySQL replication process. 

=back

=head2 ACTIONS

=over 7

=item B<status>
    
    Get mysql status in a specific server.

    bx-mysql -a status --server pool_hostname

=item B<list>

    Get mysql status for all mysql servers in the Bitrix pool.

    bx-mysql -a list

=item B<slave>

    Create MySQL replication on the specified pool server.
    
    bx-mysql -a slave --server pool_hostname \
        --cluster_login cluster_login --cluster_password cluster_password \
        --replica_login replica_login --replica_password replica_password


=item B<master>

    Change MySQL master.

    bx-mysql -a master --server pool_hostname

=item B<remove>

    Delete MySQL service on the specified pool server.
    Only slave-server can be deleted.

    bx-mysql -a remove --server pool_hostname
 
=item B<change_password>

    Update MySQL root password.
    Password value is saved in password file, 
    it will be deleted upfter update process.

    bx-mysql -a change_password --server pool_hostname \
        --password_file /path/to/password_file

=item B<stop_service|start_service>

    Start and stop MySQL service on the specified pool server

    bx-mysql -a stop_service --server pool_hostname
    
=back

=head1 EXAMPLES


=head1 DESCRIPTION

    The script <bx-mysql> is used to manage, create and delete MySQL servers.
    It allows you to get the current settings for the MySQL service, 
    as well as take a number of steps to configure it: 
    change the password, update the configuration, and restart.

 =cut