feat: debuggable executeNow commands

This commit is contained in:
Andras Bacsai 2023-04-14 13:18:55 +02:00
parent 1ba57ef6c3
commit 14919980c2
5 changed files with 42 additions and 23 deletions

View File

@ -53,6 +53,14 @@ public function stop()
$this->application->status = 'stopped'; $this->application->status = 'stopped';
$this->application->save(); $this->application->save();
} }
public function kill()
{
runRemoteCommandSync($this->destination->server, ["docker rm -f {$this->application_uuid}"]);
if ($this->application->status != 'exited') {
$this->application->status = 'exited';
$this->application->save();
}
}
public function pollingStatus() public function pollingStatus()
{ {

View File

@ -19,6 +19,14 @@ public function __construct(
public string|null $container_id = null, public string|null $container_id = null,
) { ) {
} }
public function handle(): void
{
if ($this->container_id) {
$this->checkContainerStatus();
} else {
$this->checkAllServers();
}
}
protected function checkAllServers() protected function checkAllServers()
{ {
try { try {
@ -69,12 +77,4 @@ protected function checkContainerStatus()
Log::error($e->getMessage()); Log::error($e->getMessage());
} }
} }
public function handle(): void
{
if ($this->container_id) {
$this->checkContainerStatus();
} else {
$this->checkAllServers();
}
}
} }

View File

@ -97,7 +97,7 @@ public function handle(): void
if ($this->activity->properties->get('stopped_container_check') == 0) { if ($this->activity->properties->get('stopped_container_check') == 0) {
$this->executeNow([ $this->executeNow([
"echo 'Container {$this->application->uuid} was stopped, starting it...'" "echo -n 'Container {$this->application->uuid} was stopped, starting it...'"
]); ]);
$this->executeNow([ $this->executeNow([
"docker start {$this->application->uuid}" "docker start {$this->application->uuid}"
@ -144,30 +144,39 @@ public function handle(): void
$this->executeNow([ $this->executeNow([
"echo -n 'Generating nixpacks configuration... '", "echo -n 'Generating nixpacks configuration... '",
]);
$this->executeNow([
$this->nixpacks_build_cmd(), $this->nixpacks_build_cmd(),
$this->execute_in_builder("cp {$this->workdir}/.nixpacks/Dockerfile {$this->workdir}/Dockerfile"), $this->execute_in_builder("cp {$this->workdir}/.nixpacks/Dockerfile {$this->workdir}/Dockerfile"),
$this->execute_in_builder("rm -f {$this->workdir}/.nixpacks/Dockerfile"), $this->execute_in_builder("rm -f {$this->workdir}/.nixpacks/Dockerfile"),
"echo 'Done.'", ], isDebuggable: true);
]);
$this->executeNow([ $this->executeNow([
"echo 'Done.'",
"echo -n 'Building image... '", "echo -n 'Building image... '",
$this->execute_in_builder("docker build -f {$this->workdir}/Dockerfile --build-arg SOURCE_COMMIT={$this->git_commit} --progress plain -t {$this->application->uuid}:{$this->git_commit} {$this->workdir}"),
"echo 'Done.'",
]); ]);
$this->executeNow([ $this->executeNow([
"echo -n 'Removing old container... '", $this->execute_in_builder("docker build -f {$this->workdir}/Dockerfile --build-arg SOURCE_COMMIT={$this->git_commit} --progress plain -t {$this->application->uuid}:{$this->git_commit} {$this->workdir}"),
], isDebuggable: true);
$this->executeNow([
"echo 'Done.'",
"echo -n 'Removing old instance... '",
$this->execute_in_builder("docker rm -f {$this->application->uuid} >/dev/null 2>&1"), $this->execute_in_builder("docker rm -f {$this->application->uuid} >/dev/null 2>&1"),
"echo 'Done.'", "echo 'Done.'",
"echo -n 'Starting your application... '",
]); ]);
$this->executeNow([ $this->executeNow([
"echo -n 'Starting new container... '",
$this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"), $this->execute_in_builder("docker compose --project-directory {$this->workdir} up -d >/dev/null"),
], isDebuggable: true);
$this->executeNow([
"echo 'Done. 🎉'", "echo 'Done. 🎉'",
"docker stop -t 0 {$this->deployment_uuid} >/dev/null" "docker stop -t 0 {$this->deployment_uuid} >/dev/null"
], setStatus: true); ], setStatus: true);
} }
dispatch(new ContainerStatusJob($this->application_uuid)); dispatch(new ContainerStatusJob($this->application_uuid));
// Saving docker-compose.yml // Saving docker-compose.yml
@ -305,9 +314,10 @@ private function set_labels_for_applications()
return $labels; return $labels;
} }
private function executeNow(array|Collection $command, string $propertyName = null, bool $hideFromOutput = false, $setStatus = false) private function executeNow(array|Collection $command, string $propertyName = null, bool $hideFromOutput = false, $setStatus = false, bool $isDebuggable = false)
{ {
static::$batch_counter++; static::$batch_counter++;
if ($command instanceof Collection) { if ($command instanceof Collection) {
$commandText = $command->implode("\n"); $commandText = $command->implode("\n");
} else { } else {
@ -318,7 +328,9 @@ private function executeNow(array|Collection $command, string $propertyName = nu
'command' => $commandText, 'command' => $commandText,
]); ]);
$this->activity->save(); $this->activity->save();
if ($isDebuggable && !$this->application->settings->is_debug) {
$hideFromOutput = true;
}
$remoteProcess = resolve(RunRemoteProcess::class, [ $remoteProcess = resolve(RunRemoteProcess::class, [
'activity' => $this->activity, 'activity' => $this->activity,
'hideFromOutput' => $hideFromOutput, 'hideFromOutput' => $hideFromOutput,

View File

@ -17,10 +17,8 @@ class ApplicationSettingsSeeder extends Seeder
*/ */
public function run(): void public function run(): void
{ {
// $application_1 = Application::find(1); $application_1 = Application::find(1)->load(['settings']);
// ApplicationSetting::create([ $application_1->settings->is_debug = false;
// 'id' => 1, $application_1->settings->save();
// 'application_id' => $application_1->id,
// ]);
} }
} }

View File

@ -4,5 +4,6 @@
@else @else
<button wire:click='start'>Start</button> <button wire:click='start'>Start</button>
@endif @endif
<button wire:click='kill'>Kill</button>
<span wire:poll='pollingStatus'>status: {{ $application->status }}</span> <span wire:poll='pollingStatus'>status: {{ $application->status }}</span>
</div> </div>