/** * 执行远程目标机器命令 * * @param $command * @return bool */ public final function runRemoteCommand($command) { $this->log = ''; $needTTY = ' -T '; foreach (GlobalHelper::str2arr($this->getConfig()->hosts) as $remoteHost) { $localCommand = 'ssh ' . $needTTY . ' -p ' . $this->getHostPort($remoteHost) . ' -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no ' . $this->getConfig()->release_user . '@' . $this->getHostName($remoteHost); $remoteCommand = str_replace('"', '\\"', trim($command)); $localCommand .= ' " ' . $remoteCommand . ' " '; static::log('Run remote command ' . $remoteCommand); $log = $this->log; $this->status = $this->runLocalCommand($localCommand); $this->log = $log . (($log ? PHP_EOL : '') . $remoteHost . ' : ' . $this->log); if (!$this->status) { return false; } } return true; }
/** * 执行远程目标机器命令 * * @param string $command * @param integer $delay 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用 * @return bool */ public final function runRemoteCommand($command, $delay = 0) { $this->log = ''; $needTTY = '-T'; foreach (GlobalHelper::str2arr($this->getConfig()->hosts) as $remoteHost) { $localCommand = sprintf('ssh %s -p %d -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o CheckHostIP=false %s@%s %s', $needTTY, $this->getHostPort($remoteHost), escapeshellarg($this->getConfig()->release_user), escapeshellarg($this->getHostName($remoteHost)), escapeshellarg($command)); if ($delay > 0) { // 每台机器延迟执行post_release任务间隔, 不推荐使用, 仅当业务无法平滑重启时使用 static::log(sprintf('Sleep: %d s', $delay)); sleep($delay); } static::log('Run remote command ' . $command); $log = $this->log; $this->status = $this->runLocalCommand($localCommand); $this->log = $log . (($log ? PHP_EOL : '') . $remoteHost . ' : ' . $this->log); if (!$this->status) { return false; } } return true; }