Sync eng/common directory with azure-sdk-tools for PR 3067 (#3529)
* 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 <bebroder@microsoft.com>
This commit is contained in:
parent
13330aacae
commit
f0e0070223
@ -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'"
|
||||
|
||||
@ -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'
|
||||
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'"
|
||||
}
|
||||
|
||||
$BaseName = "$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
|
||||
|
||||
|
||||
@ -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>]
|
||||
```
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
@ -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 = @(
|
||||
|
||||
@ -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'"
|
||||
}
|
||||
|
||||
|
||||
@ -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: ''
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user