2023-04-28 11:50:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
|
|
use Illuminate\Http\Client\Pool;
|
2023-08-08 09:51:36 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2023-04-28 11:50:27 +00:00
|
|
|
|
2023-09-27 19:51:06 +00:00
|
|
|
use function Laravel\Prompts\confirm;
|
|
|
|
|
2023-04-28 13:22:36 +00:00
|
|
|
class SyncBunny extends Command
|
2023-04-28 11:50:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-10-24 09:08:15 +00:00
|
|
|
protected $signature = 'sync:bunny {--templates} {--release}';
|
2023-04-28 11:50:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-04-28 13:22:36 +00:00
|
|
|
protected $description = 'Sync files to BunnyCDN';
|
2023-04-28 11:50:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2023-09-28 09:54:20 +00:00
|
|
|
$that = $this;
|
2023-10-24 09:08:15 +00:00
|
|
|
$only_template = $this->option('templates');
|
|
|
|
$only_version = $this->option('release');
|
2023-06-06 07:22:48 +00:00
|
|
|
$bunny_cdn = "https://cdn.coollabs.io";
|
|
|
|
$bunny_cdn_path = "coolify";
|
|
|
|
$bunny_cdn_storage_name = "coolcdn";
|
2023-04-28 11:50:27 +00:00
|
|
|
|
|
|
|
$parent_dir = realpath(dirname(__FILE__) . '/../../..');
|
|
|
|
|
|
|
|
$compose_file = "docker-compose.yml";
|
|
|
|
$compose_file_prod = "docker-compose.prod.yml";
|
2023-05-10 07:56:12 +00:00
|
|
|
$install_script = "install.sh";
|
2023-04-28 11:50:27 +00:00
|
|
|
$upgrade_script = "upgrade.sh";
|
|
|
|
$production_env = ".env.production";
|
2023-09-27 19:51:06 +00:00
|
|
|
$service_template = "service-templates.json";
|
2023-04-28 11:50:27 +00:00
|
|
|
|
2023-05-11 13:28:34 +00:00
|
|
|
$versions = "versions.json";
|
|
|
|
|
2023-09-28 20:42:10 +00:00
|
|
|
PendingRequest::macro('storage', function ($fileName) use($that) {
|
2023-04-28 11:50:27 +00:00
|
|
|
$headers = [
|
|
|
|
'AccessKey' => env('BUNNY_STORAGE_API_KEY'),
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'Content-Type' => 'application/octet-stream'
|
|
|
|
];
|
2023-09-28 20:42:10 +00:00
|
|
|
$fileStream = fopen($fileName, "r");
|
|
|
|
$file = fread($fileStream, filesize($fileName));
|
|
|
|
$that->info('Uploading: ' . $fileName);
|
2023-04-28 11:50:27 +00:00
|
|
|
return PendingRequest::baseUrl('https://storage.bunnycdn.com')->withHeaders($headers)->withBody($file)->throw();
|
|
|
|
});
|
2023-09-28 09:54:20 +00:00
|
|
|
PendingRequest::macro('purge', function ($url) use ($that) {
|
2023-05-10 07:56:12 +00:00
|
|
|
$headers = [
|
|
|
|
'AccessKey' => env('BUNNY_API_KEY'),
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
];
|
2023-09-28 09:54:20 +00:00
|
|
|
$that->info('Purging: ' . $url);
|
2023-06-06 07:22:48 +00:00
|
|
|
return PendingRequest::withHeaders($headers)->get('https://api.bunny.net/purge', [
|
|
|
|
"url" => $url,
|
|
|
|
"async" => false
|
|
|
|
]);
|
2023-05-10 07:56:12 +00:00
|
|
|
});
|
2023-04-28 11:50:27 +00:00
|
|
|
try {
|
2023-11-24 14:48:23 +00:00
|
|
|
if (!$only_template && !$only_version) {
|
|
|
|
$this->info('About to sync files (docker-compose.prod.yaml, upgrade.sh, install.sh, etc) to BunnyCDN.');
|
|
|
|
}
|
|
|
|
if ($only_template) {
|
|
|
|
$this->info('About to sync service-templates.json to BunnyCDN.');
|
|
|
|
}
|
|
|
|
if ($only_version) {
|
|
|
|
$this->info('About to sync versions.json to BunnyCDN.');
|
|
|
|
}
|
2023-09-28 11:07:52 +00:00
|
|
|
$confirmed = confirm('Are you sure you want to sync?');
|
2023-09-27 19:51:06 +00:00
|
|
|
if (!$confirmed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($only_template) {
|
|
|
|
Http::pool(fn (Pool $pool) => [
|
2023-09-28 20:42:10 +00:00
|
|
|
$pool->storage(fileName: "$parent_dir/templates/$service_template")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$service_template"),
|
2023-09-27 19:51:06 +00:00
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$service_template"),
|
|
|
|
]);
|
|
|
|
$this->info('Service template uploaded & purged...');
|
|
|
|
return;
|
|
|
|
}
|
2023-09-28 11:07:52 +00:00
|
|
|
if ($only_version) {
|
|
|
|
Http::pool(fn (Pool $pool) => [
|
|
|
|
$pool->storage(fileName: "$parent_dir/$versions")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$versions"),
|
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$versions"),
|
|
|
|
]);
|
|
|
|
$this->info('versions.json uploaded & purged...');
|
|
|
|
return;
|
|
|
|
}
|
2023-09-27 19:51:06 +00:00
|
|
|
|
2023-08-11 18:48:52 +00:00
|
|
|
Http::pool(fn (Pool $pool) => [
|
2023-09-28 09:54:20 +00:00
|
|
|
$pool->storage(fileName: "$parent_dir/$compose_file")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$compose_file"),
|
|
|
|
$pool->storage(fileName: "$parent_dir/$compose_file_prod")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$compose_file_prod"),
|
|
|
|
$pool->storage(fileName: "$parent_dir/$production_env")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$production_env"),
|
|
|
|
$pool->storage(fileName: "$parent_dir/scripts/$upgrade_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$upgrade_script"),
|
|
|
|
$pool->storage(fileName: "$parent_dir/scripts/$install_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$install_script"),
|
2023-06-06 07:22:48 +00:00
|
|
|
]);
|
2023-08-11 18:48:52 +00:00
|
|
|
Http::pool(fn (Pool $pool) => [
|
2023-06-06 07:22:48 +00:00
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$compose_file"),
|
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$compose_file_prod"),
|
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$production_env"),
|
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$upgrade_script"),
|
|
|
|
$pool->purge("$bunny_cdn/$bunny_cdn_path/$install_script"),
|
2023-04-28 11:50:27 +00:00
|
|
|
]);
|
2023-09-27 19:51:06 +00:00
|
|
|
$this->info("All files uploaded & purged...");
|
2023-09-11 15:36:30 +00:00
|
|
|
} catch (\Throwable $e) {
|
2023-09-27 19:51:06 +00:00
|
|
|
$this->error("Error: " . $e->getMessage());
|
2023-04-28 11:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|