Your IP : 18.226.177.203
#!/usr/bin/perl
# manage configuration for bitrix pool of servers
# create new, add hosts and soo on. Try use -h|--help for detail help message
use strict;
use warnings;
# WebDir methods live here
use lib "/opt/webdir/lib";
use Pool;
use Host;
use SSHAuthUser;
use Output;
use Pod::Usage;
# additional modules
use Data::Dumper;
use Getopt::Long;
use File::Basename qw( dirname basename );
# program options
my $prog_name = basename $0;
my $prog_dir = dirname $0;
# command line options
my $o_format = 'plain'; # format of stdout message
my $o_help = undef; # print help message
my $o_verbose = 0; # enable verbose mode
my $o_hostname = undef; # hostname for add|del|view action
my $o_ipaddres = undef; # ip address for add|del|viw action
my $o_group = undef; # group name for add action on host
my $o_action = 'view'; # type of action that script must do.
# add - add host to configuration
# view - view current config for all host or defined
# create - create empty default configuration for father use
# key - get /path/to/ssh/key for ssh connections to hosts in pool
# pw - password change on remote host for user root
# copy - copy sshkey to remote host
# timezone- change timezone for servers in the pool
# monitor-(enable|disable|status) - enable|disable and get status for monitoring
my $o_login = 'root'; # login for ssh connect
my $o_oldpass = undef; # current password for user $l_login
my $o_newpass =
undef; # new password for user $l_login ( usage when password will be change)
my $o_sshkey = undef; # ssh key
my $o_int = undef;
my $o_tz_php = 0; # update timezone for php or not
my $o_tz_name = 'Europe/Moscow'; # timezone name
my $o_host_id = undef; # host_id, used while updated network settings
my $o_update_log = undef; # update log path
my $o_bitrix_type = 'general';
my $o_new_hostname = undef;
my $o_man = undef;
# debug
my $cmd_options = join( ' ', @ARGV );
my $cmd_output = Output->new(
error => 0,
logfile => '/opt/webdir/logs/wrapper.log',
);
$cmd_output->log_data("start script with options: $cmd_options");
# get command line options
Getopt::Long::Configure("bundling");
my $result_option = GetOptions(
'v' => \$o_verbose,
'verbose' => \$o_verbose,
'h' => \$o_help,
'help' => \$o_help,
'o:s' => \$o_format,
'ouput:s' => \$o_format,
'H:s' => \$o_hostname,
'host:s' => \$o_hostname,
'i:s' => \$o_ipaddres,
'ip:s' => \$o_ipaddres,
'g:s' => \$o_group,
'group:s' => \$o_group,
"a:s" => \$o_action,
'action:s' => \$o_action,
"u:s" => \$o_login,
'user:s' => \$o_login,
"p:s" => \$o_oldpass,
'pass:s' => \$o_oldpass,
'P:s' => \$o_newpass,
'new:s' => \$o_newpass,
'k:s' => \$o_sshkey,
'key:s' => \$o_sshkey,
'I:s' => \$o_int,
'interface:s' => \$o_int,
't:s' => \$o_tz_name,
'timezone:s' => \$o_tz_name,
'php' => \$o_tz_php,
'host_id:s' => \$o_host_id,
'update_log:s' => \$o_update_log,
'bitrix_type:s' => \$o_bitrix_type,
'hostname:s' => \$o_new_hostname,
'man' => \$o_man,
'm' => \$o_man,
) or unknown_arg();
# help message
pod2usage(1) if ( $o_help || $o_format !~ /^(json|plain|te?xt)$/ );
# manual message
pod2usage(-exitval => 0, -verbose => 2) if $o_man;
# formt output
if ( $o_format =~ /^te?xt$/ ) { $o_format = "plain" }
# process request
########################## pool configuration
my $p = Pool->new(
debug => $o_verbose,
bitrix_type => $o_bitrix_type,
);
my $output;
# Pool operations
if ( $o_action =~ /^(view|status)$/i ) {
$output = $p->get_ansible_data( ($o_hostname) ? $o_hostname : $o_ipaddres );
}
elsif ( $o_action eq 'create' ) {
$output = $p->create_new_pool( $o_hostname, $o_int, $o_ipaddres );
}
elsif ( $o_action eq 'delete_pool' ) {
$output = $p->delete_pool();
}
elsif ( $o_action eq 'forget_host' ) {
$output = $p->forget_host($o_hostname);
}
elsif ( $o_action eq 'change_hostname' ) {
$output = $p->change_hostname( $o_hostname, $o_new_hostname );
}
elsif ( $o_action eq 'change_ip' ) {
$output = $p->update_network( $o_hostname, $o_ipaddres );
}
elsif ( $o_action eq 'update_network' ) {
$output = $p->UpdateHostNetwork( $o_host_id, $o_ipaddres );
}
elsif ( $o_action eq 'check_network' ) {
$output = $p->TestHostNetwork($o_update_log);
}
elsif ( $o_action eq 'enable_beta_version' ){
$output = $p->beta_version('enable');
}
elsif ( $o_action eq 'disable_beta_version' ){
$output = $p->beta_version('disable');
}
elsif ( $o_action =~ /^(bx_update|bx_upgrade)$/ ) {
$output = $p->update_pool( $o_hostname, $1 );
}
elsif ( $o_action =~ /^bx_passwd$/ ) {
$output = $p->password_on_server( $o_hostname, $o_login, $o_newpass );
}
elsif ( $o_action =~ /^bx_reboot$/ ) {
$output = $p->reboot_server($o_hostname);
}
elsif ( $o_action =~ /^timezone$/ ) {
$output = $p->timezone_in_the_pool( $o_tz_name, $o_tz_php );
}
elsif ( $o_action =~ /^(sshkey|key)$/ ) {
$output = $p->get_ssh_key();
}
# Inventory operations
elsif ( $o_action =~
/^(bx_php_upgrade|bx_php_upgrade_php56|bx_php_rollback_php[78][01234]?|bx_php_upgrade_php[78][01234]?)$/
)
{
use bxInventory;
my $inventory = bxInventory->new( debug => $o_verbose );
$output = $inventory->update_php($o_action, $o_hostname);
}
elsif ( $o_action =~ /^(bx_upgrade_mysql57|bx_upgrade_mysql80)$/ ) {
use bxInventory;
my $inventory = bxInventory->new( debug => $o_verbose );
$output = $inventory->update_mysql($o_action, $o_hostname);
# Host operations
}
elsif ( $o_action eq 'add' ) {
# the parameter corresponds to the identifier in the inventory
# my $get_host_ident = $self->get_inventory_hostname($host);
# return $get_host_ident if ( $get_host_ident->is_error );
# $host_ident = $get_host_ident->data->[1];
my $h = Host->new( host => $o_hostname, ip => $o_ipaddres );
if ( not defined $o_group ) {
$output = $h->createHost();
}
else {
$output = $h->add_to_group($o_group);
}
}
elsif ( $o_action eq 'del' ) {
my $get_hi = $p->get_inventory_hostname($o_hostname);
if ( $get_hi->is_error ) {
$output = $get_hi;
}
else {
my $h = Host->new( host => $get_hi->data->[1] );
if ( not defined $o_group ) {
$output = $h->removeHostFromPool();
}
else {
$output = $h->del_from_group($o_group);
}
}
}
elsif ( $o_action eq 'bx_info' ) {
my $get_hi = $p->get_inventory_hostname($o_hostname);
if ( $get_hi->is_error ) {
$output = $get_hi;
}
else {
my $h = Host->new( host => $get_hi->data->[1] );
$output = $h->get_bx_info();
}
}
elsif ( $o_action eq 'dbs_list' ) {
my $get_hi = $p->get_inventory_hostname($o_hostname);
if ( $get_hi->is_error ) {
$output = $get_hi;
}
else {
my $h = Host->new( host => $get_hi->data->[1] );
$output = $h->get_bx_dbs();
}
# SSH options
}
elsif ( $o_action eq 'copy' ) {
if ( !( $o_sshkey && $o_ipaddres && $o_oldpass ) ) {
$output = Output->new(
error => 1,
message => "Mandatory option is missing."
);
}
else {
my $sshAuth = SSHAuthUser->new(
sship => $o_ipaddres,
sshkey => $o_sshkey,
oldpass => $o_oldpass,
);
$output = $sshAuth->copy_ssh_key();
}
}
elsif ( $o_action =~ /^pw$/ ) {
if ( !( $o_ipaddres && $o_oldpass && $o_newpass ) ) {
$output = Output->new(
error => 1,
message => "Mandatory option is missing."
);
}
else {
my $sshAuth = SSHAuthUser->new(
sship => $o_ipaddres,
newpass => $o_newpass,
oldpass => $o_oldpass,
);
$output = $sshAuth->change_user_pass();
}
}
else {
$output = Output->new(
error => 1,
message => "Unknown action option. PLease use -h for help message.",
);
}
$output->print($o_format);
exit;
__END__
=pod
=head1 NAME
wrapper_ansible_conf - Managing MySQL servers in the Bitrix pool
=head1 SYNOPSIS
wrapper_ansible_conf --help|-h
wrapper_ansible_conf [--verbose|-v] [--output|-o json|plain] \
[-a status|view|bx_info] \
[-H|--host pool_hostname]
wrapper_ansible_conf [--verbose|-v] [--output|-o json|plain] \
[-a create|delete_pool|add|del|forget_host] \
[-H|--host pool_hostname] [-I|--interface interface_name]
wrapper_ansible_conf [--verbose|-v] [--output|-o json|plain] \
[-a change_hostname|change_ip|timezone] \
[-H|--host pool_hostname]
wrapper_ansible_conf [--verbose\-v] [--output|-o json|plain] \
[-a check_network|update_network|bx_reboot|bx_password|bx_update|bx_upgrade] \
[-H|--host pool_hostname]
wrapper_ansible_conf [--verbose\-v] [--output|-o json|plain] \
[-a enable_beta_version|disable_beta_version]
wrapper_ansible_conf [--verbose\-v] [--output|-o json|plain] \
[-a sshkey|key] \
[-H|--host pool_hostname]
wrapper_ansible_conf [--verbose\-v] [--output|-o json|plain] \
[-a bx_php_upgrade|bx_php_upgrade_php56|bx_php_rollback_php7[01234]|bx_php_upgrade_php7[01234]] \
[-H|--host pool_hostname]
wrapper_ansible_conf [--verbose\-v] [--output|-o json|plain] \
[-a bx_upgrade_mysql57|bx_upgrade_mysql8]
[-H|--host pool_hostname]
=head1 OPTIONS
=over 15
=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<-H|--host>
Server name in the Bitrix pool.
=item B<--action|-a>
Available options for actions in the Bitrix pool.
=item B<-g|--group pool_group>
Define the pool group name. Ex. memcached or mysql.
=item B<-i|--ip ipv4>
Define IPv4 adress that will be used in action.
=item B<-u|--user username>
Define user login.
=item B<-p|--pass password_string>
Define user password
=item B<-P|--new new_password_string>
Define user new password
=item B<-k|--key /path/to/ssh/key>
Define user OpenSHH key
=item B<-I|--interface interface_name>
Define network interface name
=item B<-t|--timezone timezone>
Define timezone name
=item B<--host_id pool_host_id>
Secure host identifier that used when IP address is changed
=item B<--hostname hostname>
New hostname for the pool server
=back
=head2 ACTIONS
=over 18
=item B<view|status>
Get brief information about all servers or one server in the Bitrix pool.
wrapper_ansible_conf -a status --host pool_hostname
wrapper_ansible_conf -a status -o json
=item B<bx_info>
Get all information about one server in the Bitrix pool.
wrapper_ansible_conf -a bx_info --host pool_hostname
=item B<create>
Create Bitrix pool on current host.
wrapper_ansible_conf -a create --host pool_hostname \
--interface interface_name
=item B<delete_pool>
Remove Bitrix pool on current host.
wrapper_ansible_conf -a delete_pool
=item B<forget_host>
Delete the server from Bitrix pool without updating configuration on it.
This option is used when the server is not available.
wrapper_ansible_conf -a forget_host --host pool_hostname
=item B<change_hostname>
Change hostname.
This action changes the host name, but does not change its identifier in the Bitrix pool.
wrapper_ansible_conf -a change_hostname --host pool_hostname
--hostname new_hostname
=item B<change_ip>
Change IP address for server in the Bitrix pool,
that used for connection to the server.
Update ansible configuration.
wrapper_ansible_conf -a change_ip --host pool_hostname \
--ip new_ipv4
=item B<update_network>
This action allows to client inform about the need
to update the IP address in the pool for the specified server
Update ansible configuration.
Host Id is defined in /etc/ansible/ansible-roles on client.
wrapper_ansible_conf -a update_network --host_id host_id \
--ip new_ipv4
=item B<check_network>
This action causes the wizard to check for network change requests.
wrapper_ansible_conf -a check_network
=item B<enable_beta_version|disable_beta_version>
Enable or disable Bitrix beta version on the server pool.
wrapper_ansible_conf -a enable_beta_version|disable_beta_version
=item B<bx_update|bx_upgrade>
Update all packages(bx_upgrade) or bitrix-packages(bx_update)
on servers in the Bitrix pool.
wrapper_ansible_conf -a bx_upgrade [--host pool_hostname]
=item B<bx_passwd>
Change password for defined user
wrapper_ansible_conf -a bx_passwd --host pool_hostname \
--user username
=item B<bx_reboot>
Reboot server
wrapper_ansible_conf -a bx_reboot --host pool_hostname
=item B<timezone>
Change timezone for all servers in the pool.
wrapper_ansible_conf -a timezone \
--timezone timezone_name [--php]
=item B<sshkey|key>
Get current OpenSSH key path.
wrapper_ansible_conf -a key
=item B<bx_php_upgrade*|bx_upgrade_mysql*>
Upgrade php or/and mysql version on the server
wrapper_ansible_conf -a bx_upgrade_mysql57
=item B<add|del>
Add or delete host to the Bitrix group.
If the host configuration is not in the Bitrix pool, it will be created.
wrapper_ansible_conf -a add --host pool_hostname --ip ip_address
=item B<copy>
Copy OpenSHH key to the server
wrapper_ansible_conf -a copy --key /path/to/file \
--ip ip_address --pass ROOT_PASSWORD
=item B<pw>
Update root password on the server
wrapper_ansible_conf -a pw --pass CURRENT_PASSWORD \
--new NEW_PASSWORD --ip ip_address
=back
=head1 EXAMPLES
=head1 DESCRIPTION
The script <wrapper_ansible_conf> is used to manage servers in the Bitrix Pool.
It allows you to get the current settings for main configuration like hostname,
network settings; update software like php and mysql; and manage configuration.
=cut