-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-key.php
100 lines (86 loc) · 2.07 KB
/
change-key.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
<?php
require 'func/fetch_page.php';
$config = require 'config.local.php';
const TEAMCITY_SSH_KEY_NAME = 'teamcitySshKey';
if (
! is_array($config)
|| ! isset(
$config['oldKeyName'],
$config['newKeyName'],
$config['teamcityBaseUrl'],
$config['cookie']
)
)
{
throw new RuntimeException('Please supply needed parameters! (Check the Readme!)');
}
$teamcitybaseUrl = $config['teamcityBaseUrl'];
$oldKeyName = $config['oldKeyName'];
$newKeyName = $config['newKeyName'];
$cookieString = "Cookie: {$config['cookie']}\n";
$opts = [
'http' => [
'header' =>
$cookieString,
],
];
$putOpts = [
'http' => [
'method' => 'PUT',
'header' =>
$cookieString .
"Content-Type: text/plain\n" .
"Origin: $teamcitybaseUrl\n",
],
];
$baseVcsRootsUrl = $teamcitybaseUrl . '/app/rest/vcs-roots';
$nextVcsRootsUrl = $baseVcsRootsUrl;
$page = 1;
do
{
$json = fetch_page($nextVcsRootsUrl, $opts);
echo 'Page: ', $page++, ', Count: ' . $json['count'], "\n";
foreach ($json['vcs-root'] as $root)
{
// Gather the VCS Root properties.
$rootInfo = fetch_page(
"${baseVcsRootsUrl}/{$root['id']}/properties",
$opts
);
// Extract the properties into an array.
$rootInfo = array_column($rootInfo['property'], 'value', 'name');
// If we have an SSH Key defined (some roots are password maybe), then
// check if the Key matches the key to replace, or an empty string
// then replace with the new key.
if (
isset($rootInfo[TEAMCITY_SSH_KEY_NAME])
&& $rootInfo[TEAMCITY_SSH_KEY_NAME] === $oldKeyName
)
{
$putOpts['http']['content'] = $newKeyName;
$newTcKeyName = fetch_page(
"${baseVcsRootsUrl}/{$root['id']}/properties/" . TEAMCITY_SSH_KEY_NAME,
$putOpts,
false
);
$resultString = ($newTcKeyName === $newKeyName)
? 'Success'
: 'Failure';
echo $resultString,
', ',
$root['id'],
': ',
$oldKeyName ,
' --> ',
$newKeyName,
"\n";
}
}
// If there's no next URL then we've finished.
if (! isset($json['nextHref']))
{
break;
}
$nextVcsRootsUrl = $teamcitybaseUrl . $json['nextHref'];
}
while ($json['count'] > 0);