123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- <?php
- //默认配置信息
- function defaultConfig(){
- return array(
- 'distribute-mode' => 'install',
- 'tool-dir' => '',
- //'oss-host' => 'http://sis31-disthelp.oss-cn-hangzhou.aliyuncs.com',
- 'oss-host' => 'http://192.168.3.10:8996',
- 'mysql-host'=> '',
- );
- }
- //记录信息日志
- function logInfo(){
- $content = '[INF] ';
- $args = func_get_args();
- $argc = func_num_args();
- for($i=0;$i<$argc;$i++){
- $content .= $args[$i] . ' ';
- }
- $content .= PHP_EOL;
- print($content);
- file_put_contents(LOG_PATH, $content, FILE_APPEND);
- }
- //记录错误日志
- function logError(){
- $content = '[ERR] ';
- $args = func_get_args();
- $argc = func_num_args();
- for($i=0;$i<$argc;$i++){
- $content .= $args[$i] . ' ';
- }
- $content .= PHP_EOL;
- print($content);
- file_put_contents(LOG_PATH, $content, FILE_APPEND);
- }
- //获取用户IP
- function getClientIp(){
- $url = 'http://8.136.238.47:3000/user/clientip';
- $data = file_get_contents($url);
- if($data === false){
- logError('failed to get client ip',$url);
- return false;
- }
- $json = json_decode($data);
- if(!$json){
- logError('failed to parse client ip data',$data);
- return false;
- }
- if(!$json['data']['ip']){
- logError('failed to parse client ip',$data);
- return false;
- }
- return $json['data']['ip'];
- }
- //获取操作系统名称
- function getOsName(){
- if(substr(PHP_OS,0,3)==='WIN')
- return "win";
- else
- return "linux";
- }
- //获取当前缓存目录
- function getCacheDir(){
- $os = getOsName();
- if( $os == 'win'){
- $path = __FILE__;
- $disk = substr($path,0,2);
- return $disk . '/$DIST.CACHE';
- }
- else{
- return '/tmp/distcache';
- }
- }
- //获取日志目录
- function getLysis3LogDir(){
- $user = get_current_user();
- return "c:/Users/{$user}/AppData/Local/lysis3/logs";
- }
- //判断是否键值对类型的数组
- function isAssocArray($array){
- if(! is_array($array) )
- return false;
- return array_keys($array) !== range(0, count($array) - 1);
- }
- //获取文件扩展名,小写
- function getFileExt($fileName){
- return strtolower(pathinfo($fileName,PATHINFO_EXTENSION));
- }
- //是否压缩文件
- function isZipFile($fileName){
- $fileExt = getFileExt($fileName);
- return in_array($fileExt,array("zip","7z"));
- }
- //获取 gopeed.exe 路径
- function getGoPeedPath($toolDir){
- if(!$toolDir)
- return 'gopeed.exe';
- return pathJoin($toolDir,'gopeed.exe');
- }
- //获取 7z.exe 路径
- function get7ZipPath($toolDir){
- if(!$toolDir)
- return '7z.exe';
- return pathJoin($toolDir,'7z.exe');
- }
- //两个参数的路径拼接
- function twoPathJoin($base, $path) {
- $base = str_replace('\\','/',$base);
- $path = str_replace('\\','/',$path);
- return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
- }
- //支持中文的递归创建目录
- //iconv方法是为了防止中文乱码,保证可以创建识别中文目录
- //不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
- function createDirectory($path,$encoding="GBK"){
- mkdir(iconv("UTF-8", $encoding ,$path),0777,true);
- }
- //不定参数路径拼接
- function pathJoin($base, $path) {
- $newPath = twoPathJoin($base, $path);
- $args = func_get_args();
- $argc = func_num_args();
- for($i=2;$i<$argc;$i++){
- $newPath = twoPathJoin($newPath,$args[$i]);
- }
- return $newPath;
- }
- //拷贝文件,自动创建目录,自动覆盖
- function fileCopy(string $sourcePath, string $targetPath){
- $targetDir = dirname($targetPath);
- if(!is_dir($targetDir)){
- createDirectory($targetDir);
- }
-
- if( file_exists($targetPath) ){
- unlink($targetPath);
- }
- copy($sourcePath,$targetPath);
- return file_exists($targetPath);
- }
- //拷贝目录,自动创建目录,自动覆盖
- function dirCopy(string $sourceDir, string $targetDir){
- if(! is_dir($targetDir)){
- createDirectory($targetDir);
- }
- $dir = opendir($sourceDir);
- while(false !== ( $file = readdir($dir)) ) {
- if($file == '.' ){
- continue;
- }
- if($file == '..' ){
- continue;
- }
- $filePath = $sourceDir . '/' . $file;
- $targetPath = $targetDir . '/' . $file;
- if ( is_dir($filePath) ) {
- if(! dirCopy($filePath,$targetPath) ){
- logError('failed to copy dir',$filePath,'->',$targetPath);
- return false;
- }
- }
- else {
- copy($filePath,$targetPath);
- if(! file_exists($targetPath) ){
- logError('failed to copy file',$filePath,'->',$targetPath);
- return false;
- }
- }
- }
- closedir($dir);
- return true;
- }
- //拷贝文件或目录
- function filedirCopy(string $sourcePath, string $targetPath){
- if(! file_exists($sourcePath)){
- logError('failed to copy file',$sourcePath,'->',$targetPath);
- return false;
- }
- if( is_dir($sourcePath)){
- return dirCopy($sourcePath,$targetPath);
- }else{
- return fileCopy($sourcePath,$targetPath);
- }
- }
- //替换文件中的宏变量
- function replaceMacro(string $filePath,string $targetPath,array $macros){
- $content = file_get_contents($filePath);
- foreach($macros as $key=>$value){
- $content = str_replace($key,$value,$content);
- }
- return file_put_contents($targetPath,$content);
- }
- //枚举子目录
- function scanSubDir($dir,$exclude_names = array()){
- $filelist = scandir($dir);
- $subdirs = array();
- foreach($filelist as $filename){
- //过滤掉当前目录和上级目录
- if($filename == "." || $filename ==".."){
- continue;
- }
- //过滤掉排查的名称
- if(in_array($filename,$exclude_names)){
- continue;
- }
- //判断是否是文件夹
- $filepath = pathJoin($dir,$filename);
- if(is_dir($filepath) ){
- array_push($subdirs,$filename);
- }
- }
- return $subdirs;
- }
- //枚举所有仓库名
- function scanRepoNames($vendoDir){
- $repos = array();
- $users = scanSubDir($vendorDir);
- foreach($users as $user){
- $userdir = pathJoin($vendorDir,$user);
- $names = scanSubDir($userdir);
- foreach($names as $name){
- array_push($repos,$user . '/' . $name);
- }
- }
- array_push($repos,"."); //.名称表示顶层包,放最后
- return $repos;
- }
- //解析所有仓库配置
- function loadRepoConfigs($vendorDir,$repoNames){
- $configs = array();
- foreach($repoNames as $repoName){
- if($repoName == '.')
- $cfgPath = pathJoin(dirname($vendorDir),'composer.json');
- else
- $cfgPath = pathJoin($vendorDir,$repoName,'composer.json');
- $data = json_decode(file_get_contents($cfgPath), true);
- if(!$data){
- logError('invalid composer.json',$cfgPath);
- return false;
- }
- $configs[$repoName] = $data;
- }
- return $configs;
- }
- //解析部署配置文件
- function loadConfig($cfgPath){
- $config = json_decode(file_get_contents($cfgPath), true);
- if(!$config){
- logError("failed to decode config file",$cfgPath);
- return false;
- }
-
- if(! $config['distribute-mode']){
- logError("distribute-mode node not exists");
- return false;
- }
- return $config;
- }
- //多线程下载文件
- function downloadToDir($toolDir,$ossHost,$repoName,$fileName,$targetDir){
- if (!file_exists($targetDir)){
- mkdir($targetDir,0777,true);
- }
- $targetPath = pathJoin($targetDir,$fileName);
- if( file_exists($targetPath) ) {
- return $targetPath;
- }
- $gopeed = getGoPeedPath($toolDir);
-
- $remoteUrl = pathJoin($ossHost,$repoName,$fileName);
- $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
- logInfo("start to fetch common file --",$cmd);
- system($cmd, $ret);
- if ($ret != 0) {
- return false;
- }
- return $targetPath;
- }
- //解压缩文件
- function unzipFile($toolDir,$zipFile, $targetDir){
- $fileExt = getFileExt($zipFile);
- if($fileExt == "zip"){
- $zip= new \ZipArchive;
- if($zip->open($zipFile)==true){
- $zip->extractTo($targetDir);
- $zip->close();
- return true;
- }
- else{
- return false;
- }
- }
- if($fileExt == "7z"){
- $sevenZip = get7ZipPath($toolDir);
- $logPath = $zipFile . '.log';
- $cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath;
- system($cmd, $ret);
- if ($ret != 0) {
- return false;
- }
- return true;
- }
- return false;
- }
|