config.pl 22 KB

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