Skip to content

Commit 223eb5d

Browse files
Sync eng/common directory with azure-sdk-tools for PR 2830 (Azure#27381)
* 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 <[email protected]>
1 parent 61d7bcf commit 223eb5d

File tree

2 files changed

+172
-90
lines changed

2 files changed

+172
-90
lines changed

eng/common/pipelines/templates/steps/cosmos-emulator.yml

Lines changed: 31 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,34 @@ parameters:
33
StartParameters: ''
44

55
steps:
6-
- powershell: |
7-
$targetDir = $env:temp
8-
Write-Host "Downloading and extracting Cosmos DB Emulator - ${{ parameters.EmulatorMsiUrl }}"
9-
Write-Host "Target Dir: $targetDir"
10-
msiexec /a ${{ parameters.EmulatorMsiUrl }} TARGETDIR=$targetDir /qn | wait-process
11-
displayName: Download and Extract Public Cosmos DB Emulator
12-
- powershell: |
13-
Write-Host "Deleting Cosmos DB Emulator data"
14-
if (Test-Path $Env:LOCALAPPDATA\CosmosDbEmulator) { Remove-Item -Recurse -Force $Env:LOCALAPPDATA\CosmosDbEmulator }
15-
displayName: Delete Cosmos DB Emulator data
16-
- powershell: |
17-
Write-Host "Getting Cosmos DB Emulator Version"
18-
$ProductName = "Azure Cosmos DB Emulator"
19-
$Emulator = (Join-Path $env:temp (Join-Path $ProductName "Microsoft.Azure.Cosmos.Emulator.exe"))
20-
$fileVersion = Get-ChildItem $Emulator
21-
Write-Host $Emulator $fileVersion.VersionInfo
22-
displayName: Get Cosmos DB Emulator Version
23-
- powershell: |
24-
Write-Host "Launching Cosmos DB Emulator"
25-
$ProductName = "Azure Cosmos DB Emulator"
26-
$Emulator = (Join-Path $env:temp (Join-Path $ProductName "Microsoft.Azure.Cosmos.Emulator.exe"))
27-
if (!(Test-Path $Emulator)) {
28-
Write-Error "The emulator is not installed where expected at '$Emulator'"
29-
return
30-
}
31-
$process = Start-Process $Emulator -ArgumentList "/getstatus" -PassThru -Wait
32-
switch ($process.ExitCode) {
33-
1 {
34-
Write-Host "The emulator is already starting"
35-
return
36-
}
37-
2 {
38-
Write-Host "The emulator is already running"
39-
return
40-
}
41-
3 {
42-
Write-Host "The emulator is stopped"
43-
}
44-
default {
45-
Write-Host "Unrecognized exit code $process.ExitCode"
46-
return
47-
}
48-
}
49-
$argumentList = ""
50-
if (-not [string]::IsNullOrEmpty("${{ parameters.StartParameters }}")) {
51-
$argumentList += , "${{ parameters.StartParameters }}"
52-
} else {
53-
# Use the default params if none provided
54-
$argumentList = "/noexplorer /noui /enablepreview /disableratelimiting /enableaadauthentication"
55-
}
56-
Write-Host "Starting emulator process: $Emulator $argumentList"
57-
$process=Start-Process $Emulator -ArgumentList $argumentList -ErrorAction Stop -PassThru
58-
Write-Host "Emulator process started: $($process.Name), $($process.FileVersion)"
59-
$Timeout = 600
60-
$result="NotYetStarted"
61-
$complete = if ($Timeout -gt 0) {
62-
$start = [DateTimeOffset]::Now
63-
$stop = $start.AddSeconds($Timeout)
64-
{
65-
$result -eq "Running" -or [DateTimeOffset]::Now -ge $stop
66-
}
67-
}
68-
else {
69-
{
70-
$result -eq "Running"
71-
}
72-
}
73-
do {
74-
$process = Start-Process $Emulator -ArgumentList "/getstatus" -PassThru -Wait
75-
switch ($process.ExitCode) {
76-
1 {
77-
Write-Host "The emulator is starting"
78-
}
79-
2 {
80-
Write-Host "The emulator is running"
81-
$result="Running"
82-
return
83-
}
84-
3 {
85-
Write-Host "The emulator is stopped"
86-
}
87-
default {
88-
Write-Host "Unrecognized exit code $process.ExitCode"
89-
}
90-
}
91-
Start-Sleep -Seconds 5
92-
}
93-
until ($complete.Invoke())
94-
Write-Error "The emulator failed to reach Running status within ${Timeout} seconds"
95-
displayName: Start Cosmos DB Emulator
6+
- task: Powershell@2
7+
inputs:
8+
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Cosmos-Emulator.ps1
9+
arguments: >
10+
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
11+
-StartParameters "${{ parameters.StartParameters }}"
12+
-Stage "Install"
13+
pwsh: true
14+
displayName: Install Public Cosmos DB Emulator
15+
16+
- task: Powershell@2
17+
inputs:
18+
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Cosmos-Emulator.ps1
19+
arguments: >
20+
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
21+
-StartParameters "${{ parameters.StartParameters }}"
22+
-Stage "Launch"
23+
pwsh: true
24+
displayName: Launch Public Cosmos DB Emulator
25+
continueOnError: true
26+
27+
- task: Powershell@2
28+
inputs:
29+
filePath: $(Build.SourcesDirectory)/eng/common/scripts/Cosmos-Emulator.ps1
30+
arguments: >
31+
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
32+
-StartParameters "${{ parameters.StartParameters }}"
33+
-Stage "Launch"
34+
pwsh: true
35+
displayName: Retry Launch of Public Cosmos DB Emulator
36+
condition: failed()
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<#
2+
.SYNOPSIS
3+
Script for installing and launching cosmos emulator
4+
5+
.DESCRIPTION
6+
This script downloads, installs and launches cosmosdb-emulator.
7+
8+
.PARAMETER EmulatorMsiUrl
9+
Uri for downloading the cosmosdb-emulator
10+
11+
.PARAMETER StartParameters
12+
Parameter with which to launch the cosmosdb-emulator
13+
14+
.PARAMETER Stage
15+
Determines what part of the script to run. Has to be either Install or Launch
16+
#>
17+
[CmdletBinding()]
18+
Param (
19+
[string] $EmulatorMsiUrl = "https://aka.ms/cosmosdb-emulator",
20+
[string] $StartParameters,
21+
[Parameter(Mandatory=$True)]
22+
[ValidateSet('Install', 'Launch')]
23+
[string] $Stage
24+
)
25+
26+
$targetDir = Join-Path $Env:Temp AzureCosmosEmulator
27+
$logFile = Join-Path $Env:Temp log.txt
28+
$productName = "Azure Cosmos DB Emulator"
29+
$emulator = (Join-Path $targetDir (Join-Path $productName "Microsoft.Azure.Cosmos.Emulator.exe"))
30+
31+
if ($Stage -eq "Install")
32+
{
33+
$downloadTryCount = 0
34+
New-Item $targetDir -Type Directory
35+
New-Item $logFile -Type File
36+
do
37+
{
38+
# Download and Extract Public Cosmos DB Emulator
39+
Write-Host "Downloading and extracting Cosmos DB Emulator - $EmulatorMsiUrl"
40+
Write-Host "Target Directory $targetDir"
41+
Write-Host "Log File $logFile"
42+
43+
$downloadTryCount++
44+
Write-Host "Download Try Count: $downloadTryCount"
45+
Remove-Item -Path (Join-Path $targetDir '*') -Recurse
46+
Clear-Content -Path $logFile
47+
48+
$installProcess = Start-Process msiexec -Wait -PassThru -ArgumentList "/a $EmulatorMsiUrl TARGETDIR=$targetDir /qn /liew $logFile"
49+
Get-Content $logFile
50+
Write-Host "Exit Code: $($installProcess.ExitCode)"
51+
}
52+
while(($installProcess.ExitCode -ne 0) -and ($downloadTryCount -lt 3))
53+
54+
if(Test-Path (Join-Path $Env:LOCALAPPDATA CosmosDbEmulator))
55+
{
56+
Write-Host "Deleting Cosmos DB Emulator data"
57+
Remove-Item -Recurse -Force $Env:LOCALAPPDATA\CosmosDbEmulator
58+
}
59+
60+
Write-Host "Getting Cosmos DB Emulator Version"
61+
$fileVersion = Get-ChildItem $emulator
62+
Write-Host $emulator $fileVersion.VersionInfo
63+
}
64+
65+
if ($Stage -eq "Launch")
66+
{
67+
Write-Host "Launching Cosmos DB Emulator"
68+
if (!(Test-Path $emulator)) {
69+
Write-Error "The emulator is not installed where expected at '$emulator'"
70+
return
71+
}
72+
73+
$process = Start-Process $emulator -ArgumentList "/getstatus" -PassThru -Wait
74+
switch ($process.ExitCode) {
75+
1 {
76+
Write-Host "The emulator is already starting"
77+
return
78+
}
79+
2 {
80+
Write-Host "The emulator is already running"
81+
return
82+
}
83+
3 {
84+
Write-Host "The emulator is stopped"
85+
}
86+
default {
87+
Write-Host "Unrecognized exit code $($process.ExitCode)"
88+
return
89+
}
90+
}
91+
92+
$argumentList = ""
93+
if (-not [string]::IsNullOrEmpty($StartParameters)) {
94+
$argumentList += , $StartParameters
95+
} else {
96+
# Use the default params if none provided
97+
$argumentList = "/noexplorer /noui /enablepreview /disableratelimiting /enableaadauthentication"
98+
}
99+
100+
Write-Host "Starting emulator process: $emulator $argumentList"
101+
$process = Start-Process $emulator -ArgumentList $argumentList -ErrorAction Stop -PassThru
102+
Write-Host "Emulator process started: $($process.Name), $($process.FileVersion)"
103+
104+
$Timeout = 600
105+
$result="NotYetStarted"
106+
$complete = if ($Timeout -gt 0) {
107+
$start = [DateTimeOffset]::Now
108+
$stop = $start.AddSeconds($Timeout)
109+
{
110+
$result -eq "Running" -or [DateTimeOffset]::Now -ge $stop
111+
}
112+
}
113+
else {
114+
{
115+
$result -eq "Running"
116+
}
117+
}
118+
119+
do {
120+
$process = Start-Process $emulator -ArgumentList "/getstatus" -PassThru -Wait
121+
switch ($process.ExitCode) {
122+
1 {
123+
Write-Host "The emulator is starting"
124+
}
125+
2 {
126+
Write-Host "The emulator is running"
127+
$result="Running"
128+
return
129+
}
130+
3 {
131+
Write-Host "The emulator is stopped"
132+
}
133+
default {
134+
Write-Host "Unrecognized exit code $($process.ExitCode)"
135+
}
136+
}
137+
Start-Sleep -Seconds 5
138+
}
139+
until ($complete.Invoke())
140+
Write-Error "The emulator failed to reach Running status within ${Timeout} seconds"
141+
}

0 commit comments

Comments
 (0)