Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5286 dpurdie 1
<#
2
.SYNOPSIS
3
This Powershell script will change the Mastership of a SVN Repo
4
 
5
.DESCRIPTION
6
	This Powershell script will chnage the Mastership of a SVN Repo
7
	Assumtion: The Repo is present on the current machine
8
 
9
	It will
10
		Determine the location of the Master Repo
11
		Ask the Master for the list of Slave Repo servers
12
		Check that the new master is not the current master
13
		Check that the new master is one of the slave servers
14
		Reconfigure the Repos on all servers
15
 
16
.PARAMETER  Repo
17
Name of the Repo to re-master
18
 
19
.PARAMETER  NewMaster
20
Name of new the new master server
21
 
22
.PARAMETER	ComputerName
23
Name of computer to examne for repo
24
Default is the current computer
25
 
26
.EXAMPLE
27
ChangeMaster.ps1 -Repo MyRepo -NewMaster NewMaster
28
 
29
.NOTES
30
Uses a number of cmdlets provided by VisualSVN.
31
 
32
#>
33
#
34
 
35
#
36
 
37
Param
38
(
39
    [ parameter (Mandatory=$true,
40
	             HelpMessage="Enter the name of the new Master Server")]
41
    [string] $Repo,
42
    [ parameter (Mandatory=$true,
43
				 HelpMessage="Enter the name of the Repository to be remastered")]
44
    [string] $NEWMASTER,
45
    [ parameter () ]
46
    [string] $ComputerName
47
)
48
 
49
 
50
Function Remaster-Repo( $REPO, $NEWMASTER)
51
{
52
    # Init Data
53
    $ALLSERVERS = @()
54
 
55
    #
56
    # Examine current machine and determine data about the repo
57
    #
58
    $argList = @{Namespace= 'root\VisualSVN' ; ClassName = 'VisualSVN_Repository'; ErrorVariable = 'gciError'}
59
    If ($ComputerName)
60
    {
61
        $testSession = New-CimSession $ComputerName -ErrorAction Ignore
62
        If($testSession -eq $null)
63
        {
64
            Write-Error "The named computer ($ComputerName) does not exist" -Category InvalidArgument
65
            Return    
66
        }
67
        Remove-CimSession $testSession
68
        $argList.Add('ComputerName', $ComputerName )
69
    }
70
    #Write-Host 'argList:' , ($argList | Format-List | Out-String)
71
    $repoData = Get-CimInstance @argList | where { $_.Name -eq $REPO }
72
    If ($repoData -eq $null )
73
    {
74
        Write-Error "The repo ($REPO) does not exist on the local machine" -Category InvalidArgument
75
        Return
76
    }
77
    #Write-Host 'RepoData:' , ($repoData | Format-List | Out-String)
78
    $CURMASTER = $repoData.MasterServer
79
    If (! $CURMASTER) { $CURMASTER = $env:COMPUTERNAME }
80
    Write-Host "Current Master:", $CURMASTER
81
    $CURMASTER = $CURMASTER.ToUpper()
82
    $NEWMASTER = $NEWMASTER.ToUpper()
83
    $ALLSERVERS += $CURMASTER
84
 
85
    If ( $NEWMASTER -eq $CURMASTER )
86
    {
87
		# This is not an error
88
        Write-Host "New Master is the same as the Current Master"
89
        Return
90
    }
91
 
92
 
93
    #
94
    # Examine the repo on the Master
95
    #
96
    $RepoData =  Get-SvnRepositoryReplication $REPO -CimSession $CURMASTER
97
    #Write-Host ($RepoData | Format-List | Out-String)
98
    $RepoData.Replicators | ForEach-Object {
99
        $name = $_ -match '\w+\\(\w+)'
100
        If ( $name )
101
        {
102
            $ALLSERVERS += $matches[1].ToUpper()
103
        }
104
        else
105
        {
106
            Write-Error "Replicator data in unexepcted form: $_" -Category InvalidData
107
            Return;
108
        }
109
    }
110
    Write-Host "All Servers:", $ALLSERVERS
111
 
112
    #
113
    # Test that new master is in the set of known servers
114
    #
115
    If ($ALLSERVERS -contains $NEWMASTER )
116
    {
117
        #   Build up a list of replicators
118
        #       All the server names, remove the new master, append '$' so that its the machine name
119
        # 
120
        $REPLICATORS =  $ALLSERVERS | where { $_ -ne $NEWMASTER } | ForEach-Object { $_ + '$' }
121
 
122
        #
123
        # Display stuff
124
        #
125
        Write-Host "New Master:", $NEWMASTER
126
        Write-Host "Replicators: ", $REPLICATORS
127
 
128
        #
129
        # Do the hard part
130
        #
131
        $ALLSERVERS | ForEach-Object { Suspend-SvnRepository -name $REPO -CimSession $_ }
132
 
133
        Switch-SvnRepository -Name $REPO -Role Master -Confirm:$false -CimSession $NEWMASTER
134
 
135
        Set-SvnRepositoryReplication $REPO -Replicators $REPLICATORS -CimSession $NEWMASTER
136
        Set-SvnRepositoryReplication $REPO -Enabled $true -CimSession $NEWMASTER
137
 
138
        Switch-SvnRepository -name $REPO -Role Slave -MasterServer $NEWMASTER -Confirm:$false -CimSession $CURMASTER
139
 
140
        $ALLSERVERS | Where { $_ -ne $NEWMASTER } | ForEach-Object {
141
            Set-SvnRepository $REPO -MasterServer $NEWMASTER  -Confirm:$false -CimSession $_
142
        }
143
 
144
        $ALLSERVERS | ForEach-Object { Resume-SvnRepository -name $REPO -CimSession $_ }
145
    }
146
    else
147
    {
148
        Write-Error "The new master ($NEWMASTER) is not one of the replicated slaves : $ALLSERVERS" -Category InvalidArgument
149
        Return
150
    }
151
 }
152
 
153
 #
154
 # Invoke the function
155
 #
156
 #Write-Host "Repo:", $REPO, "NewMaster:", $NEWMASTER
157
Remaster-Repo $REPO $NEWMASTER