-
Notifications
You must be signed in to change notification settings - Fork 5
/
Infra.php
156 lines (136 loc) Β· 4.8 KB
/
Infra.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
151
152
153
154
155
156
<?php
namespace Infra;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Schema;
use Infra\Resource\ResourceInterface;
use Symfony\Component\Yaml\Yaml;
use Infra\Exception;
use Infra\Resource\HostResource;
use Doctrine\Common\Inflector\Inflector;
use SSHClient\ClientConfiguration\ClientConfiguration;
use SSHClient\ClientBuilder\ClientBuilder;
use RuntimeException;
use Graph\Graph;
class Infra
{
protected $graph;
public function __construct(Graph $graph)
{
$this->graph = $graph;
$graph->registerType(Resource\LabelResource::class);
$graph->registerType(Resource\HostResource::class);
$graph->registerType(Resource\HostGroupResource::class);
$graph->registerType(Resource\FirewallRuleResource::class);
$graph->registerType(Resource\UserResource::class);
$graph->registerType(Resource\MonitoringCheckResource::class);
$graph->registerType(Resource\DnsDomainResource::class);
$graph->registerType(Resource\DnsRecordResource::class);
$graph->registerType(Resource\GitRepositoryResource::class);
$graph->registerType(Resource\CronJobResource::class);
$graph->registerType(Resource\FileResource::class);
$graph->registerType(Resource\OsReleaseResource::class);
$graph->registerType(Resource\DockerEngineResource::class);
$graph->registerType(Resource\DockerAppResource::class);
$graph->registerType(Resource\QueryResource::class);
$graph->registerType(Resource\BackupRuleResource::class);
$graph->registerType(Resource\ServiceResource::class);
$graph->init($this);
}
public function getGraph()
{
return $this->graph;
}
public function getInflector()
{
return $this->inflector;
}
// public function getSchema()
// {
// return $this->schema;
// }
public function validate()
{
foreach ($this->graph->getResourcesByType('Host') as $host) {
if ($this->graph->hasResource('HostGroup', $host->getName())) {
throw new RuntimeException("Host with same name as a HostGroup detected: " . $host->getName());
}
}
}
/**
* Returns array of hostnames matched by host name or host group name
*/
private function getHostsAuto(string $name): array
{
if (!$name) {
return [];
}
if ($this->graph->hasResource('HostGroup', $name)) {
$hostGroup = $this->graph->getResource('HostGroup', $name);
return $hostGroup->getHosts();
}
if ($this->graph->hasResource('Host', $name)) {
return [$this->graph->getResource('Host', $name)];
}
throw new Exception\UnknownHostsException($name);
}
/**
* Pass in name(s) as a string, csv or array of strings. Names can be host and/or hostgroup names
*/
public function getHosts($names): array
{
if (is_null($names)) {
return [];
}
if ($names == '*') {
return $this->getResourcesByType('Host');
}
if (is_string($names)) {
$names = explode(',', $names); // turn into array
foreach ($names as $i => $name) {
$names[$i] = trim($name);
}
}
if (!is_array($names)) {
throw new RuntimeException('undefined type (not string or array) passed to infra getHosts');
}
$res = [];
foreach ($names as $i => $name) {
$hosts = $this->getHostsAuto($name);
foreach ($hosts as $host) {
$res[$host->getName()] = $host;
}
}
return $res;
}
public function getSshBuilder(HostResource $host)
{
$config = new ClientConfiguration($host->getSshAddress(), $host->getSshUsername());
// $config->setOptions(array(
// 'IdentityFile' => '~/.ssh/id_rsa',
// 'IdentitiesOnly' => 'yes',
// ));
$builder = new ClientBuilder($config);
return $builder;
}
public function copyTemplate(HostResource $host, $template, $destination)
{
$loader = new \Twig_Loader_Filesystem(__DIR__ . '/../../templates');
$twig = new \Twig_Environment($loader, []);
$data = [];
$data['host'] = $host;
$data['infra'] = $this;
$tmpfile = tempnam(sys_get_temp_dir(), 'infra_');
$content = $twig->render($template, $data);
file_put_contents($tmpfile, $content);
$scpBuilder = $this->getSshBuilder($host);
$scp = $scpBuilder->buildSecureCopyClient();
$scp->copy(
$tmpfile,
$scp->getRemotePath($destination)
);
if ($scp->getExitCode() != 0) {
throw new RuntimeException($scp->getErrorOutput());
}
unlink($tmpfile);
}
}