code style fix

add command tests
This commit is contained in:
Saeed Vaziry
2023-09-02 16:41:42 +02:00
parent 71f9dabc24
commit e1eb42059f
135 changed files with 1859 additions and 135 deletions

View File

@ -59,7 +59,7 @@ private function validate($type, Server $server, array $input): void
'0 0 * * *',
'0 0 * * 0',
'0 0 1 * *',
'custom'
'custom',
]),
],
];

View File

@ -6,7 +6,6 @@
use App\Models\FirewallRule;
use App\Models\Server;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
class CreateRule

View File

@ -15,7 +15,7 @@ public function connect(array $input): void
$sourceControl = new SourceControl([
'provider' => $input['provider'],
'profile' => $input['name'],
'access_token' => $input['token']
'access_token' => $input['token'],
]);
if (! $sourceControl->provider()->connect()) {
@ -24,7 +24,7 @@ public function connect(array $input): void
),
]);
}
$sourceControl->save();
}
@ -36,14 +36,14 @@ private function validate(array $input): void
$rules = [
'provider' => [
'required',
Rule::in(\App\Enums\SourceControl::getValues())
Rule::in(\App\Enums\SourceControl::getValues()),
],
'name' => [
'required',
],
'token' => [
'required'
]
'required',
],
];
Validator::make($input, $rules)->validate();
}

View File

@ -22,15 +22,15 @@ public function create(User $user, array $input): void
'provider' => $input['provider'],
'profile' => $input['name'],
'credentials' => [
'token' => $input['token']
]
'token' => $input['token'],
],
]);
if (! $storageProvider->provider()->connect()) {
throw ValidationException::withMessages([
'token' => __("Couldn't connect to the provider")
'token' => __("Couldn't connect to the provider"),
]);
}
$storageProvider->save();;
$storageProvider->save();
}
private function validate(User $user, array $input): void
@ -45,8 +45,8 @@ private function validate(User $user, array $input): void
Rule::unique('storage_providers', 'profile')->where('user_id', $user->id),
],
'token' => [
'required'
]
'required',
],
])->validate();
}
}

View File

@ -4,7 +4,6 @@
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class CreateUserCommand extends Command
{
@ -20,6 +19,6 @@ public function handle(): void
'password' => bcrypt($this->argument('password')),
]);
$this->info("User created with password");
$this->info('User created with password');
}
}

View File

@ -3,7 +3,6 @@
namespace App\Contracts;
use App\Models\Server;
use Symfony\Component\HttpFoundation\RedirectResponse;
interface StorageProvider
{

View File

@ -7,5 +7,6 @@
final class ServerType extends Enum
{
const REGULAR = 'regular';
const DATABASE = 'database';
}

View File

@ -74,11 +74,11 @@ public function connect(bool $sftp = false): void
$login = $this->connection->login($this->user, $this->privateKey);
if (! $login) {
throw new SSHAuthenticationError("Error authenticating");
throw new SSHAuthenticationError('Error authenticating');
}
} catch (Throwable $e) {
Log::error("Error connecting", [
"msg" => $e->getMessage()
Log::error('Error connecting', [
'msg' => $e->getMessage(),
]);
throw $e;
}

View File

@ -64,6 +64,6 @@ class Kernel extends HttpKernel
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'server-is-ready' => ServerIsReadyMiddleware::class
'server-is-ready' => ServerIsReadyMiddleware::class,
];
}

View File

@ -20,9 +20,9 @@ class DatabaseBackupFiles extends Component
public Backup $backup;
public string $restoreId = "";
public string $restoreId = '';
public string $restoreDatabaseId = "";
public string $restoreDatabaseId = '';
public int $deleteId;
@ -61,7 +61,7 @@ public function delete(): void
public function render(): View
{
return view('livewire.databases.database-backup-files', [
'files' => $this->backup->files()->orderByDesc('id')->simplePaginate(10)
'files' => $this->backup->files()->orderByDesc('id')->simplePaginate(10),
]);
}
}

View File

@ -75,7 +75,7 @@ public function render(): View
return view('livewire.databases.database-backups', [
'backups' => $this->server->backups,
'databases' => $this->server->databases,
'files' => $this->files
'files' => $this->files,
]);
}
}

View File

@ -21,6 +21,7 @@ public function handle(): void
{
if ($this->server->provider()->isRunning()) {
$this->server->install();
return;
}
@ -33,6 +34,7 @@ public function handle(): void
'server' => $this->server,
])
);
return;
}

View File

@ -5,7 +5,7 @@
use App\Jobs\Job;
use App\Models\FirewallRule;
use App\Models\Service;
use App\SSHCommands\PHPMyAdmin\DeleteNginxPHPMyAdminVHost;
use App\SSHCommands\PHPMyAdmin\DeleteNginxPHPMyAdminVHostCommand;
use Exception;
use Throwable;
@ -48,7 +48,7 @@ private function removeFirewallRule(): void
private function deleteVHost(): void
{
$this->service->server->ssh()->exec(
new DeleteNginxPHPMyAdminVHost('/home/vito/phpmyadmin'),
new DeleteNginxPHPMyAdminVHostCommand('/home/vito/phpmyadmin'),
'delete-phpmyadmin-vhost'
);
}

View File

@ -7,7 +7,7 @@
use App\Helpers\SSH;
use App\Jobs\Job;
use App\Models\Deployment;
use App\SSHCommands\System\RunScript;
use App\SSHCommands\System\RunScriptCommand;
use Throwable;
class Deploy extends Job
@ -34,7 +34,7 @@ public function handle(): void
{
$this->ssh = $this->deployment->site->server->ssh();
$this->ssh->exec(
new RunScript($this->path, $this->script),
new RunScriptCommand($this->path, $this->script),
'deploy',
$this->deployment->site_id
);

View File

@ -34,7 +34,7 @@ public function handle(): void
);
$this->site->save();
$this->site->sourceControl()->provider()->deployKey(
$this->site->domain.'-key-' . $this->site->id,
$this->site->domain.'-key-'.$this->site->id,
$this->site->repository,
$this->site->ssh_key
);

View File

@ -15,7 +15,7 @@ public function handle(Broadcast $event): void
{
Cache::set('broadcast', [
'type' => $event->type,
'data' => $event->data
'data' => $event->data,
], now()->addMinutes(5));
}
}

View File

@ -110,7 +110,7 @@ public static function boot(): void
static::created(function (Site $site) {
$site->deploymentScript()->create([
'name' => 'default',
'content' => ''
'content' => '',
]);
});
}

View File

@ -7,7 +7,6 @@
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{

View File

@ -13,7 +13,7 @@ public function __construct(protected string $provider, protected string $databa
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/backup.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/backup.sh', $this->provider)));
}
public function content(): string

View File

@ -13,7 +13,7 @@ public function __construct(protected string $provider, protected string $name)
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/create.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/create.sh', $this->provider)));
}
public function content(): string

View File

@ -17,7 +17,7 @@ public function __construct(
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/create-user.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/create-user.sh', $this->provider)));
}
public function content(): string

View File

@ -13,7 +13,7 @@ public function __construct(protected string $provider, protected string $name)
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/delete.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/delete.sh', $this->provider)));
}
public function content(): string

View File

@ -13,7 +13,7 @@ public function __construct(protected string $provider, protected string $userna
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/delete-user.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/delete-user.sh', $this->provider)));
}
public function content(): string

View File

@ -17,7 +17,7 @@ public function __construct(
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/link.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/link.sh', $this->provider)));
}
public function content(): string

View File

@ -13,7 +13,7 @@ public function __construct(protected string $provider, protected string $databa
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/restore.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/restore.sh', $this->provider)));
}
public function content(): string

View File

@ -16,7 +16,7 @@ public function __construct(
public function file(): string
{
return File::get(resource_path(sprintf("commands/database/%s/unlink.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/database/%s/unlink.sh', $this->provider)));
}
public function content(): string

View File

@ -21,6 +21,6 @@ public function __construct(
public function file(): string
{
return File::get(resource_path(sprintf("commands/firewall/%s/add-rule.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/firewall/%s/add-rule.sh', $this->provider)));
}
}

View File

@ -21,6 +21,6 @@ public function __construct(
public function file(): string
{
return File::get(resource_path(sprintf("commands/firewall/%s/remove-rule.sh", $this->provider)));
return File::get(resource_path(sprintf('commands/firewall/%s/remove-rule.sh', $this->provider)));
}
}

View File

@ -4,7 +4,6 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class InstallNginxCommand extends Command
{

View File

@ -4,7 +4,6 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class InstallPHPCommand extends Command
{

View File

@ -4,7 +4,6 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class UninstallPHPCommand extends Command
{

View File

@ -5,7 +5,7 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
class DeleteNginxPHPMyAdminVHost extends Command
class DeleteNginxPHPMyAdminVHostCommand extends Command
{
public function __construct(protected string $path)
{

View File

@ -17,6 +17,6 @@ public function file(): string
public function content(): string
{
return 'sudo rm -rf '.$this->path.'*'."\n";
return 'sudo rm -rf '.$this->path.'*';
}
}

View File

@ -5,7 +5,7 @@
use App\SSHCommands\Command;
use Illuminate\Support\Facades\File;
class RunScript extends Command
class RunScriptCommand extends Command
{
public function __construct(protected string $path, protected string $script)
{
@ -20,7 +20,7 @@ public function content(): string
{
return str($this->file())
->replace('__path__', $this->path)
->replace('__script__', make_bash_script($this->script))
->replace('__script__', $this->script)
->toString();
}
}

View File

@ -4,7 +4,6 @@
use App\Enums\OperatingSystem;
use App\Exceptions\CouldNotConnectToProvider;
use App\Notifications\FailedToDeleteServerFromProvider;
use Aws\Ec2\Ec2Client;
use Aws\EC2InstanceConnect\EC2InstanceConnectClient;
use Exception;

View File

@ -4,7 +4,6 @@
use App\Exceptions\CouldNotConnectToProvider;
use App\Exceptions\ServerProviderError;
use App\Notifications\FailedToDeleteServerFromProvider;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

View File

@ -4,7 +4,6 @@
use App\Exceptions\CouldNotConnectToProvider;
use App\Exceptions\ServerProviderError;
use App\Notifications\FailedToDeleteServerFromProvider;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;

View File

@ -4,7 +4,6 @@
use App\Exceptions\CouldNotConnectToProvider;
use App\Exceptions\ServerProviderError;
use App\Notifications\FailedToDeleteServerFromProvider;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@ -104,7 +103,7 @@ public function create(): void
if (count($errors) > 0) {
$msg = $errors[0]['reason'];
}
Log::error("Linode error", $errors);
Log::error('Linode error', $errors);
throw new ServerProviderError($msg);
}
$this->server->ip = $create->json()['ipv4'][0];

View File

@ -4,7 +4,6 @@
use App\Exceptions\CouldNotConnectToProvider;
use App\Exceptions\ServerProviderError;
use App\Notifications\FailedToDeleteServerFromProvider;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

View File

@ -4,9 +4,6 @@
use App\Events\Broadcast;
use App\Jobs\Installation\Initialize;
use App\Jobs\Installation\InstallCertbot;
use App\Jobs\Installation\InstallComposer;
use App\Jobs\Installation\InstallNodejs;
use App\Jobs\Installation\InstallRequirements;
use App\Jobs\Installation\Upgrade;
use Illuminate\Support\Facades\Bus;

View File

@ -5,9 +5,6 @@
use App\Exceptions\FailedToDeployGitHook;
use App\Exceptions\FailedToDeployGitKey;
use App\Exceptions\FailedToDestroyGitHook;
use App\Exceptions\RepositoryNotFound;
use App\Exceptions\RepositoryPermissionDenied;
use App\Exceptions\SourceControlIsNotConnected;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
@ -39,7 +36,7 @@ public function getRepo(string $repo = null): mixed
public function fullRepoUrl(string $repo, string $key): string
{
return sprintf("git@bitbucket.org-%s:%s.git", $key, $repo);
return sprintf('git@bitbucket.org-%s:%s.git', $key, $repo);
}
/**

View File

@ -2,8 +2,8 @@
namespace App\SourceControlProviders;
use App\Exceptions\FailedToDeployGitKey;
use App\Exceptions\FailedToDeployGitHook;
use App\Exceptions\FailedToDeployGitKey;
use App\Exceptions\FailedToDestroyGitHook;
use Exception;
use Illuminate\Support\Facades\Http;
@ -44,7 +44,7 @@ public function getRepo(string $repo = null): mixed
public function fullRepoUrl(string $repo, string $key): string
{
return sprintf("git@github.com-%s:%s.git", $key, $repo);
return sprintf('git@github.com-%s:%s.git', $key, $repo);
}
/**

View File

@ -2,8 +2,8 @@
namespace App\SourceControlProviders;
use App\Exceptions\FailedToDeployGitKey;
use App\Exceptions\FailedToDeployGitHook;
use App\Exceptions\FailedToDeployGitKey;
use App\Exceptions\FailedToDestroyGitHook;
use Exception;
use Illuminate\Support\Facades\Http;
@ -36,7 +36,7 @@ public function getRepo(string $repo = null): mixed
public function fullRepoUrl(string $repo, string $key): string
{
return sprintf("git@gitlab.com-%s:%s.git", $key, $repo);
return sprintf('git@gitlab.com-%s:%s.git', $key, $repo);
}
/**

View File

@ -17,7 +17,7 @@ public function connect(): bool
{
$res = Http::withToken($this->storageProvider->credentials['token'])
->post($this->apiUrl.'/check/user', [
'query' => ''
'query' => '',
]);
return $res->successful();
@ -40,7 +40,7 @@ public function upload(Server $server, string $src, string $dest): array
$data = json_decode($upload, true);
if (isset($data['error'])) {
throw new BackupFileException("Failed to upload to Dropbox ".$data['error_summary'] ?? '');
throw new BackupFileException('Failed to upload to Dropbox '.$data['error_summary'] ?? '');
}
return [