-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSimpleBackupTool.php
More file actions
166 lines (140 loc) · 4 KB
/
Copy pathSimpleBackupTool.php
File metadata and controls
166 lines (140 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
// SPDX-FileCopyrightText: 2020 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Vspheredb\Addon;
use Icinga\Module\Vspheredb\DbObject\CustomValues;
use Icinga\Module\Vspheredb\DbObject\VirtualMachine;
use RuntimeException;
abstract class SimpleBackupTool implements BackupTool
{
public const PREFIX = 'no-such-prefix';
protected $lastAttributes;
protected $customValues = [];
/**
* @return string[]
*/
protected function getCustomValues(): array
{
return $this->customValues;
}
/**
* @param $annotation
* @return bool
*/
public function wantsAnnotation($annotation)
{
return $annotation !== null && strpos($annotation, static::PREFIX) !== false;
}
/**
* @param VirtualMachine $vm
*/
public function handle(VirtualMachine $vm)
{
$this->parseAnnotation($vm->get('annotation'));
$this->parseCustomValues($vm->customValues());
}
/**
* Eventually override this method
*
* @param CustomValues $values
*/
protected function parseCustomValues(CustomValues $values)
{
$attributes = [];
foreach ($this->getCustomValues() as $name) {
if ($values->has($name)) {
$attributes[$name] = $values->get($name);
}
}
if (! empty($attributes)) {
$this->lastAttributes = $attributes;
}
}
/**
* @param VirtualMachine $vm
* @return bool
*/
public function wants(VirtualMachine $vm)
{
$values = $vm->customValues();
foreach ($this->getCustomValues() as $name) {
if ($values->has($name)) {
return true;
}
}
return $this->wantsAnnotation($vm->get('annotation'));
}
/**
* @return array|null
*/
public function getAttributes()
{
return $this->lastAttributes;
}
/**
* @return array
*/
public function requireParsedAttributes()
{
$attributes = $this->getAttributes();
if ($attributes === null) {
throw new RuntimeException('Got no ' . $this->getName() . ' annotation info');
}
return $attributes;
}
protected function parseAnnotation($annotation)
{
if ($annotation === null) {
return;
}
$this->lastAttributes = null;
$begin = strpos($annotation, static::PREFIX);
if ($begin === false) {
return;
}
$end = strpos($annotation, "\n", $begin);
if ($end === false) {
$end = strlen($annotation);
}
$realBegin = $begin + strlen(static::PREFIX);
$match = substr($annotation, $realBegin, $end - $realBegin);
$parts = preg_split('/],\s/', rtrim($match, ']'));
$attributes = [];
foreach ($parts as $part) {
if (strpos($part, ': [') === false) {
continue;
}
[$key, $value] = preg_split('/:\s\[/', $part, 2);
$attributes[trim($key)] = $value;
}
if (array_key_exists('Time', $attributes)) {
$attributes['Time'] = strtotime($attributes['Time']);
}
$this->lastAttributes = $attributes;
}
public function stripAnnotation(&$annotation)
{
$begin = strpos($annotation, static::PREFIX);
if ($begin === false) {
return;
}
$end = strpos($annotation, "\n", $begin);
if ($end === false) {
$end = strlen($annotation);
}
$annotation = substr($annotation, 0, $begin)
. substr($annotation, $end);
}
public function stripCustomValues(CustomValues $values)
{
foreach ($this->getCustomValues() as $name) {
$values->remove($name);
}
}
public function removeCustomValues(CustomValues $values)
{
foreach ($this->getCustomValues() as $name) {
$values->remove($name);
}
}
}