Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 2830 (#27381)
Browse files Browse the repository at this point in the history
* Improve Cosmos Emulator logic

* Run cosmos db emulator in two steps

* Using povershell < core for emulator

* add retry logic to cosmosdb-emulator launch

* Add Retry step fir cosmos emulator launch

Co-authored-by: Chidozie Ononiwu <chononiw@microsoft.com>
  • Loading branch information
azure-sdk and chidozieononiwu authored Mar 3, 2022
1 parent 61d7bcf commit 223eb5d
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 90 deletions.
121 changes: 31 additions & 90 deletions eng/common/pipelines/templates/steps/cosmos-emulator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,93 +3,34 @@ parameters:
StartParameters: ''

steps:
- powershell: |
$targetDir = $env:temp
Write-Host "Downloading and extracting Cosmos DB Emulator - ${{ parameters.EmulatorMsiUrl }}"
Write-Host "Target Dir: $targetDir"
msiexec /a ${{ parameters.EmulatorMsiUrl }} TARGETDIR=$targetDir /qn | wait-process
displayName: Download and Extract Public Cosmos DB Emulator
- powershell: |
Write-Host "Deleting Cosmos DB Emulator data"
if (Test-Path $Env:LOCALAPPDATA\CosmosDbEmulator) { Remove-Item -Recurse -Force $Env:LOCALAPPDATA\CosmosDbEmulator }
displayName: Delete Cosmos DB Emulator data
- powershell: |
Write-Host "Getting Cosmos DB Emulator Version"
$ProductName = "Azure Cosmos DB Emulator"
$Emulator = (Join-Path $env:temp (Join-Path $ProductName "Microsoft.Azure.Cosmos.Emulator.exe"))
$fileVersion = Get-ChildItem $Emulator
Write-Host $Emulator $fileVersion.VersionInfo
displayName: Get Cosmos DB Emulator Version
- powershell: |
Write-Host "Launching Cosmos DB Emulator"
$ProductName = "Azure Cosmos DB Emulator"
$Emulator = (Join-Path $env:temp (Join-Path $ProductName "Microsoft.Azure.Cosmos.Emulator.exe"))
if (!(Test-Path $Emulator)) {
Write-Error "The emulator is not installed where expected at '$Emulator'"
return
}
$process = Start-Process $Emulator -ArgumentList "/getstatus" -PassThru -Wait
switch ($process.ExitCode) {
1 {
Write-Host "The emulator is already starting"
return
}
2 {
Write-Host "The emulator is already running"
return
}
3 {
Write-Host "The emulator is stopped"
}
default {
Write-Host "Unrecognized exit code $process.ExitCode"
return
}
}
$argumentList = ""
if (-not [string]::IsNullOrEmpty("${{ parameters.StartParameters }}")) {
$argumentList += , "${{ parameters.StartParameters }}"
} else {
# Use the default params if none provided
$argumentList = "/noexplorer /noui /enablepreview /disableratelimiting /enableaadauthentication"
}
Write-Host "Starting emulator process: $Emulator $argumentList"
$process=Start-Process $Emulator -ArgumentList $argumentList -ErrorAction Stop -PassThru
Write-Host "Emulator process started: $($process.Name), $($process.FileVersion)"
$Timeout = 600
$result="NotYetStarted"
$complete = if ($Timeout -gt 0) {
$start = [DateTimeOffset]::Now
$stop = $start.AddSeconds($Timeout)
{
$result -eq "Running" -or [DateTimeOffset]::Now -ge $stop
}
}
else {
{
$result -eq "Running"
}
}
do {
$process = Start-Process $Emulator -ArgumentList "/getstatus" -PassThru -Wait
switch ($process.ExitCode) {
1 {
Write-Host "The emulator is starting"
}
2 {
Write-Host "The emulator is running"
$result="Running"
return
}
3 {
Write-Host "The emulator is stopped"
}
default {
Write-Host "Unrecognized exit code $process.ExitCode"
}
}
Start-Sleep -Seconds 5
}
until ($complete.Invoke())
Write-Error "The emulator failed to reach Running status within ${Timeout} seconds"
displayName: Start Cosmos DB Emulator
- task: Powershell@2
inputs:
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Cosmos-Emulator.ps1
arguments: >
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
-StartParameters "${{ parameters.StartParameters }}"
-Stage "Install"
pwsh: true
displayName: Install Public Cosmos DB Emulator

- task: Powershell@2
inputs:
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Cosmos-Emulator.ps1
arguments: >
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
-StartParameters "${{ parameters.StartParameters }}"
-Stage "Launch"
pwsh: true
displayName: Launch Public Cosmos DB Emulator
continueOnError: true

- task: Powershell@2
inputs:
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Cosmos-Emulator.ps1
arguments: >
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
-StartParameters "${{ parameters.StartParameters }}"
-Stage "Launch"
pwsh: true
displayName: Retry Launch of Public Cosmos DB Emulator
condition: failed()
141 changes: 141 additions & 0 deletions eng/common/scripts/Cosmos-Emulator.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<#
.SYNOPSIS
Script for installing and launching cosmos emulator
.DESCRIPTION
This script downloads, installs and launches cosmosdb-emulator.
.PARAMETER EmulatorMsiUrl
Uri for downloading the cosmosdb-emulator
.PARAMETER StartParameters
Parameter with which to launch the cosmosdb-emulator
.PARAMETER Stage
Determines what part of the script to run. Has to be either Install or Launch
#>
[CmdletBinding()]
Param (
[string] $EmulatorMsiUrl = "https://aka.ms/cosmosdb-emulator",
[string] $StartParameters,
[Parameter(Mandatory=$True)]
[ValidateSet('Install', 'Launch')]
[string] $Stage
)

$targetDir = Join-Path $Env:Temp AzureCosmosEmulator
$logFile = Join-Path $Env:Temp log.txt
$productName = "Azure Cosmos DB Emulator"
$emulator = (Join-Path $targetDir (Join-Path $productName "Microsoft.Azure.Cosmos.Emulator.exe"))

if ($Stage -eq "Install")
{
$downloadTryCount = 0
New-Item $targetDir -Type Directory
New-Item $logFile -Type File
do
{
# Download and Extract Public Cosmos DB Emulator
Write-Host "Downloading and extracting Cosmos DB Emulator - $EmulatorMsiUrl"
Write-Host "Target Directory $targetDir"
Write-Host "Log File $logFile"

$downloadTryCount++
Write-Host "Download Try Count: $downloadTryCount"
Remove-Item -Path (Join-Path $targetDir '*') -Recurse
Clear-Content -Path $logFile

$installProcess = Start-Process msiexec -Wait -PassThru -ArgumentList "/a $EmulatorMsiUrl TARGETDIR=$targetDir /qn /liew $logFile"
Get-Content $logFile
Write-Host "Exit Code: $($installProcess.ExitCode)"
}
while(($installProcess.ExitCode -ne 0) -and ($downloadTryCount -lt 3))

if(Test-Path (Join-Path $Env:LOCALAPPDATA CosmosDbEmulator))
{
Write-Host "Deleting Cosmos DB Emulator data"
Remove-Item -Recurse -Force $Env:LOCALAPPDATA\CosmosDbEmulator
}

Write-Host "Getting Cosmos DB Emulator Version"
$fileVersion = Get-ChildItem $emulator
Write-Host $emulator $fileVersion.VersionInfo
}

if ($Stage -eq "Launch")
{
Write-Host "Launching Cosmos DB Emulator"
if (!(Test-Path $emulator)) {
Write-Error "The emulator is not installed where expected at '$emulator'"
return
}

$process = Start-Process $emulator -ArgumentList "/getstatus" -PassThru -Wait
switch ($process.ExitCode) {
1 {
Write-Host "The emulator is already starting"
return
}
2 {
Write-Host "The emulator is already running"
return
}
3 {
Write-Host "The emulator is stopped"
}
default {
Write-Host "Unrecognized exit code $($process.ExitCode)"
return
}
}

$argumentList = ""
if (-not [string]::IsNullOrEmpty($StartParameters)) {
$argumentList += , $StartParameters
} else {
# Use the default params if none provided
$argumentList = "/noexplorer /noui /enablepreview /disableratelimiting /enableaadauthentication"
}

Write-Host "Starting emulator process: $emulator $argumentList"
$process = Start-Process $emulator -ArgumentList $argumentList -ErrorAction Stop -PassThru
Write-Host "Emulator process started: $($process.Name), $($process.FileVersion)"

$Timeout = 600
$result="NotYetStarted"
$complete = if ($Timeout -gt 0) {
$start = [DateTimeOffset]::Now
$stop = $start.AddSeconds($Timeout)
{
$result -eq "Running" -or [DateTimeOffset]::Now -ge $stop
}
}
else {
{
$result -eq "Running"
}
}

do {
$process = Start-Process $emulator -ArgumentList "/getstatus" -PassThru -Wait
switch ($process.ExitCode) {
1 {
Write-Host "The emulator is starting"
}
2 {
Write-Host "The emulator is running"
$result="Running"
return
}
3 {
Write-Host "The emulator is stopped"
}
default {
Write-Host "Unrecognized exit code $($process.ExitCode)"
}
}
Start-Sleep -Seconds 5
}
until ($complete.Invoke())
Write-Error "The emulator failed to reach Running status within ${Timeout} seconds"
}

0 comments on commit 223eb5d

Please sign in to comment.