2023-04-28 11:50:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Http\Client\Pool;
|
|
|
|
|
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-04-28 13:22:36 +00:00
|
|
|
protected $signature = 'sync:bunny';
|
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-05-10 07:56:12 +00:00
|
|
|
$bunny_cdn = "https://coolify-cdn.b-cdn.net";
|
2023-04-28 11:50:27 +00:00
|
|
|
$bunny_cdn_path = "files";
|
|
|
|
$bunny_cdn_storage_name = "coolify-cdn";
|
|
|
|
|
|
|
|
$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";
|
2023-05-03 07:57:06 +00:00
|
|
|
$docker_install_script = "install-docker.sh";
|
2023-04-28 11:50:27 +00:00
|
|
|
$production_env = ".env.production";
|
|
|
|
|
|
|
|
PendingRequest::macro('storage', function ($file) {
|
|
|
|
$headers = [
|
|
|
|
'AccessKey' => env('BUNNY_STORAGE_API_KEY'),
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'Content-Type' => 'application/octet-stream'
|
|
|
|
];
|
|
|
|
$fileStream = fopen($file, "r");
|
|
|
|
$file = fread($fileStream, filesize($file));
|
|
|
|
return PendingRequest::baseUrl('https://storage.bunnycdn.com')->withHeaders($headers)->withBody($file)->throw();
|
|
|
|
});
|
2023-05-10 07:56:12 +00:00
|
|
|
PendingRequest::macro('purge', function ($url) {
|
|
|
|
$headers = [
|
|
|
|
'AccessKey' => env('BUNNY_API_KEY'),
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
];
|
|
|
|
return PendingRequest::withHeaders($headers)->post('https://api.bunny.net/purge', [
|
|
|
|
"urls" => [$url],
|
|
|
|
]);
|
|
|
|
});
|
2023-04-28 11:50:27 +00:00
|
|
|
try {
|
|
|
|
Http::pool(fn (Pool $pool) => [
|
|
|
|
$pool->storage(file: "$parent_dir/$compose_file")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$compose_file"),
|
|
|
|
$pool->storage(file: "$parent_dir/$compose_file_prod")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$compose_file_prod"),
|
|
|
|
$pool->storage(file: "$parent_dir/$production_env")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$production_env"),
|
2023-05-03 07:57:06 +00:00
|
|
|
$pool->storage(file: "$parent_dir/scripts/$upgrade_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$upgrade_script"),
|
2023-05-10 07:56:12 +00:00
|
|
|
$pool->storage(file: "$parent_dir/scripts/$install_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$install_script"),
|
2023-05-03 07:57:06 +00:00
|
|
|
$pool->storage(file: "$parent_dir/scripts/$docker_install_script")->put("/$bunny_cdn_storage_name/$bunny_cdn_path/$docker_install_script"),
|
2023-04-28 11:50:27 +00:00
|
|
|
]);
|
2023-05-10 07:56:12 +00:00
|
|
|
Http::pool(fn (Pool $pool) => [
|
|
|
|
$pool->purge(url: "$bunny_cdn/$bunny_cdn_path/$compose_file"),
|
|
|
|
$pool->purge(url: "$bunny_cdn/$bunny_cdn_path/$compose_file_prod"),
|
|
|
|
$pool->purge(url: "$bunny_cdn/$bunny_cdn_path/$production_env"),
|
|
|
|
$pool->purge(url: "$bunny_cdn/$bunny_cdn_path/$upgrade_script"),
|
|
|
|
$pool->purge(url: "$bunny_cdn/$bunny_cdn_path/$install_script"),
|
|
|
|
$pool->purge(url: "$bunny_cdn/$bunny_cdn_path/$docker_install_script"),
|
2023-04-28 11:50:27 +00:00
|
|
|
]);
|
2023-05-10 07:56:12 +00:00
|
|
|
echo "All files uploaded & purged...\n";
|
|
|
|
return;
|
2023-05-03 07:57:06 +00:00
|
|
|
throw new \Exception("Something went wrong.");
|
2023-04-28 11:50:27 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|