461 lines
16 KiB
PowerShell
461 lines
16 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$Mapping,
|
|
[string]$Projects,
|
|
[string]$DiscoveryRoot,
|
|
[string]$CompatibilityProfile,
|
|
[string]$PythonExecutable,
|
|
[string]$RunRoot,
|
|
[string]$DXToolsHome,
|
|
[string]$Certifications,
|
|
[string]$SettingsRoot,
|
|
[string]$ResumeJob,
|
|
[switch]$WhatIf
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = "Stop"
|
|
[Console]::OutputEncoding = New-Object Text.UTF8Encoding($false)
|
|
. (Join-Path $PSScriptRoot "PathSafety.ps1")
|
|
. (Join-Path $PSScriptRoot "PartReplacementContract.ps1")
|
|
|
|
function Resolve-FullPath {
|
|
param([string]$Value)
|
|
return [IO.Path]::GetFullPath($Value)
|
|
}
|
|
|
|
function Resolve-RequiredPath {
|
|
param([string]$Value, [string]$Name)
|
|
if ([string]::IsNullOrWhiteSpace($Value)) {
|
|
throw "$Name is required"
|
|
}
|
|
$resolved = Resolve-FullPath $Value
|
|
Assert-DxNoReparsePath $resolved $Name
|
|
return $resolved
|
|
}
|
|
|
|
function Test-SamePath {
|
|
param([string]$Left, [string]$Right)
|
|
return [string]::Equals(
|
|
(Resolve-FullPath $Left).TrimEnd('\'),
|
|
(Resolve-FullPath $Right).TrimEnd('\'),
|
|
[StringComparison]::OrdinalIgnoreCase
|
|
)
|
|
}
|
|
|
|
function Test-DirectChildPath {
|
|
param([string]$Parent, [string]$Child)
|
|
$childParent = Split-Path -Parent (Resolve-FullPath $Child)
|
|
return Test-SamePath $Parent $childParent
|
|
}
|
|
|
|
function Convert-ToRunRelativePath {
|
|
param([string]$ToolRoot, [string]$Path, [string]$Name)
|
|
$resolvedRoot = Resolve-FullPath $ToolRoot
|
|
Assert-DxNoReparsePath $resolvedRoot "DXTOOLS_HOME"
|
|
$homePrefix = $resolvedRoot.TrimEnd('\') + "\"
|
|
$resolved = Resolve-FullPath $Path
|
|
Assert-DxNoReparsePath $resolved $Name
|
|
if (-not $resolved.StartsWith(
|
|
$homePrefix,
|
|
[StringComparison]::OrdinalIgnoreCase
|
|
)) {
|
|
throw "$Name must be under DXTOOLS_HOME: $resolved"
|
|
}
|
|
return $resolved.Substring($homePrefix.Length).Replace('\', '/')
|
|
}
|
|
|
|
function Read-Settings {
|
|
param([string]$Base)
|
|
$resolvedBase = Resolve-FullPath $Base
|
|
Assert-DxNoReparsePath $resolvedBase "settings root"
|
|
$settingsPath = Join-Path (Join-Path $resolvedBase "DxProductivity") "settings.json"
|
|
Assert-DxNoReparsePath $settingsPath "settings path"
|
|
try {
|
|
$settings = Get-Content -LiteralPath $settingsPath -Raw -Encoding UTF8 |
|
|
ConvertFrom-Json -ErrorAction Stop
|
|
}
|
|
catch {
|
|
throw "settings are invalid or missing: $settingsPath"
|
|
}
|
|
Assert-ExactObject $settings @(
|
|
"compatibility_profile", "dxtools_home", "python_executable", "run_root"
|
|
) "settings"
|
|
foreach ($name in @(
|
|
"compatibility_profile", "dxtools_home", "python_executable", "run_root"
|
|
)) {
|
|
if ($settings.$name -isnot [string] -or
|
|
[string]::IsNullOrWhiteSpace($settings.$name)) {
|
|
throw "settings field is invalid: $name"
|
|
}
|
|
}
|
|
return $settings
|
|
}
|
|
|
|
$needsSettings = [string]::IsNullOrWhiteSpace($CompatibilityProfile) -or
|
|
[string]::IsNullOrWhiteSpace($PythonExecutable) -or
|
|
[string]::IsNullOrWhiteSpace($RunRoot) -or
|
|
[string]::IsNullOrWhiteSpace($DXToolsHome)
|
|
if ($needsSettings) {
|
|
if ([string]::IsNullOrWhiteSpace($SettingsRoot)) {
|
|
$SettingsRoot = [Environment]::GetFolderPath(
|
|
[Environment+SpecialFolder]::LocalApplicationData
|
|
)
|
|
}
|
|
$settingsBase = Resolve-FullPath $SettingsRoot
|
|
Assert-DxNoReparsePath $settingsBase "settings root"
|
|
$settings = Read-Settings $settingsBase
|
|
if ([string]::IsNullOrWhiteSpace($CompatibilityProfile)) {
|
|
$CompatibilityProfile = $settings.compatibility_profile
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($PythonExecutable)) {
|
|
$PythonExecutable = $settings.python_executable
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($RunRoot)) {
|
|
$RunRoot = $settings.run_root
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($DXToolsHome)) {
|
|
$DXToolsHome = $settings.dxtools_home
|
|
}
|
|
}
|
|
|
|
$toolHome = Resolve-RequiredPath $DXToolsHome "DXToolsHome"
|
|
$run = Resolve-RequiredPath $RunRoot "RunRoot"
|
|
$profile = Resolve-RequiredPath $CompatibilityProfile "CompatibilityProfile"
|
|
$python = Resolve-RequiredPath $PythonExecutable "PythonExecutable"
|
|
if (-not (Test-Path -LiteralPath $toolHome -PathType Container)) {
|
|
throw "DXTOOLS_HOME does not exist: $toolHome"
|
|
}
|
|
if (-not (Test-Path -LiteralPath $python -PathType Leaf)) {
|
|
throw "PythonExecutable does not exist: $python"
|
|
}
|
|
|
|
$deployedRun = Join-Path $toolHome "runs"
|
|
$deployedProfile = Join-Path $deployedRun "compatibility-profile.json"
|
|
Assert-DxNoReparsePath $deployedRun "deployed run root"
|
|
Assert-DxNoReparsePath $deployedProfile "deployed compatibility profile"
|
|
if (-not (Test-SamePath $run $deployedRun)) {
|
|
throw "RunRoot must use the deployed DXTOOLS_HOME runs directory"
|
|
}
|
|
if (-not (Test-SamePath $profile $deployedProfile)) {
|
|
throw "CompatibilityProfile must use the deployed compatibility profile"
|
|
}
|
|
if (-not (Test-Path -LiteralPath $deployedProfile -PathType Leaf)) {
|
|
throw "deployed compatibility profile does not exist: $deployedProfile"
|
|
}
|
|
|
|
$discovery = $null
|
|
$mappingPath = $null
|
|
$projectsPath = $null
|
|
$certificationPath = $null
|
|
$resume = $null
|
|
$resumeMode = -not [string]::IsNullOrWhiteSpace($ResumeJob)
|
|
if ($resumeMode) {
|
|
foreach ($value in @($Mapping, $Projects, $Certifications, $DiscoveryRoot)) {
|
|
if (-not [string]::IsNullOrWhiteSpace($value)) {
|
|
throw "ResumeJob cannot be combined with create or discovery inputs"
|
|
}
|
|
}
|
|
if (-not [IO.Path]::IsPathRooted($ResumeJob)) {
|
|
throw "ResumeJob must be an absolute job directory"
|
|
}
|
|
$resume = Resolve-RequiredPath $ResumeJob "ResumeJob"
|
|
if (-not (Test-DirectChildPath $deployedRun $resume)) {
|
|
throw "ResumeJob must be a direct child of the deployed run root"
|
|
}
|
|
if (-not (Test-Path -LiteralPath $resume -PathType Container)) {
|
|
throw "ResumeJob does not exist: $resume"
|
|
}
|
|
}
|
|
elseif (-not [string]::IsNullOrWhiteSpace($DiscoveryRoot)) {
|
|
$discovery = Resolve-RequiredPath $DiscoveryRoot "DiscoveryRoot"
|
|
}
|
|
else {
|
|
$mappingPath = Resolve-RequiredPath $Mapping "Mapping"
|
|
$projectsPath = Resolve-RequiredPath $Projects "Projects"
|
|
$certificationPath = Resolve-RequiredPath $Certifications "Certifications"
|
|
}
|
|
|
|
if ($null -ne $mappingPath) {
|
|
Write-Output "Mapping=$mappingPath"
|
|
Write-Output "Certifications=$certificationPath"
|
|
}
|
|
if ($null -ne $resume) {
|
|
Write-Output "ResumeJob=$resume"
|
|
}
|
|
Write-Output "CompatibilityProfile=$deployedProfile"
|
|
Write-Output "PythonExecutable=$python"
|
|
Write-Output "RunRoot=$deployedRun"
|
|
Write-Output "DXToolsHome=$toolHome"
|
|
if ($null -ne $discovery) {
|
|
Write-Output "DiscoveryRoot=$discovery"
|
|
}
|
|
elseif ($null -ne $projectsPath) {
|
|
Write-Output "Projects=$projectsPath"
|
|
}
|
|
if ($WhatIf) {
|
|
exit 0
|
|
}
|
|
|
|
Assert-DxNoReparsePath $toolHome "DXTOOLS_HOME"
|
|
Assert-DxNoReparsePath $deployedRun "deployed run root"
|
|
Assert-DxNoReparsePath $deployedProfile "deployed compatibility profile"
|
|
Assert-DxNoReparsePath $python "PythonExecutable"
|
|
if ($null -ne $resume) {
|
|
Assert-DxNoReparsePath $resume "ResumeJob"
|
|
}
|
|
elseif ($null -ne $discovery) {
|
|
Assert-DxNoReparsePath $discovery "DiscoveryRoot"
|
|
}
|
|
else {
|
|
Assert-DxNoReparsePath $mappingPath "Mapping"
|
|
Assert-DxNoReparsePath $projectsPath "Projects"
|
|
Assert-DxNoReparsePath $certificationPath "Certifications"
|
|
}
|
|
New-Item -ItemType Directory -Force -Path $deployedRun | Out-Null
|
|
if ($null -ne $discovery) {
|
|
$discoveredProjects = Join-Path $deployedRun "discovered-projects.txt"
|
|
Assert-DxNoReparsePath $python "PythonExecutable"
|
|
Assert-DxNoReparsePath $discovery "DiscoveryRoot"
|
|
Assert-DxNoReparsePath $discoveredProjects "discovery output"
|
|
$discoverArguments = @(
|
|
"-m", "dx_productivity", "discover", $discovery,
|
|
"--output", $discoveredProjects
|
|
)
|
|
& $python @discoverArguments
|
|
if ($LASTEXITCODE -ne 0) {
|
|
exit $LASTEXITCODE
|
|
}
|
|
Write-Output "GeneratedProjects=$discoveredProjects"
|
|
Write-Output "Review discovered candidates before creating a job."
|
|
exit 0
|
|
}
|
|
|
|
if ($null -ne $resume) {
|
|
$job = $resume
|
|
Assert-DxNoReparsePath $python "PythonExecutable"
|
|
Assert-DxNoReparsePath $job "job directory"
|
|
$statusArguments = @(
|
|
"-m", "dx_productivity", "--json", "status", $job
|
|
)
|
|
$statusOutput = @(& $python @statusArguments)
|
|
if ($LASTEXITCODE -ne 0) {
|
|
exit $LASTEXITCODE
|
|
}
|
|
try {
|
|
$resumeStatus = [string]::Join(
|
|
[Environment]::NewLine,
|
|
$statusOutput
|
|
) | ConvertFrom-Json -ErrorAction Stop
|
|
}
|
|
catch {
|
|
throw "status did not return valid JSON"
|
|
}
|
|
Assert-ResumeStatus $resumeStatus
|
|
if ([string]$resumeStatus.job_id -cne (Split-Path -Leaf $job)) {
|
|
throw "status result job_id does not match ResumeJob"
|
|
}
|
|
$jobId = [string]$resumeStatus.job_id
|
|
$projectRecords = @(
|
|
$resumeStatus.projects | ForEach-Object {
|
|
[pscustomobject]@{
|
|
display_name = [string]$_.display_name
|
|
project_id = [string]$_.project_id
|
|
project_path = [string]$_.project_path
|
|
}
|
|
}
|
|
)
|
|
$projectContext = "status"
|
|
}
|
|
else {
|
|
$job = Join-Path $deployedRun (
|
|
"part-replace-" + [guid]::NewGuid().ToString("N")
|
|
)
|
|
Assert-DxNoReparsePath $python "PythonExecutable"
|
|
Assert-DxNoReparsePath $mappingPath "Mapping"
|
|
Assert-DxNoReparsePath $projectsPath "Projects"
|
|
Assert-DxNoReparsePath $certificationPath "Certifications"
|
|
Assert-DxNoReparsePath $job "job directory"
|
|
$createArguments = @(
|
|
"-m", "dx_productivity", "--json", "create", $job,
|
|
"--mapping", $mappingPath,
|
|
"--projects", $projectsPath,
|
|
"--certifications", $certificationPath
|
|
)
|
|
$createOutput = @(& $python @createArguments)
|
|
if ($LASTEXITCODE -ne 0) {
|
|
exit $LASTEXITCODE
|
|
}
|
|
try {
|
|
$created = [string]::Join([Environment]::NewLine, $createOutput) |
|
|
ConvertFrom-Json -ErrorAction Stop
|
|
}
|
|
catch {
|
|
throw "create did not return valid JSON"
|
|
}
|
|
Assert-ExactObject $created @("job_id", "projects", "states") "create result"
|
|
if ($created.projects -isnot [object[]]) {
|
|
throw "create result projects must be an array"
|
|
}
|
|
if ($created.projects.Count -eq 0) {
|
|
throw "create returned no projects"
|
|
}
|
|
if ($created.job_id -isnot [string] -or
|
|
[string]::IsNullOrWhiteSpace($created.job_id)) {
|
|
throw "create result job_id is invalid"
|
|
}
|
|
$jobId = [string]$created.job_id
|
|
$projectRecords = $created.projects
|
|
$projectContext = "create"
|
|
}
|
|
|
|
$entries = @()
|
|
$seenProjectIds = New-Object 'Collections.Generic.HashSet[string]' (
|
|
[StringComparer]::Ordinal
|
|
)
|
|
$seenProjectPaths = New-Object 'Collections.Generic.HashSet[string]' (
|
|
[StringComparer]::OrdinalIgnoreCase
|
|
)
|
|
foreach ($project in $projectRecords) {
|
|
Assert-ExactObject $project @(
|
|
"display_name", "project_id", "project_path"
|
|
) "$projectContext project"
|
|
foreach ($name in @("display_name", "project_id", "project_path")) {
|
|
if ($project.$name -isnot [string] -or
|
|
[string]::IsNullOrWhiteSpace($project.$name)) {
|
|
throw "$projectContext returned an invalid project $name"
|
|
}
|
|
}
|
|
if (-not [IO.Path]::IsPathRooted([string]$project.project_path)) {
|
|
throw "$projectContext returned a relative project path"
|
|
}
|
|
$projectId = [string]$project.project_id
|
|
if (-not $seenProjectIds.Add($projectId)) {
|
|
throw "$projectContext returned duplicate project_id: $projectId"
|
|
}
|
|
if ($projectId.IndexOfAny([IO.Path]::GetInvalidFileNameChars()) -ge 0 -or
|
|
$projectId.Contains('/') -or $projectId.Contains('\')) {
|
|
throw "create returned an invalid project_id: $projectId"
|
|
}
|
|
$canonicalProjectPath = Resolve-FullPath ([string]$project.project_path)
|
|
if (-not $seenProjectPaths.Add($canonicalProjectPath)) {
|
|
throw "$projectContext returned duplicate project path"
|
|
}
|
|
$mailboxDirectory = Join-Path (Join-Path $job "mailbox") $projectId
|
|
$commandPath = Join-Path $mailboxDirectory "command.json"
|
|
Assert-DxNoReparsePath $mailboxDirectory "mailbox directory"
|
|
Assert-DxNoReparsePath $commandPath "mailbox command"
|
|
$entries += [ordered]@{
|
|
project_id = $projectId
|
|
project_path = [string]$project.project_path
|
|
profile = "runs/compatibility-profile.json"
|
|
command = Convert-ToRunRelativePath $toolHome $commandPath "command"
|
|
status = Convert-ToRunRelativePath $toolHome (
|
|
Join-Path $mailboxDirectory "status.json"
|
|
) "status"
|
|
snapshot = Convert-ToRunRelativePath $toolHome (
|
|
Join-Path $mailboxDirectory "snapshot.json"
|
|
) "snapshot"
|
|
}
|
|
}
|
|
|
|
$manifestPath = Join-Path $deployedRun "current-part-replacement.json"
|
|
Assert-DxNoReparsePath $toolHome "DXTOOLS_HOME"
|
|
Assert-DxNoReparsePath $deployedRun "deployed run root"
|
|
Assert-DxNoReparsePath $deployedProfile "deployed compatibility profile"
|
|
Assert-DxNoReparsePath $job "job directory"
|
|
Assert-DxNoReparsePath $manifestPath "manifest destination"
|
|
|
|
while ($true) {
|
|
Assert-DxNoReparsePath $python "PythonExecutable"
|
|
Assert-DxNoReparsePath $job "job directory"
|
|
$stepArguments = @(
|
|
"-m", "dx_productivity", "--json", "operator-step", $job
|
|
)
|
|
$stepOutput = @(& $python @stepArguments)
|
|
$stepExitCode = $LASTEXITCODE
|
|
if ($stepExitCode -notin @(0, 4)) {
|
|
exit $stepExitCode
|
|
}
|
|
try {
|
|
$step = [string]::Join([Environment]::NewLine, $stepOutput) |
|
|
ConvertFrom-Json -ErrorAction Stop
|
|
}
|
|
catch {
|
|
throw "operator-step did not return valid JSON"
|
|
}
|
|
Assert-OperatorStepResult $step $jobId
|
|
|
|
if ($step.complete) {
|
|
if ($stepExitCode -ne 0) {
|
|
throw "completed operator-step returned a nonzero status"
|
|
}
|
|
$manifest = [ordered]@{
|
|
complete = $true
|
|
current_action = $null
|
|
current_command_id = $null
|
|
current_project_id = $null
|
|
format_version = 2
|
|
projects = $entries
|
|
}
|
|
$manifestText = ($manifest | ConvertTo-Json -Depth 7 -Compress) + "`n"
|
|
Write-DxUtf8Atomic $manifestPath $manifestText
|
|
Write-Output "Part replacement job complete: $job"
|
|
break
|
|
}
|
|
if ($stepExitCode -ne 4) {
|
|
throw "pending operator-step must return status 4"
|
|
}
|
|
$matchingEntries = @(
|
|
$entries | Where-Object {
|
|
$_.project_id -ceq [string]$step.project.project_id
|
|
}
|
|
)
|
|
if ($matchingEntries.Count -ne 1 -or
|
|
-not (Test-SamePath $matchingEntries[0].project_path `
|
|
$step.project.project_path)) {
|
|
throw "operator-step project is not an exact created-project match"
|
|
}
|
|
$currentEntry = $matchingEntries[0]
|
|
$currentCommandPath = Join-Path $toolHome (
|
|
[string]$currentEntry.command
|
|
)
|
|
Assert-DxNoReparsePath $currentCommandPath "current mailbox command"
|
|
if (-not (Test-Path -LiteralPath $currentCommandPath -PathType Leaf)) {
|
|
throw "operator-step did not publish its current command"
|
|
}
|
|
$manifest = [ordered]@{
|
|
complete = $false
|
|
current_action = [string]$step.command.action
|
|
current_command_id = [string]$step.command.command_id
|
|
current_project_id = [string]$step.project.project_id
|
|
format_version = 2
|
|
projects = $entries
|
|
}
|
|
$manifestText = ($manifest | ConvertTo-Json -Depth 7 -Compress) + "`n"
|
|
Write-DxUtf8Atomic $manifestPath $manifestText
|
|
|
|
Write-Output ("CurrentProject=" + [string]$step.project.project_path)
|
|
Write-Output ("CurrentAction=" + [string]$step.command.action)
|
|
if ($null -ne $step.manual_route) {
|
|
Write-Output ("ManualRoute=" + [string]$step.manual_route)
|
|
}
|
|
if ($null -ne $step.instruction) {
|
|
Write-Output ("Instruction=" + [string]$step.instruction)
|
|
}
|
|
$operatorPrompt = (
|
|
"Run current part replacement step in DxDesigner, " +
|
|
"then press Enter to continue or Q to quit"
|
|
)
|
|
Write-Output $operatorPrompt
|
|
$response = Read-Host "Enter/Q"
|
|
if ([string]::Equals(
|
|
[string]$response,
|
|
"q",
|
|
[StringComparison]::OrdinalIgnoreCase
|
|
)) {
|
|
Write-Output "Part replacement job paused: $job"
|
|
break
|
|
}
|
|
}
|