lasthourcloud/database/seeders/ProductionSeeder.php

119 lines
4.1 KiB
PHP
Raw Normal View History

2023-04-14 08:00:42 +00:00
<?php
namespace Database\Seeders;
2023-05-22 10:47:15 +00:00
use App\Data\ServerMetadata;
use App\Enums\ProxyStatus;
use App\Enums\ProxyTypes;
2023-05-02 12:12:15 +00:00
use App\Models\GithubApp;
use App\Models\GitlabApp;
2023-04-27 09:29:02 +00:00
use App\Models\InstanceSettings;
2023-04-14 08:00:42 +00:00
use App\Models\PrivateKey;
use App\Models\Server;
2023-05-24 12:56:41 +00:00
use App\Models\StandaloneDocker;
2023-04-14 08:00:42 +00:00
use Illuminate\Database\Seeder;
2023-06-14 06:51:30 +00:00
use Illuminate\Support\Facades\Process;
2023-04-14 08:00:42 +00:00
use Illuminate\Support\Facades\Storage;
class ProductionSeeder extends Seeder
{
public function run(): void
{
2023-04-27 09:29:02 +00:00
if (InstanceSettings::find(0) == null) {
InstanceSettings::create([
'id' => 0
]);
}
2023-05-08 07:33:10 +00:00
if (GithubApp::find(0) == null) {
GithubApp::create([
'id' => 0,
'name' => 'Public GitHub',
'api_url' => 'https://api.github.com',
'html_url' => 'https://github.com',
'is_public' => true,
'team_id' => 0,
]);
}
if (GitlabApp::find(0) == null) {
GitlabApp::create([
'id' => 0,
'name' => 'Public GitLab',
'api_url' => 'https://gitlab.com/api/v4',
'html_url' => 'https://gitlab.com',
'is_public' => true,
'team_id' => 0,
]);
}
2023-04-27 09:29:02 +00:00
// Save SSH Keys for the Coolify Host
$coolify_key_name = "id.root@host.docker.internal";
2023-05-25 10:55:34 +00:00
$coolify_key = Storage::disk('ssh-keys')->get("{$coolify_key_name}");
2023-04-27 10:25:32 +00:00
if ($coolify_key) {
2023-06-17 18:52:15 +00:00
PrivateKey::updateOrCreate(
[
2023-04-27 10:25:32 +00:00
'id' => 0,
'name' => 'localhost\'s key',
'description' => 'The private key for the Coolify host machine (localhost).',
'team_id' => 0,
2023-06-17 18:52:15 +00:00
],
['private_key' => $coolify_key]
);
2023-04-27 10:25:32 +00:00
} else {
echo "No SSH key found for the Coolify host machine (localhost).\n";
2023-06-17 18:52:15 +00:00
echo "Please generate one and save it in /data/coolify/ssh/keys/{$coolify_key_name}\n";
echo "Then try to install again.\n";
exit(1);
}
2023-04-27 09:29:02 +00:00
// Add Coolify host (localhost) as Server if it doesn't exist
if (Server::find(0) == null) {
2023-05-22 10:47:15 +00:00
$server_details = [
'id' => 0,
'name' => "localhost",
2023-05-22 10:47:15 +00:00
'description' => "This is the server where Coolify is running on. Don't delete this!",
'user' => 'root',
'ip' => "host.docker.internal",
'team_id' => 0,
2023-05-22 10:47:15 +00:00
'private_key_id' => 0
];
2023-06-12 12:47:42 +00:00
$server_details['extra_attributes'] = ServerMetadata::from([
'proxy_type' => ProxyTypes::TRAEFIK_V2->value,
'proxy_status' => ProxyStatus::EXITED->value
]);
2023-05-22 10:47:15 +00:00
$server = Server::create($server_details);
2023-06-15 11:51:31 +00:00
$server->settings->is_reachable = true;
2023-05-08 07:33:10 +00:00
$server->settings->save();
2023-05-02 12:12:15 +00:00
}
2023-05-24 12:56:41 +00:00
if (StandaloneDocker::find(0) == null) {
StandaloneDocker::create([
'id' => 0,
'name' => 'localhost-coolify',
'network' => 'coolify',
'server_id' => 0,
]);
}
2023-06-14 06:51:30 +00:00
try {
$settings = InstanceSettings::get();
2023-06-14 09:54:00 +00:00
if (is_null($settings->public_ipv4)) {
$ipv4 = Process::run('curl -4s https://ifconfig.io')->output();
if ($ipv4) {
$ipv4 = trim($ipv4);
$ipv4 = filter_var($ipv4, FILTER_VALIDATE_IP);
$settings->update(['public_ipv4' => $ipv4]);
}
2023-06-14 06:57:17 +00:00
}
2023-06-14 09:54:00 +00:00
if (is_null($settings->public_ipv6)) {
$ipv6 = Process::run('curl -6s https://ifconfig.io')->output();
if ($ipv6) {
$ipv6 = trim($ipv6);
$ipv6 = filter_var($ipv6, FILTER_VALIDATE_IP);
$settings->update(['public_ipv6' => $ipv6]);
}
2023-06-14 06:51:30 +00:00
}
} catch (\Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
2023-04-14 08:00:42 +00:00
}
}