/** * Execute the job. * @throws \RuntimeException * @dispatches UpdateGitReferences */ public function handle() { $private_key = tempnam(storage_path('app/'), 'sshkey'); file_put_contents($private_key, $this->project->private_key); $wrapper = with(new ScriptParser())->parseFile('tools.SSHWrapperScript', ['private_key' => $private_key]); $wrapper_file = tempnam(storage_path('app/'), 'gitssh'); file_put_contents($wrapper_file, $wrapper); $process = new Process('tools.MirrorGitRepository', ['wrapper_file' => $wrapper_file, 'mirror_path' => $this->project->mirrorPath(), 'repository' => $this->project->repository]); $process->run(); unlink($wrapper_file); unlink($private_key); if (!$process->isSuccessful()) { throw new \RuntimeException('Could not mirror repository - ' . $process->getErrorOutput()); } $this->project->last_mirrored = date('Y-m-d H:i:s'); $this->project->save(); $this->dispatch(new UpdateGitReferences($this->project)); }
/** * 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()); } }
/** * Generates an SSH key and sets the private/public key properties. */ protected function regeneratePublicKey() { $key = tempnam(storage_path('app/'), 'sshkey'); file_put_contents($key, $this->private_key); $process = new Process('tools.RegeneratePublicSSHKey', ['key_file' => $key]); $process->run(); if (!$process->isSuccessful()) { throw new \RuntimeException($process->getErrorOutput()); } $this->attributes['public_key'] = file_get_contents($key . '.pub'); unlink($key); unlink($key . '.pub'); }