Skip to content

Commit

Permalink
Merge branch 'pythagor-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfaassen committed Jun 23, 2019
2 parents 93cbafe + 95ed867 commit e077308
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 15 deletions.
2 changes: 1 addition & 1 deletion example/graphql/hosts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ query {
publicIp
privateIp
}
}
}
7 changes: 7 additions & 0 deletions example/resources/BackupRule/config-backups-db.yaml
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"
7 changes: 7 additions & 0 deletions example/resources/BackupRule/config-backups.yaml
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"
60 changes: 60 additions & 0 deletions scripts/backuppc/hosts
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;
}
8 changes: 8 additions & 0 deletions scripts/backuppc/hosts.md
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.
10 changes: 10 additions & 0 deletions scripts/backuppc/show
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;
8 changes: 8 additions & 0 deletions scripts/backuppc/show.md
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.
15 changes: 1 addition & 14 deletions scripts/file/write
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require_once __DIR__ . '/../../vendor/autoload.php';

$args = Docopt::handle(file_get_contents(__FILE__ . '.md'));

$infra = getInfra();
$infra = Infra::make();

$fileName = $args->args['<name>'];

Expand All @@ -28,19 +28,6 @@ if (empty($hosts)) {

writeFileToHosts($fileData, $hosts, $infra);

function getInfra()
{
$infra = new Infra();
$infraConfig = getenv('INFRA_CONFIG');
if (!$infraConfig) {
$infraConfig = __DIR__ . '/../../example';
}
$infra->load($infraConfig);
$infra->validate();

return $infra;
}

function getFileData($fileName)
{
$query = <<<GRAPHQL
Expand Down
2 changes: 2 additions & 0 deletions src/Infra.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Infra
protected $graph;

public function __construct(Graph $graph)

{
$this->graph = $graph;

Expand All @@ -36,6 +37,7 @@ public function __construct(Graph $graph)
$graph->registerType(Resource\DockerEngineResource::class);
$graph->registerType(Resource\DockerAppResource::class);
$graph->registerType(Resource\QueryResource::class);
$graph->registerType(Resource\BackupRuleResource::class);

$graph->init($this);
}
Expand Down
41 changes: 41 additions & 0 deletions src/Resource/BackupRuleResource.php
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',
],
],
];
}
}

0 comments on commit e077308

Please sign in to comment.