diff --git a/session1/content/seed/payload/bootstrap.ps1 b/session1/content/seed/payload/bootstrap.ps1 index ecba3d8..505394f 100644 --- a/session1/content/seed/payload/bootstrap.ps1 +++ b/session1/content/seed/payload/bootstrap.ps1 @@ -98,10 +98,13 @@ try { Set-Content -Path "$base\stamp.txt" -Encoding ascii # ------------------------------------------------------------------ role +# The role stages are driven by the host (`./lab.sh provision`), which orders +# them: the DC must be promoted before a member tries to join. Running them +# here as well made every guest race the harness — fs01 joined before the +# domain existed, and dc01 ran two promotions at once. $role = Join-Path $here "roles\$($Config.Role).ps1" if (Test-Path $role) { - Write-Host "bootstrap: running role $($Config.Role)" - & $role + Write-Host "bootstrap: role $($Config.Role) staged, waiting for ./lab.sh provision" } else { Write-Host "bootstrap: no role script for '$($Config.Role)' yet (phase 5)" } diff --git a/session1/content/windows/lib/common.ps1 b/session1/content/windows/lib/common.ps1 index aae3f98..7b5cd0f 100644 --- a/session1/content/windows/lib/common.ps1 +++ b/session1/content/windows/lib/common.ps1 @@ -27,6 +27,29 @@ function Fail-Stage { throw $Message } +# A stage can be started twice: the harness schedules it, and a retried +# transport call or a manual run can schedule it again. Two concurrent +# Install-ADDSForest calls fight over the same promotion and one of them +# writes FAIL, which stops a provisioning that actually worked. +function Enter-StageLock { + param([string]$Path) + $lock = "$Path.lock" + if (Test-Path $lock) { + $owner = Get-Content $lock -ErrorAction SilentlyContinue + if ($owner -and (Get-Process -Id ([int]$owner) -ErrorAction SilentlyContinue)) { + return $false + } + # The owner is gone (reboot, crash): the lock is stale. + } + Set-Content -Path $lock -Value $PID -Encoding ascii + return $true +} + +function Exit-StageLock { + param([string]$Path) + Remove-Item "$Path.lock" -Force -ErrorAction SilentlyContinue +} + function Wait-ForDomain { param([int]$TimeoutSeconds = 600) $deadline = (Get-Date).AddSeconds($TimeoutSeconds) diff --git a/session1/content/windows/roles/dc01-post.ps1 b/session1/content/windows/roles/dc01-post.ps1 index eef90fe..28d7399 100644 --- a/session1/content/windows/roles/dc01-post.ps1 +++ b/session1/content/windows/roles/dc01-post.ps1 @@ -4,6 +4,10 @@ . "$PSScriptRoot\..\lib\common.ps1" $self = $PSCommandPath +if (-not (Enter-StageLock $self)) { + Write-LabLog 'stage already running on this guest; duplicate start ignored' + return +} Write-LabLog 'dc01: post stage starting' try { @@ -119,14 +123,17 @@ try { $domainAcl.CommitChanges() Write-LabLog 'dc01: Sauvegarde-AD granted replication rights' + # Set-ADObject does not resolve a sAMAccountName reliably (it raises + # ADIdentityNotFoundException), so resolve the DN once and use it below. + $plainDn = (Get-ADUser 'm.legault').DistinguishedName + # m.legault must not carry a stale adminCount flag from an earlier design, # or SDProp would keep treating the account as protected and wipe the ACE. - Set-ADObject -Identity 'm.legault' -Replace @{ adminCount = 0 } -ErrorAction SilentlyContinue + Set-ADObject -Identity $plainDn -Replace @{ adminCount = 0 } -ErrorAction SilentlyContinue # Realistic delegation: the service account manages a plain user who is a # member of the server-admin group. m.legault is NOT in a protected group, # so SDProp leaves this ACE alone — unlike the Domain Admin target above. - $plainDn = (Get-ADUser 'm.legault').DistinguishedName if (-not (dsacls.exe $plainDn | Select-String 'svc_backup')) { $null = dsacls.exe $plainDn /G '@@NETBIOS@@\svc_backup:GA' Write-LabLog 'dc01: GenericAll granted to svc_backup over m.legault' @@ -240,4 +247,6 @@ try { Complete-Stage $self 'ok' } catch { Fail-Stage $self $_.Exception.Message +} finally { + Exit-StageLock $self } diff --git a/session1/content/windows/roles/dc01.ps1 b/session1/content/windows/roles/dc01.ps1 index 22fd312..51a9f1a 100644 --- a/session1/content/windows/roles/dc01.ps1 +++ b/session1/content/windows/roles/dc01.ps1 @@ -5,6 +5,10 @@ . "$PSScriptRoot\..\lib\common.ps1" $self = $PSCommandPath +if (-not (Enter-StageLock $self)) { + Write-LabLog 'stage already running on this guest; duplicate start ignored' + return +} Write-LabLog 'dc01: stage 1 starting' try { @@ -37,4 +41,6 @@ try { Restart-Computer -Force } catch { Fail-Stage $self $_.Exception.Message +} finally { + Exit-StageLock $self } diff --git a/session1/content/windows/roles/fs01.ps1 b/session1/content/windows/roles/fs01.ps1 index 7a828c6..212c97b 100644 --- a/session1/content/windows/roles/fs01.ps1 +++ b/session1/content/windows/roles/fs01.ps1 @@ -5,6 +5,10 @@ . "$PSScriptRoot\..\lib\common.ps1" $self = $PSCommandPath +if (-not (Enter-StageLock $self)) { + Write-LabLog 'stage already running on this guest; duplicate start ignored' + return +} Write-LabLog 'fs01: starting' try { @@ -29,14 +33,19 @@ try { # IIS ships no MIME mapping for .url; the archived "Your Love" shortcut # needs one or IIS answers 404.3. Import-Module WebAdministration + # -Name '.' returns the staticContent element, not its entries, so the + # existence test always failed and a re-run died on a duplicate entry. + # The collection of mappings is -Name 'collection'. $urlMime = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' ` - -Filter 'system.webServer/staticContent' -Name '.' | + -Filter 'system.webServer/staticContent' -Name 'collection' | Where-Object { $_.fileExtension -eq '.url' } if (-not $urlMime) { Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' ` -Filter 'system.webServer/staticContent' -Name '.' ` -Value @{ fileExtension = '.url'; mimeType = 'application/internet-shortcut' } Write-LabLog 'fs01: .url MIME mapping added' + } else { + Write-LabLog 'fs01: .url MIME mapping already present' } $root = 'C:\inetpub\wwwroot' @@ -81,4 +90,6 @@ Ne pas supprimer. Restart-Computer -Force } catch { Fail-Stage $self $_.Exception.Message +} finally { + Exit-StageLock $self } diff --git a/session1/content/windows/roles/ws01-post.ps1 b/session1/content/windows/roles/ws01-post.ps1 index 37b859b..b649be5 100644 --- a/session1/content/windows/roles/ws01-post.ps1 +++ b/session1/content/windows/roles/ws01-post.ps1 @@ -3,6 +3,10 @@ . "$PSScriptRoot\..\lib\common.ps1" $self = $PSCommandPath +if (-not (Enter-StageLock $self)) { + Write-LabLog 'stage already running on this guest; duplicate start ignored' + return +} Write-LabLog 'ws01: post stage starting' try { @@ -36,4 +40,6 @@ try { Complete-Stage $self 'ok' } catch { Fail-Stage $self $_.Exception.Message +} finally { + Exit-StageLock $self } diff --git a/session1/content/windows/roles/ws01.ps1 b/session1/content/windows/roles/ws01.ps1 index d03a9c3..2aa71f1 100644 --- a/session1/content/windows/roles/ws01.ps1 +++ b/session1/content/windows/roles/ws01.ps1 @@ -5,6 +5,10 @@ . "$PSScriptRoot\..\lib\common.ps1" $self = $PSCommandPath +if (-not (Enter-StageLock $self)) { + Write-LabLog 'stage already running on this guest; duplicate start ignored' + return +} Write-LabLog 'ws01: starting' try { @@ -27,4 +31,6 @@ try { Restart-Computer -Force } catch { Fail-Stage $self $_.Exception.Message +} finally { + Exit-StageLock $self }