Skip to content

Commit

Permalink
Update snapshot.yml
Browse files Browse the repository at this point in the history
Fix error if no `instances` or no `volumes` are defined
  • Loading branch information
jbelien committed Mar 4, 2018
1 parent 747f404 commit 3099e15
Showing 1 changed file with 68 additions and 60 deletions.
128 changes: 68 additions & 60 deletions snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
foreach ($config['projects'] as $p) {
echo '--------------------------------------------------'.PHP_EOL;
echo 'PROJECT: '.$p['id'].PHP_EOL;
echo count($p['instances']).' INSTANCE(S)'.PHP_EOL;
echo count($p['volumes']).' VOLUME(S)'.PHP_EOL;
if (isset($p['instances']) && is_array($p['instances'])) {
echo count($p['instances']).' INSTANCE(S)'.PHP_EOL;
}
if (isset($p['volumes']) && is_array($p['volumes'])) {
echo count($p['volumes']).' VOLUME(S)'.PHP_EOL;
}

if (isset($config['duration']) && !empty($config['duration'])) {
$time = new DateTime();
Expand Down Expand Up @@ -106,70 +110,74 @@
echo sprintf('%d deleted snapshot(s)', $count).PHP_EOL;
}

if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Created snapshot of '.count($p['instances']).' instance(s)', [
'project' => $p['id'],
]);
}

foreach ($p['instances'] as $instance) {
echo 'INSTANCE: '.$instance['name'].PHP_EOL;

if (isset($p['instances']) && is_array($p['instances'])) {
if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/instance/'.$instance['id'].'/snapshot';
$snapshot = $ovh->post($url, [
'snapshotName' => $instance['name'].' ('.date('Y-m-d H:i:s').')',
]);

$log->debug('PROJECT: {project} - Created snapshot instance "'.$instance['name'].'"', [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
'exception' => $exception,
]);
}
$log->debug('PROJECT: {project} - Created snapshot of '.count($p['instances']).' instance(s)', [
'project' => $p['id'],
]);
}
}

if ($dryrun !== true) {
$log->debug('PROJECT: {project} - Create snapshot of '.count($p['volumes']).' volume(s)', [
'project' => $p['id'],
]);
foreach ($p['instances'] as $instance) {
echo 'INSTANCE: '.$instance['name'].PHP_EOL;

if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/instance/'.$instance['id'].'/snapshot';
$snapshot = $ovh->post($url, [
'snapshotName' => $instance['name'].' ('.date('Y-m-d H:i:s').')',
]);

$log->debug('PROJECT: {project} - Created snapshot instance "'.$instance['name'].'"', [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
'exception' => $exception,
]);
}
}
}
}

foreach ($p['volumes'] as $volume) {
echo 'VOLUME: '.$volume['name'].PHP_EOL;

if (isset($p['volumes']) && is_array($p['volumes'])) {
if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/volume/'.$volume['id'].'/snapshot';
$snapshot = $ovh->post($url, [
'name' => $volume['name'].' ('.date('Y-m-d H:i:s').')',
]);

$log->debug('PROJECT: {project} - Create snapshot volume "'.$volume['name'].'"', [
'project' => $p['id'],
'volume' => $volume,
'snapshot' => $snapshot,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
'exception' => $exception,
]);
$log->debug('PROJECT: {project} - Create snapshot of '.count($p['volumes']).' volume(s)', [
'project' => $p['id'],
]);
}

foreach ($p['volumes'] as $volume) {
echo 'VOLUME: '.$volume['name'].PHP_EOL;

if ($dryrun !== true) {
try {
$url = '/cloud/project/'.$p['id'].'/volume/'.$volume['id'].'/snapshot';
$snapshot = $ovh->post($url, [
'name' => $volume['name'].' ('.date('Y-m-d H:i:s').')',
]);

$log->debug('PROJECT: {project} - Create snapshot volume "'.$volume['name'].'"', [
'project' => $p['id'],
'volume' => $volume,
'snapshot' => $snapshot,
'url' => $url,
]);
} catch (ServerException $exception) {
$log->error($exception->getMessage(), [
'project' => $p['id'],
'instance' => $instance,
'snapshot' => $snapshot,
'url' => $url,
'exception' => $exception,
]);
}
}
}
}
Expand Down

0 comments on commit 3099e15

Please sign in to comment.