-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #64 allow to run arbitrary backup commands for processes
- Loading branch information
1 parent
3aba5f1
commit 698905f
Showing
9 changed files
with
260 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Node; | ||
use App\Models\NodeTaskGroupType; | ||
use App\Models\NodeTaskType; | ||
use App\Models\Service; | ||
use Exception; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class DispatchProcessBackupTask extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'app:backups:processes:create {--service-id=} {--process=} {--backup-cmd-id=}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Adds a task to create Volume Backup on a Node.'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(): void | ||
{ | ||
DB::transaction(function () { | ||
$this->dispatchBackupTask(); | ||
}); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
protected function dispatchBackupTask(): void | ||
{ | ||
/* @var Service $service */ | ||
$service = Service::findOrFail($this->option('service-id')); | ||
|
||
$deployment = $service->latestDeployment; | ||
|
||
$process = $deployment->data->findProcess($this->option('process')); | ||
if ($process === null) { | ||
throw new Exception("Could not find process {$this->option('process')} in deployment {$deployment->id}."); | ||
} | ||
|
||
$backupCmd = $process->findProcessBackup($this->option('backup-cmd-id')); | ||
if ($backupCmd === null) { | ||
throw new Exception("Could not find backup command {$this->option('backup-cmd-id')} in process {$process->name}."); | ||
} | ||
|
||
$node = Node::findOrFail($deployment->data->placementNodeId); | ||
|
||
$taskGroup = $node->taskGroups()->create([ | ||
'swarm_id' => $node->swarm_id, | ||
'node_id' => $node->id, | ||
'type' => NodeTaskGroupType::CreateBackup, | ||
'invoker_id' => $deployment->latestTaskGroup->invoker_id, | ||
'team_id' => $service->team_id, | ||
]); | ||
|
||
$date = now()->format('Y-m-d_His'); | ||
$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}"; | ||
|
||
$s3Storage = $node->swarm->data->findS3Storage($backupCmd->backupSchedule->s3StorageId); | ||
if ($s3Storage === null) { | ||
throw new Exception("Could not find S3 storage {$backupCmd->backupSchedule->s3StorageId} in swarm {$node->swarm_id}."); | ||
} | ||
|
||
$taskGroup->tasks()->createMany([ | ||
[ | ||
'type' => NodeTaskType::ServiceExec, | ||
'meta' => [ | ||
'serviceId' => $service->id, | ||
'command' => $backupCommand, | ||
], | ||
'payload' => [ | ||
'ProcessName' => $process->dockerName, | ||
'ExecSpec' => [ | ||
'AttachStdout' => true, | ||
'AttachStderr' => true, | ||
'Cmd' => ['sh', '-c', $backupCommand], | ||
] | ||
] | ||
], | ||
[ | ||
'type' => NodeTaskType::UploadS3File, | ||
'meta' => [ | ||
'serviceId' => $service->id, | ||
], | ||
'payload' => [ | ||
'S3StorageConfigName' => $s3Storage->dockerName, | ||
'VolumeSpec' => [ | ||
'Type' => 'volume', | ||
'Source' => $process->backupVolume->dockerName, | ||
'Target' => $process->backupVolume->path, | ||
], | ||
'SrcFilePath' => $archivePath, | ||
'DestFilePath' => $s3Storage->pathPrefix . '/' . $backupFileName, | ||
], | ||
] | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace App\Models\DeploymentData; | ||
|
||
use Spatie\LaravelData\Data; | ||
|
||
class ProcessBackup extends Data | ||
{ | ||
|
||
public function __construct( | ||
public string $id, | ||
public string $name, | ||
public string $command, | ||
public BackupSchedule $backupSchedule, | ||
) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
database/migrations/2024_07_16_141738_update_deployments_add_backups_to_processes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
DB::update(" | ||
UPDATE deployments | ||
SET data = jsonb_set( | ||
data, | ||
'{processes}', | ||
( | ||
SELECT jsonb_agg(process || jsonb_build_object('backups', '[]'::jsonb)) | ||
FROM jsonb_array_elements(data->'processes') AS process | ||
) | ||
); | ||
"); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
// | ||
} | ||
}; |
Oops, something went wrong.