Subversion Repositories DevTools

Rev

Rev 5287 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<#
.SYNOPSIS
This Powershell script will update the repo-Wide hook in all the Repos
 
.DESCRIPTION
        This Powershell script update the repo-Wide hook in all the Repos on
    the current machine and the named servers

        It will
                Locate the Repository Root
        Copy *.cmd file from the root into each Repo Subdir
            Unless the special hooks/specialhooks file exists
        Process each Named server
            Copy the global files to the server
            Copy the global files to each (non-special) repo on the server

.PARAMETER      Servers
A list of Slave servers that are a part of the subversion network
The hooks will be propergated to all the servers and all the Repos on all the Servers

.EXAMPLE
UpdateHooks.ps1 -Servers auawsasvn001,cloudasvn03
 
.NOTES
Uses a number of cmdlets provided by VisualSVN.
 
#>
#

Param
(
    [ parameter () ]
    [string[]] $Servers
)

#
#   Get information on a named repo
function GetRepoInfo( $repoName )
{
    $repoData = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Repository | Where { $_.Name -eq $repoName } 
    Return $RepoData
}

#
#   Get information on a named repo on the named Computer
function GetRemoteRepoInfo( $repoName, $ComputerName )
{
    $repoData = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Repository -ComputerName $ComputerName | Where { $_.Name -eq $repoName } 
    Return $RepoData
}

#
#   Get information on the current SVN server
#
function GetSvnInfo()
{
    $repoData = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Service
    Return $RepoData
}

#
#   Get information on the SVN server on the named Computer
#
function GetRemoteSvnInfo($ComputerName)
{
    $repoData = Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Service -ComputerName $ComputerName 
    Return $RepoData
}

#
# Convert Absolute Path to UNC on a named computer
#
Function AbsToUnc( $ComputerName, $absPath )
{
    return '\\' + $ComputerName + '\' + $absPath.Replace(":","$")
}

#
# Test a server to see if it exists
#
Function CheckServer( $ComputerName)
{
    $testSession = New-CimSession $ComputerName -ErrorAction Ignore
    If($testSession -eq $null)
    {
        #Write-Error "The named computer ($ComputerName) does not exist" -Category InvalidArgument
        Return $false    
    }
    Remove-CimSession $testSession
    Return $true
}

#
# Determine the root of the Repositores
$SvnInfo = GetSvnInfo
if ($SvnInfo -eq $null)
{
    Write-Error 'Cannot get VisualSVN info'
    Exit
}

# Validate the Repo Root
$repoRoot = $SvnInfo.RepositoriesRoot
If (!(Test-Path $repoRoot)) 
{
    Write-Error 'RepositoriesRoot - Does not exist'
    Exit
}

#
# Process each Repo
$HookSrc = $repoRoot
Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Repository | Sort-Object Name | ForEach-Object {
    $hookPath = $_.Path  + '/hooks'
    $repoName = $_.Name

    If (!(Test-Path ($hookPath + '/specialhooks')))
    {

        $HookFileCount = 0
        Get-ChildItem $HookSrc -Filter '*.cmd' | ForEach-Object {
            $srcFile = $_.Name
            Copy-Item $_.FullName -Destination $hookPath 
            $HookFileCount++
        }
        Write-Host ("{0,20} : {1}" -f $repoName, "Hooks copied: $HookFileCount")
    }
    else
    {
        Write-Host ("{0,20} : {1}" -f $repoName, "SPECIAL - No hooks copied")
    }

}

#
# Process named servers
#
If ($Servers)
{
    $Servers | ForEach-Object{
        $Server = $_
        Write-Host 'Server:' $Server
        If ( ! (CheckServer $Server))
        {
            Write-Host -ForegroundColor Red "The server ($Server) does not exist"
            Return
        }

        #
        #
        $remoteServer = GetRemoteSvnInfo ($Server)
        $remoteRepoBase =  AbsToUnc $Server $remoteServer.RepositoriesRoot

        $HookFileCount = 0
        Get-ChildItem $HookSrc -Filter '*.cmd' | ForEach-Object {
            $srcFile = $_.Name
            Copy-Item $_.FullName -Destination $remoteRepoBase 
            $HookFileCount++
        }
        Write-Host ("{0,20}, {1,20} : {2}" -f $Server, 'Global Hooks', "Hooks copied: $HookFileCount")

        #
        # Update all Repos on the Server
        #
        Get-CimInstance -Namespace "root\VisualSVN" -ClassName VisualSVN_Repository -ComputerName $Server | Sort-Object Name | ForEach-Object {
            $hookPath = AbsToUnc $Server ($_.Path  + '\hooks')
            $repoName = $_.Name

            If (!(Test-Path ($hookPath + '\specialhooks')))
            {

                $HookFileCount = 0
                Get-ChildItem $HookSrc -Filter '*.cmd' | ForEach-Object {
                    $srcFile = $_.Name
                    Copy-Item $_.FullName -Destination $hookPath 
                    $HookFileCount++
                }
                Write-Host ("{0,20}, {1,20} : {2}" -f $Server, $repoName, "Hooks copied: $HookFileCount")
            }
            else
            {
                Write-Host ("{0,20}, {1,20} : {2}" -f $Server, $repoName, "SPECIAL - No hooks copied")
            }

        }

    }
}