Utility.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. //默认配置信息
  3. function defaultConfig(){
  4. return array(
  5. 'distribute-mode' => 'install',
  6. 'tool-dir' => '',
  7. //'oss-host' => 'http://sis31-disthelp.oss-cn-hangzhou.aliyuncs.com',
  8. 'oss-host' => 'http://192.168.3.10:8996',
  9. 'mysql-host'=> '',
  10. );
  11. }
  12. //记录信息日志
  13. function logInfo(){
  14. $content = '[INF] ';
  15. $args = func_get_args();
  16. $argc = func_num_args();
  17. for($i=0;$i<$argc;$i++){
  18. $content .= $args[$i] . ' ';
  19. }
  20. $content .= PHP_EOL;
  21. print($content);
  22. file_put_contents(LOG_PATH, $content, FILE_APPEND);
  23. }
  24. //记录错误日志
  25. function logError(){
  26. $content = '[ERR] ';
  27. $args = func_get_args();
  28. $argc = func_num_args();
  29. for($i=0;$i<$argc;$i++){
  30. $content .= $args[$i] . ' ';
  31. }
  32. $content .= PHP_EOL;
  33. print($content);
  34. file_put_contents(LOG_PATH, $content, FILE_APPEND);
  35. }
  36. //获取用户IP
  37. function getClientIp(){
  38. $url = 'http://8.136.238.47:3000/user/clientip';
  39. $data = file_get_contents($url);
  40. if($data === false){
  41. logError('failed to get client ip',$url);
  42. return false;
  43. }
  44. $json = json_decode($data);
  45. if(!$json){
  46. logError('failed to parse client ip data',$data);
  47. return false;
  48. }
  49. if(!$json['data']['ip']){
  50. logError('failed to parse client ip',$data);
  51. return false;
  52. }
  53. return $json['data']['ip'];
  54. }
  55. //获取操作系统名称
  56. function getOsName(){
  57. if(substr(PHP_OS,0,3)==='WIN')
  58. return "win";
  59. else
  60. return "linux";
  61. }
  62. //获取当前缓存目录
  63. function getCacheDir(){
  64. $os = getOsName();
  65. if( $os == 'win'){
  66. $path = __FILE__;
  67. $disk = substr($path,0,2);
  68. return $disk . '/$DIST.CACHE';
  69. }
  70. else{
  71. return '/tmp/distcache';
  72. }
  73. }
  74. //获取日志目录
  75. function getLysis3LogDir(){
  76. $user = get_current_user();
  77. return "c:/Users/{$user}/AppData/Local/lysis3/logs";
  78. }
  79. //判断是否键值对类型的数组
  80. function isAssocArray($array){
  81. if(! is_array($array) )
  82. return false;
  83. return array_keys($array) !== range(0, count($array) - 1);
  84. }
  85. //获取文件扩展名,小写
  86. function getFileExt($fileName){
  87. return strtolower(pathinfo($fileName,PATHINFO_EXTENSION));
  88. }
  89. //是否压缩文件
  90. function isZipFile($fileName){
  91. $fileExt = getFileExt($fileName);
  92. return in_array($fileExt,array("zip","7z"));
  93. }
  94. //获取 gopeed.exe 路径
  95. function getGoPeedPath($toolDir){
  96. if(!$toolDir)
  97. return 'gopeed.exe';
  98. return pathJoin($toolDir,'gopeed.exe');
  99. }
  100. //获取 7z.exe 路径
  101. function get7ZipPath($toolDir){
  102. if(!$toolDir)
  103. return '7z.exe';
  104. return pathJoin($toolDir,'7z.exe');
  105. }
  106. //两个参数的路径拼接
  107. function twoPathJoin($base, $path) {
  108. $base = str_replace('\\','/',$base);
  109. $path = str_replace('\\','/',$path);
  110. return rtrim( $base, '/' ) . '/' . ltrim( $path, '/' );
  111. }
  112. //支持中文的递归创建目录
  113. //iconv方法是为了防止中文乱码,保证可以创建识别中文目录
  114. //不用iconv方法格式的话,将无法创建中文目录,第三参数的开启递归模式,默认是关闭的
  115. function createDirectory($path,$encoding="GBK"){
  116. mkdir(iconv("UTF-8", $encoding ,$path),0777,true);
  117. }
  118. //不定参数路径拼接
  119. function pathJoin($base, $path) {
  120. $newPath = twoPathJoin($base, $path);
  121. $args = func_get_args();
  122. $argc = func_num_args();
  123. for($i=2;$i<$argc;$i++){
  124. $newPath = twoPathJoin($newPath,$args[$i]);
  125. }
  126. return $newPath;
  127. }
  128. //拷贝文件,自动创建目录,自动覆盖
  129. function fileCopy(string $sourcePath, string $targetPath){
  130. $targetDir = dirname($targetPath);
  131. if(!is_dir($targetDir)){
  132. createDirectory($targetDir);
  133. }
  134. if( file_exists($targetPath) ){
  135. unlink($targetPath);
  136. }
  137. copy($sourcePath,$targetPath);
  138. return file_exists($targetPath);
  139. }
  140. //拷贝目录,自动创建目录,自动覆盖
  141. function dirCopy(string $sourceDir, string $targetDir){
  142. if(! is_dir($targetDir)){
  143. createDirectory($targetDir);
  144. }
  145. $dir = opendir($sourceDir);
  146. while(false !== ( $file = readdir($dir)) ) {
  147. if($file == '.' ){
  148. continue;
  149. }
  150. if($file == '..' ){
  151. continue;
  152. }
  153. $filePath = $sourceDir . '/' . $file;
  154. $targetPath = $targetDir . '/' . $file;
  155. if ( is_dir($filePath) ) {
  156. if(! dirCopy($filePath,$targetPath) ){
  157. logError('failed to copy dir',$filePath,'->',$targetPath);
  158. return false;
  159. }
  160. }
  161. else {
  162. copy($filePath,$targetPath);
  163. if(! file_exists($targetPath) ){
  164. logError('failed to copy file',$filePath,'->',$targetPath);
  165. return false;
  166. }
  167. }
  168. }
  169. closedir($dir);
  170. return true;
  171. }
  172. //拷贝文件或目录
  173. function filedirCopy(string $sourcePath, string $targetPath){
  174. if(! file_exists($sourcePath)){
  175. logError('failed to copy file',$sourcePath,'->',$targetPath);
  176. return false;
  177. }
  178. if( is_dir($sourcePath)){
  179. return dirCopy($sourcePath,$targetPath);
  180. }else{
  181. return fileCopy($sourcePath,$targetPath);
  182. }
  183. }
  184. //替换文件中的宏变量
  185. function replaceMacro(string $filePath,string $targetPath,array $macros){
  186. $content = file_get_contents($filePath);
  187. foreach($macros as $key=>$value){
  188. $content = str_replace($key,$value,$content);
  189. }
  190. return file_put_contents($targetPath,$content);
  191. }
  192. //枚举子目录
  193. function scanSubDir($dir,$exclude_names = array()){
  194. $filelist = scandir($dir);
  195. $subdirs = array();
  196. foreach($filelist as $filename){
  197. //过滤掉当前目录和上级目录
  198. if($filename == "." || $filename ==".."){
  199. continue;
  200. }
  201. //过滤掉排查的名称
  202. if(in_array($filename,$exclude_names)){
  203. continue;
  204. }
  205. //判断是否是文件夹
  206. $filepath = pathJoin($dir,$filename);
  207. if(is_dir($filepath) ){
  208. array_push($subdirs,$filename);
  209. }
  210. }
  211. return $subdirs;
  212. }
  213. //枚举所有仓库名
  214. function scanRepoNames($vendoDir){
  215. $repos = array();
  216. $users = scanSubDir($vendorDir);
  217. foreach($users as $user){
  218. $userdir = pathJoin($vendorDir,$user);
  219. $names = scanSubDir($userdir);
  220. foreach($names as $name){
  221. array_push($repos,$user . '/' . $name);
  222. }
  223. }
  224. array_push($repos,"."); //.名称表示顶层包,放最后
  225. return $repos;
  226. }
  227. //解析所有仓库配置
  228. function loadRepoConfigs($vendorDir,$repoNames){
  229. $configs = array();
  230. foreach($repoNames as $repoName){
  231. if($repoName == '.')
  232. $cfgPath = pathJoin(dirname($vendorDir),'composer.json');
  233. else
  234. $cfgPath = pathJoin($vendorDir,$repoName,'composer.json');
  235. $data = json_decode(file_get_contents($cfgPath), true);
  236. if(!$data){
  237. logError('invalid composer.json',$cfgPath);
  238. return false;
  239. }
  240. $configs[$repoName] = $data;
  241. }
  242. return $configs;
  243. }
  244. //解析部署配置文件
  245. function loadConfig($cfgPath){
  246. $config = json_decode(file_get_contents($cfgPath), true);
  247. if(!$config){
  248. logError("failed to decode config file",$cfgPath);
  249. return false;
  250. }
  251. if(! $config['distribute-mode']){
  252. logError("distribute-mode node not exists");
  253. return false;
  254. }
  255. return $config;
  256. }
  257. //多线程下载文件
  258. function downloadToDir($toolDir,$ossHost,$repoName,$fileName,$targetDir){
  259. if (!file_exists($targetDir)){
  260. mkdir($targetDir,0777,true);
  261. }
  262. $targetPath = pathJoin($targetDir,$fileName);
  263. if( file_exists($targetPath) ) {
  264. return $targetPath;
  265. }
  266. $gopeed = getGoPeedPath($toolDir);
  267. $remoteUrl = pathJoin($ossHost,$repoName,$fileName);
  268. $cmd = $gopeed." -D=" . $targetDir . " " . $remoteUrl;
  269. logInfo("start to fetch common file --",$cmd);
  270. system($cmd, $ret);
  271. if ($ret != 0) {
  272. return false;
  273. }
  274. return $targetPath;
  275. }
  276. //解压缩文件
  277. function unzipFile($toolDir,$zipFile, $targetDir){
  278. $fileExt = getFileExt($zipFile);
  279. if($fileExt == "zip"){
  280. $zip= new \ZipArchive;
  281. if($zip->open($zipFile)==true){
  282. $zip->extractTo($targetDir);
  283. $zip->close();
  284. return true;
  285. }
  286. else{
  287. return false;
  288. }
  289. }
  290. if($fileExt == "7z"){
  291. $sevenZip = get7ZipPath($toolDir);
  292. $logPath = $zipFile . '.log';
  293. $cmd = $sevenZip . " x $zipFile -o$targetDir -aoa > " . $logPath;
  294. system($cmd, $ret);
  295. if ($ret != 0) {
  296. return false;
  297. }
  298. return true;
  299. }
  300. return false;
  301. }