public/orchestrator/Apply-PSTSFromYaml.ps1
function Apply-PSTSFromYaml { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string] $inFile ) process { $anchorsRef = @{} $yamlObj = _import -inFile $inFile foreach ($item in $yamlobj.spec) { $params = @{} $command = "$($item.act)$($item.kind)" $sb = (Get-Command $command -CommandType Function).ScriptBlock $out = Invoke-Command $sb -ArgumentList $item if ($item.out) { if ($anchorsRef.ContainsKey($item.out)) { $anchorsRef[$item.out] = $out } else { $anchorsRef.Add($item.out, $out) |Out-Null } } } } } function addgitTogitcopy { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) start-sleep -Seconds 5 $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } #add check and no execute if present Get-PSTSGitRepositoryRefItemBatch -project $project -repository $params["repoName"] -folderPath "pipeline-templates" -branch $params["branch"] Add-PSTSGitRepositoryPush -project $project @params } Function addSecureFile { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } #Add use in pipeline $resp = $(Get-PSTSSecureFile -project $yamlobj.project | ` Where-Object { $_.name -eq $params["name"] }) if ($null -eq $resp) { $resp = Add-PSTSSecureFile ` -project $yamlobj.project @params } return $resp } Function AddreleasedefinitiontaskTemplate { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $resp = Get-PSTSReleaseDefinitionTaskTemplate @params $releaseDefinition = _getValue ` -value $item.target.releaseDefinition ` -ref $anchorsRef Add-PSTSReleaseDefinitionTask ` -task $resp ` -position $item.target.position ` -releaseDefinition $releaseDefinition ` -envId 0 return $resp } Function AddvariableGroup { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $resp = Get-PSTSVariablesGroup -project $yamlobj.project | ` Where-Object { $_.name -eq $params["name"] } if ($null -eq $resp) { $resp = Add-PSTSVariableGroup ` -project $yamlobj.project @params } else { if ($null -ne $item.in.variable) { $vars = [System.Collections.Specialized.OrderedDictionary]::new() foreach ($variable in $item.in.variables) { if ($null -eq $resp.variables.($variable.Key)) { $vars.Add($variable.Key, $variable.Value) |Out-Null } } Update-PSTSVariableGroup -project $yamlobj.project -variableGroup $resp -variables $vars } else { $vars = @() foreach ($vaultkey in $item.in.vaultkeys) { if ($null -eq $resp.variables.($vaultkey)) { $vars+=$vaultkey } } Update-PSTSVariableGroup -project $yamlobj.project -variableGroup $resp -vaultKeys $vars } } Update-PSTSAuthorizedResource -project $yamlobj.project ` -authorized $item.authorize ` -id $resp.id ` -name $resp.name ` -type "variablegroup" if ($null -ne $item.link) { $rd = _getValue -value $item.link -ref $anchorsRef if ($null -ne $rd.process) { Link-PSTSVariableGroup ` -variableGroup $resp ` -definition $rd } else { Link-PSTSVariableGroup ` -variableGroup $resp.id ` -definition $rd } } return $resp } Function Getreleasedefinition { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $resp = Get-PSTSReleaseDefinition @params return $resp } Function Getbuilddefinition { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $resp = Get-PSTSBuildDefinition @params return $resp } Function Getfeedbackpackage { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $feed = Get-PSTSFeed | Where-Object { $_.Name -eq $item.in.feedName } $feedbackPackage = Get-PSTSFeedPackage -feedId $feed.id $feedback = New-Object -TypeName PSCustomObject $feedback | Add-Member -MemberType NoteProperty -Name "FeedId" -Value $feed.id $feedback | Add-Member -MemberType NoteProperty -Name "PackageId" -Value $feedbackPackage.id $feedback | Add-Member -MemberType NoteProperty -Name "PackageVersion" -Value ($feedbackPackage.Versions | Select -First 1).version return $feedback } Function AddbuilddefinitionVariable { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $resp = Add-PSTSReleaseDefinitionVariable @params return $resp } #spec webapp Function UpdateReleaseDefinitionDeploymentInput { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $rd = _getValue -value $item.in.releaseDefinition -ref $anchorsRef $resp = Update-PSTSReleaseDefinitionDeployInput ` -agentName $item.in.agentName ` -artefactAliasName $item.in.artefactAliasName ` -releaseDefinition $rd return $resp } Function AddReleaseDefinitionProcessParametersInput { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $rd = _getValue -value $item.in.releaseDefinition -ref $anchorsRef if ($item.in.type -eq "connectedService:AzureRM") { $serviceConnection = Get-PSTSServiceEndPoint -project $yamlobj.project | Where-Object { $_.name -eq $item.in.serviceConnectionName} $resp = Add-PSTSReleaseDefinitionProcessParameterInput -type $item.in.type -serviceConnectionId $serviceConnection.id -releaseDefinition $rd } else { $resp = Add-PSTSReleaseDefinitionProcessParameterInput -type $item.in.type -webappName $item.in.webappName -releaseDefinition $rd } return $resp } ## Function AddreleasedefinitionVariable { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $resp = Add-PSTSReleaseDefinitionVariable @params return $resp } Function Addreleasedefinition { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $rd = _getValue -value $item.in.releaseDefinition -ref $anchorsRef $releasedExists = $(Get-PSTSReleaseDefinition -project $yamlobj.project | Where-Object { $_.name -eq $item.in.name }).Count -ne 0 if ($releasedExists -eq $true) { Write-Warning "Release definition $($item.in.name) exists, skipping" return $rd } $resp = Add-PSTSReleaseDefinitionProperty ` -releaseDefinition $rd ` -path "$($item.in.path)" ` -name $item.in.name #reset the agent pool TODO : add to deploymentinput cmdlet $agentQueue = (Get-PSTSAgentQueue -project $yamlobj.project) |Where-Object {$_.name -eq $item.in.agentQueue } # $resp.environments.DeployPhases.deploymentInput.queueId = $agentQueue.id $nbEnvironment = $resp.environments.Count for ($i=0; $i -lt $nbEnvironment; $i++){ $resp.environments[$i].DeployPhases.deploymentInput.queueId = $agentQueue.id #$environment.DeployPhases.deploymentInput.queueId = $agentQueue.id } Add-PSTSReleaseDefinitionArtefact -project $yamlobj.project -buildName $params["buildName"] -aliasName $params["artefactAliasName"] -definition $resp Add-PSTSReleaseDefinitionTrigger -releaseDefinition $resp -environment $params["path"] -artifactAlias $params["artefactAliasName"] -tag $params["triggerTag"] $stagingPath = "$([Environment]::GetFolderPath('ApplicationData'))/REST-Devops/staging" if ((Test-Path -Path $stagingPath ) -eq $false) { mkdir $stagingPath } $resp = Add-PSTSReleaseDefinition -project $yamlobj.project -releaseDefinition $resp #-inFile "$stagingPath/$($item.in.name).json" return $resp } Function Addbuilddefinition { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $rd = _getValue -value $item.in.buildDefinition -ref $anchorsRef $definitions = Get-PSTSBuildDefinition -project $yamlobj.project $buildExists = $( $definitions | Where-Object { $_.name -eq $item.in.name }).Count -ne 0 if ($buildExists -eq $true) { Write-Warning "Build definition $($item.in.name) exists, skipping" return $( $definitions | Where-Object { $_.name -eq $item.in.name }) } $resp = Add-PSTSReleaseDefinitionProperty ` -releaseDefinition $rd ` -path "$($item.in.path)" ` -name $item.in.name $stagingPath = "$([Environment]::GetFolderPath('ApplicationData'))/REST-Devops/staging" if ((Test-Path -Path $stagingPath ) -eq $false) { mkdir $stagingPath } if ($null -ne $params["buildTriggerName"]) { $resp = Add-PSTSBuildDefinitionTrigger ` -buildDefinition $resp ` -environment $params["branch"] ` -targetBuildName $params["buildTriggerName"] ` -targetBuildBranch $params["buildTriggerBranch"] ` -targetBuildproject $params["buildTriggerProject"] } else { $resp = Add-PSTSBuildDefinitionTrigger ` -buildDefinition $resp ` -environment $params["branch"] } $project = $(Get-PSTSProject | Where-Object {$_.name -eq $yamlobj.project }) $resp.project = $project $resp.project.description="" $repository = Get-PSTSGitRepository -project $yamlobj.project | Where-Object { $_.name -eq $params["repository"]} $repository | Add-Member -MemberType NoteProperty -Name type -Value "TfsGit" $branch = $params["branch"] $repository.defaultBranch = "refs/heads/$branch" $resp.repository=$repository $agentQueue = (Get-PSTSAgentQueue -project $yamlobj.project) |Where-Object {$_.name -eq $item.in.agentQueue } $resp.queue = $agentQueue $resp = Add-PSTSBuildDefinition -project $yamlobj.project -buildDefinition $resp #-inFile "$stagingPath/$($item.in.name).json" return $resp } Function AddGitRepository { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $test = Get-PSTSGitRepository -project $yamlobj.project | where-object {$_.name -eq $params.repoName} if ( $test) { return $test } else{ $resp = Add-PSTSGitRepository -project $yamlobj.project @params return $resp } } Function AddGitRepositoryRef { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $id = Get-PSTSGitRepository -project $yamlobj.project | where-object {$_.name -eq $params.repoName} | Select-Object -ExpandProperty id $test = Get-PSTSGitRepositoryRef -project $yamlobj.project -id $id | Where-Object {$_.name -eq "refs/heads/$($params.refName.ToString())"} if ($test) { return $test } else{ $resp = Add-PSTSGitRepositoryRef -project $yamlobj.project @params return $resp } } Function AddGitRepositoryImportRequest { [CmdletBinding()] param( [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][object] $item ) $item.in.Keys | ForEach-Object { $params[$_] = _getValue ` -value $item.in[$_] ` -ref $anchorsRef } $credPath = "$([Environment]::GetFolderPath('ApplicationData'))/REST-Devops" $credFile= "$($credPath)/$([Environment]::GetEnvironmentVariable('ACCOUNT'))" $password =(get-content $credFile) | ConvertTo-SecureString $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($password) $result = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr) [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr) $id = Get-PSTSGitRepository -project $yamlobj.project | where-object {$_.name -eq $params.repoName} | Select-Object -ExpandProperty id $testSe = Get-PSTSServiceEndPoint -project $yamlobj.project | Where-Object { $_.name -eq $params.serviceConnectionName} if ($testSe) { Write-Warning "$($params.serviceConnectionName) already exists" } else { $resp = Add-PSTSServiceEndPointGit -project $yamlobj.project -name $params.serviceConnectionName -username $params.serviceConnectionName -password "$result" @params } $ServId=Get-PSTSServiceEndPoint -project $yamlobj.project | Where-Object { $_.name -eq $params.serviceConnectionName} | Select-Object -ExpandProperty id $test2 = Get-PSTSGitRepositoryRef -project $yamlobj.project -id $id| Where-Object {$_.name -eq "refs/heads/master"} if ( $test2 ) { write-warning "We can only import on empty repo" $resp1 = $test2 } else { $params.Remove("serviceConnectionName") $resp1 = Add-PSTSGitRepositoryImportRequest -project $yamlobj.project -serviceEndPointId $ServId @params } Start-Sleep -Seconds 3 $test = Get-PSTSGitRepositoryRef -project $yamlobj.project -id $id | where-object {$_.name -eq "refs/heads/ops-develop"} if ($test) { Remove-PSTSGitRepositoryRef -project $yamlobj.project -branch "ops-develop" -repoName $params.repoName } return $resp1 } |