only stop containers, do not kill them

This commit is contained in:
Andras Bacsai 2023-04-14 11:24:58 +02:00
parent 4ecf1edb25
commit 14be2c1ebf
3 changed files with 75 additions and 56 deletions

View File

@ -32,7 +32,7 @@ class DeployApplication extends Component
}
public function deploy()
public function start()
{
// Create Deployment ID
$this->deployment_uuid = new Cuid2(7);
@ -49,8 +49,8 @@ class DeployApplication extends Component
public function stop()
{
runRemoteCommandSync($this->destination->server, ["docker rm -f {$this->application_uuid} >/dev/null 2>&1"]);
$this->application->status = 'exited';
runRemoteCommandSync($this->destination->server, ["docker stop -t 0 {$this->application_uuid} >/dev/null 2>&1"]);
$this->application->status = 'stopped';
$this->application->save();
}

View File

@ -90,6 +90,23 @@ class DeployApplicationJob implements ShouldQueue
}
$this->workdir = "/artifacts/{$this->deployment_uuid}";
$this->executeNow([
"docker inspect {$this->application->uuid} >/dev/null 2>&1",
"echo $?"
], 'stopped_container_check', hideFromOutput: true);
if ($this->activity->properties->get('stopped_container_check') == 0) {
$this->executeNow([
"echo 'Container {$this->application->uuid} was stopped, starting it...'"
]);
$this->executeNow([
"docker start {$this->application->uuid}"
], hideFromOutput: true);
$this->executeNow([
"echo 'Started! 🎉'"
]);
} else {
// Pull builder image
$this->executeNow([
"echo 'Starting deployment of {$this->application->git_repository}:{$this->application->git_branch}...'",
@ -148,11 +165,13 @@ class DeployApplicationJob implements ShouldQueue
"echo 'Done. 🎉'",
"docker stop -t 0 {$this->deployment_uuid} >/dev/null"
], setStatus: true);
}
dispatch(new ContainerStatusJob($this->application_uuid));
// Saving docker-compose.yml
Storage::disk('deployments')->put(Str::kebab($this->application->name) . '/docker-compose.yml', Yaml::dump($docker_compose, 10));
Storage::disk('deployments')->put(Str::kebab($this->application->name) . '/docker-compose.yml', $docker_compose);
}
private function execute_in_builder(string $command)
@ -208,7 +227,7 @@ class DeployApplicationJob implements ShouldQueue
if (count($volume_names) > 0) {
$docker_compose['volumes'] = $volume_names;
}
return Yaml::dump($docker_compose);
return Yaml::dump($docker_compose, 10);
}
private function generate_local_persistent_volumes()
{

View File

@ -2,7 +2,7 @@
@if ($application->status === 'running')
<button wire:click='stop'>Stop</button>
@else
<button wire:click='deploy'>Deploy</button>
<button wire:click='start'>Start</button>
@endif
<span wire:poll='pollingStatus'>status: {{ $application->status }}</span>
</div>