| 5288 |
dpurdie |
1 |
<#
|
|
|
2 |
.SYNOPSIS
|
|
|
3 |
This Powershell script will update the hooks for ONE Repo an all servers
|
|
|
4 |
|
|
|
5 |
.DESCRIPTION
|
|
|
6 |
This Powershell script will update the hooks for ONE Repo an all servers
|
|
|
7 |
from the current machine.
|
|
|
8 |
|
|
|
9 |
It will
|
|
|
10 |
Locate the Repo Master
|
|
|
11 |
Determine the Replica machines (Slaves)
|
|
|
12 |
This will give a complete list of machines to replicate hooks scripts
|
|
|
13 |
|
|
|
14 |
Determine the source of the hooks scripts
|
|
|
15 |
If the Master instance of the Repo has a 'specialhooks' marker file then
|
|
|
16 |
the hooks in the Master Repo will be replicated to all slaves
|
|
|
17 |
|
|
|
18 |
If the Master does not have the 'specialhooks' marker then
|
|
|
19 |
then global hooks will be copied from the current machine
|
|
|
20 |
|
|
|
21 |
|
|
|
22 |
For each slave it will:
|
|
|
23 |
Locate the Machine path to Slave Repo
|
|
|
24 |
Copy appropriate hook files to the Repo
|
|
|
25 |
Propagate the specialhooks file
|
|
|
26 |
|
|
|
27 |
Global Hook files are found in the Repostitory root and are *.cmd
|
|
|
28 |
|
|
|
29 |
.PARAMETER Repo
|
|
|
30 |
Name of the Repo to create
|
|
|
31 |
|
|
|
32 |
.PARAMETER TestMode
|
|
|
33 |
If present then no copy operations will be done
|
|
|
34 |
|
|
|
35 |
.EXAMPLE
|
|
|
36 |
UpdateRepoHooks.ps1 -Repo myRepo
|
|
|
37 |
|
|
|
38 |
.NOTES
|
|
|
39 |
Uses a number of cmdlets provided by VisualSVN.
|
|
|
40 |
|
|
|
41 |
#>
|
|
|
42 |
#
|
|
|
43 |
|
|
|
44 |
Param
|
|
|
45 |
(
|
|
|
46 |
[ parameter (Mandatory=$true,
|
|
|
47 |
HelpMessage="Enter the name of the Repository")]
|
|
|
48 |
[string] $Repo,
|
|
|
49 |
[switch] $TestMode
|
|
|
50 |
)
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
#
|
|
|
54 |
# Get information on a named repo on the named Computer
|
|
|
55 |
function GetRepoInfo( $repoName, $ComputerName )
|
|
|
56 |
{
|
|
|
57 |
$repoData = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Repository -ComputerName $ComputerName | Where { $_.Name -eq $repoName }
|
|
|
58 |
Return $RepoData
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
#
|
|
|
62 |
# Get information on the current SVN server on the named Computer
|
|
|
63 |
#
|
|
|
64 |
function GetSvnInfo($ComputerName)
|
|
|
65 |
{
|
|
|
66 |
$repoData = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Service -ComputerName $ComputerName
|
|
|
67 |
Return $RepoData
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
#
|
|
|
71 |
# Convert Absolute Path to UNC on a named computer
|
|
|
72 |
#
|
|
|
73 |
Function AbsToUnc( $ComputerName, $absPath )
|
|
|
74 |
{
|
|
|
75 |
return '\\' + $ComputerName + '\' + $absPath.Replace(":","$")
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
# Init Data
|
|
|
79 |
$ALLSERVERS = @()
|
|
|
80 |
|
|
|
81 |
#
|
|
|
82 |
# Locate the Repo Master
|
|
|
83 |
# Examine current machine and determine data about the repo
|
|
|
84 |
#
|
|
|
85 |
$repoData = GetRepoInfo $Repo $env:COMPUTERNAME
|
|
|
86 |
If ($repoData -eq $null )
|
|
|
87 |
{
|
|
|
88 |
Write-Error "The repo ($REPO) does not exist on the local machine" -Category InvalidArgument
|
|
|
89 |
Return
|
|
|
90 |
}
|
|
|
91 |
#Write-Host 'RepoData:' , ($repoData | Format-List | Out-String)
|
|
|
92 |
$CURMASTER = $repoData.MasterServer
|
|
|
93 |
If (! $CURMASTER) { $CURMASTER = $env:COMPUTERNAME }
|
|
|
94 |
Write-Host "Current Master:", $CURMASTER
|
|
|
95 |
$CURMASTER = $CURMASTER.ToUpper()
|
|
|
96 |
$ALLSERVERS += $CURMASTER
|
|
|
97 |
|
|
|
98 |
#
|
|
|
99 |
# Examine the repo on the Master
|
|
|
100 |
#
|
|
|
101 |
$RepoData = Get-SvnRepositoryReplication $REPO -CimSession $CURMASTER
|
|
|
102 |
#Write-Host ($RepoData | Format-List | Out-String)
|
|
|
103 |
$RepoData.Replicators | ForEach-Object {
|
|
|
104 |
$name = $_ -match '\w+\\(\w+)'
|
|
|
105 |
If ( $name )
|
|
|
106 |
{
|
|
|
107 |
$ALLSERVERS += $matches[1].ToUpper()
|
|
|
108 |
}
|
|
|
109 |
else
|
|
|
110 |
{
|
|
|
111 |
Write-Error "Replicator data in unexepcted form: $_" -Category InvalidData
|
|
|
112 |
Return;
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
Write-Host "All Servers:", $ALLSERVERS
|
|
|
116 |
|
|
|
117 |
#
|
|
|
118 |
# Determine the root of the Repositores on this computer
|
|
|
119 |
# This will be the source for the Hook Scripts
|
|
|
120 |
#
|
|
|
121 |
$SvnInfo = GetSvnInfo $env:COMPUTERNAME
|
|
|
122 |
if ($SvnInfo -eq $null)
|
|
|
123 |
{
|
|
|
124 |
Write-Error 'Cannot get VisualSVN info'
|
|
|
125 |
Exit
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
# Validate the Repo Root
|
|
|
129 |
$repoRoot = $SvnInfo.RepositoriesRoot
|
|
|
130 |
If (!(Test-Path $repoRoot))
|
|
|
131 |
{
|
|
|
132 |
Write-Error 'RepositoriesRoot - Does not exist'
|
|
|
133 |
Exit
|
|
|
134 |
}
|
|
|
135 |
Write-Host "Local Repo Root: ", $repoRoot
|
|
|
136 |
|
|
|
137 |
#
|
|
|
138 |
# Examine the 'Master' repo
|
|
|
139 |
# Determine if it contains a specialhooks marker
|
|
|
140 |
#
|
|
|
141 |
$masterRepo = GetRepoInfo $Repo $CURMASTER
|
|
|
142 |
$masterHookPathUnc = AbsToUnc $CURMASTER ($masterRepo.Path + '\hooks')
|
|
|
143 |
$specialHooks = Test-Path ($masterHookPathUnc + '\specialhooks')
|
|
|
144 |
Write-Host 'Master Hooks: ', $masterHookPathUnc
|
|
|
145 |
Write-Host 'Special Hooks: ', $specialHooks
|
|
|
146 |
|
|
|
147 |
|
|
|
148 |
#
|
|
|
149 |
# Process each Machine in the set
|
|
|
150 |
#
|
|
|
151 |
$ALLSERVERS | ForEach-Object {
|
|
|
152 |
Write-Host "Processing: ", $_
|
|
|
153 |
|
|
|
154 |
#
|
|
|
155 |
# If the Master Repo had specialhooks, then copy the Master repo Hooks
|
|
|
156 |
# Otherwise copy the global hooks
|
|
|
157 |
#
|
|
|
158 |
If ($specialHooks)
|
|
|
159 |
{
|
|
|
160 |
If ( $_ -eq $CURMASTER )
|
|
|
161 |
{
|
|
|
162 |
Return
|
|
|
163 |
}
|
|
|
164 |
$HookSrc = $masterHookPathUnc
|
|
|
165 |
}
|
|
|
166 |
else
|
|
|
167 |
{
|
|
|
168 |
$HookSrc = $repoRoot
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
#
|
|
|
172 |
# Get target Repo Information
|
|
|
173 |
# Want the path to the hooks directory
|
|
|
174 |
#
|
|
|
175 |
$targetRepo = GetRepoInfo $Repo $_
|
|
|
176 |
$hookPath = $targetRepo.Path + '\hooks'
|
|
|
177 |
$hookPathUnc = AbsToUnc $_ $hookPath
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
#
|
|
|
181 |
# Copy files to Remote Computer
|
|
|
182 |
#
|
|
|
183 |
Write-Host "Hooks Found", $hookPathUnc
|
|
|
184 |
$HookFileCount = 0
|
|
|
185 |
|
|
|
186 |
Get-ChildItem $HookSrc -Filter '*.cmd' | ForEach-Object {
|
|
|
187 |
$srcFile = $_.Name
|
|
|
188 |
If ( ! $TestMode )
|
|
|
189 |
{
|
|
|
190 |
Copy-Item $_.FullName -Destination $hookPathUnc
|
|
|
191 |
}
|
|
|
192 |
$HookFileCount++
|
|
|
193 |
}
|
|
|
194 |
Write-Host ("{0,20} : {1}" -f $Repo, "Hooks copied: $HookFileCount")
|
|
|
195 |
|
|
|
196 |
#
|
|
|
197 |
# Propagate specialhook information
|
|
|
198 |
#
|
|
|
199 |
If ( ! $TestMode )
|
|
|
200 |
{
|
|
|
201 |
If ($specialHooks)
|
|
|
202 |
{
|
|
|
203 |
Write-Host ("{0,20} : {1}" -f $Repo, "Copy specialhooks")
|
|
|
204 |
Copy-Item ($HookSrc + '\specialhooks') -Destination $hookPathUnc
|
|
|
205 |
}
|
|
|
206 |
else
|
|
|
207 |
{
|
|
|
208 |
Remove-Item ($hookPathUnc + '\specialhooks') -ErrorAction SilentlyContinue
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|