config.pl 19 KB

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