-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ query { | |
publicIp | ||
privateIp | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
kind: BackupRule | ||
metadata: | ||
name: config-backups-db | ||
spec: | ||
hosts: db # all db hosts | ||
path: "/etc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
kind: BackupRule | ||
metadata: | ||
name: config-backups | ||
spec: | ||
hosts: prod # all production hosts | ||
path: "/etc" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
use Infra\Infra; | ||
use Infra\Resource\HostResource; | ||
use Infra\Sdk\Utils; | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$args = Docopt::handle(file_get_contents(__FILE__ . '.md')); | ||
|
||
$query = <<<GRAPHQL | ||
query { | ||
allBackupRules { | ||
name, | ||
hosts | ||
} | ||
} | ||
GRAPHQL; | ||
|
||
$data = Utils::query($query); | ||
|
||
$hostNames = []; | ||
|
||
foreach ($data['data']['allBackupRules'] as $rule) { | ||
$hostNames[] = $rule['hosts']; | ||
} | ||
|
||
if (empty($hostNames)) { | ||
echo 'There is no any rules.' . PHP_EOL; | ||
|
||
exit(0); | ||
} | ||
|
||
$infra = Infra::make(); | ||
|
||
/** @var HostResource[] $hosts */ | ||
$hosts = $infra->getHosts(implode(', ', $hostNames)); | ||
|
||
if (empty($hosts)) { | ||
echo 'There is no any hosts.' . PHP_EOL; | ||
|
||
exit(0); | ||
} | ||
|
||
foreach ($hosts as $host) { | ||
$sshBuilder = $infra->getSshBuilder($host); | ||
$ssh = $sshBuilder->buildClient(); | ||
|
||
$ssh->exec(['cat /etc/hosts']); | ||
|
||
if ($ssh->getExitCode() !== 0) { | ||
throw new RuntimeException($ssh->getErrorOutput()); | ||
} | ||
|
||
echo '####' . PHP_EOL; | ||
echo '#### Hosts file for ' . $host->getName() . PHP_EOL; | ||
echo '####' . PHP_EOL . PHP_EOL; | ||
echo $ssh->getOutput() . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## backuppc:hosts | ||
|
||
Usage: | ||
hosts | ||
hosts (-h | --help) | ||
|
||
Options: | ||
-h --help Show this screen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require_once __DIR__ . '/../../vendor/autoload.php'; | ||
|
||
$args = Docopt::handle(file_get_contents(__FILE__ . '.md')); | ||
|
||
$hostName = $args->args['<hostname>']; | ||
|
||
echo 'This is a command stub. Hostname is ' . $hostName . PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## backuppc:show | ||
|
||
Usage: | ||
show <hostname> | ||
show (-h | --help) | ||
|
||
Options: | ||
-h --help Show this screen. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Infra\Resource; | ||
|
||
use GraphQL\Type\Definition\Type; | ||
use Graph\Graph; | ||
|
||
class BackupRuleResource extends AbstractResource | ||
{ | ||
public function getHosts(): ?string | ||
{ | ||
return $this->spec['hosts'] ?? null; | ||
} | ||
|
||
public function getPath(): ?string | ||
{ | ||
return $this->spec['path'] ?? null; | ||
} | ||
|
||
public static function getConfig(Graph $graph): array | ||
{ | ||
return [ | ||
'name' => 'BackupRule', | ||
'fields' => [ | ||
'name' => Type::id(), | ||
'description' => [ | ||
'type' => Type::string(), | ||
'description' => 'Description', | ||
], | ||
'hosts' => [ | ||
'type' => Type::string(), | ||
'description' => 'Returns all hosts where this cronjob rule is active', | ||
], | ||
'path' => [ | ||
'type' => Type::string(), | ||
'description' => 'Path', | ||
], | ||
], | ||
]; | ||
} | ||
} |