/** * [copyMethod 拷贝包] * @param [type] $srcPath [description] * @param [type] $srcVersion [description] * @param [type] $targetPath [description] * @return [array] [resukt:bool-true/false error:string] */ public function copyMethod($srcPath, $srcVersion, $targetPath) { $packageItem = new Package(); $srcInfo = $packageItem->getInfoByPath($srcPath, $srcVersion); $targetInfo = $packageItem->getLastValidPackageByPath($targetPath); if (empty($srcInfo) || empty($targetInfo)) { $resultArray['error'] = '包不存在'; $resultArray['result'] = false; return $resultArray; } $svnVersion = $srcInfo['svnVersion']; $svnBase = $this->svnBase; $tmpPath = $this->svnBaseTmp . $srcPath; $targetPackageArray = explode('/', trim($targetPath, '/')); if (count($targetPackageArray) == 3) { $targetName = $targetPackageArray[2]; } else { $targetName = $targetPackageArray[1]; } $targetProduct = $targetPackageArray[0]; $srcPackageArray = explode('/', trim($srcPath, '/')); if (count($srcPackageArray) == 3) { $srcName = $srcPackageArray[2]; } else { $srcName = $srcPackageArray[1]; } $srcProduct = $srcPackageArray[0]; //版本号处理 $targetVersion = $targetInfo['version']; $versionArray = explode('.', $targetVersion); $versionArray[count($versionArray) - 1]++; $targetVersion = implode('.', $versionArray); $baseVersion = $versionArray; unset($baseVersion[count($versionArray) - 1]); $baseVersion = implode('.', $baseVersion); $cmdArray = array(); $cmdArray[] = "rm {$tmpPath} -r ;" . "/usr/local/bin/svn export --config-dir /etc/subversion -r {$svnVersion} svn://" . $this->svnServer . rtrim($srcPath, '/') . "/ {$tmpPath} --force >/dev/null 2>&1;echo \$?"; $cmdArray[] = '/usr/bin/sed -i s:^name=\\"' . $srcName . '\\":name=\\"' . $targetName . '\\": ' . $tmpPath . '/init.xml; echo $?'; $cmdArray[] = '/usr/bin/sed -i s:^product=\\"' . $srcProduct . '\\":product=\\"' . $targetProduct . '\\": ' . $tmpPath . '/init.xml; echo $?'; $cmdArray[] = '/usr/bin/sed -i s:^version=\\"[0-9.]*\\":version=\\"' . $baseVersion . '\\": ' . $tmpPath . '/init.xml; echo $?'; $cmdArray[] = "/usr/bin/rsync -a --delete --exclude=.svn " . rtrim($tmpPath, '/') . "/ {$svnBase}{$targetPath} 2>&1;echo \$?"; foreach ($cmdArray as $index => $singleCommand) { $singleCommand = escapeshellcmd($singleCommand); $this->log($singleCommand); $shellRunResult = shell_exec($singleCommand); $this->log($shellRunResult); if ($shellRunResult != '0') { $resultArray['error'] = '执行命令出错:命令' . ($index + 1); $resultArray['result'] = false; return $resultArray; } } //修改包数据信息 $packageItem = new Package(); $packageItem->packageInfo['product'] = $targetInfo['product']; $packageItem->packageInfo['name'] = $targetInfo['name']; $packageItem->packageInfo['version'] = $targetVersion; $packageItem->packageInfo['path'] = $targetInfo['path']; $packageItem->packageInfo['user'] = $srcInfo['version']; $packageItem->packageInfo['remark'] = "从{$srcPath} {$srcVersion} " . "{$srcInfo['remark']} 拷贝"; $packageItem->packageInfo['author'] = $srcInfo['author']; $packageItem->packageInfo['status'] = 1; $packageItem->packageInfo['frameworkType'] = $srcInfo['frameworkType']; $packageItem->packageInfo['stateless'] = $srcInfo['stateless']; $packageItem->packageInfo['os'] = $srcInfo['os']; //运行升级脚本 $shellRun = new ExecShell('update_package.sh'); $shellRun->run($this->pkgHome . $targetPath, $targetProduct, $targetName, $targetVersion); $resultArray = array(); $shellRunCode = $shellRun->rtCode(); if ($shellRun->result(0) != 'result' || $shellRun->result(1) != 'success' && $shellRun->result(1) != 'failed') { $resultArray['error'] = '后台异常返回'; $resultArray['result'] = false; return $resultArray; } if ($shellRun->result(1) === 'success') { $packageItem->packageInfo['svnVersion'] = $shellRun->result(3); $packageItem->savePackage(); } else { $resultArray['error'] = '升级失败' . $shellRun->result(2); $resultArray['result'] = false; return $resultArray; } $resultArray['error'] = ''; $resultArray['result'] = true; return $resultArray; }
public function submitUpdateVersion() { //获取post数据 调用 $this->log->info("START FUNCTION " . __FUNCTION__); $data = Flight::request()->data->getData(); $this->log->info('data:' . json_encode($data)); // var_dump($data); $errorArray = array('error' => ''); //参数检查 if (!isset($data['confProduct']) || !isset($data['confName']) || !isset($data['confVersion']) || !isset($data['path']) || !isset($data['confUser']) || !isset($data['confRemark']) || !isset($data['confFrameworkType']) || !isset($data['confAuthor']) || !isset($data['isShell']) || !isset($data['confContent'])) { $errorArray['error'] = "missing parameters, please check. confProduct confName confVersion path..."; Flight::json($errorArray, 400); } $confProduct = $data['confProduct']; $confName = $data['confName']; $confVersion = $data['confVersion']; $confContent = $data['confContent']; $confUser = $data['confUser']; $confRemark = $data['confRemark']; $confAuthor = $data['confAuthor']; $confFrameworkType = $data['confFrameworkType']; $isShell = $data['isShell']; $path = $data['path']; $packageItem = new Package(); $packageInfo = $packageItem->getInfo($confProduct, $confName, $confVersion); $packagePath = $this->pkgHome . $data['path']; $realPackagePath = realpath($packagePath); if (empty($realPackagePath) || !is_dir($realPackagePath)) { $errorArray['error'] = "目录不存在:{$data['path']}"; Flight::json($errorArray, 404); } //运行脚本提交 $shellRun = new ExecShell('check_svn_dir.sh', Conf::get('tool_operate')); $shellRun->run($realPackagePath); $this->log->info('shell run result:' . json_encode($shellRun->getOutput())); $shellRunCode = $shellRun->rtCode(); //提交出错 if ($shellRunCode != 0) { $errorArray['error'] = "目录中存在无效文件,如:管道文件、块设备文件、字符设备文件等"; Flight::json($errorArray, 400); } $confContent = str_replace("\r\n", "\n", $confContent); $confContent = iconv("UTF-8", "GBK//IGNORE", $confContent); $xmlPath = $realPackagePath . "/init.xml"; if (!file_exists($xmlPath)) { $errorArray['error'] = "配置文件init.xml不存在:{$path}/init.xml"; Flight::json($errorArray, 404); } $xmlTmp = $xmlPath . date("U") . rand(1000, 100000); if (!copy($xmlPath, $xmlTmp)) { $errorArray['error'] = "备份文件失败, 取消保存"; Flight::json($errorArray, 400); } $copyLen = file_put_contents($xmlPath, $confContent); if ($copyLen != strlen($confContent)) { $rollBack = unlink($realPackagePath) && rename($xmlTmp, $xmlPath); if ($rollBack) { $errorArray['error'] = "保存文件失败,文件已回滚"; } else { $errorArray['error'] = "原始文件已被清空,无法恢复.备份文件名为:" . basename($xmlTmp) . ",请手工处理"; } Flight::json($errorArray, 400); } unlink($xmlTmp); $packageItem->packageInfo['product'] = $confProduct; $packageItem->packageInfo['name'] = $confName; $packageItem->packageInfo['version'] = $confVersion; $packageItem->packageInfo['path'] = $packageItem->getSvnPath($confProduct, $confName); $packageItem->packageInfo['user'] = $confUser; $packageItem->packageInfo['remark'] = $confRemark; $packageItem->packageInfo['author'] = $confAuthor; $packageItem->packageInfo['status'] = 200; if ($isShell != 'true') { $packageItem->packageInfo['frameworkType'] = $confFrameworkType; } else { $packageItem->packageInfo['frameworkType'] = "undefined"; } $pkgSvnScript = new PkgSvn(); $shellRunRes = $pkgSvnScript->buildUpdatePackage($path, $realPackagePath); $shellRunCode = $shellRunRes['code']; $shellRunMsg = $shellRunRes['msg']; $this->log->info('shell run code:' . $shellRunCode); $this->log->info('shell run result:' . $shellRunMsg); if ($shellRunCode != 0) { $errorArray['error'] = '升级失败' . $shellRunMsg; Flight::json($errorArray, 400); } else { $svnVersion = $shellRunRes['data']['svnVersion']; $packageItem->packageInfo['status'] = 1; $packageItem->packageInfo['svnVersion'] = $svnVersion; $packageItem->savePackage(); } //用户信息获取没确认! // $user = new UserFavorite; // $user->insert($data['userId'], $confProduct, $confName); Flight::json($errorArray, 200); }