config.pl 20 KB

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