@@ -20,14 +20,33 @@ class DevelopmentRailpackExamplesSeeder extends Seeder
2020{
2121 public const PROJECT_UUID = 'railpack-examples ' ;
2222
23- public const ENVIRONMENT_UUID = 'railpack-examples-production ' ;
24-
2523 public const GIT_REPOSITORY = 'coollabsio/coolify-examples ' ;
2624
2725 public const GIT_BRANCH = 'next ' ;
2826
2927 public const REPOSITORY_PROJECT_ID = 603035348 ;
3028
29+ public const LIMA_SERVERS = [
30+ [
31+ 'server_uuid ' => 'lima-ubuntu-2404 ' ,
32+ 'server_name ' => 'lima-ubuntu-2404 ' ,
33+ 'port ' => 2222 ,
34+ 'environment_name ' => 'ubuntu24 ' ,
35+ 'environment_uuid ' => 'railpack-examples-ubuntu24 ' ,
36+ 'uuid_prefix ' => 'ubuntu24- ' ,
37+ ],
38+ [
39+ 'server_uuid ' => 'lima-ubuntu-2604 ' ,
40+ 'server_name ' => 'lima-ubuntu-2604 ' ,
41+ 'port ' => 2223 ,
42+ 'environment_name ' => 'ubuntu26 ' ,
43+ 'environment_uuid ' => 'railpack-examples-ubuntu26 ' ,
44+ 'uuid_prefix ' => 'ubuntu26- ' ,
45+ ],
46+ ];
47+
48+ private const LIMA_SENTINEL_URL = 'http://host.lima.internal:8000 ' ;
49+
3150 public function run (): void
3251 {
3352 if (! $ this ->isDevelopmentEnvironment ()) {
@@ -37,16 +56,22 @@ public function run(): void
3756 }
3857
3958 $ this ->ensureDevelopmentPrerequisitesExist ();
40- $ destination = StandaloneDocker::query ()->find (0 );
4159
42- if (! $ destination ) {
60+ if (! StandaloneDocker:: query ()-> find ( 0 ) ) {
4361 throw new RuntimeException ('StandaloneDocker with id=0 is required before running DevelopmentRailpackExamplesSeeder. ' );
4462 }
4563
46- $ environment = $ this ->prepareEnvironment ();
64+ $ this ->cleanupLegacyLimaProjects ();
65+ $ this ->cleanupLegacyProductionExamples ();
4766
48- foreach (self ::examples () as $ example ) {
49- $ this ->upsertApplication ($ environment , $ destination , $ example );
67+ foreach (self ::LIMA_SERVERS as $ limaServer ) {
68+ $ this ->seedEnvironment (
69+ environmentUuid: $ limaServer ['environment_uuid ' ],
70+ environmentName: $ limaServer ['environment_name ' ],
71+ destination: $ this ->limaDestination ($ limaServer ['server_uuid ' ]),
72+ uuidPrefix: $ limaServer ['uuid_prefix ' ],
73+ nameSuffix: " ( {$ limaServer ['environment_name ' ]}) " ,
74+ );
5075 }
5176 }
5277
@@ -440,6 +465,37 @@ private function ensureDevelopmentPrerequisitesExist(): void
440465 ],
441466 );
442467
468+ foreach (self ::LIMA_SERVERS as $ limaServer ) {
469+ $ server = Server::query ()->firstOrCreate (
470+ ['uuid ' => $ limaServer ['server_uuid ' ]],
471+ [
472+ 'name ' => $ limaServer ['server_name ' ],
473+ 'description ' => 'This is a Lima VM for local development testing ' ,
474+ 'ip ' => 'host.docker.internal ' ,
475+ 'port ' => $ limaServer ['port ' ],
476+ 'team_id ' => 0 ,
477+ 'private_key_id ' => 1 ,
478+ 'proxy ' => [
479+ 'type ' => ProxyTypes::TRAEFIK ->value ,
480+ 'status ' => ProxyStatus::EXITED ->value ,
481+ ],
482+ ],
483+ );
484+
485+ $ server ->settings ->forceFill ([
486+ 'sentinel_custom_url ' => self ::LIMA_SENTINEL_URL ,
487+ ])->saveQuietly ();
488+
489+ StandaloneDocker::query ()->firstOrCreate (
490+ ['server_id ' => $ server ->id ],
491+ [
492+ 'uuid ' => "{$ limaServer ['server_uuid ' ]}-docker " ,
493+ 'name ' => "{$ limaServer ['server_name ' ]} Docker " ,
494+ 'network ' => 'coolify ' ,
495+ ],
496+ );
497+ }
498+
443499 StandaloneDocker::query ()->firstOrCreate (
444500 ['id ' => 0 ],
445501 [
@@ -489,7 +545,71 @@ private function isDevelopmentEnvironment(): bool
489545 return in_array (config ('app.env ' ), ['local ' , 'development ' , 'dev ' ], true );
490546 }
491547
492- private function prepareEnvironment (): Environment
548+ private function limaDestination (string $ serverUuid ): StandaloneDocker
549+ {
550+ $ limaDestination = Server::query ()
551+ ->where ('uuid ' , $ serverUuid )
552+ ->first ()
553+ ?->standaloneDockers()
554+ ->first ();
555+
556+ if (! $ limaDestination ) {
557+ throw new RuntimeException ("Lima StandaloneDocker destination is required for {$ serverUuid } before running DevelopmentRailpackExamplesSeeder. " );
558+ }
559+
560+ return $ limaDestination ;
561+ }
562+
563+ private function cleanupLegacyLimaProjects (): void
564+ {
565+ Project::query ()
566+ ->whereIn ('uuid ' , [
567+ 'railpack-examples-lima-ubuntu-2404 ' ,
568+ 'railpack-examples-lima-ubuntu-2604 ' ,
569+ ])
570+ ->get ()
571+ ->each (function (Project $ project ): void {
572+ Application::withTrashed ()
573+ ->whereIn ('environment_id ' , $ project ->environments ()->pluck ('id ' ))
574+ ->get ()
575+ ->each
576+ ->forceDelete ();
577+
578+ $ project ->delete ();
579+ });
580+ }
581+
582+ private function cleanupLegacyProductionExamples (): void
583+ {
584+ $ project = Project::query ()->where ('uuid ' , self ::PROJECT_UUID )->first ();
585+
586+ if (! $ project ) {
587+ return ;
588+ }
589+
590+ Application::withTrashed ()
591+ ->whereIn ('environment_id ' , $ project ->environments ()->pluck ('id ' ))
592+ ->whereIn ('uuid ' , collect (self ::examples ())->pluck ('uuid ' ))
593+ ->get ()
594+ ->each
595+ ->forceDelete ();
596+ }
597+
598+ private function seedEnvironment (
599+ string $ environmentUuid ,
600+ string $ environmentName ,
601+ StandaloneDocker $ destination ,
602+ string $ uuidPrefix = '' ,
603+ string $ nameSuffix = '' ,
604+ ): void {
605+ $ environment = $ this ->prepareEnvironment ($ environmentUuid , $ environmentName );
606+
607+ foreach (self ::examples () as $ example ) {
608+ $ this ->upsertApplication ($ environment , $ destination , $ example , $ uuidPrefix , $ nameSuffix );
609+ }
610+ }
611+
612+ private function prepareEnvironment (string $ environmentUuid , string $ environmentName ): Environment
493613 {
494614 $ project = Project::query ()->firstOrNew (['uuid ' => self ::PROJECT_UUID ]);
495615 $ project ->fill ([
@@ -499,17 +619,29 @@ private function prepareEnvironment(): Environment
499619 ]);
500620 $ project ->save ();
501621
502- $ environment = $ project ->environments ()->first ();
622+ $ environment = $ project ->environments ()
623+ ->where (function ($ query ) use ($ environmentName , $ environmentUuid ): void {
624+ $ query
625+ ->where ('name ' , $ environmentName )
626+ ->orWhere ('uuid ' , $ environmentUuid );
627+ })
628+ ->first ();
629+
630+ $ existingEnvironment = $ project ->environments ()->first ();
631+
632+ if (! $ environment && $ project ->environments ()->count () === 1 && $ existingEnvironment ?->name === 'production ' ) {
633+ $ environment = $ existingEnvironment ;
634+ }
503635
504636 if (! $ environment ) {
505637 $ environment = $ project ->environments ()->create ([
506- 'name ' => ' production ' ,
507- 'uuid ' => self :: ENVIRONMENT_UUID ,
638+ 'name ' => $ environmentName ,
639+ 'uuid ' => $ environmentUuid ,
508640 ]);
509641 } else {
510642 $ environment ->update ([
511- 'name ' => ' production ' ,
512- 'uuid ' => self :: ENVIRONMENT_UUID ,
643+ 'name ' => $ environmentName ,
644+ 'uuid ' => $ environmentUuid ,
513645 ]);
514646 }
515647
@@ -519,13 +651,15 @@ private function prepareEnvironment(): Environment
519651 /**
520652 * @param array<string, mixed> $example
521653 */
522- private function upsertApplication (Environment $ environment , StandaloneDocker $ destination , array $ example ): void
654+ private function upsertApplication (Environment $ environment , StandaloneDocker $ destination , array $ example, string $ uuidPrefix = '' , string $ nameSuffix = '' ): void
523655 {
524- $ application = Application::withTrashed ()->firstOrNew (['uuid ' => $ example ['uuid ' ]]);
656+ $ uuid = $ uuidPrefix .$ example ['uuid ' ];
657+ $ name = $ example ['name ' ].$ nameSuffix ;
658+ $ application = Application::withTrashed ()->firstOrNew (['uuid ' => $ uuid ]);
525659 $ application ->fill ([
526- 'name ' => $ example [ ' name ' ] ,
527- 'description ' => $ example [ ' name ' ] ,
528- 'fqdn ' => "http:// {$ example [ ' uuid ' ] }.127.0.0.1.sslip.io " ,
660+ 'name ' => $ name ,
661+ 'description ' => $ name ,
662+ 'fqdn ' => "http:// {$ uuid }.127.0.0.1.sslip.io " ,
529663 'repository_project_id ' => $ example ['repository_project_id ' ] ?? self ::REPOSITORY_PROJECT_ID ,
530664 'git_repository ' => $ example ['git_repository ' ] ?? self ::GIT_REPOSITORY ,
531665 'git_branch ' => $ example ['git_branch ' ] ?? self ::GIT_BRANCH ,
0 commit comments