|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Spatie\ServerMonitor\Models\Enums\CheckStatus; |
|
7
|
|
|
|
|
8
|
|
|
class AddHost extends BaseCommand |
|
9
|
|
|
{ |
|
10
|
|
|
protected $signature = 'server-monitor:add-host'; |
|
11
|
|
|
|
|
12
|
|
|
protected $description = 'Add a host'; |
|
13
|
|
|
|
|
14
|
|
|
public static $allChecksLabel = '<all checks>'; |
|
15
|
|
|
|
|
16
|
|
|
public function handle() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->info("Let's add a host!"); |
|
19
|
|
|
|
|
20
|
|
|
$hostName = $this->ask('What is the name of the host'); |
|
21
|
|
|
|
|
22
|
|
|
$sshUser = $this->confirm('Should a custom ssh user be used?') |
|
23
|
|
|
? $this->ask('Which user?') |
|
24
|
|
|
: null; |
|
25
|
|
|
|
|
26
|
|
|
$port = $this->confirm('Should a custom port be used?') |
|
27
|
|
|
? $this->ask('Which port?') |
|
28
|
|
|
: null; |
|
29
|
|
|
|
|
30
|
|
|
$ip = $this->confirm('Should a specific ip address be used?') |
|
31
|
|
|
? $this->ask('Which ip address?') |
|
32
|
|
|
: null; |
|
33
|
|
|
|
|
34
|
|
|
$checkNames = array_merge([static::$allChecksLabel], $this->getAllCheckNames()); |
|
35
|
|
|
|
|
36
|
|
|
$chosenChecks = $this->choice('Which checks should be performed?', $checkNames, 0, null, true); |
|
37
|
|
|
|
|
38
|
|
|
$chosenChecks = $this->determineChecks($chosenChecks, $checkNames); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if ($this->determineHostModelClass()::where('name', $hostName)->first()) { |
|
|
|
|
|
|
41
|
|
|
throw new InvalidArgumentException("Host `{$hostName}` already exists"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->determineHostModelClass()::create([ |
|
|
|
|
|
|
45
|
|
|
'name' => $hostName, |
|
46
|
|
|
'ssh_user' => $sshUser, |
|
47
|
|
|
'port' => $port, |
|
48
|
|
|
'ip' => $ip, |
|
49
|
|
|
])->checks()->saveMany(collect($chosenChecks)->map(function (string $checkName) { |
|
50
|
|
|
$checkModel = $this->determineCheckModelClass(); |
|
51
|
|
|
|
|
52
|
|
|
return new $checkModel([ |
|
53
|
|
|
'type' => $checkName, |
|
54
|
|
|
'status' => CheckStatus::NOT_YET_CHECKED, |
|
55
|
|
|
'custom_properties' => [], |
|
56
|
|
|
]); |
|
57
|
|
|
})); |
|
58
|
|
|
|
|
59
|
|
|
$this->info("Host `{$hostName}` added"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function determineChecks(array $chosenChecks, array $checkNames): array |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
if (in_array(static::$allChecksLabel, $chosenChecks)) { |
|
65
|
|
|
return $this->getAllCheckNames(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return array_diff($chosenChecks, [static::$allChecksLabel]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function getAllCheckNames(): array |
|
72
|
|
|
{ |
|
73
|
|
|
return array_keys(config('server-monitor.checks')); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.