config.pl 19 KB

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