Skip to content

Commit

Permalink
Merge pull request #1179 from coollabsio/next
Browse files Browse the repository at this point in the history
v4.0.0-beta.19
  • Loading branch information
andrasbacsai authored Aug 15, 2023
2 parents 1de2c22 + 316f855 commit aba86df
Show file tree
Hide file tree
Showing 421 changed files with 9,360 additions and 3,345 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Coolify Builder (v4)
name: Coolify Helper Image (v4)

on:
push:
branches: ["main", "v4"]
branches: [ "main", "next" ]
paths:
- .github/workflows/coolify-builder.yml
- docker/coolify-builder/Dockerfile
- .github/workflows/coolify-helper.yml
- docker/coolify-helper/Dockerfile

env:
REGISTRY: ghcr.io
IMAGE_NAME: "coollabsio/coolify-builder"
IMAGE_NAME: "coollabsio/coolify-helper"

jobs:
amd64:
Expand All @@ -30,12 +30,12 @@ jobs:
with:
no-cache: true
context: .
file: docker/coolify-builder/Dockerfile
file: docker/coolify-helper/Dockerfile
platforms: linux/amd64
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
aarch64:
runs-on: [self-hosted, arm64]
runs-on: [ self-hosted, arm64 ]
permissions:
contents: read
packages: write
Expand All @@ -52,7 +52,7 @@ jobs:
with:
no-cache: true
context: .
file: docker/coolify-builder/Dockerfile
file: docker/coolify-helper/Dockerfile
platforms: linux/aarch64
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:aarch64
Expand All @@ -61,7 +61,7 @@ jobs:
permissions:
contents: read
packages: write
needs: [amd64, aarch64]
needs: [ amd64, aarch64 ]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -77,4 +77,4 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create & publish manifest
run: |
docker buildx imagetools create --append ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:aarch64 --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
docker buildx imagetools create --append ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:aarch64 --tag ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
1 change: 1 addition & 0 deletions app/Actions/CoolifyTask/PrepareCoolifyTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PrepareCoolifyTask
{
protected Activity $activity;
protected CoolifyTaskArgs $remoteProcessArgs;

public function __construct(CoolifyTaskArgs $remoteProcessArgs)
{
$this->remoteProcessArgs = $remoteProcessArgs;
Expand Down
68 changes: 34 additions & 34 deletions app/Actions/CoolifyTask/RunRemoteProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ public function __construct(Activity $activity, bool $hide_from_output = false,
$this->ignore_errors = $ignore_errors;
}

public static function decodeOutput(?Activity $activity = null): string
{
if (is_null($activity)) {
return '';
}

try {
$decoded = json_decode(
data_get($activity, 'description'),
associative: true,
flags: JSON_THROW_ON_ERROR
);
} catch (\JsonException $exception) {
return '';
}

return collect($decoded)
->sortBy(fn ($i) => $i['order'])
->map(fn ($i) => $i['output'])
->implode("");
}

public function __invoke(): ProcessResult
{
$this->time_start = hrtime(true);
Expand Down Expand Up @@ -83,15 +105,6 @@ public function __invoke(): ProcessResult
return $processResult;
}

protected function getLatestCounter(): int
{
$description = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR);
if ($description === null || count($description) === 0) {
return 1;
}
return end($description)['order'] + 1;
}

protected function getCommand(): string
{
$user = $this->activity->getExtraProperty('user');
Expand Down Expand Up @@ -120,6 +133,13 @@ protected function handleOutput(string $type, string $output)
}
}

protected function elapsedTime(): int
{
$timeMs = (hrtime(true) - $this->time_start) / 1_000_000;

return intval($timeMs);
}

public function encodeOutput($type, $output)
{
$outputStack = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR);
Expand All @@ -135,26 +155,13 @@ public function encodeOutput($type, $output)
return json_encode($outputStack, flags: JSON_THROW_ON_ERROR);
}

public static function decodeOutput(?Activity $activity = null): string
protected function getLatestCounter(): int
{
if (is_null($activity)) {
return '';
}

try {
$decoded = json_decode(
data_get($activity, 'description'),
associative: true,
flags: JSON_THROW_ON_ERROR
);
} catch (\JsonException $exception) {
return '';
$description = json_decode($this->activity->description, associative: true, flags: JSON_THROW_ON_ERROR);
if ($description === null || count($description) === 0) {
return 1;
}

return collect($decoded)
->sortBy(fn ($i) => $i['order'])
->map(fn ($i) => $i['output'])
->implode("");
return end($description)['order'] + 1;
}

/**
Expand All @@ -171,11 +178,4 @@ protected function isAfterLastThrottle()

return ($this->current_time - $this->throttle_interval_ms) > $this->last_write_at;
}

protected function elapsedTime(): int
{
$timeMs = (hrtime(true) - $this->time_start) / 1_000_000;

return intval($timeMs);
}
}
164 changes: 164 additions & 0 deletions app/Actions/Database/StartPostgresql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

namespace App\Actions\Database;

use App\Models\Server;
use App\Models\StandalonePostgresql;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;

class StartPostgresql
{
public StandalonePostgresql $database;
public array $commands = [];
public array $init_scripts = [];
public string $configuration_dir;

public function __invoke(Server $server, StandalonePostgresql $database)
{
$this->database = $database;
$container_name = generate_container_name($this->database->uuid);
$this->configuration_dir = database_configuration_dir() . '/' . $container_name;

$this->commands = [
"mkdir -p $this->configuration_dir",
"mkdir -p $this->configuration_dir/docker-entrypoint-initdb.d/"
];

$persistent_storages = $this->generate_local_persistent_volumes();
$volume_names = $this->generate_local_persistent_volumes_only_volume_names();
$environment_variables = $this->generate_environment_variables();
$this->generate_init_scripts();
$docker_compose = [
'version' => '3.8',
'services' => [
$container_name => [
'image' => $this->database->image,
'container_name' => $container_name,
'environment' => $environment_variables,
'restart' => 'always',
'networks' => [
$this->database->destination->network,
],
'healthcheck' => [
'test' => [
'CMD-SHELL',
'pg_isready',
'-d',
$this->database->postgres_db,
'-U',
$this->database->postgres_user,
],
'interval' => '5s',
'timeout' => '5s',
'retries' => 10,
'start_period' => '5s'
],
'mem_limit' => $this->database->limits_memory,
'memswap_limit' => $this->database->limits_memory_swap,
'mem_swappiness' => $this->database->limits_memory_swappiness,
'mem_reservation' => $this->database->limits_memory_reservation,
'cpus' => $this->database->limits_cpus,
'cpuset' => $this->database->limits_cpuset,
'cpu_shares' => $this->database->limits_cpu_shares,
]
],
'networks' => [
$this->database->destination->network => [
'external' => false,
'name' => $this->database->destination->network,
'attachable' => true,
]
]
];
if (count($this->database->ports_mappings_array) > 0) {
$docker_compose['services'][$container_name]['ports'] = $this->database->ports_mappings_array;
}
if (count($persistent_storages) > 0) {
$docker_compose['services'][$container_name]['volumes'] = $persistent_storages;
}
if (count($volume_names) > 0) {
$docker_compose['volumes'] = $volume_names;
}
if (count($this->init_scripts) > 0) {
foreach ($this->init_scripts as $init_script) {
$docker_compose['services'][$container_name]['volumes'][] = [
'type' => 'bind',
'source' => $init_script,
'target' => '/docker-entrypoint-initdb.d/' . basename($init_script),
'read_only' => true,
];
}
}
$docker_compose = Yaml::dump($docker_compose, 10);
$docker_compose_base64 = base64_encode($docker_compose);
$this->commands[] = "echo '{$docker_compose_base64}' | base64 -d > $this->configuration_dir/docker-compose.yml";
$readme = generate_readme_file($this->database->name, now());
$this->commands[] = "echo '{$readme}' > $this->configuration_dir/README.md";
$this->commands[] = "docker compose -f $this->configuration_dir/docker-compose.yml up -d";
return remote_process($this->commands, $server);
}

private function generate_local_persistent_volumes()
{
$local_persistent_volumes = [];
foreach ($this->database->persistentStorages as $persistentStorage) {
$volume_name = $persistentStorage->host_path ?? $persistentStorage->name;
$local_persistent_volumes[] = $volume_name . ':' . $persistentStorage->mount_path;
}
return $local_persistent_volumes;
}

private function generate_local_persistent_volumes_only_volume_names()
{
$local_persistent_volumes_names = [];
foreach ($this->database->persistentStorages as $persistentStorage) {
if ($persistentStorage->host_path) {
continue;
}
$name = $persistentStorage->name;
$local_persistent_volumes_names[$name] = [
'name' => $name,
'external' => false,
];
}
return $local_persistent_volumes_names;
}

private function generate_environment_variables()
{
$environment_variables = collect();
ray('Generate Environment Variables')->green();
ray($this->database->runtime_environment_variables)->green();
foreach ($this->database->runtime_environment_variables as $env) {
$environment_variables->push("$env->key=$env->value");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_USER'))->isEmpty()) {
$environment_variables->push("POSTGRES_USER={$this->database->postgres_user}");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_PASSWORD'))->isEmpty()) {
$environment_variables->push("POSTGRES_PASSWORD={$this->database->postgres_password}");
}

if ($environment_variables->filter(fn ($env) => Str::of($env)->contains('POSTGRES_DB'))->isEmpty()) {
$environment_variables->push("POSTGRES_DB={$this->database->postgres_db}");
}
return $environment_variables->all();
}

private function generate_init_scripts()
{
if (is_null($this->database->init_scripts) || count($this->database->init_scripts) === 0) {
return;
}
foreach ($this->database->init_scripts as $init_script) {
$filename = data_get($init_script, 'filename');
$content = data_get($init_script, 'content');
$content_base64 = base64_encode($content);
$this->commands[] = "echo '{$content_base64}' | base64 -d > $this->configuration_dir/docker-entrypoint-initdb.d/{$filename}";
$this->init_scripts[] = "$this->configuration_dir/docker-entrypoint-initdb.d/{$filename}";
}
}
}
6 changes: 1 addition & 5 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
namespace App\Actions\Fortify;

use App\Models\InstanceSettings;
use App\Models\Team;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
Expand All @@ -19,13 +16,12 @@ class CreateNewUser implements CreatesNewUsers
/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
* @param array<string, string> $input
*/
public function create(array $input): User
{
$settings = InstanceSettings::get();
if (!$settings->is_registration_enabled) {
Log::info('Registration is disabled');
abort(403);
}
Validator::make($input, [
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Fortify/ResetUserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ResetUserPassword implements ResetsUserPasswords
/**
* Validate and reset the user's forgotten password.
*
* @param array<string, string> $input
* @param array<string, string> $input
*/
public function reset(User $user, array $input): void
{
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Fortify/UpdateUserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UpdateUserPassword implements UpdatesUserPasswords
/**
* Validate and update the user's password.
*
* @param array<string, string> $input
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
Expand Down
Loading

0 comments on commit aba86df

Please sign in to comment.