This commit is contained in:
Joao Patricio
2023-03-20 12:04:22 +00:00
parent a0da981ec7
commit 75326f6626
27 changed files with 1770 additions and 95 deletions

View File

@@ -0,0 +1,45 @@
<?php
namespace App\Services;
use App\Jobs\ExecuteCoolifyProcess;
use Illuminate\Process\ProcessResult;
use Spatie\Activitylog\Contracts\Activity;
class CoolifyProcess
{
protected Activity $activity;
// TODO Left 'root' as default user instead of 'coolify' because
// there's a task at TODO.md to run docker without sudo
public function __construct(
protected string $destination,
protected string $command,
protected ?int $port = 22,
protected ?string $user = 'root',
){
$this->activity = activity()
->withProperty('type', 'COOLIFY_PROCESS')
->withProperty('user', $this->user)
->withProperty('destination', $this->destination)
->withProperty('port', $this->port)
->withProperty('command', $this->command)
->withProperty('status', ProcessStatus::HOLDING)
->log("Awaiting to start command...\n\n");
}
public function __invoke(): Activity|ProcessResult
{
$job = new ExecuteCoolifyProcess($this->activity);
if (app()->environment('testing')) {
return $job->handle();
}
dispatch($job);
return $this->activity;
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Services;
enum ProcessStatus: string
{
case HOLDING = 'holding';
case IN_PROGRESS = 'in_progress';
case FINISHED = 'finished';
case ERROR = 'error';
}

259
app/Services/Ssh.php Normal file
View File

@@ -0,0 +1,259 @@
<?php
namespace App\Services;
use Closure;
use Exception;
use Symfony\Component\Process\Process;
/**
* Started from a copy of spatie/ssh
*/
class Ssh
{
protected string $user;
protected string $host;
protected array $extraOptions = [];
protected Closure $processConfigurationClosure;
protected Closure $onOutput;
public function __construct(string $user, string $host, int $port = null)
{
$this->user = $user;
$this->host = $host;
if ($port !== null) {
$this->usePort($port);
}
$this->processConfigurationClosure = fn(Process $process) => null;
$this->onOutput = fn($type, $line) => null;
}
public static function create(...$args): self
{
return new static(...$args);
}
public function usePrivateKey(string $pathToPrivateKey): self
{
$this->extraOptions['private_key'] = '-i ' . $pathToPrivateKey;
return $this;
}
public function useJumpHost(string $jumpHost): self
{
$this->extraOptions['jump_host'] = '-J ' . $jumpHost;
return $this;
}
public function usePort(int $port): self
{
if ($port < 0) {
throw new Exception('Port must be a positive integer.');
}
$this->extraOptions['port'] = '-p ' . $port;
return $this;
}
public function useMultiplexing(string $controlPath, string $controlPersist = '10m'): self
{
$this->extraOptions['control_master'] = '-o ControlMaster=auto -o ControlPath=' . $controlPath . ' -o ControlPersist=' . $controlPersist;
return $this;
}
public function configureProcess(Closure $processConfigurationClosure): self
{
$this->processConfigurationClosure = $processConfigurationClosure;
return $this;
}
public function onOutput(Closure $onOutput): self
{
$this->onOutput = $onOutput;
return $this;
}
public function enableStrictHostKeyChecking(): self
{
unset($this->extraOptions['enable_strict_check']);
return $this;
}
public function disableStrictHostKeyChecking(): self
{
$this->extraOptions['enable_strict_check'] = '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null';
return $this;
}
public function enableQuietMode(): self
{
$this->extraOptions['quiet'] = '-q';
return $this;
}
public function disableQuietMode(): self
{
unset($this->extraOptions['quiet']);
return $this;
}
public function disablePasswordAuthentication(): self
{
$this->extraOptions['password_authentication'] = '-o PasswordAuthentication=no';
return $this;
}
public function enablePasswordAuthentication(): self
{
unset($this->extraOptions['password_authentication']);
return $this;
}
public function addExtraOption(string $option): self
{
$this->extraOptions[] = $option;
return $this;
}
/**
* @param string|array $command
*
* @return string
*/
public function getExecuteCommand($command): string
{
$commands = $this->wrapArray($command);
$extraOptions = implode(' ', $this->getExtraOptions());
$commandString = implode(PHP_EOL, $commands);
$delimiter = 'EOF-SPATIE-SSH';
$target = $this->getTargetForSsh();
if (in_array($this->host, ['local', 'localhost', '127.0.0.1'])) {
return $commandString;
}
return "ssh {$extraOptions} {$target} 'bash -se' << \\$delimiter" . PHP_EOL
. $commandString . PHP_EOL
. $delimiter;
}
/**
* @param string|array $command
*
* @return \Symfony\Component\Process\Process
**/
public function execute($command): Process
{
$sshCommand = $this->getExecuteCommand($command);
return $this->run($sshCommand);
}
/**
* @param string|array $command
*
* @return \Symfony\Component\Process\Process
*/
public function executeAsync($command): Process
{
$sshCommand = $this->getExecuteCommand($command);
return $this->run($sshCommand, 'start');
}
public function getDownloadCommand(string $sourcePath, string $destinationPath): string
{
return "scp {$this->getExtraScpOptions()} {$this->getTargetForScp()}:$sourcePath $destinationPath";
}
public function download(string $sourcePath, string $destinationPath): Process
{
$downloadCommand = $this->getDownloadCommand($sourcePath, $destinationPath);
return $this->run($downloadCommand);
}
public function getUploadCommand(string $sourcePath, string $destinationPath): string
{
return "scp {$this->getExtraScpOptions()} $sourcePath {$this->getTargetForScp()}:$destinationPath";
}
public function upload(string $sourcePath, string $destinationPath): Process
{
$uploadCommand = $this->getUploadCommand($sourcePath, $destinationPath);
return $this->run($uploadCommand);
}
protected function getExtraScpOptions(): string
{
$extraOptions = $this->extraOptions;
if (isset($extraOptions['port'])) {
$extraOptions['port'] = str_replace('-p', '-P', $extraOptions['port']);
}
$extraOptions[] = '-r';
return implode(' ', array_values($extraOptions));
}
private function getExtraOptions(): array
{
return array_values($this->extraOptions);
}
protected function wrapArray($arrayOrString): array
{
return (array)$arrayOrString;
}
protected function run(string $command, string $method = 'run'): Process
{
$process = Process::fromShellCommandline($command);
$process->setTimeout(0);
($this->processConfigurationClosure)($process);
$process->{$method}($this->onOutput);
return $process;
}
protected function getTargetForScp(): string
{
$host = filter_var($this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? '[' . $this->host . ']' : $this->host;
return "{$this->user}@{$host}";
}
protected function getTargetForSsh(): string
{
return "{$this->user}@{$this->host}";
}
}