update.pl 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #!/usr/bin/perl
  2. # upgrading rA emulator, (src,npc,db..).
  3. # perform sql db update
  4. # perform binary recompilation
  5. use strict;
  6. use Getopt::Long;
  7. use Cwd;
  8. use Git::Repository;
  9. use File::Copy;
  10. use DBI;
  11. use DBD::mysql;
  12. use YAML::XS;
  13. use rA_Common;
  14. #prgm option
  15. my $sHelp = 0;
  16. my $srABaseGitHttp = 'https://github.com/rathena/rathena.git';
  17. my $srABaseGitSSH = 'git@github.com:rathena/rathena.git';
  18. use constant {
  19. STATE_FILE => "SQL_Status.yml",
  20. ST_OLD => "old",
  21. ST_SK => "skipped",
  22. ST_DONE => "done",
  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. };
  30. my %hFileState = ();
  31. my $sValidTarget = "All|DB|Compile|Restart|Upd|MapDB";
  32. #those following could be edited by user
  33. my $sAutoDB = 0; #by default we ask for db setting
  34. my $sTarget = "Upd|DB|Compile|MapDB"; #default target doesn't restart
  35. my %hDefConf = (
  36. SQL_HOST => "localhost",
  37. SQL_PORT => "3306",
  38. SQL_UID => "ragnarok",
  39. SQL_PW => "ragnarok",
  40. SQL_MAIN_DB => "ragnarok",
  41. SQL_LOG_DB => ,"ragnarok",
  42. );
  43. GetArgs();
  44. Main();
  45. sub GetArgs {
  46. GetOptions(
  47. 'target=s' => \$sTarget, #Target (wich setup to run)=
  48. 'help!' => \$sHelp,
  49. ) or $sHelp=1; #display help if invalid option
  50. if( $sHelp ) {
  51. print "Incorect option specified, available option are:\n"
  52. ."\t --target => target (specify wich check to ignore [$sValidTarget])\n";
  53. exit;
  54. }
  55. if(!$sTarget || !($sTarget =~ /$sValidTarget/i)){
  56. print "Incorect target specified, available target are:\n"
  57. ."\t --target => target (specify wich check to ignore [(default)$sValidTarget])\n
  58. NB restart is compiling dependant\n";
  59. exit;
  60. }
  61. }
  62. sub Main {
  63. my $sCurdir = getcwd;
  64. chdir "..";
  65. UpdateSQL($sCurdir,1,\%hFileState);
  66. if($sTarget =~ "All|Upd") { GitUpdate($sCurdir); }
  67. if($sTarget =~ "All|DB") { UpdateSQL($sCurdir,0,\%hFileState); }
  68. if($sTarget =~ "All|Compile") { RunCompilation($sCurdir,$sTarget); }
  69. }
  70. sub GetSqlFileInDir { my($sDir) = @_;
  71. opendir (DIR, $sDir) or die $!;
  72. my @aFiles
  73. = grep {
  74. /^(?!\.)/ # not begins with a period
  75. && /\.sql$/ # finish by .sql
  76. && -f "$sDir/$_" # and is a file
  77. } readdir(DIR);
  78. closedir(DIR);
  79. return \@aFiles;
  80. }
  81. sub UpdateSQL { my($sBaseDir,$sInit,$rhFileState) = @_;
  82. my @aMapDBFiles = ();
  83. my @aCharDBFiles = (); #for now we assum they all in same DB
  84. my @aLoginDBFiles = ();
  85. my @aLogDBFiles = ();
  86. print "Preparing SQL folder\n" if($sInit==1);
  87. if(-e -r "sql-files/".STATE_FILE) {
  88. print "Reading file status \n";
  89. $rhFileState = YAML::XS::LoadFile("sql-files/".STATE_FILE);
  90. }
  91. if($sTarget =~ "All|MapDB") {
  92. chdir "sql-files";
  93. print "Getting Map SQL Db file \n";
  94. my $raFilesMap = GetSqlFileInDir("./");
  95. foreach my $sFile (@$raFilesMap){
  96. if($sInit==1){
  97. if(exists $$rhFileState{$sFile} && $$rhFileState{$sFile}{"status"} == ST_DONE ){
  98. next;
  99. }
  100. $$rhFileState{$sFile}{"status"} = ST_OLD;
  101. $$rhFileState{$sFile}{"lastmod"} = (stat ($sFile))[9];
  102. }
  103. elsif($$rhFileState{$sFile}{"lastmod"} != (stat ($sFile))[9] ) {
  104. print "The file $sFile was updated\n {\t ".$$rhFileState{$sFile}->{"lastmod"}." , ".(stat ($sFile))[9]."} \n";
  105. push(@aMapDBFiles,$sFile);
  106. $$rhFileState{$sFile}{"status"} = ST_DONE;
  107. }
  108. }
  109. chdir "..";
  110. }
  111. chdir "sql-files/upgrades";
  112. my $raFiles = GetSqlFileInDir("./");
  113. foreach my $sFile (@$raFiles){
  114. #print "Cur file = $sFile \n";
  115. if($sInit==1){
  116. if(exists $$rhFileState{$sFile} && $$rhFileState{$sFile}{"status"} == ST_DONE ){
  117. next;
  118. }
  119. if( $sFile =~ /_opt_/){
  120. $$rhFileState{$sFile}{"status"} = ST_SK;
  121. } else {
  122. $$rhFileState{$sFile}{"status"} = ST_OLD;
  123. }
  124. $$rhFileState{$sFile}{"lastmod"} = (stat ($sFile))[9];
  125. }
  126. else {
  127. if(exists $$rhFileState{$sFile}){
  128. my $sT = $$rhFileState{$sFile}{"status"};
  129. my $sLastMode = $$rhFileState{$sFile}{"lastmod"};
  130. # #if it's done or skipped don't do it, if it's old but updated do it
  131. next if ( $sT eq ST_OLD or $sT eq ST_DONE or $sLastMode == (stat ($sFile))[9] );
  132. }
  133. if( $sFile =~ /_log.sql$/) {
  134. print "Found log file = $sFile \n";
  135. push(@aLogDBFiles,$sFile);
  136. }
  137. else {
  138. print "Found char file = $sFile \n";\
  139. push(@aCharDBFiles,$sFile);
  140. }
  141. $$rhFileState{$sFile}{"status"} = "done"; # the query will be applied so mark it so
  142. # This part is for distributed DB, not supported yet
  143. # proposed nomenclature [lighta] : update_date_{opt_}(map|chr|acc|log).sql
  144. # (e.g : update_20141218_opt_map.sql or update_20141218_acc.sql
  145. # if( $sFile =~ /_map.sql$/) {
  146. # print "Found log file = $sFile \n";
  147. # push(@aMapDBFiles,$sFile);
  148. #
  149. # }
  150. # elsif( $sFile =~ /_acc.sql$/) {
  151. # print "Found log file = $sFile \n";
  152. # push(@aLoginDBFiles,$sFile);
  153. #
  154. # }
  155. # elsif( $sFile =~ /_chr.sql$/) {
  156. # print "Found log file = $sFile \n";
  157. # push(@aCharDBFiles,$sFile);
  158. #
  159. # }
  160. }
  161. }
  162. if($sInit==0){ #apply update
  163. return;
  164. if( scalar(@aCharDBFiles)==0 and scalar(@aLogDBFiles)==0
  165. and scalar(@aMapDBFiles)==0 and scalar(@aLoginDBFiles)==0
  166. ){
  167. print "No SQL Update to perform\n";
  168. }
  169. else {
  170. print "Updating DB \n";
  171. my $rhUserConf;
  172. if($sAutoDB==0){
  173. $rhUserConf=GetValidateConf(\%hDefConf);
  174. print "To make this step auto you can edit hDefConf and set sAutoDB to 1.
  175. both parameter at the begining of file for the moment\n";
  176. }
  177. else {
  178. $rhUserConf=\%hDefConf; #we assum it's set correctly
  179. }
  180. CheckAndLoadSQL(\@aMapDBFiles,$rhUserConf,$$rhUserConf{SQL_MAP_DB});
  181. CheckAndLoadSQL(\@aCharDBFiles,$rhUserConf,$$rhUserConf{SQL_MAIN_DB});
  182. #CheckAndLoadSQL(\@aLoginDBFiles,$rhUserConf,$$rhUserConf{SQL_ACC_DB});
  183. CheckAndLoadSQL(\@aLogDBFiles,$rhUserConf,$$rhUserConf{SQL_LOG_DB});
  184. }
  185. chdir "../..";
  186. } else {
  187. chdir "../..";
  188. print "Saving stateFile \n";
  189. YAML::XS::DumpFile("sql-files/".STATE_FILE,$rhFileState);
  190. }
  191. }
  192. sub RunCompilation { my($sBaseDir,$sTarget) = @_;
  193. if($^O =~ "linux"){
  194. print "Recompiling \n";
  195. system('./configure && make clean server');
  196. if($sTarget =~ "All|Restart") {
  197. print "Restarting \n";
  198. system('./athena-start restart');
  199. }
  200. }
  201. else {
  202. print "AutoCompilation ain't supported for this OS yet (OS detected=$^O \n";
  203. }
  204. }
  205. sub GitUpdate { my($sBaseDir) = @_;
  206. my $sGit = Git::Repository->new(
  207. work_tree => "$sBaseDir/..",
  208. );
  209. my $sIsOrigin = CheckRemote($sGit);
  210. if($sIsOrigin==0){
  211. print "Saving current work\n";
  212. $sGit->run( "stash" );
  213. print "Fetching new content and merging\n";
  214. $sGit->run( "pull" );
  215. print "Attempt applying save work\n";
  216. $sGit->run( "stash" => "pop" );
  217. }
  218. else { #it's a fork
  219. print "Fetching upstream\n";
  220. $sGit->run( "fetch" => "upstream" );
  221. print "Switching to master branch\n";
  222. $sGit->run( "checkout" => "master" );
  223. print "Merging upstream with master\n";
  224. $sGit->run( "merge" => "upstream/master" );
  225. }
  226. }
  227. #Checking rA as a remote or origin
  228. sub CheckRemote { my($sGit) = @_;
  229. my $sRaOrigin=0;
  230. my $sRaUpstream=0;
  231. print "Checking remotes\n";
  232. my @aRemotes = $sGit->run("remote" => "-v");
  233. #print "My Remotes are\n";
  234. foreach my $sCurRem (@aRemotes){
  235. my @aCol = split(' ',$sCurRem);
  236. #print "$sCurRem\n";
  237. if( $aCol[1] =~ "$srABaseGitHttp" or $aCol[1] =~ "$srABaseGitSSH"){
  238. #print "Has rA as origin\n";
  239. if($aCol[0] =~ "origin") { $sRaOrigin = 1; }
  240. if($aCol[0] =~ "upstream") { $sRaUpstream = 1; }
  241. }
  242. }
  243. if($sRaOrigin==0 and $sRaUpstream==0){
  244. print "Adding rA as a upstream\n";
  245. $sGit->run("remote" => "add" => "upstream" => "$srABaseGitHttp");
  246. }
  247. return ($sRaOrigin==0);
  248. }