Provisioning fixes: auth-based readiness, domain join guards, site push
- wait for WinRM with an authenticated call, not a TCP probe: the port opens before the service can authenticate - skip promotion/join when the machine is already there, so provision is idempotent - probe the domain with System.DirectoryServices: Get-ADDomain needs RSAT, which a member server does not have - passwords must not contain the account name (Legault2024! is rejected) - Web-Dir-Browsing is the real feature name - push the site over SMB from C$: WinRM caps the command line at 8 KB
This commit is contained in:
+13
-6
@@ -12,20 +12,21 @@ _winrm() {
|
|||||||
# A guest that is installing features or restarting WinRM answers intermittently.
|
# A guest that is installing features or restarting WinRM answers intermittently.
|
||||||
_winrm_retry() {
|
_winrm_retry() {
|
||||||
local n=1
|
local n=1
|
||||||
while (( n <= 5 )); do
|
while (( n <= 10 )); do
|
||||||
if _winrm "$@"; then return 0; fi
|
if _winrm "$@"; then return 0; fi
|
||||||
warn "winrm attempt ${n}/5 failed, retrying"
|
warn "winrm attempt ${n}/10 failed, retrying"
|
||||||
sleep 15
|
sleep 15
|
||||||
n=$(( n + 1 ))
|
n=$(( n + 1 ))
|
||||||
done
|
done
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# The TCP port opens before WinRM can authenticate, so probe with a real call.
|
||||||
_wait_winrm() {
|
_wait_winrm() {
|
||||||
local host=$1 timeout=${2:-1800}
|
local host=$1 timeout=${2:-1800}
|
||||||
local deadline=$(( SECONDS + timeout ))
|
local deadline=$(( SECONDS + timeout ))
|
||||||
while (( SECONDS < deadline )); do
|
while (( SECONDS < deadline )); do
|
||||||
if tcp_open "$host" 5985; then
|
if _winrm "$host" ps 'exit 0' >/dev/null 2>&1; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
sleep 10
|
sleep 10
|
||||||
@@ -82,12 +83,18 @@ _provision_one() {
|
|||||||
ok "$name: $marker"
|
ok "$name: $marker"
|
||||||
|
|
||||||
# A reboot stage drops WinRM; wait for the guest to come back before post.
|
# A reboot stage drops WinRM; wait for the guest to come back before post.
|
||||||
if [[ $marker == *reboot* ]] || [[ -f $post ]]; then
|
if [[ -f $post ]]; then
|
||||||
info " waiting for the guest to come back"
|
info " waiting for the guest to come back"
|
||||||
_wait_winrm "$ip" 900 || { fail "$name: did not come back after reboot"; return 1; }
|
_wait_winrm "$ip" 900 || { fail "$name: did not come back after reboot"; return 1; }
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ -f $post ]]; then
|
# Clear a marker from an earlier attempt, then run the stage ourselves —
|
||||||
|
# an ONSTART task has already fired by the time we get here.
|
||||||
|
_winrm_retry "$ip" ps "Remove-Item -Force -ErrorAction SilentlyContinue 'C:\\norbourg\\roles\\$role-post.ps1.done'" >/dev/null || true
|
||||||
|
_winrm_retry "$ip" start "C:\\norbourg\\roles\\$role-post.ps1" || {
|
||||||
|
fail "$name: could not start $role-post.ps1"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
marker=$(_winrm "$ip" wait "C:\\norbourg\\roles\\$role-post.ps1" 3600) || {
|
marker=$(_winrm "$ip" wait "C:\\norbourg\\roles\\$role-post.ps1" 3600) || {
|
||||||
fail "$name: $role-post.ps1 did not finish"
|
fail "$name: $role-post.ps1 did not finish"
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
+42
-4
@@ -4,6 +4,14 @@
|
|||||||
|
|
||||||
cmd_web() {
|
cmd_web() {
|
||||||
local site="$LAB_DIR/web/site" seed="$LAB_DIR/web/seed"
|
local site="$LAB_DIR/web/site" seed="$LAB_DIR/web/seed"
|
||||||
|
local sync=0 arg
|
||||||
|
for arg in "$@"; do
|
||||||
|
case $arg in
|
||||||
|
--sync) sync=1 ;;
|
||||||
|
-h|--help) info "usage: ./lab.sh web [--sync]"; return 0 ;;
|
||||||
|
*) die "unknown argument: $arg" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
step "assemble"
|
step "assemble"
|
||||||
python3 "$LAB_DIR/scripts/prepare_site.py" || die "site assembly failed"
|
python3 "$LAB_DIR/scripts/prepare_site.py" || die "site assembly failed"
|
||||||
@@ -21,8 +29,38 @@ cmd_web() {
|
|||||||
printf ' %-34s %s\n' "index.html" \
|
printf ' %-34s %s\n' "index.html" \
|
||||||
"$([[ -f $site/index.html ]] && echo present || echo MISSING)"
|
"$([[ -f $site/index.html ]] && echo present || echo MISSING)"
|
||||||
|
|
||||||
printf '\n'
|
if (( ! sync )); then
|
||||||
ok "web root ready: ${site#$LAB_DIR/}"
|
printf '\n'
|
||||||
info " preview: python3 -m http.server 8080 --directory ${site#$LAB_DIR/}"
|
ok "web root ready: ${site#$LAB_DIR/}"
|
||||||
info " sync to FS01 arrives in phase 7"
|
info " preview: python3 -m http.server 8080 --directory ${site#$LAB_DIR/}"
|
||||||
|
info " push to FS01: ./lab.sh web --sync"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
_web_sync "$site"
|
||||||
|
}
|
||||||
|
|
||||||
|
_web_sync() {
|
||||||
|
local site=$1
|
||||||
|
local i ip remote='C:\\norbourg\\site'
|
||||||
|
|
||||||
|
i=$(vm_index fs01) || die "fs01 is not defined in lab.conf"
|
||||||
|
ip=${VM_IPS[$i]}
|
||||||
|
|
||||||
|
step "push to fs01 ($ip)"
|
||||||
|
tcp_open "$ip" 445 || die "fs01 SMB is not reachable — is it provisioned?"
|
||||||
|
have_cmd smbclient || die "smbclient is required to push the site"
|
||||||
|
|
||||||
|
# Bulk copy goes over SMB: WinRM caps the command line at ~8 KB.
|
||||||
|
smbclient "//$ip/C\$" -W "$NETBIOS" \
|
||||||
|
-U "$LAB_USER%$LAB_ADMIN_PASSWORD" \
|
||||||
|
-c "prompt OFF; recurse ON; mkdir norbourg; mkdir norbourg\\site; lcd $site; cd norbourg\\site; mput *" \
|
||||||
|
>/dev/null 2>&1 || true
|
||||||
|
|
||||||
|
_wait_winrm "$ip" 120 || die "fs01 did not answer WinRM"
|
||||||
|
_winrm_retry "$ip" ps "Copy-Item -Path 'C:\\norbourg\\site\\*' -Destination 'C:\\inetpub\\wwwroot' -Recurse -Force; Test-Path 'C:\\inetpub\\wwwroot\\index.html'" \
|
||||||
|
| grep -qi true || die "copy failed on fs01"
|
||||||
|
|
||||||
|
ok "site published to C:\\inetpub\\wwwroot"
|
||||||
|
info " http://10.10.10.20/ · http://www.norbourg.lab/"
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -27,7 +27,7 @@ except ImportError: # pragma: no cover
|
|||||||
print("pywinrm is not installed: python3 -m pip install --user pywinrm", file=sys.stderr)
|
print("pywinrm is not installed: python3 -m pip install --user pywinrm", file=sys.stderr)
|
||||||
raise SystemExit(2)
|
raise SystemExit(2)
|
||||||
|
|
||||||
CHUNK = 1400 # keep each WinRM command well under the payload limit
|
CHUNK = 1500 # -EncodedCommand is UTF-16, so 1500 chars becomes ~4000 base64 chars
|
||||||
|
|
||||||
|
|
||||||
def connect(host: str, user: str, password: str, port: int) -> "winrm.Session":
|
def connect(host: str, user: str, password: str, port: int) -> "winrm.Session":
|
||||||
|
|||||||
@@ -30,9 +30,11 @@ function Fail-Stage {
|
|||||||
function Wait-ForDomain {
|
function Wait-ForDomain {
|
||||||
param([int]$TimeoutSeconds = 600)
|
param([int]$TimeoutSeconds = 600)
|
||||||
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
|
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
|
||||||
|
# Get-ADDomain needs the AD PowerShell module, which only exists on the DC.
|
||||||
|
# System.DirectoryServices works on any domain-joined machine.
|
||||||
while ((Get-Date) -lt $deadline) {
|
while ((Get-Date) -lt $deadline) {
|
||||||
try {
|
try {
|
||||||
$null = Get-ADDomain -ErrorAction Stop
|
$null = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
|
||||||
return
|
return
|
||||||
} catch {
|
} catch {
|
||||||
Start-Sleep -Seconds 10
|
Start-Sleep -Seconds 10
|
||||||
|
|||||||
@@ -27,14 +27,14 @@ try {
|
|||||||
|
|
||||||
# ------------------------------------------------------------ users
|
# ------------------------------------------------------------ users
|
||||||
$users = @(
|
$users = @(
|
||||||
@{ Sam='m.legault'; Given='Marie'; Surname='Legault'; Pwd='Legault2024!'; OU='TI'; Title='Directrice des TI' }
|
@{ Sam='m.legault'; Given='Marie'; Surname='Legault'; Pwd='Maple2024!'; OU='TI'; Title='Directrice des TI' }
|
||||||
@{ Sam='s.bergeron'; Given='Sophie'; Surname='Bergeron'; Pwd='Bergeron2024!'; OU='TI'; Title='Technicienne, soutien aux usagers' }
|
@{ Sam='s.bergeron'; Given='Sophie'; Surname='Bergeron'; Pwd='Nuage2024!'; OU='TI'; Title='Technicienne, soutien aux usagers' }
|
||||||
@{ Sam='helpdesk1'; Given='Sylvie'; Surname='Tremblay'; Pwd='Welcome2024!'; OU='TI'; Title='Compte partage du helpdesk' }
|
@{ Sam='helpdesk1'; Given='Sylvie'; Surname='Tremblay'; Pwd='Welcome2024!'; OU='TI'; Title='Compte partage du helpdesk' }
|
||||||
@{ Sam='svc_backup'; Given='Service'; Surname='Sauvegarde'; Pwd='Backup2023!'; OU='TI'; Title='Compte de service, sauvegarde' }
|
@{ Sam='svc_backup'; Given='Service'; Surname='Sauvegarde'; Pwd='Backup2023!'; OU='TI'; Title='Compte de service, sauvegarde' }
|
||||||
@{ Sam='t.smith'; Given='Thomas'; Surname='Smith'; Pwd='Str0ngAdmin!2026'; OU='TI'; Title='Administrateur de domaine' }
|
@{ Sam='t.smith'; Given='Thomas'; Surname='Smith'; Pwd='Str0ngAdmin!2026'; OU='TI'; Title='Administrateur de domaine' }
|
||||||
@{ Sam='j.contractor'; Given='Jacques'; Surname='Contractor'; Pwd='Winter2024!'; OU='Utilisateurs'; Title='Consultant, migration' }
|
@{ Sam='j.contractor'; Given='Jacques'; Surname='Contractor'; Pwd='Winter2024!'; OU='Utilisateurs'; Title='Consultant, migration' }
|
||||||
@{ Sam='a.gagnon'; Given='Annie'; Surname='Gagnon'; Pwd='Gagnon2023!'; OU='Utilisateurs'; Title='Comptabilite' }
|
@{ Sam='a.gagnon'; Given='Annie'; Surname='Gagnon'; Pwd='Riviere2023!'; OU='Utilisateurs'; Title='Comptabilite' }
|
||||||
@{ Sam='p.roy'; Given='Pierre'; Surname='Roy'; Pwd='Roy2022!'; OU='Utilisateurs'; Title='Analyste, operations' }
|
@{ Sam='p.roy'; Given='Pierre'; Surname='Roy'; Pwd='Foret2022!'; OU='Utilisateurs'; Title='Analyste, operations' }
|
||||||
)
|
)
|
||||||
|
|
||||||
foreach ($u in $users) {
|
foreach ($u in $users) {
|
||||||
@@ -145,7 +145,6 @@ try {
|
|||||||
}
|
}
|
||||||
Write-LabLog 'dc01: advanced audit policy enabled'
|
Write-LabLog 'dc01: advanced audit policy enabled'
|
||||||
|
|
||||||
$null = schtasks.exe /Delete /TN 'NorbourgDcPost' /F
|
|
||||||
Complete-Stage $self 'ok'
|
Complete-Stage $self 'ok'
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
|||||||
+6
-10
@@ -8,6 +8,12 @@ $self = $PSCommandPath
|
|||||||
Write-LabLog 'dc01: stage 1 starting'
|
Write-LabLog 'dc01: stage 1 starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
# Re-running provision must not try to promote a server that already is one.
|
||||||
|
if ((Get-Service NTDS -ErrorAction SilentlyContinue).Status -eq 'Running') {
|
||||||
|
Complete-Stage $self 'ok already-dc'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
$feature = Get-WindowsFeature -Name AD-Domain-Services
|
$feature = Get-WindowsFeature -Name AD-Domain-Services
|
||||||
if (-not $feature.Installed) {
|
if (-not $feature.Installed) {
|
||||||
Write-LabLog 'dc01: installing AD-Domain-Services'
|
Write-LabLog 'dc01: installing AD-Domain-Services'
|
||||||
@@ -16,14 +22,6 @@ try {
|
|||||||
Write-LabLog 'dc01: AD-Domain-Services already installed'
|
Write-LabLog 'dc01: AD-Domain-Services already installed'
|
||||||
}
|
}
|
||||||
|
|
||||||
$post = Join-Path $PSScriptRoot 'dc01-post.ps1'
|
|
||||||
if (-not (Test-Path $post)) { Fail-Stage $self 'dc01-post.ps1 missing next to dc01.ps1' }
|
|
||||||
|
|
||||||
# Run the account/ACL stage once, at next startup, as SYSTEM.
|
|
||||||
$null = schtasks.exe /Create /TN 'NorbourgDcPost' /F /SC ONSTART /RU SYSTEM /RL HIGHEST `
|
|
||||||
/TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$post`""
|
|
||||||
Write-LabLog 'dc01: scheduled dc01-post.ps1 at startup'
|
|
||||||
|
|
||||||
$safeMode = ConvertTo-SecureString '@@SAFE_MODE_PASSWORD@@' -AsPlainText -Force
|
$safeMode = ConvertTo-SecureString '@@SAFE_MODE_PASSWORD@@' -AsPlainText -Force
|
||||||
|
|
||||||
Write-LabLog 'dc01: creating forest norbourg.lab'
|
Write-LabLog 'dc01: creating forest norbourg.lab'
|
||||||
@@ -32,8 +30,6 @@ try {
|
|||||||
-DomainNetbiosName '@@NETBIOS@@' `
|
-DomainNetbiosName '@@NETBIOS@@' `
|
||||||
-InstallDns `
|
-InstallDns `
|
||||||
-SafeModeAdministratorPassword $safeMode `
|
-SafeModeAdministratorPassword $safeMode `
|
||||||
-ForestMode WinThreshold `
|
|
||||||
-DomainMode WinThreshold `
|
|
||||||
-NoRebootOnCompletion `
|
-NoRebootOnCompletion `
|
||||||
-Force
|
-Force
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,16 @@ $self = $PSCommandPath
|
|||||||
Write-LabLog 'fs01: starting'
|
Write-LabLog 'fs01: starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
# Re-running provision must not try to join a machine that already joined.
|
||||||
|
if ((Get-CimInstance Win32_ComputerSystem).Domain -eq '@@DOMAIN@@') {
|
||||||
|
Write-LabLog 'fs01: already joined'
|
||||||
|
Complete-Stage $self 'ok'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
$features = @(
|
$features = @(
|
||||||
'Web-Server', 'Web-Common-Http', 'Web-Default-Doc',
|
'Web-Server', 'Web-Common-Http', 'Web-Default-Doc',
|
||||||
'Web-Static-Content', 'Web-Directory-Browsing', 'Web-Http-Logging'
|
'Web-Static-Content', 'Web-Dir-Browsing', 'Web-Http-Logging'
|
||||||
)
|
)
|
||||||
foreach ($f in $features) {
|
foreach ($f in $features) {
|
||||||
if (-not (Get-WindowsFeature -Name $f).Installed) {
|
if (-not (Get-WindowsFeature -Name $f).Installed) {
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ try {
|
|||||||
Set-Acl -Path $flagDir -AclObject $acl
|
Set-Acl -Path $flagDir -AclObject $acl
|
||||||
Write-LabLog 'ws01: user.txt in place'
|
Write-LabLog 'ws01: user.txt in place'
|
||||||
|
|
||||||
$null = schtasks.exe /Delete /TN 'NorbourgWsPost' /F
|
|
||||||
Complete-Stage $self 'ok'
|
Complete-Stage $self 'ok'
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Stage $self $_.Exception.Message
|
Fail-Stage $self $_.Exception.Message
|
||||||
|
|||||||
@@ -8,11 +8,12 @@ $self = $PSCommandPath
|
|||||||
Write-LabLog 'ws01: starting'
|
Write-LabLog 'ws01: starting'
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$post = Join-Path $PSScriptRoot 'ws01-post.ps1'
|
# Re-running provision must not try to join a machine that already joined.
|
||||||
if (-not (Test-Path $post)) { Fail-Stage $self 'ws01-post.ps1 missing' }
|
if ((Get-CimInstance Win32_ComputerSystem).Domain -eq '@@DOMAIN@@') {
|
||||||
|
Write-LabLog 'ws01: already joined'
|
||||||
$null = schtasks.exe /Create /TN 'NorbourgWsPost' /F /SC ONSTART /RU SYSTEM /RL HIGHEST `
|
Complete-Stage $self 'ok'
|
||||||
/TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$post`""
|
return
|
||||||
|
}
|
||||||
|
|
||||||
$cred = New-Object System.Management.Automation.PSCredential(
|
$cred = New-Object System.Management.Automation.PSCredential(
|
||||||
'@@DOMAIN@@\Administrator',
|
'@@DOMAIN@@\Administrator',
|
||||||
|
|||||||
Reference in New Issue
Block a user