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) { $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(); }