Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 3067 (Azure#23861)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
azure-sdk and benbp authored Apr 7, 2022
1 parent 685d06f commit 451c63a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
33 changes: 9 additions & 24 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'"
Expand Down
39 changes: 26 additions & 13 deletions eng/common/TestResources/Remove-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,

Expand All @@ -73,6 +77,7 @@ trap {
$exitActions.Invoke()
}

. $PSScriptRoot/SubConfig-Helpers.ps1
# Source helpers to purge resources.
. "$PSScriptRoot\..\scripts\Helpers\Resource-Helpers.ps1"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions eng/common/TestResources/Remove-TestResources.ps1.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ Remove-TestResources.ps1 -BaseName <String> -TenantId <String> [-SubscriptionId
```
Remove-TestResources.ps1 -ResourceGroupName <String> -TenantId <String> [-SubscriptionId <String>]
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [[-ServiceDirectory] <String>]
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
[-Environment <String>] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

### ResourceGroup
```
Remove-TestResources.ps1 -ResourceGroupName <String> [-SubscriptionId <String>] [[-ServiceDirectory] <String>]
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
[-Environment <String>] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
[<CommonParameters>]
```

Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions eng/common/TestResources/SubConfig-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @(
Expand Down
9 changes: 4 additions & 5 deletions eng/common/TestResources/Update-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ param (
[int] $DeleteAfterHours = 48
)

. $PSScriptRoot/SubConfig-Helpers.ps1

# By default stop for any error.
if (!$PSBoundParameters.ContainsKey('ErrorAction')) {
$ErrorActionPreference = 'Stop'
Expand Down Expand Up @@ -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'"
}

Expand Down
8 changes: 4 additions & 4 deletions eng/common/TestResources/remove-test-resources.yml
Original file line number Diff line number Diff line change
@@ -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: <ServiceDirectory>_RESOURCE_GROUP and Az PowerShell module

parameters:
ServiceDirectory: ''
Expand Down Expand Up @@ -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

0 comments on commit 451c63a

Please sign in to comment.