Browse Source
New Server Upgrade Package Builder script
New Server Upgrade Package Builder script
3 changed files with 310 additions and 0 deletions
@ -0,0 +1,310 @@ |
|||
#!/bin/php
|
|||
<?php |
|||
|
|||
function showProgress($current, $total, $barWidth = 50) { |
|||
// Prevent division by zero
|
|||
if ($total <= 0) return; |
|||
|
|||
// Calculate percentages and visual bar fill
|
|||
$percentage = round(($current / $total) * 100); |
|||
$filledLength = round(($barWidth * $percentage) / 100); |
|||
|
|||
// Create the visual block bar
|
|||
$bar = str_repeat("█", $filledLength) . str_repeat("-", $barWidth - $filledLength); |
|||
|
|||
// Write to standard output and return cursor to the start line via \r
|
|||
echo sprintf("\r[%s] %d%% (%d/%d)", $bar, $percentage, $current, $total); |
|||
|
|||
// Flush output buffer to show immediate updates
|
|||
flush(); |
|||
} |
|||
function checkCreateDir( $path ){ |
|||
if( substr( $path, -1 ) == '/' ) $path = substr( $path, 0, strlen( $path ) -1 ); |
|||
if( substr( $path, 0, 1 ) != '.' && substr( $path, 0, 1 ) != '/' ){ |
|||
$path = getcwd() . '/' . $path; |
|||
} else if( substr( $path, 0, 2 ) == './' ) { |
|||
$path = getcwd() . substr( $path, 1); |
|||
} else if( substr( $path, 0, 2 ) == '~/' ){ |
|||
$userInfo = posix_getpwuid(posix_getuid()); |
|||
$path = $userInfo['dir'] . substr( $path, 1); |
|||
} else if( substr( $path, 0, 2 ) == '..' ){ |
|||
$path = getcwd() . '/' . $path; |
|||
}; |
|||
$AR = explode( '/', $path ); |
|||
$CP = ''; |
|||
foreach( $AR as $R ){ |
|||
if( $R != '' ){ |
|||
$CP .= '/' . $R; |
|||
if( file_exists( $CP ) ){ |
|||
if( !is_dir( $CP ) ) { |
|||
echo "The path [{$path}] can not be created!\nThe [{$CP}] is a file!\n"; |
|||
return false; |
|||
}; |
|||
} else { |
|||
if( !mkdir( $CP ) ) { |
|||
echo "The path [{$path}] can not be created!\nFailed to create [{$CP}]!\n"; |
|||
return false; |
|||
}; |
|||
}; |
|||
}; |
|||
}; |
|||
return true; |
|||
} |
|||
function getTerminalWidth(int $default = 80): int |
|||
{ |
|||
// Check for Windows OS
|
|||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { |
|||
$mode = @shell_exec('mode con'); |
|||
if ($mode && preg_match('/Columns:\s+(\d+)/i', $mode, $matches)) { |
|||
return (int) $matches[1]; |
|||
} |
|||
} else { |
|||
// Linux / macOS / Unix platforms
|
|||
// Try stty first
|
|||
$stty = @shell_exec('stty size 2>/dev/null'); |
|||
if ($stty) { |
|||
$dimensions = explode(' ', trim($stty)); |
|||
if (isset($dimensions[1])) { |
|||
return (int) $dimensions[1]; |
|||
} |
|||
} |
|||
|
|||
// Fallback to tput
|
|||
$tput = @shell_exec('tput cols 2>/dev/null'); |
|||
if ($tput !== null && is_numeric(trim($tput))) { |
|||
return (int) trim($tput); |
|||
} |
|||
} |
|||
|
|||
// Default fallback if detection fails
|
|||
return $default; |
|||
} |
|||
|
|||
function buildAndInstall( $TITLE, $PROJECT_DIR, $PROJECT_BUILD_DIR, $TARGET_DIR, $TARGET_NAME ){ |
|||
global $BUILDER_PATH, $SRC_GIT_ROOT, $BUILD_ROOT; |
|||
if( stripos( $PROJECT_BUILD_DIR, 'debug' ) !== false ) { |
|||
$LTARGET = $TARGET_NAME . '_d'; |
|||
} else { |
|||
$LTARGET = $TARGET_NAME; |
|||
}; |
|||
if( file_exists( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ) ){ |
|||
echo "Curently version of {$TITLE} is: " . date( "Ymd", filemtime( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ) ) ."\n"; |
|||
$aa = readline("Do you wish to update to more recent?[Y/n]: " ); |
|||
if( $aa == '' || strtolower( $aa ) == 'y' ) { |
|||
if( file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){ |
|||
echo "We have version " . date( "Ymd", filemtime( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ) . " at GIT repository\n"; |
|||
$aa = readline("Do you wish to rebuild it?[N/y]: " ); |
|||
if( strtolower( $aa ) == 'y' ){ |
|||
chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR ); |
|||
system( './build' ); |
|||
if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){ |
|||
echo "There was a build Error - Fix it then rerun!\n"; |
|||
exit(); |
|||
}; |
|||
}; |
|||
} else { |
|||
$aa = readline("There is no builded version. Do you with build one?[Y/n]: " ); |
|||
if( $aa == '' || strtolower( $aa ) == 'y' ){ |
|||
chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR ); |
|||
system( './build' ); |
|||
if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){ |
|||
echo "There was a build Error - Fix it then rerun!\n"; |
|||
exit(); |
|||
}; |
|||
}; |
|||
}; |
|||
if( $LTARGET != $TARGET_NAME ){ |
|||
copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET, $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME ); |
|||
}; |
|||
$CC = 'strip ' . $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME; |
|||
system( $CC ); |
|||
chdir( $BUILDER_PATH ); |
|||
copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME, $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ); |
|||
if( $LTARGET != $TARGET_NAME ){ |
|||
unlink( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME ); |
|||
}; |
|||
chmod( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME, 0755 ); |
|||
}; |
|||
} else { |
|||
if( file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){ |
|||
echo "We have version " . date( "Ymd", filemtime( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ) . " at GIT repository\n"; |
|||
$aa = readline("Do you wish to rebuild it?[N/y]: " ); |
|||
if( strtolower( $aa ) == 'y' ){ |
|||
chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR ); |
|||
system( './build' ); |
|||
if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){ |
|||
echo "There was a build Error - Fix it then rerun!\n"; |
|||
exit(); |
|||
}; |
|||
}; |
|||
} else { |
|||
$aa = readline("There is no builded version. Do you with build one?[Y/n]: " ); |
|||
if( $aa == '' || strtolower( $aa ) == 'y' ){ |
|||
chdir( $SRC_GIT_ROOT.'/'.$PROJECT_DIR ); |
|||
system( './build' ); |
|||
if( !file_exists( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET ) ){ |
|||
echo "There was a build Error - Fix it then rerun!\n"; |
|||
exit(); |
|||
}; |
|||
}; |
|||
}; |
|||
if( $LTARGET != $TARGET_NAME ){ |
|||
copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$LTARGET, $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME ); |
|||
}; |
|||
$CC = 'strip ' . $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME; |
|||
system( $CC ); |
|||
chdir( $BUILDER_PATH ); |
|||
copy( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME, $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ); |
|||
if( $LTARGET != $TARGET_NAME ){ |
|||
unlink( $SRC_GIT_ROOT.'/'.$PROJECT_DIR.'/'.$PROJECT_BUILD_DIR.'/'.$TARGET_NAME ); |
|||
}; |
|||
chmod( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME, 0755 ); |
|||
}; |
|||
return file_exists( $BUILD_ROOT.'/'.$TARGET_DIR.'/'.$TARGET_NAME ); |
|||
} |
|||
|
|||
$GLOB_TERM_WIDTH = getTerminalWidth(); |
|||
$C_UID = posix_getuid(); |
|||
if( $C_UID != 0 ) { |
|||
echo "You Must be a ROOT to run this script\n"; |
|||
exit(); |
|||
}; |
|||
if( isset( $argv[1] ) ){ |
|||
$BUILDER_PATH = __DIR__; |
|||
$AR = explode('/', $BUILDER_PATH ); |
|||
$SRC_GIT_ROOT = ''; |
|||
if( substr( $argv[1], -1 ) == '/' ) $argv[1] = substr( $argv[1], 0, strlen( $argv[1] ) -1 ); |
|||
if( substr( $argv[1], 0, 2 ) == './' ) { |
|||
$argv[1] = getcwd() . substr( $argv[1], 1); |
|||
} else if( substr( $argv[1], 0, 2 ) == '~/' ){ |
|||
$userInfo = posix_getpwuid( $C_UID ); |
|||
$argv[1] = $userInfo['dir'] . substr( $argv[1], 1); |
|||
} else if( substr( $argv[1], 0, 2 ) == '..' ){ |
|||
$argv[1] = getcwd() . '/' . $argv[1]; |
|||
}; |
|||
|
|||
$WEB_GIT_ROOT = $argv[1]; |
|||
for( $i=1; $i < count( $AR ) -1; $i++ ) $SRC_GIT_ROOT .= '/' . $AR[$i]; |
|||
if( file_exists( $SRC_GIT_ROOT . '/Projects' ) ){ |
|||
if( file_exists( $WEB_GIT_ROOT . '/lib' ) ){ |
|||
if( !isset($argv[2]) ){ |
|||
$argv[2] = readline("The output dir is not set. Please provide one (empty enter will abort): " ); |
|||
}; |
|||
$BUILD_ROOT = $argv[2]; |
|||
if( $BUILD_ROOT != '' ){ |
|||
if( file_exists( $BUILD_ROOT ) ){ |
|||
$aa = readline("The output dir is exists. All filed will be wiped out! Do you wish to continue?[Y/n]: " ); |
|||
if( $aa == '' || strtolower( $aa ) == 'y' ) { |
|||
system( "rm -f -R {$BUILD_ROOT}" ); |
|||
checkCreateDir( $BUILD_ROOT ); |
|||
} else exit(); |
|||
} else { |
|||
$aa = readline("The output dir is not exists. Do you wish to create?[Y/n]: " ); |
|||
if( $aa == '' || strtolower( $aa ) == 'y' ) { |
|||
if( !checkCreateDir( $BUILD_ROOT ) ) exit(); |
|||
} else exit(); |
|||
}; |
|||
echo "Source GIT is OK \t[{$SRC_GIT_ROOT}]\n"; |
|||
echo "Web GIT is OK \t\t[{$WEB_GIT_ROOT}]\n"; |
|||
echo "Build path is OK \t[$BUILD_ROOT]\n"; |
|||
|
|||
//Prep Folders
|
|||
$CC = 'tar -xvjf '.$BUILDER_PATH . '/server.tar.bz2 -C ' . $BUILD_ROOT; |
|||
system( $CC ); |
|||
|
|||
if( !buildAndInstall( 'Serva', 'Projects/la_serva', 'Debug86', 'serva', 'serva' ) ){ |
|||
echo "The Software Binary is missing!\n"; |
|||
exit(); |
|||
}; |
|||
if( !buildAndInstall( 'Watchdog', 'Projects/watchDogDaemon', 'Release', 'watchdog', 'watchdogDaemon' ) ){ |
|||
echo "The Software Binary is missing!\n"; |
|||
exit(); |
|||
}; |
|||
if( !buildAndInstall( 'Startup', 'Projects/starProcDaemon', 'Release', 'start', 'starProcDaemon' ) ){ |
|||
echo "The Software Binary is missing!\n"; |
|||
exit(); |
|||
}; |
|||
|
|||
|
|||
// Web UI
|
|||
checkCreateDir( $BUILD_ROOT . '/www/server' ); |
|||
$fd = fopen( $WEB_GIT_ROOT . "/files.list", "r"); |
|||
$PD = array(); |
|||
$f_cnt = 0; |
|||
$d_cnt = 0; |
|||
if( $fd ) { |
|||
while( ($cl = fgets($fd) ) !== false ){ |
|||
$cl = trim( $cl ); |
|||
$CI = array(); |
|||
if( substr( $cl, 0, 2 ) == './' ) $cl = substr( $cl, 1 ); |
|||
$citem = $WEB_GIT_ROOT . $cl; |
|||
if( file_exists( $citem ) ){ |
|||
$CI['name'] = $cl; |
|||
if( is_dir( $citem ) ){ |
|||
$CI['type']='D'; |
|||
$d_cnt++; |
|||
} else { |
|||
$CI['type']='F'; |
|||
$f_cnt++; |
|||
}; |
|||
$PD[] = $CI; |
|||
} else { |
|||
echo "The File [{$cl}] Does not exists!!\n"; |
|||
}; |
|||
}; |
|||
fclose($fd); |
|||
echo "Loaded " . count( $PD ) . " Items\n"; |
|||
echo "\t{$d_cnt} Directories\n"; |
|||
echo "\t{$f_cnt} Files\n"; |
|||
//create Sceleton
|
|||
$i = 0; |
|||
echo "Create Web Structure\n"; |
|||
foreach( $PD as $D ) if( $D['type'] == 'D' ) { |
|||
checkCreateDir( $BUILD_ROOT . '/www/server' . $D['name'] ); |
|||
$i++; |
|||
showProgress( $i, $d_cnt, $GLOB_TERM_WIDTH - 30 ); |
|||
}; |
|||
$i = 0; |
|||
echo "\nCopy Content\n"; |
|||
foreach( $PD as $D ) if( $D['type'] == 'F' ){ |
|||
copy( $WEB_GIT_ROOT . $D['name'], $BUILD_ROOT . '/www/server' . $D['name'] ); |
|||
$i++; |
|||
showProgress( $i, $f_cnt, $GLOB_TERM_WIDTH - 30 ); |
|||
}; |
|||
|
|||
//Special
|
|||
$CC = 'ln -s ../../../../data '. $BUILD_ROOT . '/www/server' . '/lib/controller/data'; |
|||
system( $CC ); |
|||
$CC = 'chown -R www-data:www-data ' . $BUILD_ROOT . '/www'; |
|||
system( $CC ); |
|||
|
|||
//Create Pack
|
|||
$CC="tar -cvjf {$BUILDER_PATH}/tmp_serv/upgrade.tar.bz2 --absolute-names -C {$BUILD_ROOT} ."; |
|||
system( $CC ); |
|||
$MD5 = md5_file( $BUILDER_PATH .'/tmp_serv/upgrade.tar.bz2' ); |
|||
chdir( $BUILDER_PATH . '/pp_cc' ); |
|||
$CERT = trim(`php ./create_backup_cert.php {$MD5}`); |
|||
echo "Cert: {$CERT}\n"; |
|||
copy( $CERT, '../tmp_serv/manifest.crt' ); |
|||
unlink( $CERT ); |
|||
chdir( '..' ); |
|||
$PN = 'server_package_' . date("Ymd",time()); |
|||
$CC="tar -cvjf {$BUILDER_PATH}/{$PN}.tar.bz2 --absolute-names -C {$BUILDER_PATH}/tmp_serv ."; |
|||
system( $CC ); |
|||
unlink( $BUILDER_PATH . '/tmp_serv/manifest.crt' ); |
|||
unlink( $BUILDER_PATH . '/tmp_serv/upgrade.tar.bz2' ); |
|||
echo "All Done!\n"; |
|||
} else { |
|||
// Error opening the file
|
|||
}; |
|||
}; |
|||
} else { |
|||
echo "Cannot Detect Web Repository [{$WEB_GIT_ROOT}]\nAre you sure that you have supplied proper path?\n"; |
|||
}; |
|||
} else { |
|||
echo "Cannot Detect Source Repository\nAre you sure that you Run proper build tool?\n"; |
|||
}; |
|||
} else { |
|||
echo "Error: No Parameter Supplied!\nUsage: {$argv[0]} <path_to_a_web_repo> [path_to_output_dir]\n"; |
|||
}; |
|||
?>
|
|||
Binary file not shown.
Binary file not shown.
Write
Preview
Loading…
Cancel
Save
Reference in new issue