-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.php
150 lines (135 loc) · 5.78 KB
/
snapshot.php
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
<?php
/**
* Documentation
* https://api.ovh.com/console/
*/
require __DIR__ . '/vendor/autoload.php';
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Monolog\Processor\PsrLogMessageProcessor;
use Ovh\Api;
use Symfony\Component\Yaml\Yaml;
$options = getopt('', ['dry-run']);
$dryRun = isset($options['dry-run']);
$log = new Logger('snapshot');
$log->pushHandler(new StreamHandler(__DIR__ . '/snapshot.log', Logger::DEBUG));
$log->pushProcessor(new PsrLogMessageProcessor());
$config = Yaml::parse(file_get_contents(__DIR__ . '/snapshot.yml'));
$ovh = new Api($config['applicationKey'], $config['applicationSecret'], $config['apiEndpoint'], $config['consumerKey']);
foreach ($config['vps'] as $vpsServiceName) {
echo '--------------------------------------------------' . PHP_EOL;
echo 'VPS: ' . $vpsServiceName . PHP_EOL;
try {
$url = '/vps/' . $vpsServiceName;
$vpsDetails = $ovh->get($url);
} catch (\GuzzleHttp\Exception\RequestException $exception) {
$message = 'The VPS does not exist or is not assigned to this account.';
echo ' ❌ Error: ' . $message . PHP_EOL;
$log->error($message, [
'vps' => $vpsServiceName,
'url' => $url,
'exception' => $exception,
]);
continue;
}
try {
$url = '/vps/' . $vpsServiceName . '/snapshot';
$snapshotDetails = $ovh->get($url);
$snapshotTime = new DateTime($snapshotDetails['creationDate']);
echo ' ✔ Deleting snapshot created at ' . $snapshotTime->format('Y-m-d H:i:s') . PHP_EOL;
if ($dryRun !== true) {
try {
$url = '/vps/' . $vpsServiceName . '/snapshot';
$taskDetails = $ovh->delete($url);
$log->debug('VPS snapshot deleted.', [
'vps' => $vpsServiceName,
'url' => $url,
'snapshot' => $snapshotDetails,
'task' => $taskDetails,
]);
$taskId = $taskDetails['id'];
$maxIterations = 18; // 3 minutes
$currentIteration = 0;
do {
try {
sleep(10); // Wait 10 seconds
$task = $ovh->get('/vps/' . $vpsServiceName . '/tasks/' . $taskId);
$log->debug('Awaiting the deletion of snapshot to finish.', [
'vps' => $vpsServiceName,
'url' => $url,
'task' => $task,
]);
} catch (\GuzzleHttp\Exception\RequestException $exception) {
$message = 'Could not retrieve task status for the deletion of snapshot.';
echo ' ❌ Error: ' . $message . PHP_EOL;
$log->error($message, [
'vps' => $vpsServiceName,
'snapshotDetails' => $snapshotDetails,
'url' => $url,
'exception' => $exception,
]);
}
$currentIteration++;
} while (
!in_array($task['state'] ?? 'error', ['done', 'blocked', 'cancelled', 'error']) &&
$currentIteration < $maxIterations
);
} catch (\GuzzleHttp\Exception\RequestException $exception) {
$message = 'Could not delete the exiting snapshot of the VPS.';
echo ' ❌ Error: ' . $message . PHP_EOL;
$log->error($message, [
'vps' => $vpsServiceName,
'snapshotDetails' => $snapshotDetails,
'url' => $url,
'exception' => $exception,
]);
}
}
} catch (\GuzzleHttp\Exception\RequestException $exception) {
if ($exception->getCode() === 404) {
// There's no error. The Snapshot is not created.
$log->debug('The VPS doesn\'t have a snapshot created.', [
'vps' => $vpsServiceName,
'url' => $url,
'exception' => $exception,
]);
} else {
$message = 'Could not retrieve snapshot information.';
echo ' ❌ Error: ' . $message . PHP_EOL;
$log->error($message, [
'vps' => $vpsServiceName,
'url' => $url,
'exception' => $exception,
]);
}
}
echo ' ✔ Creating snapshot "' . $vpsServiceName . '"' . PHP_EOL;
if ($dryRun !== true) {
try {
$taskDetails = $ovh->post('/vps/' . $vpsServiceName . '/createSnapshot');
$log->debug('VPS snapshot creation is started.', [
'vps' => $vpsServiceName,
'url' => $url,
'task' => $taskDetails,
]);
} catch (\GuzzleHttp\Exception\RequestException $exception) {
$error = json_decode((string)$exception->getResponse()->getBody(), true);
if (preg_match('/There is already \d+ task\(s\) occuring/', $error['message']) === 1) {
// There's no error. The Snapshot is in process of creation.
$log->debug('A Snapshot is already in process of creation.', [
'vps' => $vpsServiceName,
'url' => $url,
'exception' => $exception,
]);
} else {
$message = 'Could not create the snapshot.';
echo ' ❌ Error: ' . $message . PHP_EOL;
$log->error($message, [
'vps' => $vpsServiceName,
'url' => $url,
'exception' => $exception,
]);
}
}
}
}