Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] CI: Fixes emulator set-up to leverage central SDK teams scripts #4813

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions templates/Cosmos-Emulator.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<#
.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 Emulator
Exact path to Microsoft.Azure.Cosmos.Emulator.exe

.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,
[string] $Emulator,
[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"

if ([string]::IsNullOrEmpty($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

Add-MpPreference -ExclusionPath $targetDir

$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 /EnableSqlComputeEndpoint /SqlComputePort=9999 /disableratelimiting /partitioncount=10 /consistency=Strong"
kirankumarkolli marked this conversation as resolved.
Show resolved Hide resolved
}

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"
}
81 changes: 14 additions & 67 deletions templates/emulator-setup.yml
Original file line number Diff line number Diff line change
@@ -1,69 +1,16 @@
# File: templates/emulator-setup.yml
parameters:
EmulatorInstallPath: "$(Agent.HomeDirectory)/../../Program Files/Azure Cosmos DB Emulator/Microsoft.Azure.Cosmos.Emulator.exe"
EmulatorMsiUrl: "https://aka.ms/cosmosdb-emulator"
StartParameters: ''

steps:
- pwsh: |
Write-Host "Downloading Cosmos Emulator - $env:EMULATORMSIURL" -ForegroundColor green
Invoke-WebRequest "$env:EMULATORMSIURL" -OutFile "$env:temp\azure-cosmosdb-emulator.msi"
Write-Host "Finished Downloading Cosmos Emulator - $env:temp\azure-cosmosdb-emulator.msi" -ForegroundColor green
dir "$env:temp"

function Remove-DirectoryIfExists {
param ([string]$Path)

if (Test-Path -Path $Path -PathType Container) {
Remove-Item -Path $Path -Recurse -Force
Write-Output "Folder deleted: $Path"
} else {
Write-Output "Folder does not exist: $Path"
}
}

$lessMsiDir="$env:temp\lessmsi"
$emulatorDir="$env:temp\Azure Cosmos DB Emulator\"

Remove-DirectoryIfExists -Path $lessMsiDir
Remove-DirectoryIfExists -Path $emulatorDir

Expand-Archive -LiteralPath 'tools\lessmsi-v2.1.1.zip' -DestinationPath $lessMsiDir
&"$env:temp\lessmsi\lessmsi.exe" x "$env:temp\azure-cosmosdb-emulator.msi" "$emulatorDir"

Add-MpPreference -ExclusionPath "$emulatorDir\SourceDir\Azure Cosmos DB Emulator"
Add-MpPreference -ExclusionPath "$env:localappdata\CosmosDBEmulator"
displayName: Downloading and Installing Cosmos DB Emulator
failOnStderr: true
errorActionPreference: stop
- pwsh: |
Write-Host "Starting Cosmos DB Emulator" -ForegroundColor green
Import-Module "$env:temp\Azure Cosmos DB Emulator\SourceDir\Azure Cosmos DB Emulator\PSModules\Microsoft.Azure.CosmosDB.Emulator"
Get-Item env:* | Sort-Object -Property Name

for ($j=0; $j -lt 3; $j++) {
Write-Host "Attempt $j"
Start-Process "$env:temp\Azure Cosmos DB Emulator\SourceDir\Azure Cosmos DB Emulator\CosmosDB.Emulator.exe" "/NoExplorer /NoUI /DisableRateLimiting /PartitionCount=10 /Consistency=Strong /EnablePreview /EnableSqlComputeEndpoint" -Verb RunAs
for ($i=0; $i -lt (3+2*$j); $i++) {
$status = Get-CosmosDbEmulatorStatus
Write-Host "Cosmos DB Emulator Status: $status"
if ($status -ne "Running") {
sleep 30;
}
else {
break;
}
}
if ($status -ne "Running") {
Write-Host "Shutting down and restarting"
Start-Process "$env:temp\Azure Cosmos DB Emulator\SourceDir\Azure Cosmos DB Emulator\CosmosDB.Emulator.exe" "/Shutdown" -Verb RunAs
sleep 30;
}
else {
break;
}
}

if ($status -ne "Running") {
Write-Error "Emulator failed to start"
}

displayName: Waiting for Cosmos DB Emulator status
failOnStderr: true
errorActionPreference: stop
- task: Powershell@2
inputs:
filePath: $(Build.SourcesDirectory)/templates/Cosmos-Emulator.ps1
arguments: >
-EmulatorMsiUrl "${{ parameters.EmulatorMsiUrl }}"
-StartParameters "${{ parameters.StartParameters }}"
-Emulator "${{ parameters.EmulatorInstallPath }}"
-Stage "Launch"
pwsh: true
displayName: Launch Public Cosmos DB Emulator
Binary file removed tools/lessmsi-v2.1.1.zip
Binary file not shown.
Loading