From 451c63ada4cc020dcd8d709ec389f9eeee381b7c Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Thu, 7 Apr 2022 11:33:50 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 3067 (#23861) * Infer live resource group name based on service directory name * Simplify service directory path splitting * Use common logic for username and basename generation * Rename GetServiceName to GetServiceLeafDirectoryName Co-authored-by: Ben Broderick Phillips --- .../TestResources/New-TestResources.ps1 | 33 +++++----------- .../TestResources/Remove-TestResources.ps1 | 39 ++++++++++++------- .../TestResources/Remove-TestResources.ps1.md | 7 +++- .../TestResources/SubConfig-Helpers.ps1 | 19 +++++++++ .../TestResources/Update-TestResources.ps1 | 9 ++--- .../TestResources/remove-test-resources.yml | 8 ++-- 6 files changed, 67 insertions(+), 48 deletions(-) diff --git a/eng/common/TestResources/New-TestResources.ps1 b/eng/common/TestResources/New-TestResources.ps1 index 6abeb291cb0b..b0ab66ea95fa 100644 --- a/eng/common/TestResources/New-TestResources.ps1 +++ b/eng/common/TestResources/New-TestResources.ps1 @@ -368,18 +368,13 @@ try { exit } - $UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" } - # Remove spaces, etc. that may be in $UserName - $UserName = $UserName -replace '\W' + $UserName = GetUserName - # Make sure $BaseName is set. if ($CI) { $BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16) Log "Generated base name '$BaseName' for CI build" } elseif (!$BaseName) { - # Handle service directories in nested directories, e.g. `data/aztables` - $serviceDirectorySafeName = $ServiceDirectory -replace '[/\\]', '' - $BaseName = "$UserName$serviceDirectorySafeName" + $BaseName = GetBaseName $UserName $ServiceDirectory Log "BaseName was not set. Using default base name '$BaseName'" } @@ -517,13 +512,7 @@ try { $ProvisionerApplicationOid = $sp.Id } - # If the ServiceDirectory has multiple segments use the last directory name - # e.g. D:\foo\bar -> bar or foo/bar -> bar - $serviceName = if (Split-Path $ServiceDirectory) { - Split-Path -Leaf $ServiceDirectory - } else { - $ServiceDirectory.Trim('/') - } + $serviceName = GetServiceLeafDirectoryName $ServiceDirectory $ResourceGroupName = if ($ResourceGroupName) { $ResourceGroupName @@ -553,16 +542,12 @@ try { BuildReason = "${env:BUILD_REASON}" } - # Set the resource group name variable. - Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName" - LogVsoCommand "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName" - if ($EnvironmentVariables.ContainsKey('AZURE_RESOURCEGROUP_NAME') -and ` - $EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] -ne $ResourceGroupName) - { - Write-Warning ("Overwriting 'EnvironmentVariables.AZURE_RESOURCEGROUP_NAME' with value " + - "'$($EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'])' " + "to new value '$($ResourceGroupName)'") - } - $EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName + # Set an environment variable marking that resources have been deployed + # This variable can be consumed as a yaml condition in later stages of the pipeline + # to determine whether resources should be removed. + Write-Host "Setting variable 'CI_HAS_DEPLOYED_RESOURCES': 'true'" + LogVsoCommand "##vso[task.setvariable variable=CI_HAS_DEPLOYED_RESOURCES;]true" + $EnvironmentVariables['CI_HAS_DEPLOYED_RESOURCES'] = $true } Log "Creating resource group '$ResourceGroupName' in location '$Location'" diff --git a/eng/common/TestResources/Remove-TestResources.ps1 b/eng/common/TestResources/Remove-TestResources.ps1 index 3e697d789c60..a1bd773772e0 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1 +++ b/eng/common/TestResources/Remove-TestResources.ps1 @@ -16,8 +16,8 @@ param ( [ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')] [string] $BaseName, - [Parameter(ParameterSetName = 'ResourceGroup', Mandatory = $true)] - [Parameter(ParameterSetName = 'ResourceGroup+Provisioner', Mandatory = $true)] + [Parameter(ParameterSetName = 'ResourceGroup')] + [Parameter(ParameterSetName = 'ResourceGroup+Provisioner')] [string] $ResourceGroupName, [Parameter(ParameterSetName = 'Default+Provisioner', Mandatory = $true)] @@ -48,6 +48,10 @@ param ( [ValidateSet('AzureCloud', 'AzureUSGovernment', 'AzureChinaCloud', 'Dogfood')] [string] $Environment = 'AzureCloud', + [Parameter(ParameterSetName = 'ResourceGroup')] + [Parameter(ParameterSetName = 'ResourceGroup+Provisioner')] + [switch] $CI, + [Parameter()] [switch] $Force, @@ -73,6 +77,7 @@ trap { $exitActions.Invoke() } +. $PSScriptRoot/SubConfig-Helpers.ps1 # Source helpers to purge resources. . "$PSScriptRoot\..\scripts\Helpers\Resource-Helpers.ps1" @@ -126,18 +131,23 @@ if ($ProvisionerApplicationId) { $context = Get-AzContext if (!$ResourceGroupName) { - # Make sure $BaseName is set. - if (!$BaseName) { - $UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" } - # Remove spaces, etc. that may be in $UserName - $UserName = $UserName -replace '\W' - - $BaseName = "$UserName$ServiceDirectory" - Log "BaseName was not set. Using default base name '$BaseName'" - } + if ($CI) { + $envVarName = (BuildServiceDirectoryPrefix (GetServiceLeafDirectoryName $ServiceDirectory)) + "RESOURCE_GROUP" + $ResourceGroupName = [Environment]::GetEnvironmentVariable($envVarName) + if (!$ResourceGroupName) { + Write-Error "Could not find resource group name environment variable '$envVarName'" + exit 1 + } + } else { + if (!$BaseName) { + $UserName = GetUserName + $BaseName = GetBaseName $UserName $ServiceDirectory + Log "BaseName was not set. Using default base name '$BaseName'" + } - # Format the resource group name like in New-TestResources.ps1. - $ResourceGroupName = "rg-$BaseName" + # Format the resource group name like in New-TestResources.ps1. + $ResourceGroupName = "rg-$BaseName" + } } # If no subscription was specified, try to select the Azure SDK Developer Playground subscription. @@ -282,6 +292,9 @@ specified - in which to discover pre removal script named 'remove-test-resources Name of the cloud environment. The default is the Azure Public Cloud ('PublicCloud') +.PARAMETER CI +Run script in CI mode. Infers various environment variable names based on CI convention. + .PARAMETER Force Force removal of resource group without asking for user confirmation diff --git a/eng/common/TestResources/Remove-TestResources.ps1.md b/eng/common/TestResources/Remove-TestResources.ps1.md index 4b6159b86044..f6bedadadd84 100644 --- a/eng/common/TestResources/Remove-TestResources.ps1.md +++ b/eng/common/TestResources/Remove-TestResources.ps1.md @@ -31,14 +31,14 @@ Remove-TestResources.ps1 -BaseName -TenantId [-SubscriptionId ``` Remove-TestResources.ps1 -ResourceGroupName -TenantId [-SubscriptionId ] -ProvisionerApplicationId -ProvisionerApplicationSecret [[-ServiceDirectory] ] - [-Environment ] [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] + [-Environment ] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] [] ``` ### ResourceGroup ``` Remove-TestResources.ps1 -ResourceGroupName [-SubscriptionId ] [[-ServiceDirectory] ] - [-Environment ] [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] + [-Environment ] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments ] [-WhatIf] [-Confirm] [] ``` @@ -232,6 +232,9 @@ Accept pipeline input: False Accept wildcard characters: False ``` +### -CI +Run script in CI mode. Infers various environment variable names based on CI convention. + ### -Force Force removal of resource group without asking for user confirmation diff --git a/eng/common/TestResources/SubConfig-Helpers.ps1 b/eng/common/TestResources/SubConfig-Helpers.ps1 index 4e811988371c..070c1639b083 100644 --- a/eng/common/TestResources/SubConfig-Helpers.ps1 +++ b/eng/common/TestResources/SubConfig-Helpers.ps1 @@ -2,6 +2,25 @@ function BuildServiceDirectoryPrefix([string]$serviceName) { return $serviceName.ToUpperInvariant() + "_" } +# If the ServiceDirectory has multiple segments use the last directory name +# e.g. D:\foo\bar -> bar or foo/bar -> bar +function GetServiceLeafDirectoryName([string]$serviceDirectory) { + return Split-Path -Leaf $serviceDirectory +} + +function GetUserName() { + $UserName = $env:USER ?? $env:USERNAME + # Remove spaces, etc. that may be in $UserName + $UserName = $UserName -replace '\W' + return $UserName +} + +function GetBaseName([string]$user, [string]$serviceDirectoryName) { + # Handle service directories in nested directories, e.g. `data/aztables` + $serviceDirectorySafeName = $serviceDirectoryName -replace '[/\\]', '' + return "$user$serviceDirectorySafeName" +} + function ShouldMarkValueAsSecret([string]$serviceName, [string]$key, [string]$value, [array]$allowedValues = @()) { $logOutputNonSecret = @( diff --git a/eng/common/TestResources/Update-TestResources.ps1 b/eng/common/TestResources/Update-TestResources.ps1 index a409596ff4ee..7715ec4fcfbc 100644 --- a/eng/common/TestResources/Update-TestResources.ps1 +++ b/eng/common/TestResources/Update-TestResources.ps1 @@ -30,6 +30,8 @@ param ( [int] $DeleteAfterHours = 48 ) +. $PSScriptRoot/SubConfig-Helpers.ps1 + # By default stop for any error. if (!$PSBoundParameters.ContainsKey('ErrorAction')) { $ErrorActionPreference = 'Stop' @@ -71,11 +73,8 @@ $exitActions = @({ if (!$ResourceGroupName) { # Make sure $BaseName is set. if (!$BaseName) { - $UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" } - # Remove spaces, etc. that may be in $UserName - $UserName = $UserName -replace '\W' - - $BaseName = "$UserName$ServiceDirectory" + $UserName = GetUserName + $BaseName = GetBaseName $UserName $ServiceDirectory Log "BaseName was not set. Using default base name '$BaseName'" } diff --git a/eng/common/TestResources/remove-test-resources.yml b/eng/common/TestResources/remove-test-resources.yml index 0c059f51a032..6c02706d2202 100644 --- a/eng/common/TestResources/remove-test-resources.yml +++ b/eng/common/TestResources/remove-test-resources.yml @@ -1,5 +1,5 @@ # Assumes steps in deploy-test-resources.yml was run previously. Requires -# environment variable: AZURE_RESOURCEGROUP_NAME and Az PowerShell module +# environment variable: _RESOURCE_GROUP and Az PowerShell module parameters: ServiceDirectory: '' @@ -28,11 +28,11 @@ steps: "@ | ConvertFrom-Json -AsHashtable; eng/common/TestResources/Remove-TestResources.ps1 ` - -ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" ` - -ServiceDirectory "${{ parameters.ServiceDirectory }}" ` @subscriptionConfiguration ` + -ServiceDirectory "${{ parameters.ServiceDirectory }}" ` + -CI ` -Force ` -Verbose displayName: Remove test resources - condition: ne(variables['AZURE_RESOURCEGROUP_NAME'], '') + condition: eq(variables['CI_HAS_DEPLOYED_RESOURCES'], 'true') continueOnError: true