config.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. #!/usr/bin/perl
  2. # config script by lighta
  3. #TODO list :
  4. #- don't always override import/file, sed grep ?
  5. use File::Basename;
  6. use DBI;
  7. use DBD::mysql;
  8. use YAML::XS;
  9. use Cwd;
  10. use Getopt::Long;
  11. use Net::Ping;
  12. use strict;
  13. use constant {
  14. SERV_UID => "Serv_userid",
  15. SERV_PW => "Serv_userpass",
  16. SERV_WAN_IP => "Serv_wan_ip",
  17. MAP_PORT => "Map_port",
  18. CHAR_PORT => "Char_port",
  19. LOGIN_PORT => "Login_port",
  20. MD5_ENABLE => "enable_MD5",
  21. SQL_HOST => "SQL_host",
  22. SQL_PORT => "SQL_port",
  23. SQL_UID => "SQL_userid",
  24. SQL_PW => "SQL_userpass",
  25. SQL_MAIN_DB => "SQL_maindb",
  26. SQL_LOG_DB => ,"SQL_logdb",
  27. MAP_CONF_FILE => "map_conf.txt",
  28. CHAR_CONF_FILE => "char_conf.txt",
  29. LOGIN_CONF_FILE => "login_conf.txt",
  30. INTER_CONF_FILE => "inter_conf.txt",
  31. DESD_CONF_FILE => ".tmp-desd_conf.yml",
  32. MIN_PORT => 2000, #below are usually reserved for system
  33. MAX_PORT => 65535,
  34. };
  35. #BEGIN { #check and install module
  36. # my @aCheckModule = ("File::Basename","DBI","DBD::mysql","YAML","YAML::XS","Cwd","Getopt::Long","Net::Ping");
  37. # my @aMarkInst = ();
  38. # foreach(@aCheckModule) { eval "require $_" or push(@aMarkInst,$_); }
  39. # CPAN::install("@aMarkInst") if(@aMarkInst > 0);
  40. # foreach(@aCheckModule) { $_->import(); }
  41. #}
  42. # setup my defaults option
  43. my $sDsdFile = DESD_CONF_FILE;
  44. my $sAutoyes = 0;
  45. my $sForce = 0;
  46. my $sClean = 0;
  47. my $sTarget = "All";
  48. my $sHelp = 0;
  49. my $sOS;
  50. GetArgs();
  51. Main();
  52. sub GetArgs {
  53. GetOptions(
  54. 'f=s' => \$sDsdFile, #give desired conf file
  55. 'auto=i' => \$sAutoyes, #Force (auto-yes)
  56. 'C=i' => \$sClean, #Clean (like force but remove before adding)
  57. 'target=s' => \$sTarget, #Target (wich setup to run)
  58. 'Force=i' => \$sForce, #Force (bypass verification)
  59. 'OS=s' => \$sOS, #OS (specify the os you wish to use)
  60. 'help!' => \$sHelp,
  61. ) or $sHelp=1; #display help if invalid option
  62. my $sValidTarget = "All|Conf|DB|Inst|Dump";
  63. if( $sHelp ) {
  64. print "Incorect option specified, available option are:\n"
  65. ."\t --f filename => file (specify desiredconf to use)\n"
  66. ."\t --auto => auto-yes to question ? \n"
  67. ."\t --C => Clean (remove file, db, user before adding new)\n"
  68. ."\t --target => target (specify wich setup to run [$sValidTarget])\n"
  69. ."\t --Force => Force (bypass verification)\n";
  70. ."\t --OS => (specify the os you wish to use and avoid check)";
  71. exit;
  72. }
  73. unless($sTarget =~ /$sValidTarget/i){
  74. print "Incorect target specified, available target are:\n"
  75. ."\t --target => target (specify wich setup to run [(default)$sValidTarget])\n";
  76. exit;
  77. }
  78. if($sDsdFile ne DESD_CONF_FILE && !(-e -r $sDsdFile)){
  79. print "Incorect file specified: '$sDsdFile'\n"
  80. ."\t this file doesn't seem to appear on filesystem or unable to read\n";
  81. exit;
  82. }
  83. }
  84. sub Main {
  85. my($filename, $dir, $suffix) = fileparse($0);
  86. chdir $dir; #put ourself like was called in tools
  87. print "Welcome to athena config-tool\n";
  88. #default conf
  89. my %hDefConf = ( SERV_UID => "s1",
  90. SERV_PW => "p1",
  91. SERV_WAN_IP => "localhost",
  92. MAP_PORT => "5121",
  93. CHAR_PORT => "6121",
  94. LOGIN_PORT => "6900",
  95. MD5_ENABLE => "yes",
  96. SQL_HOST => "localhost",
  97. SQL_PORT => "3306",
  98. SQL_UID => "ragnarok",
  99. SQL_PW => "ragnarok",
  100. SQL_MAIN_DB => "ragnarok",
  101. SQL_LOG_DB => ,"ragnarok",
  102. );
  103. my $sBasedir = getcwd; #for setupdb
  104. if($sTarget =~ /All|Inst/i){ InstallSoft(); }
  105. if($sTarget =~ /All|Conf/i) { ConfigConf(\%hDefConf); chdir "$sBasedir"; }
  106. if($sTarget =~ /All|DB/i) { ConfigDB(\%hDefConf); chdir "$sBasedir"; }
  107. if($sTarget =~ /All|Dump/i) { chdir "~"; EnableCoredump(); }
  108. print "Config done, you should be able to launch and connect server now\n";
  109. print "NB : Don't forget to update your client clieninfo.xml to match change\n";
  110. }
  111. sub EnableCoredump {
  112. print "\n Starting Enabling coredump \n";
  113. my $sCurfile = "~/.bashrc";
  114. my @lines = ();
  115. my $sJump = .0;
  116. open PIPE,"sudo less $sCurfile |" or die $!;
  117. foreach(<PIPE>){
  118. if($_ =~ /ulimit -c unlimited/){
  119. $sJump = 1; #already in here nothing to do
  120. last;
  121. }
  122. push(@lines,$_) if /ulimit/;
  123. }
  124. if(scalar(@lines)>0){
  125. print "ulimit instruction found in file=$sCurfile\n"
  126. ."\t lines= \n @lines \n"
  127. ."are you sure you want to continue ? [y/n] \n";
  128. $sJump=1 if(GetValidAnwser("y|o|n") =~ /n/i);
  129. }
  130. system("sudo echo \"ulimit -c unlimited\" >> $sCurfile") if $sJump==0;
  131. $sJump=0;
  132. $sOS = GetOS() unless($sOS);
  133. if($sOS =~ /Fedora|CentOs/i){;
  134. open FILE, "</etc/security/limits.conf" || die;
  135. open FILE_TMP, ">tmp_limits.conf" || die;
  136. while(<FILE>){
  137. @lines=split(' ',$_);
  138. if($lines[2] eq "core" && $lines[3] eq "0"){
  139. $lines[3]="unlimited";
  140. print FILE_TMP "* hard core unlimited\n";
  141. $sJump=1; #mark we have found it
  142. }
  143. else { print FILE_TMP $_; }
  144. }
  145. close FILE;
  146. close FILE_TMP;
  147. system("sudo mv tmp_limits.conf /etc/security/limits.conf") if $sJump==1; #don't overwritte if some config was already in there
  148. unlink "tmp_limits.conf";
  149. }
  150. $sCurfile = "/etc/sysctl.conf";
  151. @lines = ();
  152. open PIPE,"sudo less $sCurfile |" or die $!;
  153. foreach(<PIPE>){
  154. push(@lines,$_) if /^kernel.core/;
  155. }
  156. if(scalar(@lines)>0){
  157. print "ulimit instruction found in file=$sCurfile\n"
  158. ."\t line= \n @lines \n"
  159. ."are you sure you want to continue ? [y/n] \n";
  160. $sJump=2 if(GetValidAnwser("y|o|n") =~ /n/i);
  161. }
  162. unless($sJump==2){
  163. system('sudo su root -c "echo \"echo kernel.core_uses_pid = 1 >> /etc/sysctl.conf\" | sudo bash"');
  164. system('sudo su root -c "echo \"echo kernel.core_pattern = /tmp/core-%e-%s-%u-%g-%p-%t >> /etc/sysctl.conf\" | sudo bash"');
  165. system('sudo su root -c "echo \"echo fs.suid_dumpable = 1 >> /etc/sysctl.conf\" | sudo bash"');
  166. system('sudo su root -c "sysctl -p"');
  167. }
  168. }
  169. sub GetOS {
  170. #yes we could $^0 or uname -r but $^0 only give perl binary build OS and uname hmm...
  171. open PIPE,"lsb_release -i |" or die $!;
  172. my $sDistri = <PIPE>;
  173. if($sDistri){
  174. my @aDist = split(":",$sDistri);
  175. $sDistri = $aDist[1];
  176. $sDistri =~ s/^\s+|\s+$//g;
  177. $sOS = $sDistri;
  178. }
  179. else {
  180. my @aSupportedOS = ("Debian","Ubuntu","Fedora","CentOs","FreeBSD");
  181. my $sOSregex = join("|",@aSupportedOS);
  182. until($sOS =~ /$sOSregex/i){
  183. print "Please enter your OS:[$sOSregex] or enter 'quit' to exit\n";
  184. $sOS = <>; chomp($sOS);
  185. last if($sOS eq "quit");
  186. }
  187. }
  188. return $sOS;
  189. }
  190. sub InstallSoft {
  191. print "\n Starting InstallSoft \n";
  192. print "This autoinstall feature is experimental, package name varies from distri and version, couldn't support them all\n";
  193. $sOS = GetOS() unless $sOS;
  194. if($sOS eq "quit"){ print "Skipping Software installation\n"; return; }
  195. elsif($sOS =~ /Ubuntu|Debian/i) { #tested on ubuntu 12.10,13.10
  196. my @aListSoft = ("gcc","gdb","zlibc","zlib1g-dev","make","git","mysql-client","mysql-server","mysql-common","libmysqlclient-dev","phpmyadmin","libpcre3-dev");
  197. print "Going to install: @aListSoft\n";
  198. system("sudo apt-get install @aListSoft");
  199. }
  200. elsif($sOS =~ /Fedora|CentOs/i){ #tested on fedora 18,19,20 /centos 5,6,7
  201. my @aListSoft = ("gcc","gdb","zlib","zlib-devel","make","git","mariadb-server","maria","mariadb-devel","phpmyadmin","pcre-devel");
  202. # my @aListSoft = ("gcc","gdb","zlib","zlib-devel","make","git","mysql-server","mysql-devel","phpmyadmin","pcre-devel");
  203. system("sudo yum install @aListSoft");
  204. }
  205. elsif($sOS =~ /FreeBSD/i){ #tested on FreeBSD 9.01
  206. system("portsnap fetch extract && portsnap update"); #fetch port lib and extract
  207. my @aDevel = ("binutils","git","autoconf","pcre","gmake","gdb");
  208. foreach(@aDevel){
  209. system("cd /usr/ports/devel/$_ && make install clean"); #install devels
  210. }
  211. # system("cd /usr/ports/lang/gcc46 && make install"); #gcc4.6 use latest ? 4.8 ?
  212. system("cd /usr/ports/databases/mysql55-server && make install clean");
  213. #other utils ?
  214. system("cd /usr/ports/www/wget && make install clean");
  215. system("cd /usr/ports/archivers/unrar && make install clean");
  216. }
  217. }
  218. sub ConfigConf { my ($rhDefConf) = @_;
  219. print "\n Starting ConfigConf \n";
  220. my $rhUserConf;
  221. while(1) {
  222. $rhUserConf = GetDesiredConf($rhDefConf);
  223. print "SetupConf using conf : \n";
  224. ShowConfig($rhUserConf);
  225. last if($sForce || AutoCheckConf($rhUserConf));
  226. }
  227. ApplySetupConf($rhUserConf);
  228. }
  229. sub ConfigDB { my ($rhDefConf) = @_;
  230. print "\n Starting ConfigDB \n";
  231. my $rhUserConf;
  232. while(1) {
  233. $rhUserConf = GetDesiredConf($rhDefConf);
  234. print "SetupDb using conf : \n";
  235. ShowConfig($rhUserConf);
  236. last if($sForce || AutoCheckConf($rhUserConf));
  237. }
  238. ApplySetupDB($rhUserConf);
  239. }
  240. #conf function
  241. sub ApplySetupConf { my ($rhConfig) = @_;
  242. print "\nApplying conf \n";
  243. my @aTargetfile = (MAP_CONF_FILE,CHAR_CONF_FILE,LOGIN_CONF_FILE,INTER_CONF_FILE);
  244. my $sConfDir = "conf";
  245. my $sUserConfDir = "import";
  246. die "$sConfDir doesn't seem to exist or coudldn't be read/writte" unless(-d -r -w "../$sConfDir");
  247. chdir "../$sConfDir";
  248. print "Saving tmp user-conf \n";
  249. YAML::XS::DumpFile(DESD_CONF_FILE,$rhConfig);
  250. unless(-d "$sUserConfDir") {
  251. print "conf/import directory doesn't exist, create it ? [y/n] (will be generated by compilation otherwise) \n";
  252. if(GetValidAnwser("y|o|n") =~ /n/i) { die "Couldn't apply conf without import folder\n"; }
  253. mkdir "$sUserConfDir";
  254. }
  255. chdir $sUserConfDir;
  256. if($sClean){ unlink @aTargetfile; } #deleting file before applying conf if clean
  257. opendir(DIR, ".") or die $!;
  258. my @aDirfile = grep { /\.txt/ && -f "$_" } readdir(DIR);
  259. close DIR;
  260. print "Current file in directory = [@aDirfile] target = [@aTargetfile] \n";
  261. foreach my $sCurfile(@aTargetfile) {
  262. print "Checking if target file: [$sCurfile] exist ? ";
  263. if(-e -r $sCurfile) {
  264. print "Yes\n";
  265. print "$sCurfile seem to exist, overwritte it [y/n] ?\n";
  266. if(GetValidAnwser("y|o|n") =~ /n/i) {
  267. print "Only overwritte option supported atm skip file\n\n";
  268. next;
  269. }
  270. }
  271. else { print "No\n" };
  272. print "\t Writting file $sCurfile \n";
  273. if($sCurfile eq MAP_CONF_FILE) { ApplyMapConf($rhConfig,$sCurfile); }
  274. elsif($sCurfile eq CHAR_CONF_FILE) { ApplyCharConf($rhConfig,$sCurfile); }
  275. elsif($sCurfile eq LOGIN_CONF_FILE) { ApplyLoginConf($rhConfig,$sCurfile); }
  276. elsif($sCurfile eq INTER_CONF_FILE) { ApplyInterConf($rhConfig,$sCurfile); }
  277. }
  278. }
  279. sub ApplyMapConf { my ($rhUserConf,$sCurfile) = @_;
  280. open FILE, "> $sCurfile" || die "couldn't openfile/create $sCurfile \n";
  281. print FILE "userid: " . $$rhUserConf{SERV_UID}."\n";
  282. print FILE "passwd: " . $$rhUserConf{SERV_PW}."\n\n";
  283. print FILE "map_ip: " . $$rhUserConf{SERV_WAN_IP}."\n";
  284. print FILE "map_port: " . $$rhUserConf{MAP_PORT}."\n";
  285. print FILE "char_port: " . $$rhUserConf{CHAR_PORT}."\n";
  286. }
  287. sub ApplyCharConf { my ($rhUserConf,$sCurfile) = @_;
  288. open FILE, "> $sCurfile" || die "couldn't openfile $sCurfile \n";
  289. print FILE "userid: " . $$rhUserConf{SERV_UID}."\n";
  290. print FILE "passwd: " . $$rhUserConf{SERV_PW}."\n\n";
  291. print FILE "char_ip: " . $$rhUserConf{SERV_WAN_IP}."\n";
  292. print FILE "char_port: " . $$rhUserConf{CHAR_PORT}."\n";
  293. print FILE "login_port: " . $$rhUserConf{LOGIN_PORT}."\n";
  294. }
  295. sub ApplyLoginConf { my ($rhUserConf,$sCurfile) = @_;
  296. open FILE, "> $sCurfile" || die "couldn't openfile $sCurfile \n";
  297. print FILE "login_port: " . $$rhUserConf{LOGIN_PORT}."\n";
  298. print FILE "use_MD5_passwords: " . $$rhUserConf{MD5_ENABLE}."\n";
  299. }
  300. sub ApplyInterConf { my ($rhUserConf,$sCurfile) = @_;
  301. open FILE, "> $sCurfile" || die "couldn't openfile $sCurfile \n";
  302. print FILE "sql.db_hostname: " . $$rhUserConf{SQL_HOST}."\n";
  303. print FILE "sql.db_port: " . $$rhUserConf{SQL_PORT}."\n";
  304. print FILE "sql.db_username: " . $$rhUserConf{SQL_UID}."\n";
  305. print FILE "sql.db_password: " . $$rhUserConf{SQL_PW}."\n";
  306. print FILE "sql.db_database: " . $$rhUserConf{SQL_MAIN_DB}."\n\n";
  307. print FILE "char_server_ip: " . $$rhUserConf{SQL_HOST}."\n";
  308. print FILE "char_server_port: " . $$rhUserConf{SQL_PORT}."\n";
  309. print FILE "char_server_id: " . $$rhUserConf{SQL_UID}."\n";
  310. print FILE "char_server_pw: " . $$rhUserConf{SQL_PW}."\n";
  311. print FILE "char_server_db: " . $$rhUserConf{SQL_MAIN_DB}."\n\n";
  312. print FILE "sql.map_server_ip: " . $$rhUserConf{SQL_HOST}."\n";
  313. print FILE "sql.map_server_port: " . $$rhUserConf{SQL_PORT}."\n";
  314. print FILE "map_server_id: " . $$rhUserConf{SQL_UID}."\n";
  315. print FILE "map_server_pw: " . $$rhUserConf{SQL_PW}."\n";
  316. print FILE "map_server_db: " . $$rhUserConf{SQL_MAIN_DB}."\n\n";
  317. #todo may we want 2 schema ??
  318. print FILE "log_db_ip: " . $$rhUserConf{SQL_HOST} ."\n";
  319. print FILE "log_db_port: " . $$rhUserConf{SQL_PORT}."\n";
  320. print FILE "log_db_id: " . $$rhUserConf{SQL_UID}."\n";
  321. print FILE "log_db_pw: " . $$rhUserConf{SQL_PW}."\n";
  322. print FILE "log_db_db: " . $$rhUserConf{SQL_LOG_DB}."\n\n";
  323. }
  324. sub AutoCheckConf { my ($rhConfig) = @_;
  325. print "\n AutoCheckConf, \n you can use option --force=1 to bypass this \n";
  326. foreach my $sKeys (keys %$rhConfig){
  327. my $sVal = $$rhConfig{$sKeys};
  328. if($sKeys =~ /PORT/) { #chek if valid port
  329. if(($sVal<MIN_PORT) && ($sVal>MAX_PORT)) {
  330. warn "Invalid port specified for $sKeys => $sVal, must be in [".MIN_PORT.":".MAX_PORT."]\n";
  331. return 0;
  332. }
  333. elsif(!($sKeys =~ /SQL/) && CheckUsedPort($sVal)) { #skip SQL service
  334. warn "Port:$sVal seem to be already in use by system \n";
  335. return 0;
  336. }
  337. elsif(CheckDupPort($rhConfig,$sKeys)) {
  338. warn "Port:$sVal seem to be already used by other key in config \n";
  339. return 0;
  340. }
  341. }
  342. elsif($sKeys =~ /IP|HOST/){ #chek if ip valid, can we reach it ? trough SYN ACK
  343. my $p = Net::Ping->new("syn");
  344. my $sTest = $p->ping($sVal);
  345. $p->close();
  346. unless($sTest) {
  347. print "Invalide IP/Host, ping couldn't reach $sKeys => $sVal \n NB : ICMP may just be unallowed\n";
  348. return 0;
  349. }
  350. }
  351. }
  352. return 1;
  353. }
  354. sub CheckDupPort { my ($rhConfig,$sChkKeys) = @_;
  355. my $sChkport = $$rhConfig{$sChkKeys};
  356. foreach my $sKeys (keys %$rhConfig){
  357. next if($sKeys eq $sChkKeys); #skip ourself
  358. my $sVal = $$rhConfig{$sKeys};
  359. return 1 if($sChkport eq $sVal);
  360. }
  361. return 0;
  362. }
  363. sub CheckUsedPort { my($sPort) = @_;
  364. open PIPE,"netstat -nat |" or die $!;
  365. my @line = grep { /$sPort/ } <PIPE>;
  366. return scalar(@line);
  367. }
  368. #Db function
  369. sub ApplySetupDB { my($rhConfig) = @_;
  370. my $sDbH; #db handle
  371. my $sHost = $$rhConfig{SQL_HOST};
  372. my $sPort = $$rhConfig{SQL_PORT};
  373. my $sDsn = "dbi:mysql::$sHost:$sPort"; #don't try to auto connect to db
  374. $$rhConfig{"Dsn"} = $sDsn;
  375. $sDbH = RootCo($rhConfig);
  376. CreateDB($sDbH,$rhConfig); #create db if not exist
  377. $sDbH = CreateUser($sDbH,$rhConfig); #loged as user now
  378. LoadSqlFile($sDbH,$rhConfig); #Load .sql file into db
  379. CreateServUser($sDbH,$rhConfig);
  380. print "SetupDb done \n";
  381. }
  382. sub RootCo { my($rhConfig) = @_;
  383. print "\n Entering RootCo \n";
  384. my $sDbH;
  385. my $sDsn = $$rhConfig{"Dsn"}; #mysql server dest
  386. my $sUser = $$rhConfig{SQL_UID}; #verify desired user
  387. print "My dsn = $sDsn \n";
  388. if($sUser eq "root"){
  389. my $sPw = $$rhConfig{SQL_PW};
  390. $sDbH = DBI->connect($sDsn, "root", $sPw);
  391. unless($sDbH) { warn "Your root password doesn't seem valid for mysql, your desired-conf is wrong \n"; }
  392. }
  393. while($sDbH == 0) { #if can't use user to connect user root
  394. print "Please inser DB root passeword (this won't be saved in any configuration file, needed to create dbs and user)\n";
  395. my $sRPw = <>; chop($sRPw);
  396. $sDbH = DBI->connect($sDsn, "root", $sRPw);
  397. }
  398. return $sDbH;
  399. }
  400. sub CreateDB { my($sDbH,$rhConfig) = @_;
  401. print "\n Entering CreateDB \n";
  402. my $sDBn = $$rhConfig{SQL_MAIN_DB};
  403. my $sLogDBn = $$rhConfig{SQL_LOG_DB};
  404. my @aQuery = ("create database IF NOT EXISTS $sDBn;","create database IF NOT EXISTS $sLogDBn;");
  405. if($sClean){ #deleting database if clean
  406. unshift(@aQuery,"drop database IF EXISTS $sDBn;");
  407. unshift(@aQuery,"drop database IF EXISTS $sLogDBn;");
  408. }
  409. else {
  410. my $sRes = $sDbH->selectcol_arrayref('show databases');
  411. foreach my $db (@$sRes){ #relevant later for import
  412. if($db eq "$sDBn") { ValidateDBMerge($db); } #may exit here
  413. elsif ($db eq "$sLogDBn") { ValidateDBMerge($db); } #may exit here
  414. }
  415. }
  416. ExeQuery($sDbH,@aQuery);
  417. }
  418. sub ValidateDBMerge { my($sDBn) = @_;
  419. warn "Database: '$sDBn' seem to already exist exiting\n";
  420. warn "Continue will load data in existing db would you like to continue ? [y/n] \n";
  421. if(GetValidAnwser("y|o|n") =~ /n/i) {
  422. print "Exiting setup, please either setup with another dbname or manually\n";
  423. exit;
  424. }
  425. }
  426. sub CreateUser { my($sDbH,$rhConfig) = @_;
  427. print "\n Entering CreateUser \n";
  428. my $sDsn = $$rhConfig{"Dsn"};
  429. print "My dsn = $sDsn \n";
  430. my $sHost = $$rhConfig{SQL_HOST};
  431. my $sPw = $$rhConfig{SQL_PW};
  432. my $sUser = $$rhConfig{SQL_UID};
  433. my $sDBn = $$rhConfig{SQL_MAIN_DB};
  434. my $sLogDBn = $$rhConfig{SQL_LOG_DB};
  435. my @aQuery= ("GRANT ALL PRIVILEGES ON $sDBn.* TO $sUser\@'$sHost' IDENTIFIED BY '$sPw' WITH GRANT OPTION", #maindb
  436. "GRANT ALL PRIVILEGES ON $sLogDBn.* TO $sUser\@'$sHost' IDENTIFIED BY '$sPw' WITH GRANT OPTION"); #logdb
  437. my $sUserDbh = DBI->connect($sDsn, $sUser, $sPw, {"PrintError" => 0}); #try connect with user
  438. if($sUserDbh && !$sClean) {
  439. print "User : $sUser seem to already exist, skipping creation\n"
  440. ."NB please check if you have correct privilege set for db: $sDBn \n";
  441. }
  442. else { #create user only if not exist (or mode clean)
  443. if($sClean && $sUser ne "root"){ unshift(@aQuery,"DELETE FROM mysql.user WHERE User = '$sUser';"); }
  444. print "Creating user $sUser for dbs : $sDBn and $sLogDBn on $sHost \n";
  445. ExeQuery($sDbH,@aQuery);
  446. $sUserDbh = DBI->connect($sDsn, $sUser, $sPw);
  447. }
  448. return $sUserDbh; #drop old co and connect with user now
  449. }
  450. sub LoadSqlFile { my($sDbH,$rhConfig) = @_;
  451. print "\n Entering LoadSqlFile \n";
  452. my $sDBn = $$rhConfig{SQL_MAIN_DB};
  453. my $sLogDBn = $$rhConfig{SQL_LOG_DB};
  454. my $sSqldir = "sql-files";
  455. my @aMainFiles = ("main.sql"); #add other file to load for main db here
  456. my @aLogFiles = ("logs.sql"); #add other file to load for log db here
  457. die "$sSqldir doesn't seem to exist or coudldn't be read" unless(-d -r "../$sSqldir");
  458. chdir "../$sSqldir";
  459. print "Checking if target files exist :\n\tMain: [@aMainFiles]\n\tLog: [@aLogFiles]\n";
  460. CheckAndLoad(\@aMainFiles,$rhConfig,$sDBn);
  461. CheckAndLoad(\@aLogFiles,$rhConfig,$sLogDBn);
  462. # my $raMainQuerys = CheckAndAddQuery(\@aMainFiles,$rhConfig);
  463. # my $raLogQuerys = CheckAndAddQuery(\@aLogFiles,$rhConfig);
  464. # ExeQuery($sDbH,"use $sDBn;",@$raMainQuerys,"use $sLogDBn;", @$raLogQuerys);
  465. }
  466. sub CheckAndLoad { my ($raFiles,$rhConfig,$sDBn) = @_;
  467. my $sHost = $$rhConfig{SQL_HOST};
  468. my $sPw = $$rhConfig{SQL_PW};
  469. my $sUser = $$rhConfig{SQL_UID};
  470. foreach(@$raFiles) {
  471. unless(-f -r $_){
  472. print "File : $_ doesn't seem to exist or was unreadable skipped\n";
  473. next;
  474. }
  475. my $sFileFullPath = Cwd::abs_path($_);
  476. system("mysql -u $sUser --password=$sPw -h $sHost $sDBn < $sFileFullPath");
  477. }
  478. }
  479. # query failure atm (shitty perl)
  480. #sub CheckAndAddQuery { my ($raFiles) = @_;
  481. # my @aQuery = ();
  482. # foreach(@$raFiles) {
  483. # unless(-f -r $_){
  484. # print "File : $_ doesn't seem to exist or was unreadable skipped\n";
  485. # next;
  486. # }
  487. # my $sFileFullPath = Cwd::abs_path($_);
  488. # my $sInfileQuery = "source $sFileFullPath";
  489. # #my $sInfileQuery = "\\. $sFileFullPath;";
  490. # push(@aQuery,$sInfileQuery);
  491. #
  492. # }
  493. # return \@aQuery;
  494. #}
  495. sub CreateServUser { my($sDbH,$rhConfig) = @_;
  496. my $sUid = $$rhConfig{SERV_UID};
  497. my $sUpw = $$rhConfig{SERV_PW};
  498. my $sMD5 = $$rhConfig{MD5_ENABLE};
  499. my $sDBn = $$rhConfig{SQL_MAIN_DB};
  500. my @aQuery = ("use $sDBn;","DELETE FROM login WHERE sex='S';");
  501. if($sMD5){ push(@aQuery,"INSERT INTO login(account_id, userid, user_pass, sex) values(1,'$sUid',MD5('$sUpw'),'S');"); }
  502. else { push(@aQuery,"INSERT INTO login(account_id, userid, user_pass, sex) values(1,'$sUid','$sUpw','S');"); }
  503. ExeQuery($sDbH,@aQuery);
  504. }
  505. sub ExeQuery { my $sDbH = shift;
  506. my @aQuery = @_;
  507. print "my querys are = [ @aQuery ]\n";
  508. foreach(@aQuery) {
  509. unless($sDbH->do($_)){ print "Failed to execute query : $_ => $DBI::errstr \n"; }
  510. }
  511. }
  512. #Common
  513. sub GetValidateConf { my($rhConfig) = @_;
  514. my $rhUserConf;
  515. while (1) {
  516. $rhUserConf = GetUserConf($rhConfig);
  517. print "\n Please Check desired conf \n";
  518. ShowConfig($rhUserConf);
  519. print "Would you like to apply those setting ? [y/n] ";
  520. last if(GetValidAnwser("y|o|n") =~ /y|o/i);
  521. print "\n Restarting configuration sequence \n";
  522. }
  523. return $rhUserConf;
  524. }
  525. sub GetUserConf { my ($rhDefConf) = @_;
  526. my %hConf;
  527. my @sortedkeys = sort keys (%$rhDefConf);
  528. foreach my $sKey (@sortedkeys){
  529. my $sVal = $$rhDefConf{$sKey};
  530. print "$sKey : [$sVal] ";
  531. my $sAnwser = <>; chop($sAnwser);
  532. $hConf{"$sKey"} = $sAnwser || $sVal;
  533. }
  534. return \%hConf;
  535. }
  536. sub ShowConfig { my ($rhUserConf) = @_;
  537. my @sortedkeys = sort keys (%$rhUserConf);
  538. foreach my $sKey (@sortedkeys){
  539. my $sVal = $$rhUserConf{$sKey};
  540. if(ref($sVal) eq 'ARRAY') { print " $sKey => [@$sVal] \n";}
  541. else { print " $sKey => [$sVal] \n"; }
  542. }
  543. }
  544. sub GetValidAnwser { my($sOptReg) = @_;
  545. my $sAnwser = "";
  546. if($sAutoyes) { $sAnwser="y"; print "\n"; }
  547. else {
  548. while(!($sAnwser =~ /$sOptReg/i)) {
  549. $sAnwser = <>; chop($sAnwser);
  550. print "Please enter a valid option : $sOptReg " unless($sAnwser =~ /$sOptReg/i);
  551. }
  552. }
  553. return $sAnwser;
  554. }
  555. sub GetDesiredConf { my ($rhDefConf) = @_;
  556. print "Please enter desired confiration\n";
  557. my $rhUserConf;
  558. my $sDesdConfFile = $sDsdFile;
  559. #if default search in conf otherwise get specified name with cwd
  560. if($sDsdFile eq DESD_CONF_FILE) { $sDesdConfFile = "../conf/".$sDsdFile; }
  561. print "Checking if there an Desiredconf file \n";
  562. if(-e -r $sDesdConfFile) {
  563. print "Found Desiredconf \n";
  564. $rhUserConf = YAML::XS::LoadFile($sDesdConfFile);
  565. if(!($rhUserConf)){
  566. print "Desiredconf seem invalid or empty, please check file and relaunch setup or entry Config\n";
  567. $rhUserConf=GetValidateConf($rhDefConf);
  568. }
  569. else {
  570. ShowConfig($rhUserConf);
  571. print "Would you like to apply those setting ? [y/n] ";
  572. if(GetValidAnwser("y|o|n") =~ /n/i) { #no take user entry
  573. print "DesiredConf not applyed, please enter config\n";
  574. $rhUserConf=GetValidateConf($rhDefConf);
  575. }
  576. }
  577. }
  578. else { #no files take user entry
  579. print "No Desiredconf found, please enter config \n";
  580. $rhUserConf=GetValidateConf($rhDefConf);
  581. }
  582. return $rhUserConf;
  583. }