forked from Azure/azure-sdk-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCosmos-Emulator.ps1
More file actions
149 lines (131 loc) · 4.05 KB
/
Cosmos-Emulator.ps1
File metadata and controls
149 lines (131 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<#
.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
$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"
}