public function sitePermission() { $siteList = (new WebSite())->getList(); $pattern = '/:([\\w\\d-_\\.]+\\/[\\w\\d-_\\.]+)\\.git$/i'; $this->permissions = array(); foreach ($siteList as $m) { $dc = new DC($m['siteId']); if (preg_match($pattern, $dc->get(DC::GIT_ORIGIN), $matchs)) { $this->permissions[$m['siteId']] = $this->maxPermissionOfRepo($matchs[1]); } } }
public static function getSiteIdByFullName($repoFullName) { $siteList = (new WebSite())->getList(); $pattern = '/:([\\w\\d-_\\.]+\\/[\\w\\d-_\\.]+)\\.git$/i'; foreach ($siteList as $m) { $dc = new DC($m['siteId']); if (preg_match($pattern, $dc->get(DC::GIT_ORIGIN), $matchs)) { if ($matchs[1] == $repoFullName) { return $m['siteId']; } } } return NULL; }
public function fire($job, $message) { $siteId = $message['siteId']; $buildId = $message['id']; $branch = $message['branch']; $dc = new DC($siteId); $df = new DeployInfo($siteId); $root = (new SystemConfig())->get(SystemConfig::WORK_ROOT_FIELD) . '/' . $siteId; $commitRoot = "{$root}/commit/"; $branchPath = "{$commitRoot}/{$branch}"; $gitOrigin = $dc->get(DC::GIT_ORIGIN); $buildCommand = 'make deploy'; $defaultBranch = 'default'; $developRoot = "{$root}/branch/{$defaultBranch}"; Log::info("\n---------------------------\njob id : {$job->getJobId()} start"); $progress = 0; $redis = app('redis')->connection(); $lock = new \Eleme\Rlock\Lock($redis, JobLock::buildLock($developRoot), array('timeout' => 600000, 'blocking' => false)); if (!$lock->acquire()) { Log::info("Job : {$job->getJobId()} Release"); $job->release(30); return; } try { if (!File::exists($developRoot)) { Log::info('Git clone'); (new Process('mkdir -p ' . $commitRoot))->mustRun(); (new Process('mkdir -p ' . $developRoot))->mustRun(); (new Process('git clone ' . $gitOrigin . ' ' . $developRoot . ' --depth 20'))->setTimeout(600)->mustRun(); } $build = $df->get($buildId); $build['result'] = 'Fetch Origin'; $build['last_time'] = date('Y-m-d H:i:s'); $df->save($build); Log::info("git fetch origin"); (new Process("git fetch origin", $developRoot))->setTimeout(600)->mustRun(); $progress = 1; (new Process("cp -r {$developRoot} {$branchPath}", $commitRoot))->mustRun(); $revParseProcess = new Process("git rev-parse origin/{$branch}", $branchPath); $revParseProcess->run(); if (!$revParseProcess->isSuccessful()) { throw new Exception('Error Message : ' . $revParseProcess->getErrorOutput()); } $commit = trim($revParseProcess->getOutput()); $commitPath = "{$commitRoot}/{$commit}"; $build['result'] = 'Building'; $build['last_time'] = date('Y-m-d H:i:s'); $df->save($build); $needBuild = true; if ($commit !== $branch) { if (File::exists($commitPath)) { File::deleteDirectory($branchPath); $needBuild = false; } else { $progress = 2; File::move($branchPath, $commitPath); } } if ($needBuild) { Log::info("Build {$siteId} branch: {$branch}"); (new Process("git checkout {$commit}", $commitPath))->mustRun(); Log::info("make deploy"); (new Process($buildCommand, $commitPath))->setTimeout(600)->mustRun(); } (new CommitVersion($siteId))->add($commit); $build['commit'] = $commit; $build['result'] = 'Build Success'; $build['last_time'] = date('Y-m-d H:i:s'); $df->save($build); Log::info($job->getJobId() . " finish\n---------------------------"); } catch (Exception $e) { $build['errMsg'] = $e->getMessage(); $build['result'] = 'Error'; $build['last_time'] = date('Y-m-d H:i:s'); $df->save($build); switch ($progress) { case 2: (new Process('rm -rf ' . $commitPath))->run(); case 1: (new Process('rm -rf ' . $branchPath))->run(); } Log::error($e->getMessage()); Log::info($job->getJobId() . " Error Finish\n---------------------------"); } $lock->release(); $job->delete(); }
public function fire($job, $message) { Log::info("\n---------------------------\njob id : {$job->getJobId()} start"); $commit = $message['commit']; $siteId = $message['siteId']; $pr = new PullRequest($siteId); $commitInfo = $pr->get($commit); $repoName = $commitInfo->repo; $gitOrigin = "git@github.com:{$repoName}.git"; $dc = new DC($siteId); $branch = $commitInfo->branch; $root = (new SystemConfig())->get(SystemConfig::WORK_ROOT_FIELD) . '/' . $siteId; $branchRoot = "{$root}/pull_requests/repo/{$repoName}"; $commitPath = "{$root}/pull_requests/commit/{$commit}"; $progress = 0; $cmd = ''; $lock = new Eleme\Rlock\Lock(app('redis')->connection(), JobLock::pullRequestBuildLock($repoName), array('timeout' => 600000, 'blocking' => false)); if (!$lock->acquire()) { Log::info("Job locked, now {$job->getJobId()} Release"); $job->release(30); return; } try { if (!File::exists($branchRoot)) { Log::info('Git clone'); $cmd = 'mkdir -p ' . $branchRoot; (new Process($cmd))->mustRun(); $cmd = "mkdir -p {$root}/pull_requests/commit/"; (new Process($cmd))->mustRun(); $progress = 1; $cmd = "git clone {$gitOrigin} {$branchRoot} --depth 10"; (new Process($cmd))->setTimeout(600)->mustRun(); $progress = 2; } Log::info("git fetch origin"); $cmd = "git fetch -f origin {$branch}:{$branch} --depth 20"; (new Process($cmd, $branchRoot))->setTimeout(600)->mustRun(); if (File::exists($commitPath)) { $cmd = "rm -rf {$commitPath}"; (new Process($cmd))->setTimeout(600)->mustRun(); } $progress = 3; $cmd = "cp -r {$branchRoot} {$commitPath}"; Log::info($cmd); (new Process($cmd))->setTimeout(600)->mustRun(); $cmd = "git checkout {$branch}"; Log::info($cmd); (new Process($cmd, $commitPath))->mustRun(); $cmd = "git checkout {$commit}"; Log::info($cmd); (new Process($cmd, $commitPath))->mustRun(); $commitInfo->buildStatus = 'Building'; $pr->save($commitInfo); $progress = 4; $cmd = $dc->get(DC::BUILD_COMMAND); Log::info($cmd); (new Process($cmd, $commitPath))->setTimeout(600)->mustRun(); $commitInfo->buildStatus = 'Success'; $pr->save($commitInfo); $progress = 5; $commitInfo->testStatus = 'Testing'; $pr->save($commitInfo); $cmd = $dc->get(DC::TEST_COMMAND); if (!empty($cmd)) { Log::info($cmd); (new Process($cmd, $commitPath))->setTimeout(600)->mustRun(); } $commitInfo->testStatus = 'Success'; $pr->save($commitInfo); } catch (Exception $e) { Log::info("ERROR!!! : " . $e->getMessage()); $commitInfo->buildStatus = 'Error'; $commitInfo->testStatus = 'Error'; $commitInfo->errorMsg = "Command : {$cmd}\nError Message : {$e->getMessage()}"; switch ($progress) { case 5: $commitInfo->buildStatus = 'Success'; break; case 4: (new Process("rm -rf {$commitPath}"))->run(); case 3: case 2: $commitInfo->testStatus = 'Abort'; break; case 1: (new Process("rm -rf {$branchRoot}"))->run(); $commitInfo->testStatus = 'Abort'; break; } $pr->save($commitInfo); } $lock->release(); Log::info("progress : {$progress}"); Log::info("job id : {$job->getJobId()} finish"); $job->delete(); }
public function saveConfig() { $siteId = Input::get('siteId'); $dc = new DC($siteId); $ifContent = Input::get('identifyfile'); if ($ifContent != '**** Secret ****') { $dc->set(DC::IDENTIFYFILE, Input::get('identifyfile')); } $passphrase = Input::get('passphrase'); if ($passphrase != '******') { $dc->set(DC::PASSPHRASE, Input::get('passphrase')); } $hipchatToken = Input::get('hipchatToken'); if ($hipchatToken != '******') { $dc->set(DC::HIPCHAT_TOKEN, Input::get('hipchatToken')); } $github_token = Input::get('github_token'); if ($github_token != '******') { $dc->set(DC::GITHUB_TOKEN, Input::get('github_token')); } $dc->set(DC::HIPCHAT_ROOM, Input::get('hipchatRoom')); $dc->set(DC::STATIC_DIR, Input::get('staticDir')); $dc->set(DC::DEFAULT_BRANCH, Input::get('defaultBranch')); $dc->set(DC::REMOTE_USER, Input::get('remoteUser')); //$dc->set(DC::SERVICE_NAME, Input::get('serviceName')); $dc->set(DC::REMOTE_APP_DIR, Input::get('remoteAppDir')); $dc->set(DC::REMOTE_STATIC_DIR, Input::get('remoteStaticDir')); $dc->set(DC::BUILD_COMMAND, Input::get('buildCommand')); $dc->set(DC::RSYNC_EXCLUDE, Input::get('rsyncExclude')); $dc->set(DC::TEST_COMMAND, Input::get('testCommand')); $dc->set(DC::REMOTE_OWNER, Input::get('remoteOwner')); $staticHostScript = Input::get('staticHostScript'); $webHostScript = Input::get('webHostScript'); $compile = 'Static Host'; try { $list = ScriptCommand::complie($staticHostScript, $siteId); $compile = 'Web Host'; $list = ScriptCommand::complie($webHostScript, $siteId); } catch (Exception $e) { return Response::json(array('res' => 1, 'errMsg' => "[{$compile} Script] " . $e->getMessage())); } $dc->set(DC::DEPLOY_STATIC_SCRIPT, $staticHostScript); $dc->set(DC::DEPLOY_WEB_SCRIPT, Input::get('webHostScript')); return Response::json(array('res' => 0)); }
public function fire($job, $message) { $this->warnings = array(); $commit = $message['commit']; $hostType = $message['hostType']; $siteId = $message['siteId']; $dc = new DC($siteId); $staticDir = $dc->get(DC::STATIC_DIR); if (isset($message['type']) && $message['type'] == self::TYPE_PULL_REQUEST) { $id = $message['id']; $this->type = self::TYPE_PULL_REQUEST; $this->pr = new PullRequestDeploy($siteId); $this->prDeployInfo = $this->pr->get($id); $root = (new SystemConfig())->get(SystemConfig::WORK_ROOT_FIELD) . '/' . $siteId . '/pull_requests'; $commitPath = "{$root}/commit/{$commit}"; } else { $this->type = self::TYPE_NORMAL_DEPLOY; $this->dfManger = new DeployInfo($siteId); $id = $message['id']; $this->deployInfo = $this->dfManger->get($id); $root = (new SystemConfig())->get(SystemConfig::WORK_ROOT_FIELD) . '/' . $siteId . '/commit'; $commitPath = "{$root}/{$commit}"; } $LOCAL_STATIC_DIR = "{$commitPath}/{$staticDir}"; $LOCAL_DIR = $commitPath; $remoteUser = $dc->get(DC::REMOTE_USER); $remoteOwner = $dc->get(DC::REMOTE_OWNER); $RSYNC_EXCLUDE = "{$commitPath}/" . $dc->get(DC::RSYNC_EXCLUDE); $REMOTE_STATIC_DIR = $dc->get(DC::REMOTE_STATIC_DIR); $REMOTE_DIR = $dc->get(DC::REMOTE_APP_DIR); $staticScript = ScriptCommand::complie($dc->get(DC::DEPLOY_STATIC_SCRIPT), $siteId); $webScript = ScriptCommand::complie($dc->get(DC::DEPLOY_WEB_SCRIPT), $siteId); $this->updateStatus('Deploying'); Log::info("\n---------------------------\njob id : {$job->getJobId()} start "); Log::info("commit deploy: {$commit}"); //本地同步锁,不能在同一个commit下同步 $redis = app('redis')->connection(); $commitLock = new \Eleme\Rlock\Lock($redis, JobLock::buildLock($commitPath), array('timeout' => 600000, 'blocking' => false)); if (!$commitLock->acquire()) { Log::info("Job : {$job->getJobId()} Release"); $job->release(30); } $rsyLock = NULL; try { $hosts = (new SiteHost($siteId, $hostType, SiteHost::STATIC_HOST))->getList(); $staticHosts = new SplQueue(); foreach ($hosts as $h) { $staticHosts->push($h); } $hosts = (new SiteHost($siteId, $hostType, SiteHost::WEB_HOST))->getList(); $webHosts = new SplQueue(); foreach ($hosts as $h) { $webHosts->push($h); } /***************************************** * * 执行静态文件同步 * *****************************************/ //执行同步前本地命令 $this->processCommands($staticScript['before']['handle']); while (!$staticHosts->isEmpty()) { $host = $staticHosts->shift(); $rsyLock = new \Eleme\Rlock\Lock($redis, JobLock::rsyLock($host['hostip']), array('timeout' => 600000, 'blocking' => false)); if ($rsyLock->acquire()) { try { $HOST_NAME = $host['hostname']; //执行同步前每次都执行的本地命令 $this->processCommands($staticScript['before']['local']); //执行同步前每次都执行的远端命令 $this->processCommands($staticScript['before']['remote'], $HOST_NAME); Log::info("deploying static files to {$HOST_NAME}."); (new Process($this->remoteProcess($HOST_NAME, "sudo mkdir -p {$REMOTE_STATIC_DIR}")))->mustRun(); (new Process($this->remoteProcess($HOST_NAME, "sudo chown {$remoteUser} -R {$REMOTE_STATIC_DIR}")))->mustRun(); (new Process("rsync -az --progress --force --delay-updates --exclude-from={$RSYNC_EXCLUDE} {$LOCAL_STATIC_DIR}/ {$HOST_NAME}:{$REMOTE_STATIC_DIR}/", $commitPath))->setTimeout(600)->mustRun(); (new Process($this->remoteProcess($HOST_NAME, "sudo chown {$remoteOwner} -R {$REMOTE_STATIC_DIR}")))->mustRun(); //执行同步后每次都执行的本地命令 $this->processCommands($staticScript['after']['local']); //执行同步后每次都执行的远端命令 $this->processCommands($staticScript['after']['remote'], $HOST_NAME); $rsyLock->release(); } catch (Exception $e) { $rsyLock->release(); throw $e; } } else { // 正在同步,重新放回队列 $staticHosts->push($host); } } //执行同步后本地命令 $this->processCommands($staticScript['after']['handle']); /***************************************** * * 执行WEB应用同步 * *****************************************/ //执行同步前本地命令 $this->processCommands($webScript['before']['handle']); while (!$webHosts->isEmpty()) { $host = $webHosts->shift(); $rsyLock = new \Eleme\Rlock\Lock($redis, JobLock::rsyLock($host['hostip']), array('timeout' => 600000, 'blocking' => false)); if ($rsyLock->acquire()) { try { $HOST_NAME = $host['hostname']; //执行同步前每次都执行的本地命令 $this->processCommands($webScript['before']['local']); //执行同步前每次都执行的远端命令 $this->processCommands($webScript['before']['remote'], $HOST_NAME); Log::info("deploying web apps to {$HOST_NAME}."); (new Process($this->remoteProcess($HOST_NAME, "sudo mkdir -p {$REMOTE_DIR}")))->mustRun(); (new Process($this->remoteProcess($HOST_NAME, "sudo chown {$remoteUser} -R {$REMOTE_DIR}")))->mustRun(); (new Process("rsync -azq --progress --force --delete --delay-updates --exclude-from={$RSYNC_EXCLUDE} {$LOCAL_DIR}/ {$HOST_NAME}:{$REMOTE_DIR}/", $commitPath))->setTimeout(600)->mustRun(); (new Process($this->remoteProcess($HOST_NAME, "sudo chown {$remoteOwner} -R {$REMOTE_DIR}")))->mustRun(); //执行同步后每次都执行的本地命令 $this->processCommands($webScript['after']['local']); //执行同步后每次都执行的远端命令 $this->processCommands($webScript['after']['remote'], $HOST_NAME); $rsyLock->release(); } catch (Exception $e) { $rsyLock->release(); throw $e; } } else { $webHosts->push($host); } } //执行同步后本地命令 $this->processCommands($webScript['after']['handle']); $errMsg = ''; foreach ($this->warnings as $w) { $errMsg .= "{$w}\n"; } $errMsg = empty($errMsg) ? null : $errMsg; $this->updateStatus('Deploy Success', $errMsg); Log::info($job->getJobId() . " finish"); } catch (Exception $e) { //if ($rsyLock != null) $rsyLock->release(); $this->updateStatus('Error', "file : " . $e->getFile() . "\nline : " . $e->getLine() . "\n Error Msg : " . $e->getMessage()); Log::error($e->getMessage()); Log::info($job->getJobId() . " Error Finish\n---------------------------"); } $commitLock->release(); $job->delete(); }
/** * Currently, this method simply overwrites an existing fragment !!! * WARNING: this influences all mashups having integrated this fragment * Possible solution: create a new copy of the fragment, however fragment URIs are currently provided by the loomp client * therefore some modification in the loomp architecture are required * * @param Fragment $fragment Fragment Object */ private function _saveFragment($fragment) { $this->_removeFragment($fragment); $fragmentRes = new Resource($fragment->getUri()); $this->rdfModel->add(new Statement($fragmentRes, RDF::TYPE(), LOOMP::FRAGMENT())); $this->rdfModel->add(new Statement($fragmentRes, DC::CREATOR(), new Resource($fragment->getCreatorId()))); $this->rdfModel->add(new Statement($fragmentRes, DC::TITLE(), new Literal($fragment->getTitle()))); $this->rdfModel->add(new Statement($fragmentRes, DC::CREATED(), new Literal($fragment->getCreateDate()))); $this->rdfModel->add(new Statement($fragmentRes, DC::MODIFIED(), new Literal($fragment->getModifyDate()))); $this->rdfModel->add(new Statement($fragmentRes, LOOMP::RDFA(), new Literal($fragment->getSaveContent()))); $this->rdfModel->add(new Statement($fragmentRes, LOOMP::TYPE(), new Literal($fragment->getType()))); $this->_saveResFromRDFa($fragmentRes, '<html xmlns="http://www.w3.org/1999/xhtml"><body about="' . $fragment->getURI() . '">' . $fragment->getSaveContent() . '</body></html>'); }