rA_Common.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package rA_Common;
  2. use strict;
  3. use warnings;
  4. use Exporter;
  5. our @ISA= qw( Exporter );
  6. # these CAN be exported.
  7. our @EXPORT_OK = qw( GetValidateConf GetUserConf ExeQuery GetUserConf
  8. GetValidAnwser GetValidateConf RootCo ShowConfig CheckUsedPort CheckAndLoadSQL);
  9. # these are exported by default.
  10. our @EXPORT = @EXPORT_OK; # qw( GetValidateConf GetUserConf ExeQuery GetUserConf GetValidAnwser GetValidateConf RootCo ShowConfig CheckUsedPort CheckAndLoadSQL);
  11. sub GetValidateConf { my($rhConfig) = @_;
  12. my $rhUserConf;
  13. while (1) {
  14. $rhUserConf = GetUserConf($rhConfig);
  15. print "\n Please Check desired conf \n";
  16. ShowConfig($rhUserConf);
  17. print "Would you like to apply these settings? [y/n] ";
  18. last if(GetValidAnwser("y|o|n") =~ /y|o/i);
  19. print "\n Restarting configuration sequence...\n";
  20. }
  21. return $rhUserConf;
  22. }
  23. #remplis tous un hash
  24. sub GetUserConf { my ($rhDefConf) = @_;
  25. my %hConf;
  26. my @sortedkeys = sort keys (%$rhDefConf);
  27. foreach my $sKey (@sortedkeys){
  28. my $sVal = $$rhDefConf{$sKey};
  29. print "$sKey : [$sVal] ";
  30. my $sAnwser = <>; chop($sAnwser);
  31. $hConf{"$sKey"} = $sAnwser || $sVal;
  32. }
  33. return \%hConf;
  34. }
  35. sub ShowConfig { my ($rhUserConf) = @_;
  36. my @sortedkeys = sort keys (%$rhUserConf);
  37. foreach my $sKey (@sortedkeys){
  38. my $sVal = $$rhUserConf{$sKey};
  39. if(ref($sVal) eq 'ARRAY') { print " $sKey => [@$sVal] \n";}
  40. else { print " $sKey => [$sVal] \n"; }
  41. }
  42. }
  43. sub GetValidAnwser { my($sOptReg,$sAutoyes) = @_;
  44. my $sAnwser = "";
  45. if($sAutoyes) { $sAnwser="y"; print "\n"; }
  46. else {
  47. while(!($sAnwser =~ /$sOptReg/i)) {
  48. $sAnwser = <>; chop($sAnwser);
  49. print "Please enter a valid option: $sOptReg " unless($sAnwser =~ /$sOptReg/i);
  50. }
  51. }
  52. return $sAnwser;
  53. }
  54. sub CheckUsedPort { my($sPort) = @_;
  55. open PIPE,"netstat -nat |" or die $!;
  56. my @line = grep { /$sPort/ } <PIPE>;
  57. return scalar(@line);
  58. }
  59. sub RootCo { my($rhConfig) = @_;
  60. print "\n== Entering RootCo ==\n";
  61. my $sDbH = 0;
  62. my $sDsn = $$rhConfig{"Dsn"}; #mysql server dest
  63. my $sUser = $$rhConfig{SQL_UID}; #verify desired user
  64. print "My dsn = $sDsn \n";
  65. if($sUser eq "root"){
  66. my $sPw = $$rhConfig{SQL_PW};
  67. $sDbH = DBI->connect($sDsn, "root", $sPw);
  68. unless($sDbH) { warn "Your root password doesn't seem valid for mysql. Please check your desired-conf.\n"; }
  69. }
  70. while($sDbH == 0) { #if can't use user to connect user root
  71. print "Please enter database root password. (NOTE: This is needed to create the users and databases, and will not be saved in any configuration file.)\n";
  72. my $sRPw = <>; chop($sRPw);
  73. $sDbH = DBI->connect($sDsn, "root", $sRPw);
  74. }
  75. return $sDbH;
  76. }
  77. sub CheckAndLoadSQL { my ($raFiles,$rhConfig,$sDBn) = @_;
  78. my $sHost = $$rhConfig{SQL_HOST};
  79. my $sPw = $$rhConfig{SQL_PW};
  80. my $sUser = $$rhConfig{SQL_UID};
  81. foreach(@$raFiles) {
  82. unless(-f -r $_){
  83. print "File '$_' does not exist or could not be read, skipped...\n";
  84. next;
  85. }
  86. my $sFileFullPath = Cwd::abs_path($_);
  87. system("mysql -u $sUser --password=$sPw -h $sHost $sDBn < $sFileFullPath");
  88. }
  89. }
  90. sub ExeQuery { my $sDbH = shift;
  91. my @aQuery = @_;
  92. print "Queries: [ @aQuery ]\n";
  93. foreach(@aQuery) {
  94. unless($sDbH->do($_)){ print "Failed to execute query: $_ => $DBI::errstr \n"; }
  95. }
  96. }