/** * ServerLogChanged constructor. * * @param ServerLog $log */ public function __construct(ServerLog $log) { $this->status = $log->status; $this->started_at = $log->started_at ? $log->started_at->toDateTimeString() : null; $this->finished_at = $log->finished_at ? $log->finished_at->toDateTimeString() : null; $this->log_id = $log->id; $this->output = is_null($log->output) || !strlen($log->output) ? null : ''; $this->runtime = $log->runtime() === false ? null : $log->getPresenter()->readable_runtime; }
/** * Cleans up any stalled deployments in the database. * * @tpdp Maybe readd pending to the queue if possible? * @return void */ public function cleanupDeployments() { // Mark any pending steps as cancelled ServerLog::where('status', '=', ServerLog::PENDING)->update(['status' => ServerLog::CANCELLED]); // Mark any running steps as failed ServerLog::where('status', '=', ServerLog::RUNNING)->update(['status' => ServerLog::FAILED]); // Mark any running/pending deployments as failed Deployment::whereIn('status', [Deployment::DEPLOYING, Deployment::PENDING])->update(['status' => Deployment::FAILED]); // Mark any deploying/pending projects as failed Project::whereIn('status', [Project::DEPLOYING, Project::PENDING])->update(['status' => Project::FAILED]); }
/** * Create an instance of DeployStep and a ServerLog entry for each server which can have code deployed. * * @param int $stage * @return void */ private function createDeployStep($stage) { $step = DeployStep::create(['stage' => $stage, 'deployment_id' => $this->deployment->id]); foreach ($this->project->servers as $server) { // If command is null it is preparing one of the 4 default steps so // skip servers which shouldn't have the code deployed if (!$server->deploy_code) { continue; } ServerLog::create(['server_id' => $server->id, 'deploy_step_id' => $step->id]); } }
/** * Gets the log output of a particular deployment step. * * @param ServerLog $log * @return ServerLog */ public function log(ServerLog $log) { $log->runtime = $log->runtime() === false ? null : $log->getPresenter()->readable_runtime; return $log; }
/** * Sends a file to a remote server. * * @param string $local_file * @param string $remote_file * @param ServerLog $log * * @throws \RuntimeException */ private function sendFile($local_file, $remote_file, ServerLog $log) { $process = new Process('deploy.SendFileToServer', ['port' => $log->server->port, 'private_key' => $this->private_key, 'local_file' => $local_file, 'remote_file' => $remote_file, 'username' => $log->server->user, 'ip_address' => $log->server->ip_address]); $output = ''; $process->run(function ($type, $output_line) use(&$output, &$log) { if ($type === \Symfony\Component\Process\Process::ERR) { $output .= $this->logError($output_line); } else { // Switching sent/received around $output_line = str_replace('received', 'xxx', $output_line); $output_line = str_replace('sent', 'received', $output_line); $output_line = str_replace('xxx', 'sent', $output_line); $output .= $this->logSuccess($output_line); } $log->output = $output; $log->save(); }); if (!$process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } }