1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ServerMonitor\Commands; |
4
|
|
|
|
5
|
|
|
use Spatie\ServerMonitor\Models\Check; |
6
|
|
|
use Spatie\ServerMonitor\Models\Host; |
7
|
|
|
|
8
|
|
|
class SyncFile extends BaseCommand |
9
|
|
|
{ |
10
|
|
|
protected $signature = 'server-monitor:sync-file |
11
|
|
|
{path : Path to JSON file with hosts} |
12
|
|
|
{--delete-missing : Delete hosts from the database which are not in the hosts file}'; |
13
|
|
|
|
14
|
|
|
protected $description = 'One way sync hosts from JSON file to database'; |
15
|
|
|
|
16
|
|
|
public function handle() |
17
|
|
|
{ |
18
|
|
|
$json = file_get_contents($this->argument('path')); |
19
|
|
|
|
20
|
|
|
$hostsInFile = collect(json_decode($json, true)); |
21
|
|
|
|
22
|
|
|
$this->createOrUpdateHostsFromFile($hostsInFile); |
23
|
|
|
|
24
|
|
|
$this->deleteMissingHosts($hostsInFile); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function createOrUpdateHostsFromFile($hostsInFile) |
28
|
|
|
{ |
29
|
|
|
$hostsInFile->each(function ($hostAttributes) { |
30
|
|
|
$host = $this->createOrUpdateHost($hostAttributes); |
31
|
|
|
|
32
|
|
|
$this->syncChecks($host, $hostAttributes['checks']); |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
$this->info("Synced {$hostsInFile->count()} host(s) to database"); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function deleteMissingHosts($hostsInFile) |
39
|
|
|
{ |
40
|
|
|
if (! $this->option('delete-missing')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->determineHostModelClass()::all() |
|
|
|
|
45
|
|
|
->reject(function (Host $host) use ($hostsInFile) { |
46
|
|
|
return $hostsInFile->contains('name', $host->name); |
47
|
|
|
}) |
48
|
|
|
->each(function (Host $host) { |
49
|
|
|
$this->comment("Deleted host `{$host->name}` from database because was not found in hosts file"); |
50
|
|
|
$host->delete(); |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function createOrUpdateHost(array $hostAttributes): Host |
55
|
|
|
{ |
56
|
|
|
unset($hostAttributes['checks']); |
57
|
|
|
|
58
|
|
|
return tap($this->determineHostModelClass()::firstOrNew([ |
|
|
|
|
59
|
|
|
'name' => $hostAttributes['name'], |
60
|
|
|
]), function (Host $hostModel) use ($hostAttributes) { |
61
|
|
|
$hostModel |
62
|
|
|
->fill($hostAttributes) |
63
|
|
|
->save(); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function syncChecks(Host $host, array $checkTypes): Host |
68
|
|
|
{ |
69
|
|
|
$this->removeChecksNotInArray($host, $checkTypes); |
70
|
|
|
|
71
|
|
|
$this->addChecksFromArray($host, $checkTypes); |
72
|
|
|
|
73
|
|
|
return $host; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function removeChecksNotInArray(Host $host, array $checkTypes) |
77
|
|
|
{ |
78
|
|
|
$host->checks |
79
|
|
|
->reject(function (Check $check) use ($checkTypes) { |
80
|
|
|
return in_array($check->type, $checkTypes); |
81
|
|
|
}) |
82
|
|
|
->each(function (Check $check) use ($host) { |
83
|
|
|
$this->comment("Deleted `{$check->type}` from host `{$host->name}` (not found in hosts file)"); |
84
|
|
|
|
85
|
|
|
return $check->delete(); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function addChecksFromArray(Host $host, array $checkTypes) |
90
|
|
|
{ |
91
|
|
|
collect($checkTypes) |
92
|
|
|
->reject(function (string $checkType) use ($host) { |
93
|
|
|
return $host->hasCheckType($checkType); |
94
|
|
|
}) |
95
|
|
|
->each(function (string $checkType) use ($host) { |
96
|
|
|
$host->checks()->create(['type' => $checkType]); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.