Skip to content

Commit

Permalink
Merge pull request #89 from ptah-sh/4-configure-cicd
Browse files Browse the repository at this point in the history
chore: #4 add ci step
  • Loading branch information
bohdan-shulha authored Jul 31, 2024
2 parents cff95f9 + 368f484 commit 1b2807b
Show file tree
Hide file tree
Showing 182 changed files with 1,136 additions and 727 deletions.
95 changes: 95 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Code Quality

on: [push]

jobs:
test:
runs-on: ubuntu-latest
env:
HUSKY: 0
BROADCAST_DRIVER: log
CACHE_DRIVER: redis
QUEUE_CONNECTION: redis
SESSION_DRIVER: redis
DB_CONNECTION: pgsql
DB_HOST: localhost
DB_PASSWORD: postgres
DB_USERNAME: postgres
DB_DATABASE: postgres

# Docs: https://docs.github.com/en/actions/using-containerized-services
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432/tcp
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3

redis:
image: redis
ports:
- 6379/tcp
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: pdo pdo_pgsql pgsql bcmath
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader

- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'npm'

- name: Install Node dependencies
run: npm i --frozen-lockfile

- name: Build JS
run: npm run build

- name: Prepare the application
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
php artisan key:generate
- name: Clear Config
run: php artisan config:clear

- name: Run Migration
run: php artisan migrate -v
env:
DB_PORT: ${{ job.services.postgres.ports[5432] }}
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}

- name: Test with pest
run: php artisan test --coverage-text
env:
DB_PORT: ${{ job.services.postgres.ports[5432] }}
REDIS_PORT: ${{ job.services.redis.ports['6379'] }}

- name: Pint!
run: vendor/bin/pint --test
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lint-staged
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.x
9 changes: 3 additions & 6 deletions api-nodes/Http/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

use ApiNodes\Models\AgentStartedEventData;
use App\Models\Node;
use App\Models\NodeData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class EventController
{
Expand All @@ -29,8 +26,8 @@ public function started(Node $node, AgentStartedEventData $data)

return [
'settings' => [
'poll_interval' => 5
]
'poll_interval' => 5,
],
];
}
}
}
13 changes: 6 additions & 7 deletions api-nodes/Http/Controllers/NextTaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ApiNodes\Http\Controllers;

use App\Models\Node;
use App\Models\NodeTasks\TaskStatus;
use App\Models\NodeTaskGroup;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Response;
Expand All @@ -14,7 +13,7 @@ public function __invoke(Node $node)
{
if ($node->swarm_id === null) {
return new Response([
'message' => 'No task found.'
'message' => 'No task found.',
], 204);
}

Expand All @@ -29,20 +28,20 @@ public function __invoke(Node $node)
return $task->only([
'id',
'type',
'payload'
'payload',
]);
}

return new Response([
'message' => 'No task found.'
'message' => 'No task found.',
], 204);
}

protected function getNextTaskFromGroup(Node $node, NodeTaskGroup $taskGroup)
{
if ($taskGroup->tasks()->running()->first()) {
return new Response([
'error_message' => 'Another task should be already running.'
'error_message' => 'Another task should be already running.',
], 409);
}

Expand All @@ -63,7 +62,7 @@ protected function pickNextTask(Node $node)
return $query->where('node_id', $node->id)->orWhere('node_id', null);
})->pending()->first();

if (!$taskGroup) {
if (! $taskGroup) {
return null;
}

Expand All @@ -73,4 +72,4 @@ protected function pickNextTask(Node $node)

return $task;
}
}
}
2 changes: 1 addition & 1 deletion api-nodes/Http/Controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ public function fail(NodeTask $task, Request $request)

return new Response('', 204);
}
}
}
9 changes: 4 additions & 5 deletions api-nodes/Http/Middleware/AgentTokenAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ApiNodes\Http\Middleware;

use App\Models\Node;
use App\Models\NodeTasks;
use App\Models\Scopes\TeamScope;
use App\Models\Team;
use Closure;
Expand All @@ -23,13 +22,13 @@ public function handle(Request $request, Closure $next): Response
{
$token = $request->header(self::AUTH_HEADER);

if (!$token) {
if (! $token) {
if ($request->bearerToken()) {
return $next($request);
}

return response()->json([
'message' => 'Unauthorized'
'message' => 'Unauthorized',
], 403);
}

Expand All @@ -39,8 +38,8 @@ public function handle(Request $request, Closure $next): Response

$node->save();

app()->singleton(Node::class, fn() => $node);
app()->singleton(Team::class, fn() => $node->team);
app()->singleton(Node::class, fn () => $node);
app()->singleton(Team::class, fn () => $node->team);

return $next($request);
}
Expand Down
5 changes: 1 addition & 4 deletions api-nodes/Models/AgentStartedEventData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@ class AgentStartedEventData extends Data
public function __construct(
public NodeData $node,
public ?SwarmData $swarm,
)
{

}
) {}
}
7 changes: 2 additions & 5 deletions api-nodes/Models/AgentStartedEventData/SwarmData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ public function __construct(
#[DataCollectionOf(ManagerNode::class)]
/* @var ManagerNode[] */
public array $managerNodes,
)
{

}
}
) {}
}
2 changes: 1 addition & 1 deletion app/Actions/Jetstream/AddTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AddTeamMember implements AddsTeamMembers
/**
* Add a new team member to the given team.
*/
public function add(User $user, Team $team, string $email, string $role = null): void
public function add(User $user, Team $team, string $email, ?string $role = null): void
{
Gate::forUser($user)->authorize('addTeamMember', $team);

Expand Down
4 changes: 1 addition & 3 deletions app/Actions/Jetstream/DeleteUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class DeleteUser implements DeletesUsers
/**
* Create a new action instance.
*/
public function __construct(protected DeletesTeams $deletesTeams)
{
}
public function __construct(protected DeletesTeams $deletesTeams) {}

/**
* Delete the given user.
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Jetstream/InviteTeamMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InviteTeamMember implements InvitesTeamMembers
/**
* Invite a new team member to the given team.
*/
public function invite(User $user, Team $team, string $email, string $role = null): void
public function invite(User $user, Team $team, string $email, ?string $role = null): void
{
Gate::forUser($user)->authorize('addTeamMember', $team);

Expand Down
5 changes: 1 addition & 4 deletions app/Api/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

namespace App\Api\Controllers;

class Controller
{

}
class Controller {}
3 changes: 1 addition & 2 deletions app/Api/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Api\Controllers;

use App\Models\DeploymentData;
use App\Models\Service;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand All @@ -21,4 +20,4 @@ public function deploy(Service $service, Request $request): array
'deployment_id' => $deployment->id,
];
}
}
}
10 changes: 5 additions & 5 deletions app/Console/Commands/DispatchProcessBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function dispatchBackupTask(): void
]);

$date = now()->format('Y-m-d_His');
$backupFileName = dockerize_name("svc-{$service->id}-{$process->name}_backup-{$backupCmd->name}-{$date}") . '.tar.gz';
$backupFileName = dockerize_name("svc-{$service->id}-{$process->name}_backup-{$backupCmd->name}-{$date}").'.tar.gz';
$archivePath = "{$process->backupVolume->path}/$backupFileName";
$backupCommand = "mkdir -p /tmp/{$backupCmd->id} && cd /tmp/{$backupCmd->id} && {$backupCmd->command} && tar czfv $archivePath -C /tmp/{$backupCmd->id} . && rm -rf /tmp/{$backupCmd->id}";

Expand All @@ -89,8 +89,8 @@ protected function dispatchBackupTask(): void
'AttachStdout' => true,
'AttachStderr' => true,
'Cmd' => ['sh', '-c', $backupCommand],
]
]
],
],
],
[
'type' => NodeTaskType::UploadS3File,
Expand All @@ -105,9 +105,9 @@ protected function dispatchBackupTask(): void
'Target' => $process->backupVolume->path,
],
'SrcFilePath' => $archivePath,
'DestFilePath' => $s3Storage->pathPrefix . '/' . $backupFileName,
'DestFilePath' => $s3Storage->pathPrefix.'/'.$backupFileName,
],
]
],
]);
}
}
10 changes: 5 additions & 5 deletions app/Console/Commands/DispatchVolumeBackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected function dispatchBackupTask(): void
]);

$date = now()->format('Y-m-d_His');
$backupFileName = dockerize_name("svc-{$service->id}-{$process->name}-vol-{$volume->name}-{$date}") . '.tar.gz';
$backupFileName = dockerize_name("svc-{$service->id}-{$process->name}-vol-{$volume->name}-{$date}").'.tar.gz';
$archivePath = "{$process->backupVolume->path}/$backupFileName";
$backupCommand = "tar czfv $archivePath -C {$volume->path} .";

Expand All @@ -90,8 +90,8 @@ protected function dispatchBackupTask(): void
'AttachStdout' => true,
'AttachStderr' => true,
'Cmd' => ['sh', '-c', $backupCommand],
]
]
],
],
],
[
'type' => NodeTaskType::UploadS3File,
Expand All @@ -106,9 +106,9 @@ protected function dispatchBackupTask(): void
'Target' => $process->backupVolume->path,
],
'SrcFilePath' => $archivePath,
'DestFilePath' => $s3Storage->pathPrefix . '/' . $backupFileName,
'DestFilePath' => $s3Storage->pathPrefix.'/'.$backupFileName,
],
]
],
]);
}
}
7 changes: 4 additions & 3 deletions app/Console/Commands/MakeNodeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class MakeNodeTask extends Command
/**
* The console command description.
*34
*
* @var string
*/
protected $description = 'Create a new node task';
Expand Down Expand Up @@ -52,15 +53,15 @@ public function handle(): void
foreach ($dirs as $dir) {
$path = app_path($dir);

$this->info('Creating directory ' . $dir);
$this->info('Creating directory '.$dir);

mkdir($path);
}

foreach ($taskDataFiles as $abstractPath => $template) {
$path = app_path($abstractPath);

$this->info('Writing ' . $abstractPath);
$this->info('Writing '.$abstractPath);

file_put_contents($path, $template);
}
Expand All @@ -70,7 +71,7 @@ public function handle(): void

protected function readStub($type, $taskName): string
{
$template = file_get_contents(__DIR__ . "/stubs/MakeNodeTask/$type.stub");
$template = file_get_contents(__DIR__."/stubs/MakeNodeTask/$type.stub");

return str_replace('$taskName', $taskName, $template);
}
Expand Down
Loading

0 comments on commit 1b2807b

Please sign in to comment.