forked from Prem-Patel/rainmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure-rainmaker-keys.php
executable file
·38 lines (31 loc) · 1.13 KB
/
configure-rainmaker-keys.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
#!/usr/bin/env php
<?php
$scriptBasePath = dirname($argv[0]);
$sshConfigDirFullPath = trim(`cd ~/.ssh && pwd`);
if (!is_dir($sshConfigDirFullPath)) {
mkdir($sshConfigDirFullPath, 0700);
}
copy($scriptBasePath . '/rainmaker-keys/rainmaker_id_rsa', $sshConfigDirFullPath . '/rainmaker_id_rsa');
chmod($sshConfigDirFullPath . '/rainmaker_id_rsa', 0600);
copy($scriptBasePath . '/rainmaker-keys/rainmaker_id_rsa.pub', $sshConfigDirFullPath . '/rainmaker_id_rsa.pub');
chmod($sshConfigDirFullPath . '/rainmaker_id_rsa.pub', 0644);
$sshConfig = '';
if (file_exists($sshConfigDirFullPath . '/config')) {
$sshConfig = file_get_contents($sshConfigDirFullPath . '/config');
}
$startMarker = '# Rainmaker - Start #';
$endMarker = '# Rainmaker - End #';
$count = 0;
$keyConfigs = <<<EOT
$startMarker
Host *.localdev
User rainmaker
IdentityFile ~/.ssh/rainmaker_id_rsa
$endMarker
EOT;
$sshConfig = preg_replace("/$startMarker(?:.+)$endMarker/s", $keyConfigs, $sshConfig, -1, $count);
if ($count < 1) {
$sshConfig .= "\n$keyConfigs\n";
}
file_put_contents($sshConfigDirFullPath . '/config', $sshConfig);
chmod($sshConfigDirFullPath . '/config', 0600);