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,51 @@
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class RunCommand extends Component
{
public $activity;
public $isKeepAliveOn = false;
public $manualKeepAlive = false;
public $command = '';
public function render()
{
return view('livewire.run-command');
}
public function runCommand()
{
// TODO Execute with Livewire Normally
$this->activity = coolifyProcess($this->command, 'testing-host');
// Override manual to experiment
// $sleepingBeauty = 'x=1; while [ $x -le 40 ]; do sleep 0.1 && echo "Welcome $x times" $(( x++ )); done';
//
// $commandString = <<<EOT
// cd projects/dummy-project
// ~/.docker/cli-plugins/docker-compose build --no-cache
// # $sleepingBeauty
// EOT;
//
// $this->activity = coolifyProcess($commandString, 'testing-host');
$this->isKeepAliveOn = true;
}
public function polling()
{
$this->activity?->refresh();
if ($this->activity?->properties['status'] === 'finished') {
$this->isKeepAliveOn = false;
}
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace App\Jobs;
use Illuminate\Support\Facades\DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Process\InvokedProcess;
use Illuminate\Process\ProcessResult;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Spatie\Activitylog\Contracts\Activity;
class ExecuteCoolifyProcess implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $throttleIntervalMS = 500;
protected $timeStart;
protected $currentTime;
protected $lastWriteAt = 0;
protected string $stdOutIncremental = '';
protected string $stdErrIncremental = '';
/**
* Create a new job instance.
*/
public function __construct(
public Activity $activity,
){}
/**
* Execute the job.
*/
public function handle(): ?ProcessResult
{
ray()->clearAll();
$this->timeStart = $start = hrtime(true);
$user = $this->activity->getExtraProperty('user');
$destination = $this->activity->getExtraProperty('destination');
$port = $this->activity->getExtraProperty('port');
$command = $this->activity->getExtraProperty('command');
$delimiter = 'EOF-COOLIFY-SSH';
File::chmod(base_path('coolify_id25519'), '0600');
$sshCommand = 'ssh '
. '-i ./coolify_id25519'
. '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null '
. '-o PasswordAuthentication=no '
. "{$user}@{$destination} "
. " 'bash -se' << \\$delimiter" . PHP_EOL
. $command . PHP_EOL
. $delimiter;
// $sshCommand = "whoami ; pwd ; ls ";
$process = Process::start(
$sshCommand,
$this->handleOutput(...),
);
$res = $process->wait();
if (app()->environment('testing')) {
return $res;
}
// TODO Why is this not persisting?? Immutable property??
$this->activity->properties->put('pid', $process->id());
$this->activity->properties->put('exitCode', $res->exitCode());
$this->activity->properties->put('stdout', $res->output());
$this->activity->properties->put('stderr', $res->errorOutput());
$this->activity->save();
}
protected function handleOutput(string $type, string $output)
{
$this->currentTime = $this->elapsedTime();
if ($type === 'out') {
$this->stdOutIncremental .= $output;
} else {
$this->stdErrIncremental .= $output;
}
$this->activity->description .= $output;
if ($this->isAfterLastThrottle()) {
// Let's write to database.
DB::transaction(function () {
$this->activity->save();
$this->lastWriteAt = $this->currentTime;
});
}
}
/**
* Decides if it's time to write again to database.
*
* @return bool
*/
protected function isAfterLastThrottle()
{
// If DB was never written, then we immediately decide we have to write.
if ($this->lastWriteAt === 0) {
return true;
}
return ($this->currentTime - $this->throttleIntervalMS) > $this->lastWriteAt;
}
protected function elapsedTime(): int
{
$timeMs = (hrtime(true) - $this->timeStart) / 1_000_000;
return intval($timeMs);
}
}

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}";
}
}