#!/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;

# 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

# 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,
) or unknown_arg();

# help message
if ( $o_help ) { print_help($prog_name, 0) };
# formt output
if ( $o_format !~ /^(json|plain|te?xt)$/ ) { print_help($prog_name, 1)}
if ( $o_format =~ /^te?xt$/ ) { $o_format = "plain" };

# process request
my $confPool = Pool->new(debug => $o_verbose);

# process default action view configuration of pool
if ( $o_action =~ /^(view|status)$/i ){
  # user can specify one host for view
  my $view_host = 'all';
  if ( $o_hostname ) { $view_host = $o_hostname };
  if ( $o_ipaddres ) { $view_host = $o_hostname };
  my $confOutput = $confPool->get_ansible_data( $view_host );
  $confOutput->print($o_format);
};

# create new pool configuration with sshkey and one host(localhost)
if ( $o_action =~ /^create$/i ){
  my $createStatus = $confPool->create_new_pool($o_hostname,$o_int);
  $createStatus->print($o_format);
};

# update host connection ip address in the pool
if ($o_action =~ /update_network$/i){
  if (!($o_host_id || $o_ipaddres)){
    my $mess_host = ($o_host_id)? $o_host_id : "not_defined";
    my $mess_ip   = ($o_ipaddres)? $o_ipaddres : "not_defined";

    my $rtn = Output->new(
      error => 1,
      message => "Mandatory option is missing: host_ip=$mess_host ip=$mess_ip"
    );
    $rtn->print($o_format);
  }
  my $updatePool = $confPool->UpdateHostNetwork($o_host_id, $o_ipaddres);
  $updatePool->print($o_format);
}

# update host connection ip address in the pool
if ($o_action =~ /check_network$/i){
  my $updatePool = $confPool->TestHostNetwork($o_update_log);
  $updatePool->print($o_format);
}

# add|delete host to|from the pool ( optinal you can define group for add|delete)
if ( $o_action =~  /^(add|del)$/ ){
  if ( !( $o_hostname || $o_ipaddres ) ){
    my $mess_host = ($o_hostname)? $o_hostname : "not_defined";
    my $mess_ip   = ($o_ipaddres)? $o_ipaddres : "not_defined";
    my $rtn = Output->new( 
      error => 1, 
      message => "Mandatory option is missing: hostname=$mess_host netaddr=$mess_ip" 
    );
    $rtn->print( $o_format );
  }

  my $hostInPool;
  if ( defined $o_hostname && defined $o_ipaddres ){
    $hostInPool = Host->new( host => $o_hostname, ip => $o_ipaddres );
  }else{
    $hostInPool = Host->new( host => $o_hostname )
  }
  if ( $o_action =~ /^add$/ && not defined $o_group ) {

    my $hostAddToGroup = $hostInPool->createHost();
    $hostAddToGroup->print($o_format);

  }elsif ( $o_action =~ /^add$/ && $o_group ){
    my $hostAddToGroup = $hostInPool->add_to_group($o_group);
    $hostAddToGroup->print($o_format);
  }else{
    my $hostDelFromGroup = $hostInPool->del_from_group($o_group);
    $hostDelFromGroup->print($o_format);
  }
};


if ( $o_action =~ /^bx_info$/ ){
  if (!$o_hostname){
    my $rtn = Output->new( 
      error => 1, 
      message => "Mandatory option is missing: hostname=not_defined action=bx_info" 
    );
    $rtn->print( $o_format );
  }

  my $hostInPool = Host->new( host => $o_hostname);
  my $hostRequest = $hostInPool->get_bx_info();
  $hostRequest->print($o_format);
}

if ( $o_action =~ /^dbs_list$/ ){
  if (!$o_hostname){
    my $rtn = Output->new( 
      error => 1, 
      message => "Mandatory option is missing: hostname=not_defined action=dbs_list" );
    $rtn->print( $o_format );
  }

  my $hostInPool = Host->new( host => $o_hostname);
  my $hostRequest = $hostInPool->get_bx_dbs();
  $hostRequest->print($o_format);
}


if ( $o_action =~ /^bx_update$/ ){
  my $updateRequest = $confPool->update_pool($o_hostname);
  $updateRequest->print($o_format);
}

if ( $o_action =~ /^bx_php_upgrade$/ ){
  use bxInventory;
  my $inventory = bxInventory->new(debug => $o_verbose);
  my $updateRequest = $inventory->update_php();
  $updateRequest->print($o_format);
}

if ( $o_action =~ /^bx_passwd$/ ){
  my $passwordRequest = $confPool->password_on_server($o_hostname,$o_login,$o_newpass);
  $passwordRequest->print($o_format);
}


if ( $o_action =~ /^bx_reboot$/ ){
  my $updateRequest = $confPool->reboot_server($o_hostname);
  $updateRequest->print($o_format);
}

if ( $o_action =~ /^timezone$/ ){
  my $updateRequest = $confPool->timezone_in_the_pool($o_tz_name,$o_tz_php);
  $updateRequest->print($o_format);
}


# get path to sshkey that used for connection
if ( $o_action =~ /^(sshkey|key)$/ ){
  my $confOutput = $confPool->get_ssh_key();
  $confOutput->print($o_format);
};

# copy ssh key to auth file for root user
if ( $o_action =~ /^copy$/ ){
  if ( !( $o_sshkey && $o_ipaddres && $o_oldpass ) ){ 
    my $mess_ssh = ($o_sshkey)? $o_sshkey : "not_defined";
    my $mess_net = ($o_ipaddres)? $o_ipaddres : "not_defined";
    my $mess_pas = ($o_oldpass)? "****************" : "not_defined";

    my $rtn = Output->new( 
      error => 1, 
      message => "Mandatory option is missing: ssh_key=$mess_ssh ssh_net=$mess_net ssh_pas=$mess_pas" 
    );
    $rtn->print($o_format);
  }
  my $sshAuth = SSHAuthUser->new(
    sship   => $o_ipaddres,
    sshkey  => $o_sshkey,
    oldpass => $o_oldpass,
  );

  my $rtn = $sshAuth->copy_ssh_key();
  $rtn->print($o_format);
}

# change password for user root on remote server
if ( $o_action =~ /^pw$/ ){
  if ( !( $o_ipaddres && $o_oldpass && $o_newpass ) ){ 
    my $rtn = Output->new( error => 1, message => "Mandatory option is missing" );
    $rtn->print($o_format);
  }

  my $sshAuth = SSHAuthUser->new(
    sship   => $o_ipaddres,
    newpass => $o_newpass,
    oldpass => $o_oldpass,
  );

  my $rtn = $sshAuth->change_user_pass();
  $rtn->print($o_format);
}

my $rtn = Output->new( error => 1, message => "Unknown action option. PLease use -h for help message." );
$rtn->print($o_format);
exit;
# print usage
sub print_usage {
  my $prog = shift;
  print "Usage: $prog [-vh] [-a add|create|view|copy|pw|monitor] [-i|-ip host_ip -H|--hostname hostname -g|--group group] [ -k path/to/ssh/key ] \n";
}

# help message
sub print_help {
  my $prog = shift;
  my $exit = shift;
  print_usage( $prog );
  print <<EOT;
Options:
  -h|--help      - show this message
  -v|--verbose   - enable verbose mode.
  -o|--output    - define output format, plain or json (default: plain)
  -i|--ip        - add description of host ( group settting by --group option)
  -H|--host      - hostname for human usage ( default: host_ip )
  -g|--group     - group name ( default: hosts )
  -a|--action    - add|create|copy|pw|bx_info|bx_update|bx_reboot|dbs_list configuration in for ansible
  -u|--user      - user for ssh connection ( default: root )
  -p|--pass      - password for ssh connection ( used only for key installation  )
  -P|--new       - new password ( password can|must be changed if password policy demand it)
  -k|--key       - key file for installation
  -t|--timezone  - timezone name (default: Europe/Moscow)
  --php          - update php settings (default: not updated)
  --host_id      - host ID, used when update network settings
 Ex.
  * list of hosts and  groups in the ansible hosts file
 $prog -o json
  * add host vm1 with ip address 192.168.1.125 to config file
 $prog -a add -i 192.168.1.125 -H vm1
  * create default configuration with manager on local host
 $prog -a create 
  * install ssh key on the server
 $prog -a copy -i 192.168.1.125 -k /etc/ansible/.ssh/lBNltMqvWJ.bxkey.pub -p password
  * change password for user on remote host
 $prog -a pw -i 192.168.1.125 -p oldpassword -P newpassword -o json
  * update all hosts in the pool
 $prog -a bx_update -o json
  * update one host in the pool
 $prog -a bx_update -H vm1 -o json 
  * status for monitoring in the pool
 $prog -a monitor-status
  * reboot server
 $prog -a bx_reboot -H vm1 -o json
  * change bitrix password
 $prog -a bx_passwd -u bitrix -H vm1 -P newpassword -o json
  * list existen db on host
 $prog -a dbs_list -H vm1 -o json
  * server info
 $prog -a bx_info -H vm1 -o json
  * set timezone settings for the pool
 $prog -a timezone -o json --timezone=Europe/Lisbon --php
  * update host settings by new IP address
 $prog -a update_network --host_id 1401866581_XXXXXXXX -i 172.17.0.14
  * check the log and update the network settings if necessary (this action added to crontab)
 $prog -a check_network --update_log /opt/webdir/logs/update_pool.log

EOT
;
  exit;
}
