config.pl 21 KB

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