diff --git a/app/admin/controller/Upgrade.php b/app/admin/controller/Upgrade.php index d90522d..6495447 100644 --- a/app/admin/controller/Upgrade.php +++ b/app/admin/controller/Upgrade.php @@ -19,17 +19,17 @@ use think\facade\View; use think\facade\Request; use think\facade\Db; use think\exception\ValidateException; -use app\admin\controller\Uzip; use think\facade\Cache; use taoler\com\Api; use taoler\com\Str; +use taoler\com\Files; use think\facade\Config; +use think\facade\Log; +use app\common\lib\ZipFile; +use app\common\lib\SetConf; class Upgrade extends AdminController { - public $update_log = "../runtime/update/update_log.log"; //系统升级日志 - public $return_log = "../runtime/update/return_log.log"; //系统回滚日志 - public $progress_log = "../runtime/update/progress_log.log"; //记录进度 public $root_dir = "../"; //站点代码的根目录 public $backup_dir = "../runtime/update/backup_dir/"; //备份目录 public $upload_dir = "../runtime/update/upload_dir/"; //升级包目录 @@ -94,29 +94,25 @@ class Upgrade extends AdminController } //升级前的版本检测 - public function check($url) + public function check() { - $url = $url.'?ver='.$this->sys_version; - //$versions = json_decode(Api::urlGet($url)); + $url = $this->sys['upcheck_url'].'?ver='.$this->sys_version; $versions = Api::urlGet($url); - //var_dump($versions); - + //判断服务器状态 $version_code = $versions->code; if($version_code == -1){ - return json(['code'=>$version_code,'msg'=>$versions->msg]); + $res = json(['code'=>$version_code,'msg'=>$versions->msg]); } - if($version_code == 1){ - return json(['code'=>$versions->code,'msg'=>$versions->msg,'version'=>$versions->version,'upnum'=>$versions->up_num]); + $res = json(['code'=>$versions->code,'msg'=>$versions->msg,'version'=>$versions->version,'upnum'=>$versions->up_num]); } - if($version_code == 0){ - return json(['code'=>$versions->code,'msg'=>$versions->msg]); + $res = json(['code'=>$versions->code,'msg'=>$versions->msg]); + } - - //版本比较 -/* + +/* //版本比较 $version_num = $versions->version; //最新版本 $up_num =$versions->up_num; //可更新版本数 $res = version_compare($version_num,$this->sys_version_num,'>'); @@ -125,7 +121,8 @@ class Upgrade extends AdminController } else { return json(['code'=>0,'msg'=>'暂时还没更新哦! ==8']); } -*/ +*/ + return $res; } @@ -134,78 +131,151 @@ class Upgrade extends AdminController */ public function upload() { - $data = Request::only(['url','key']); - - if(empty($data['key'])){ - return json(["code"=>0,"msg"=>"请配置正确升级key"]); - } - $url = $data['url'].'?url='.$this->sys['domain'].'&key='.$data['key'].'&ver='.$this->sys_version; -//var_dump($url); + $url = $this->sys['upgrade_url'].'?url='.$this->sys['domain'].'&key='.$this->sys['key'].'&ver='.$this->sys_version; $versions = Api::urlGet($url); - + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'check','progress'=>'0%','msg'=>'---------------升级检测开始---------------']); //判断服务器状态 $version_code = $versions->code; if($version_code == -1){ + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'check eroor','progress'=>'5%','msg'=>'---------------服务器链接失败---------------']); return json(['code'=>$version_code,'msg'=>$versions->msg]); } - + $version_num = $versions->version; $file_url = $versions->src; - + halt($version_num); //判断远程文件是否可用存在 $header = get_headers($file_url, true); if(!isset($header[0]) && (strpos($header[0], '200') || strpos($header[0], '304'))){ return json(["code"=>-1,"msg"=>"获取远程文件失败"]); } - - if(!is_dir($this->upload_dir)){ - $this->create_dirs($this->upload_dir); - } - //把远程文件放入本地 - $package_file = $this->upload_dir.'tao_'.$version_num.'.zip'; + + //把远程文件放入本地 + + //拼接路径 + //$upload_dir = substr($this->upload_dir,-1) == '/' ? $this->upload_dir : $this->upload_dir.'/'; + $upload_dir = Files::getDirPath($this->upload_dir); + Files::mkdirs($upload_dir); + + $package_file = $upload_dir.'taoler_'.$version_num.'.zip'; //升级的压缩包文件 $cpfile = copy($file_url,$package_file); + if(!$cpfile) { return json(["code"=>-1,"msg"=>"下载升级文件失败"]); - } - + } //记录下日志 - $this->save_log("上传升级包成功!"); - $this->update_progress("20%"); - - //升级前备份代码 - $ex = array('app','view'); - $backup_code_res = $this->copydir('../', $this->backup_dir, $ex); + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'success','progress'=>'20%','msg'=>'上传升级包成功!']); +/* + //升级前备份代码 + $ex = array('.git','.idea','runtime','data','addons','config','extend'); // 排除备份文件夹 + $backup_code_res = Files::copydirs('../', $this->backup_dir, $ex); if(!$backup_code_res){ - $this->save_log("备份失败!"); + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'error','progress'=>'25%','msg'=>'备份失败!']); return json(["code"=>0,"msg"=>"备份失败"]); } - $this->update_progress("30%"); - + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'success','progress'=>'30%','msg'=>'执行文件备份成功!']); + +*/ //执行升级 $upres = $this->execute_update($package_file); //更新版本 - Db::name('system')->update(['sys_version_num'=>$version_num,'id'=>1]); - //$res = Config::set(['version' => $version_num,'salt' => 'taoler'], 'taoler'); - //var_dump($res); + //Db::name('system')->update(['sys_version_num'=>$version_num,'id'=>1]); + //$res = Config::set(['version' => $version_num], 'taoler'); + $value = [ + 'version' => $version_num + ]; + $setconf = new SetConf; + $res = $setconf->setconfig('taoler',$value); + var_dump($res); if($upres){ return json(["code"=>0,"msg"=>"升级成功"]); }else { return json(["code"=>-1,"msg"=>"升级失败"]); } } - + + /** + * @param string $package_file + * @return bool|\think\response\Json + */ + private function execute_update(string $package_file) + { + //解压 zip文件有密码的话需要解密 + $uzip = new ZipFile(); + $zipDir = strstr($package_file, '.zip',true); //返回文件名后缀前的字符串 + $zipPath = Files::getDirPath($zipDir); //转换为带/的路径 压缩文件解压到的路径 + $unzip_res = $uzip->unzip($package_file,$zipPath,true); + + if(!$unzip_res) + { + $this->save_log("解压失败"); + return json(["code"=>0,"msg"=>"解压失败"]); + } + //解压成功,得到文件夹 + //$package_name = str_replace(".zip","",$package_file); + + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'success','progress'=>'50%','msg'=>'升级文件解压成功!']); + + /* + //升级mysql + if(file_exists($this->upload_dir.'/'.$package_file."/mysql/mysql_update.sql")) + { + $result = $this->database_operation($this->upload_dir.'/'.$package_file."/mysql/mysql_update.sql"); + if(!$result['code']) + { + echo json($result);die; + } + } + */ + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'success','progress'=>'70%','msg'=>'升级文件解压成功!']); + + if(is_dir($zipPath)) + { + //升级PHP + $cp_res = Files::copyDirs($zipPath,$this->root_dir); + if(!$cp_res) + { + $this->save_log("php更新失败"); + //数据库回滚 + if(file_exists($this->upload_dir.'/'.$package_file."/mysql/mysql_rockback.sql")) + { + $this->save_log("数据库回滚"); + $this->database_operation($this->upload_dir.'/'.$package_file."/mysql/mysql_rockback.sql"); + + } + + //php代码回滚 升级前备份的代码 + + $backup_code_res = Files::copydirs($this->backup_dir, $this->root_dir); + if($backup_code_res){ + return json(["code"=>0,"msg"=>"php更新失败"]); + } + } + } + + //把解压的升级包清除 + //$del_zip = unlink($package_file); + Files::delDirAndFile($this->upload_dir); + Files::delDirAndFile($this->backup_dir); + + Log::channel('update')->info('update:{type} {progress} {msg}',['type'=>'success','progress'=>'100%','msg'=>'升级成功!']); + //更新系统的版本号了 + //更新php的版本号了(应该跟svn/git的版本号一致) + //更新数据库的版本号了(应该跟svn/git的版本号一致) + + return true; + } + /** * 处理升级包上传 */ public function uploadZip() { $files = request()->file('file'); - if($files) { $name = $files->getOriginalName(); - if(!$name) { return json(["code"=>0,"msg"=>"请上传升级包文件"]); @@ -217,9 +287,7 @@ class Upgrade extends AdminController if($ext != 'zip') { return json(["code"=>0,"msg"=>"请上传文件格式不对"]); - } - //对比版本号 $astr = explode('_',$name); $version_num = str_replace(".zip", '',array_pop($astr)); @@ -227,7 +295,6 @@ class Upgrade extends AdminController if(!$version_num) { return json(["code"=>0,"msg"=>"获取版本号失败"]); - } //对比 if(!$this->compare_version($version_num)) @@ -247,10 +314,6 @@ class Upgrade extends AdminController return json(["code"=>0,"msg"=>"上传文件失败"]); } - //记录下日志 - $this->save_log("上传升级包成功!"); - $this->update_progress("20%"); - //升级前备份代码 $ex = array('app','view'); $backup_code_res = $this->copydir('../', $this->backup_dir, $ex); @@ -258,7 +321,6 @@ class Upgrade extends AdminController $this->save_log("备份失败!"); return json(["code"=>0,"msg"=>"备份失败"]); } - $this->update_progress("30%"); //执行升级 $upres = $this->execute_update($package_file); @@ -273,81 +335,6 @@ class Upgrade extends AdminController } } - /** - * 升级操作 - * @return [type] [description] - */ - private function execute_update($package_file) - { - //解压 如何使用zip加密压缩,这里解压缩的时候注意要解密 - $uzip = new Uzip(); - $unzip_res = $uzip->uzip($package_file,$this->upload_dir,true); - if(!$unzip_res) - { - $this->save_log("解压失败"); - return json(["code"=>0,"msg"=>"解压失败"]); - } - //解压成功,得到文件夹 - $package_name = str_replace(".zip","",$package_file); -//var_dump($package_name); - $this->update_progress("50%"); - -/* - //升级mysql - if(file_exists($this->upload_dir.'/'.$package_file."/mysql/mysql_update.sql")) - { - $result = $this->database_operation($this->upload_dir.'/'.$package_file."/mysql/mysql_update.sql"); - if(!$result['code']) - { - echo json($result);die; - } - } -*/ - - $this->update_progress("70%"); - - - if(is_dir($package_name.'/')) - { - - //升级PHP - $cp_res = $this->copydir($package_name.'/', $this->root_dir); - if(!$cp_res) - { - $this->save_log("php更新失败"); - //数据库回滚 - if(file_exists($this->upload_dir.'/'.$package_file."/mysql/mysql_rockback.sql")) - { - $this->save_log("数据库回滚"); - $this->database_operation($this->upload_dir.'/'.$package_file."/mysql/mysql_rockback.sql"); - - } - - //php代码回滚 升级前备份的代码 - - $backup_code_res = $this->copydir($this->backup_dir, $this->zip_dir); - if($backup_code_res){ - $this->save_log("php回滚"); - return json(["code"=>0,"msg"=>"php更新失败"]); - } - } - } - - //把解压的升级包清除 - //$del_zip = unlink($package_file); - $this->delDirAndFile($this->upload_dir); - $this->delDirAndFile($this->backup_dir); - - $this->update_progress("100%"); - //更新系统的版本号了 - //更新php的版本号了(应该跟svn/git的版本号一致) - //更新数据库的版本号了(应该跟svn/git的版本号一致) - - return true; - } - - - /** * 比较代码版本 * @return [type] [description] @@ -375,14 +362,7 @@ class Upgrade extends AdminController return ["code"=>1,"msg"=>"数据库操作OK"]; } - /** - * 返回系统升级的进度 - */ - public function update_progress($progress) - { - exec(" echo '".$progress."' > $this->progress_log "); - - } + /** * 记录日志 */ @@ -413,15 +393,10 @@ class Upgrade extends AdminController if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($source . $file) ) { - - if(count($ex) != 0){ - if(in_array($file,$ex)){ - - $this->copydir($source . $file.'/', $dest . $file.'/'); - } - }else{ - $this->copydir($source . $file.'/', $dest . $file.'/'); - } + //拷贝排除的文件夹 + if(!in_array($file,$ex)){ + self::copyDirs($source . $file.'/', $dest . $file.'/'); + } } else { copy($source. $file, $dest . $file); } @@ -435,45 +410,6 @@ class Upgrade extends AdminController return true; } - /** - * 删除文件夹及内容 - * @param $dirPath - * @param $nowDir 是否删除当前文件夹$dirPath true false - */ - public function delDirAndFile( $dirPath, $nowDir=false ) - - { - if ( $handle = opendir($dirPath) ) { - - while ( false !== ( $item = readdir( $handle ) ) ) { - - if ( $item != '.' && $item != '..' ) { - - $path = $dirPath.$item; - //var_dump($path); - if (is_dir($path)) { - $this->delDirAndFile($path.'/'); - rmdir($path.'/'); - } else { - unlink($path); - } - } - } - - closedir( $handle ); -/* - if($nowDir == true){ - if(!rmdir($dirPath)){ - return false; - } - } -*/ - } else { - return false; - } - return true; - } - //创建多文件夹 public function create_dirs($path) { diff --git a/app/admin/view/upgrade/index.html b/app/admin/view/upgrade/index.html index e70cf33..e2f504e 100644 --- a/app/admin/view/upgrade/index.html +++ b/app/admin/view/upgrade/index.html @@ -34,7 +34,7 @@