You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
11 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #!/bin/php
  2. <?php
  3. function showProgress($current, $total, $barWidth = 50) {
  4. // Prevent division by zero
  5. if ($total <= 0) return;
  6. // Calculate percentages and visual bar fill
  7. $percentage = round(($current / $total) * 100);
  8. $filledLength = round(($barWidth * $percentage) / 100);
  9. // Create the visual block bar
  10. $bar = str_repeat("█", $filledLength) . str_repeat("-", $barWidth - $filledLength);
  11. // Write to standard output and return cursor to the start line via \r
  12. echo sprintf("\r[%s] %d%% (%d/%d)", $bar, $percentage, $current, $total);
  13. // Flush output buffer to show immediate updates
  14. flush();
  15. }
  16. function checkCreateDir( $path ){
  17. if( substr( $path, -1 ) == '/' ) $path = substr( $path, 0, strlen( $path ) -1 );
  18. if( substr( $path, 0, 1 ) != '.' && substr( $path, 0, 1 ) != '/' ){
  19. $path = getcwd() . '/' . $path;
  20. } else if( substr( $path, 0, 2 ) == './' ) {
  21. $path = getcwd() . substr( $path, 1);
  22. } else if( substr( $path, 0, 2 ) == '~/' ){
  23. $userInfo = posix_getpwuid(posix_getuid());
  24. $path = $userInfo['dir'] . substr( $path, 1);
  25. } else if( substr( $path, 0, 2 ) == '..' ){
  26. $path = getcwd() . '/' . $path;
  27. };
  28. $AR = explode( '/', $path );
  29. $CP = '';
  30. foreach( $AR as $R ){
  31. if( $R != '' ){
  32. $CP .= '/' . $R;
  33. if( file_exists( $CP ) ){
  34. if( !is_dir( $CP ) ) {
  35. echo "The path [{$path}] can not be created!\nThe [{$CP}] is a file!\n";
  36. return false;
  37. };
  38. } else {
  39. if( !mkdir( $CP ) ) {
  40. echo "The path [{$path}] can not be created!\nFailed to create [{$CP}]!\n";
  41. return false;
  42. };
  43. };
  44. };
  45. };
  46. return true;
  47. }
  48. function getTerminalWidth(int $default = 80): int
  49. {
  50. // Check for Windows OS
  51. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  52. $mode = @shell_exec('mode con');
  53. if ($mode && preg_match('/Columns:\s+(\d+)/i', $mode, $matches)) {
  54. return (int) $matches[1];
  55. }
  56. } else {
  57. // Linux / macOS / Unix platforms
  58. // Try stty first
  59. $stty = @shell_exec('stty size 2>/dev/null');
  60. if ($stty) {
  61. $dimensions = explode(' ', trim($stty));
  62. if (isset($dimensions[1])) {
  63. return (int) $dimensions[1];
  64. }
  65. }
  66. // Fallback to tput
  67. $tput = @shell_exec('tput cols 2>/dev/null');
  68. if ($tput !== null && is_numeric(trim($tput))) {
  69. return (int) trim($tput);
  70. }
  71. }
  72. // Default fallback if detection fails
  73. return $default;
  74. }
  75. function buildAndInstall( $TITLE, $PROJECT_DIR, $PROJECT_BUILD_DIR, $TARGET_DIR, $TARGET_NAME ){
  76. global $BUILDER_PATH, $SRC_GIT_ROOT, $BUILD_ROOT;
  77. if( stripos( $PROJECT_BUILD_DIR, 'debug' ) !== false ) {
  78. $LTARGET = $TARGET_NAME . '_d';
  79. } else {
  80. $LTARGET = $TARGET_NAME;
  81. };
  82. if( file_exists( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ) ){
  83. echo "Curently version of {$TITLE} is: " . date( "Ymd", filemtime( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ) ) ."\n";
  84. $aa = readline("Do you wish to update to more recent?[Y/n]: " );
  85. if( $aa == '' || strtolower( $aa ) == 'y' ) {
  86. if( file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){
  87. echo "We have version " . date( "Ymd", filemtime( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ) . " at GIT repository\n";
  88. $aa = readline("Do you wish to rebuild it?[N/y]: " );
  89. if( strtolower( $aa ) == 'y' ){
  90. chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR );
  91. system( './build' );
  92. if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){
  93. echo "There was a build Error - Fix it then rerun!\n";
  94. exit();
  95. };
  96. };
  97. } else {
  98. $aa = readline("There is no builded version. Do you with build one?[Y/n]: " );
  99. if( $aa == '' || strtolower( $aa ) == 'y' ){
  100. chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR );
  101. system( './build' );
  102. if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){
  103. echo "There was a build Error - Fix it then rerun!\n";
  104. exit();
  105. };
  106. };
  107. };
  108. if( $LTARGET != $TARGET_NAME ){
  109. copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET, $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME );
  110. };
  111. $CC = 'strip ' . $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME;
  112. system( $CC );
  113. chdir( $BUILDER_PATH );
  114. copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME, $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME );
  115. if( $LTARGET != $TARGET_NAME ){
  116. unlink( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME );
  117. };
  118. chmod( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME, 0755 );
  119. };
  120. } else {
  121. if( file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){
  122. echo "We have version " . date( "Ymd", filemtime( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ) . " at GIT repository\n";
  123. $aa = readline("Do you wish to rebuild it?[N/y]: " );
  124. if( strtolower( $aa ) == 'y' ){
  125. chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR );
  126. system( './build' );
  127. if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){
  128. echo "There was a build Error - Fix it then rerun!\n";
  129. exit();
  130. };
  131. };
  132. } else {
  133. $aa = readline("There is no builded version. Do you with build one?[Y/n]: " );
  134. if( $aa == '' || strtolower( $aa ) == 'y' ){
  135. chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR );
  136. system( './build' );
  137. if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){
  138. echo "There was a build Error - Fix it then rerun!\n";
  139. exit();
  140. };
  141. };
  142. };
  143. if( $LTARGET != $TARGET_NAME ){
  144. copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET, $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME );
  145. };
  146. $CC = 'strip ' . $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME;
  147. system( $CC );
  148. chdir( $BUILDER_PATH );
  149. copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME, $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME );
  150. if( $LTARGET != $TARGET_NAME ){
  151. unlink( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME );
  152. };
  153. chmod( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME, 0755 );
  154. };
  155. return file_exists( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME );
  156. }
  157. $GLOB_TERM_WIDTH = getTerminalWidth();
  158. $C_UID = posix_getuid();
  159. if( $C_UID != 0 ) {
  160. echo "You Must be a ROOT to run this script\n";
  161. exit();
  162. };
  163. if( isset( $argv[1] ) ){
  164. $BUILDER_PATH = __DIR__;
  165. $AR = explode('/', $BUILDER_PATH );
  166. $SRC_GIT_ROOT = '';
  167. if( substr( $argv[1], -1 ) == '/' ) $argv[1] = substr( $argv[1], 0, strlen( $argv[1] ) -1 );
  168. if( substr( $argv[1], 0, 2 ) == './' ) {
  169. $argv[1] = getcwd() . substr( $argv[1], 1);
  170. } else if( substr( $argv[1], 0, 2 ) == '~/' ){
  171. $userInfo = posix_getpwuid( $C_UID );
  172. $argv[1] = $userInfo['dir'] . substr( $argv[1], 1);
  173. } else if( substr( $argv[1], 0, 2 ) == '..' ){
  174. $argv[1] = getcwd() . '/' . $argv[1];
  175. };
  176. $WEB_GIT_ROOT = $argv[1];
  177. for( $i=1; $i < count( $AR ) -1; $i++ ) $SRC_GIT_ROOT .= '/' . $AR[$i];
  178. if( file_exists( $SRC_GIT_ROOT . '/Projects' ) ){
  179. if( file_exists( $WEB_GIT_ROOT . '/lib' ) ){
  180. if( !isset($argv[2]) ){
  181. $argv[2] = readline("The output dir is not set. Please provide one (empty enter will abort): " );
  182. };
  183. $BUILD_ROOT = $argv[2];
  184. if( $BUILD_ROOT != '' ){
  185. if( file_exists( $BUILD_ROOT ) ){
  186. $aa = readline("The output dir is exists. All filed will be wiped out! Do you wish to continue?[Y/n]: " );
  187. if( $aa == '' || strtolower( $aa ) == 'y' ) {
  188. system( "rm -f -R {$BUILD_ROOT}" );
  189. checkCreateDir( $BUILD_ROOT );
  190. } else exit();
  191. } else {
  192. $aa = readline("The output dir is not exists. Do you wish to create?[Y/n]: " );
  193. if( $aa == '' || strtolower( $aa ) == 'y' ) {
  194. if( !checkCreateDir( $BUILD_ROOT ) ) exit();
  195. } else exit();
  196. };
  197. echo "Source GIT is OK \t[{$SRC_GIT_ROOT}]\n";
  198. echo "Web GIT is OK \t\t[{$WEB_GIT_ROOT}]\n";
  199. echo "Build path is OK \t[$BUILD_ROOT]\n";
  200. //Prep Folders
  201. $CC = 'tar -xvjf '.$BUILDER_PATH . '/server.tar.bz2 -C ' . $BUILD_ROOT;
  202. system( $CC );
  203. if( !buildAndInstall( 'Serva', 'Projects/la_serva', 'Debug86', 'serva', 'serva' ) ){
  204. echo "The Software Binary is missing!\n";
  205. exit();
  206. };
  207. if( !buildAndInstall( 'Watchdog', 'Projects/watchDogDaemon', 'Release', 'watchdog', 'watchdogDaemon' ) ){
  208. echo "The Software Binary is missing!\n";
  209. exit();
  210. };
  211. if( !buildAndInstall( 'Startup', 'Projects/starProcDaemon', 'Release', 'start', 'starProcDaemon' ) ){
  212. echo "The Software Binary is missing!\n";
  213. exit();
  214. };
  215. // Web UI
  216. checkCreateDir( $BUILD_ROOT . '/www/server' );
  217. $fd = fopen( $WEB_GIT_ROOT . "/files.list", "r");
  218. $PD = array();
  219. $f_cnt = 0;
  220. $d_cnt = 0;
  221. if( $fd ) {
  222. while( ($cl = fgets($fd) ) !== false ){
  223. $cl = trim( $cl );
  224. $CI = array();
  225. if( substr( $cl, 0, 2 ) == './' ) $cl = substr( $cl, 1 );
  226. $citem = $WEB_GIT_ROOT . $cl;
  227. if( file_exists( $citem ) ){
  228. $CI['name'] = $cl;
  229. if( is_dir( $citem ) ){
  230. $CI['type']='D';
  231. $d_cnt++;
  232. } else {
  233. $CI['type']='F';
  234. $f_cnt++;
  235. };
  236. $PD[] = $CI;
  237. } else {
  238. echo "The File [{$cl}] Does not exists!!\n";
  239. };
  240. };
  241. fclose($fd);
  242. echo "Loaded " . count( $PD ) . " Items\n";
  243. echo "\t{$d_cnt} Directories\n";
  244. echo "\t{$f_cnt} Files\n";
  245. //create Sceleton
  246. $i = 0;
  247. echo "Create Web Structure\n";
  248. foreach( $PD as $D ) if( $D['type'] == 'D' ) {
  249. checkCreateDir( $BUILD_ROOT . '/www/server' . $D['name'] );
  250. $i++;
  251. showProgress( $i, $d_cnt, $GLOB_TERM_WIDTH - 30 );
  252. };
  253. $i = 0;
  254. echo "\nCopy Content\n";
  255. foreach( $PD as $D ) if( $D['type'] == 'F' ){
  256. copy( $WEB_GIT_ROOT . $D['name'], $BUILD_ROOT . '/www/server' . $D['name'] );
  257. $i++;
  258. showProgress( $i, $f_cnt, $GLOB_TERM_WIDTH - 30 );
  259. };
  260. //Special
  261. $CC = 'ln -s ../../../../data '. $BUILD_ROOT . '/www/server' . '/lib/controller/data';
  262. system( $CC );
  263. $CC = 'chown -R www-data:www-data ' . $BUILD_ROOT . '/www';
  264. system( $CC );
  265. //Create Pack
  266. $CC="tar -cvjf {$BUILDER_PATH}/tmp_serv/upgrade.tar.bz2 --absolute-names -C {$BUILD_ROOT} .";
  267. system( $CC );
  268. $MD5 = md5_file( $BUILDER_PATH .'/tmp_serv/upgrade.tar.bz2' );
  269. chdir( $BUILDER_PATH . '/pp_cc' );
  270. $CERT = trim(`php ./create_backup_cert.php {$MD5}`);
  271. echo "Cert: {$CERT}\n";
  272. copy( $CERT, '../tmp_serv/manifest.crt' );
  273. unlink( $CERT );
  274. chdir( '..' );
  275. $PN = 'server_package_' . date("Ymd",time());
  276. $CC="tar -cvjf {$BUILDER_PATH}/{$PN}.tar.bz2 --absolute-names -C {$BUILDER_PATH}/tmp_serv .";
  277. system( $CC );
  278. unlink( $BUILDER_PATH . '/tmp_serv/manifest.crt' );
  279. unlink( $BUILDER_PATH . '/tmp_serv/upgrade.tar.bz2' );
  280. echo "All Done!\n";
  281. } else {
  282. // Error opening the file
  283. };
  284. };
  285. } else {
  286. echo "Cannot Detect Web Repository [{$WEB_GIT_ROOT}]\nAre you sure that you have supplied proper path?\n";
  287. };
  288. } else {
  289. echo "Cannot Detect Source Repository\nAre you sure that you Run proper build tool?\n";
  290. };
  291. } else {
  292. echo "Error: No Parameter Supplied!\nUsage: {$argv[0]} <path_to_a_web_repo> [path_to_output_dir]\n";
  293. };
  294. ?>