示例#1
0
 /**
  * [createPackage 创建新包接口 后台创建对应文件目录]
  * @return [type] [description]
  */
 public function submitCreatePackage()
 {
     //获取post数据 调用
     $this->log->info("START FUNCTION " . __FUNCTION__);
     $data = Flight::request()->data->getData();
     $this->log->info('data:' . json_encode($data));
     $errorArray = array('error' => '');
     //参数检查
     $needPara = array('confProduct', 'confName', 'confVersion', 'path', 'confUser', 'confRemark', 'confFrameworkType', 'confAuthor', 'isShell', 'confContent');
     $optionalPara = array();
     $this->checkParameter($needPara, $data, $optionalPara);
     $path = $data['path'];
     $pkgPath = $this->pkgHome . $path;
     $realPackagePath = realpath($pkgPath);
     if (empty($realPackagePath) || !is_dir($realPackagePath)) {
         $errorArray['error'] = "Package directory not exists";
         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()));
     $this->log->info('shell run code:' . $shellRun->rtCode());
     if ($shellRun->rtCode() != 0) {
         $errorArray['error'] = "目录中存在无效文件,如:管道文件、块设备文件、字符设备文件等";
         Flight::json($errorArray, 400);
     }
     $confContent = $data['confContent'];
     $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 = new Package();
     $packageItem->packageInfo['product'] = $data['confProduct'];
     $packageItem->packageInfo['name'] = $data['confName'];
     $packageItem->packageInfo['version'] = $data['confVersion'];
     $packageItem->packageInfo['path'] = "/{$data['confProduct']}/{$data['confName']}";
     $packageItem->packageInfo['user'] = $data['confUser'];
     $packageItem->packageInfo['remark'] = $data['confRemark'];
     $packageItem->packageInfo['author'] = $data['confAuthor'];
     $packageItem->packageInfo['status'] = 200;
     if ($data['isShell'] != 'true') {
         $packageItem->packageInfo['frameworkType'] = $data['confFrameworkType'];
     } else {
         $packageItem->packageInfo['frameworkType'] = "undefined";
     }
     $pkgSvnScript = new PkgSvn();
     $shellRunRes = $pkgSvnScript->buildPackage($path, $realPackagePath);
     $this->log->info(json_encode($shellRunRes));
     $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'], $data['confProduct'], $data['confName']);
     Flight::json($errorArray, 201);
 }