Skip to content

Commit

Permalink
Merge pull request #84 from BrianFarnhill/dev
Browse files Browse the repository at this point in the history
Added MinRole, fixed DCache issues
  • Loading branch information
BrianFarnhill committed Oct 6, 2015
2 parents 9d29b45 + 7977c1c commit 420d65f
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ function Get-TargetResource
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount,
[parameter(Mandatory = $true)] [System.String] $Passphrase,
[parameter(Mandatory = $true)] [System.String] $AdminContentDatabaseName,
[parameter(Mandatory = $false)] [System.UInt32] $CentralAdministrationPort
[parameter(Mandatory = $false)] [System.UInt32] $CentralAdministrationPort,
[parameter(Mandatory = $false)] [ValidateSet("Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd")] $ServerRole
)

Write-Verbose -Message "Checking for local SP Farm"

if ($null -ne $ServerRole -and (Get-xSharePointInstalledProductVersion).FileMajorPart -ne 16) {
throw [Exception] "Server role is only supported in SharePoint 2016."
}

$result = Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock {
$params = $args[0]

Expand Down Expand Up @@ -60,9 +65,14 @@ function Set-TargetResource
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount,
[parameter(Mandatory = $true)] [System.String] $Passphrase,
[parameter(Mandatory = $true)] [System.String] $AdminContentDatabaseName,
[parameter(Mandatory = $false)] [System.UInt32] $CentralAdministrationPort
[parameter(Mandatory = $false)] [System.UInt32] $CentralAdministrationPort,
[parameter(Mandatory = $false)] [ValidateSet("Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd")] $ServerRole
)

if ($null -ne $ServerRole -and (Get-xSharePointInstalledProductVersion).FileMajorPart -ne 16) {
throw [Exception] "Server role is only supported in SharePoint 2016."
}

if (-not $PSBoundParameters.ContainsKey("CentralAdministrationPort")) { $PSBoundParameters.Add("CentralAdministrationPort", 9999) }

$result = Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock {
Expand All @@ -82,8 +92,13 @@ function Set-TargetResource
Write-Verbose -Message "Detected Version: SharePoint 2013"
}
16 {
Write-Verbose -Message "Detected Version: SharePoint 2016"
$newFarmArgs.Add("LocalServerRole", "Custom")
if ($params.ContainsKey("ServerRole") -eq $true) {
Write-Verbose -Message "Detected Version: SharePoint 2016 - configuring server as $($params.ServerRole)"
$newFarmArgs.Add("LocalServerRole", $params.ServerRole)
} else {
Write-Verbose -Message "Detected Version: SharePoint 2016 - no server role provided, configuring server without a specific role"
$newFarmArgs.Add("ServerRoleOptional", $true)
}
}
Default {
throw [Exception] "An unknown version of SharePoint (Major version $_) was detected. Only versions 15 (SharePoint 2013) or 16 (SharePoint 2016) are supported."
Expand Down Expand Up @@ -112,9 +127,14 @@ function Test-TargetResource
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount,
[parameter(Mandatory = $true)] [System.String] $Passphrase,
[parameter(Mandatory = $true)] [System.String] $AdminContentDatabaseName,
[parameter(Mandatory = $false)] [System.UInt32] $CentralAdministrationPort
[parameter(Mandatory = $false)] [System.UInt32] $CentralAdministrationPort,
[parameter(Mandatory = $false)] [ValidateSet("Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd")] $ServerRole
)

if ($null -ne $ServerRole -and (Get-xSharePointInstalledProductVersion).FileMajorPart -ne 16) {
throw [Exception] "Server role is only supported in SharePoint 2016."
}

$CurrentValues = Get-TargetResource @PSBoundParameters
Write-Verbose "Checking for local farm presence"
if ($null -eq $CurrentValues) { return $false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class MSFT_xSPCreateFarm : OMI_BaseResource
[Required] String Passphrase;
[Required] String AdminContentDatabaseName;
[Write] uint32 CentralAdministrationPort;
[Write, ValueMap{"Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd"}, Values{"Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd"}] string ServerRole;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Get-TargetResource

if ($null -eq $cacheHost) { return $nullReturnValue }
$computerName = ([System.Net.Dns]::GetHostByName($env:computerName)).HostName
$cacheHostConfig = Get-AFCacheHostConfiguration -ComputerName $computerName -CachePort $cacheHost.PortNo -ErrorAction SilentlyContinue
$cacheHostConfig = Get-AFCacheHostConfiguration -ComputerName $computerName -CachePort ($cacheHost | Where-Object { $_.HostName -eq $computerName }).PortNo -ErrorAction SilentlyContinue

$windowsService = Get-WmiObject "win32_service" -Filter "Name='AppFabricCachingService'"
$firewallRule = Get-NetFirewallRule -DisplayName "SharePoint Distributed Cache" -ErrorAction SilentlyContinue
Expand Down Expand Up @@ -65,13 +65,6 @@ function Set-TargetResource
)

$CurrentState = Get-TargetResource @PSBoundParameters

$isLocalAdmin = Test-xSharePointUserIsLocalAdmin -UserName $ServiceAccount

if (!$isLocalAdmin)
{
Add-xSharePointUserToLocalAdmin -UserName $ServiceAccount
}

if ($Ensure -eq "Present") {
Write-Verbose -Message "Adding the distributed cache to the server"
Expand Down Expand Up @@ -99,16 +92,37 @@ function Set-TargetResource
$params = $args[0]

Add-SPDistributedCacheServiceInstance

Get-SPServiceInstance | Where-Object { $_.TypeName -eq "Distributed Cache" } | Stop-SPServiceInstance -Confirm:$false

$count = 0
$maxCount = 30
while (($count -lt $maxCount) -and ((Get-SPServiceInstance | ? { $_.TypeName -eq "Distributed Cache" -and $_.Status -ne "Disabled" }) -ne $null)) {
Start-Sleep -Seconds 60
$count++
}

Update-SPDistributedCacheSize -CacheSizeInMB $params.CacheSizeInMB

Get-SPServiceInstance | Where-Object { $_.TypeName -eq "Distributed Cache" } | Start-SPServiceInstance

$count = 0
$maxCount = 30
while (($count -lt $maxCount) -and ((Get-SPServiceInstance | ? { $_.TypeName -eq "Distributed Cache" -and $_.Status -ne "Online" }) -ne $null)) {
Start-Sleep -Seconds 60
$count++
}

$farm = Get-SPFarm
$cacheService = $farm.Services | Where-Object { $_.Name -eq "AppFabricCachingService" }
$cacheService.ProcessIdentity.CurrentIdentityType = "SpecificUser"

$account = Get-SPManagedAccount -Identity $params.ServiceAccount
$cacheService.ProcessIdentity.ManagedAccount = $account
$cacheService.ProcessIdentity.Update()
$cacheService.ProcessIdentity.Deploy()
if ($cacheService.ProcessIdentity.ManagedAccount.Username -ne $params.ServiceAccount) {
$cacheService.ProcessIdentity.CurrentIdentityType = "SpecificUser"
$account = Get-SPManagedAccount -Identity $params.ServiceAccount
$cacheService.ProcessIdentity.ManagedAccount = $account
$cacheService.ProcessIdentity.Update()
$cacheService.ProcessIdentity.Deploy()
}
}
}
} else {
Expand All @@ -130,12 +144,6 @@ function Set-TargetResource
}
Write-Verbose -Message "Distributed cache removed."
}

# Remove the FarmAccount from the local Administrators group, if it was added above
if (!$isLocalAdmin)
{
Remove-xSharePointUserToLocalAdmin -UserName $ServiceAccount
}
}

function Test-TargetResource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ function Get-TargetResource
[parameter(Mandatory = $true)] [System.String] $FarmConfigDatabaseName,
[parameter(Mandatory = $true)] [System.String] $DatabaseServer,
[parameter(Mandatory = $true)] [System.String] $Passphrase,
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount,
[parameter(Mandatory = $false)] [ValidateSet("Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd")] $ServerRole
)

Write-Verbose -Message "Checking for local SP Farm"

if ($null -ne $ServerRole -and (Get-xSharePointInstalledProductVersion).FileMajorPart -ne 16) {
throw [Exception] "Server role is only supported in SharePoint 2016."
}

$result = Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock {
$params = $args[0]

Expand Down Expand Up @@ -45,11 +50,16 @@ function Set-TargetResource
[parameter(Mandatory = $true)] [System.String] $FarmConfigDatabaseName,
[parameter(Mandatory = $true)] [System.String] $DatabaseServer,
[parameter(Mandatory = $true)] [System.String] $Passphrase,
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount,
[parameter(Mandatory = $false)] [ValidateSet("Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd")] $ServerRole
)

Write-Verbose -Message "Joining existing farm configuration database"

if ($null -ne $ServerRole -and (Get-xSharePointInstalledProductVersion).FileMajorPart -ne 16) {
throw [Exception] "Server role is only supported in SharePoint 2016."
}

Invoke-xSharePointCommand -Credential $InstallAccount -Arguments $PSBoundParameters -ScriptBlock {
$params = $args[0]

Expand Down Expand Up @@ -102,11 +112,16 @@ function Test-TargetResource
[parameter(Mandatory = $true)] [System.String] $FarmConfigDatabaseName,
[parameter(Mandatory = $true)] [System.String] $DatabaseServer,
[parameter(Mandatory = $true)] [System.String] $Passphrase,
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount
[parameter(Mandatory = $false)] [System.Management.Automation.PSCredential] $InstallAccount,
[parameter(Mandatory = $false)] [ValidateSet("Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd")] $ServerRole
)

if ($null -ne $ServerRole -and (Get-xSharePointInstalledProductVersion).FileMajorPart -ne 16) {
throw [Exception] "Server role is only supported in SharePoint 2016."
}

$CurrentValues = Get-TargetResource @PSBoundParameters
Write-Verbose "Checking for local farm presence"
Write-Verbose "Testing for local farm presence"
return Test-xSharePointSpecificParameters -CurrentValues $CurrentValues -DesiredValues $PSBoundParameters -ValuesToCheck @("FarmConfigDatabaseName")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class MSFT_xSPJoinFarm : OMI_BaseResource
[Key] string DatabaseServer;
[Write, EmbeddedInstance("MSFT_Credential")] String InstallAccount;
[Required] string Passphrase;
[Write, ValueMap{"Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd"}, Values{"Application","Custom","DistributedCache","Search","SingleServer","SingleServerFarm","SpecialLoad","WebFrontEnd"}] string ServerRole;
};

23 changes: 21 additions & 2 deletions Modules/xSharePoint/Modules/xSharePoint.Util/xSharePoint.Util.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ function Invoke-xSharePointCommand() {
}
Write-Verbose "Executing as the local run as user $($Env:USERDOMAIN)\$($Env:USERNAME)"

$result = Invoke-Command @invokeArgs -Verbose
try {
$result = Invoke-Command @invokeArgs -Verbose
} catch {
if ($_.Exception.Message.Contains("An update conflict has occurred, and you must re-try this action")) {
Write-Verbose "Detected an update conflict, restarting server to allow DSC to resume and retry"
$global:DSCMachineStatus = 1
} else {
throw $_
}
}

return $result
} else {
if ($Credential.UserName.Split("\")[1] -eq $Env:USERNAME) {
Expand All @@ -76,7 +86,16 @@ function Invoke-xSharePointCommand() {

if ($session) { $invokeArgs.Add("Session", $session) }

$result = Invoke-Command @invokeArgs -Verbose
try {
$result = Invoke-Command @invokeArgs -Verbose
} catch {
if ($_.Exception.Message.Contains("An update conflict has occurred, and you must re-try this action")) {
Write-Verbose "Detected an update conflict, restarting server to allow DSC to resume and retry"
$global:DSCMachineStatus = 1
} else {
throw $_
}
}

if ($session) { Remove-PSSession $session }
return $result
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ Additional detailed documentation is included on the wiki on GitHub.

### 0.7.0.0

* Bug fixes and stability improvements
* Support for MinRole options in SharePoint 2016
* Fix to distributed cache deployment of more than one server
* Additional bug fixes and stability improvements

### 0.6.0.0

Expand Down
24 changes: 24 additions & 0 deletions Tests/xSharePoint/xSharePoint.Util.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ Describe "xSharePoint.Util" {
It "throws an exception when the run as user is the same as the InstallAccount user" {
{ Invoke-xSharePointCommand -ScriptBlock { return "value" } -Credential (New-Object System.Management.Automation.PSCredential ("$($Env:USERDOMAIN)\$($Env:USERNAME)", (ConvertTo-SecureString "password" -AsPlainText -Force)))} | Should Throw
}

It "throws normal exceptions when triggered in the script block" {
Mock Invoke-Command { throw [Exception] "A random exception" } -ModuleName "xSharePoint.Util"

{ Invoke-xSharePointCommand -ScriptBlock { return "value" } } | Should Throw
}

It "throws normal exceptions when triggered in the script block using InstallAccount" {
Mock Invoke-Command { throw [Exception] "A random exception" } -ModuleName "xSharePoint.Util"

{ Invoke-xSharePointCommand -ScriptBlock { return "value" } -Credential (New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force)))} | Should Throw
}

It "handles a SharePoint update conflict exception by rebooting the server to retry" {
Mock Invoke-Command { throw [Exception] "An update conflict has occurred, and you must re-try this action." } -ModuleName "xSharePoint.Util"

{ Invoke-xSharePointCommand -ScriptBlock { return "value" } } | Should Not Throw
}

It "handles a SharePoint update conflict exception by rebooting the server to retry using InstallAccount" {
Mock Invoke-Command { throw [Exception] "An update conflict has occurred, and you must re-try this action." } -ModuleName "xSharePoint.Util"

{ Invoke-xSharePointCommand -ScriptBlock { return "value" } -Credential (New-Object System.Management.Automation.PSCredential ("username", (ConvertTo-SecureString "password" -AsPlainText -Force)))} | Should Not Throw
}
}

Context "Validate Test-xSharePointSpecificParameters" {
Expand Down
33 changes: 31 additions & 2 deletions Tests/xSharePoint/xSharePoint.xSPCreateFarm.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,51 @@ Describe "xSPCreateFarm" {
Test-TargetResource @testParams | Should Be $false
}

It "calls the appropriate cmdlets in the set method" {
It "calls the new configuration database cmdlet in the set method" {
Set-TargetResource @testParams
switch ($majorBuildNumber)
{
15 {
Assert-MockCalled New-SPConfigurationDatabase
}
16 {
Assert-MockCalled New-SPConfigurationDatabase -ParameterFilter { $LocalServerRole -ne $null }
Assert-MockCalled New-SPConfigurationDatabase -ParameterFilter { $ServerRoleOptional -eq $true }
}
Default {
throw [Exception] "A supported version of SharePoint was not used in testing"
}
}

}

if ($majorBuildNumber -eq 16) {
$testParams.Add("ServerRole", "WebFrontEnd")
It "creates a farm with a specific server role" {
Set-TargetResource @testParams
Assert-MockCalled New-SPConfigurationDatabase -ParameterFilter { $LocalServerRole -eq "WebFrontEnd" }
}
$testParams.Remove("ServerRole")
}
}

if ($majorBuildNumber -eq 15) {
$testParams.Add("ServerRole", "WebFrontEnd")

Context "only valid parameters for SharePoint 2013 are used" {
It "throws if server role is used in the get method" {
{ Get-TargetResource @testParams } | Should Throw
}

It "throws if server role is used in the test method" {
{ Test-TargetResource @testParams } | Should Throw
}

It "throws if server role is used in the set method" {
{ Set-TargetResource @testParams } | Should Throw
}
}

$testParams.Remove("ServerRole")
}

Context "no farm is configured locally and an unsupported version of SharePoint is installed on the server" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Describe "xSPDistributedCacheService" {
Mock Add-xSharePointUserToLocalAdmin { }
Mock Test-xSharePointUserIsLocalAdmin { return $false }
Mock Remove-xSharePointUserToLocalAdmin { }
Mock Restart-Service { }
Mock Get-SPFarm { return @{
Services = @(@{
Name = "AppFabricCachingService"
Expand Down

0 comments on commit 420d65f

Please sign in to comment.