Skip to content

Commit 5c02d46

Browse files
Add build scripts for Cosmos DB
1 parent fedad68 commit 5c02d46

5 files changed

+131
-0
lines changed

Build.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ CheckLastExitCode
9999
dotnet build -c Release
100100
CheckLastExitCode
101101

102+
.\start-cosmos-db-emulator.ps1
103+
CheckLastExitCode
104+
102105
RunInspectCode
103106
RunCleanupCode
104107

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
result=1
4+
count=0
5+
6+
while [[ "$result" != "0" && "$count" < "5" ]]; do
7+
echo "Trying to download certificate ..."
8+
curl -k https://localhost:8081/_explorer/emulator.pem > ~/emulatorcert.crt 2> /dev/null
9+
result=$?
10+
let "count++"
11+
12+
if [[ "$result" != "0" && "$count" < "5" ]]
13+
then
14+
echo "Could not download certificate. Waiting 10 seconds before trying again ..."
15+
sleep 10
16+
fi
17+
done
18+
19+
if [[ $result -eq 0 ]]
20+
then
21+
echo "Updating CA certificates ..."
22+
cp ~/emulatorcert.crt /usr/local/share/ca-certificates/
23+
update-ca-certificates
24+
else
25+
echo "Could not download CA certificate!"
26+
fi
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Determine the IP address of the local machine.
4+
# This step is required when Direct mode setting is configured using Cosmos DB SDKs.
5+
# ipaddr="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"
6+
7+
# Pull the azure-cosmos-emulator Docker image for Linux from the registry.
8+
docker pull mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
9+
10+
# Run the image, creating a container called "azure-cosmos-emulator-linux".
11+
docker run \
12+
-p 8081:8081 \
13+
-p 10251:10251 \
14+
-p 10252:10252 \
15+
-p 10253:10253 \
16+
-p 10254:10254 \
17+
-m 3g \
18+
--cpus=2.0 \
19+
--name=azure-cosmos-emulator-linux \
20+
-e AZURE_COSMOS_EMULATOR_PARTITION_COUNT=3 \
21+
-e AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true \
22+
mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

shutdown-cosmos-db-emulator.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Requires -Version 7
2+
3+
function ShutdownCosmosDbEmulator {
4+
if ($PSVersionTable.Platform -eq "Unix") {
5+
ShutdownCosmosDbEmulatorDockerContainer
6+
}
7+
else {
8+
ShutdownCosmosDbEmulatorForWindows
9+
}
10+
}
11+
12+
function ShutdownCosmosDbEmulatorDockerContainer {
13+
Write-Host "Shutting down Cosmos DB Emulator Docker container ..."
14+
docker stop azure-cosmos-emulator-linux
15+
}
16+
17+
function ShutdownCosmosDbEmulatorForWindows {
18+
Write-Host "Shutting down Cosmos DB Emulator for Windows ..."
19+
Start-Process -FilePath "C:\Program Files\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe" -ArgumentList "/Shutdown"
20+
}
21+
22+
ShutdownCosmosDbEmulator

start-cosmos-db-emulator.ps1

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#Requires -Version 7
2+
3+
function StartCosmosDbEmulator {
4+
if ($PSVersionTable.Platform -eq "Unix") {
5+
StartCosmosDbEmulatorDockerContainer
6+
}
7+
else {
8+
StartCosmosDbEmulatorForWindows
9+
}
10+
}
11+
12+
function StartCosmosDbEmulatorDockerContainer {
13+
Write-Host "Starting Cosmos DB Emulator Docker container ..."
14+
Start-Process nohup 'bash ./run-docker-azure-cosmos-emulator-linux.sh'
15+
16+
Write-Host "Waiting for Cosmos DB Emulator Docker container to start up ..."
17+
Start-Sleep -Seconds 30
18+
WaitUntilCosmosSqlApiEndpointIsReady
19+
20+
Write-Host "Installing Cosmos DB Emulator certificates ..."
21+
Start-Sleep -Seconds 30
22+
Start-Process -FilePath ".\install-azure-cosmos-emulator-linux-certificates.sh"
23+
}
24+
25+
function StartCosmosDbEmulatorForWindows {
26+
Write-Host "Starting Cosmos DB Emulator for Windows ..."
27+
Start-Process -FilePath "C:\Program Files\Azure Cosmos DB Emulator\Microsoft.Azure.Cosmos.Emulator.exe" -ArgumentList "/DisableRateLimiting /NoUI /NoExplorer"
28+
29+
Write-Host "Waiting for Cosmos DB Emulator for Windows to start up ..."
30+
Start-Sleep -Seconds 10
31+
WaitUntilCosmosSqlApiEndpointIsReady
32+
}
33+
34+
function WaitUntilCosmosSqlApiEndpointIsReady {
35+
# See https://seddryck.wordpress.com/2020/01/05/running-automated-tests-with-the-azure-cosmos-db-emulator-on-appveyor/
36+
$attempt = 0; $max = 5
37+
38+
do {
39+
$client = New-Object System.Net.Sockets.TcpClient([System.Net.Sockets.AddressFamily]::InterNetwork)
40+
41+
try {
42+
$client.Connect("localhost", 8081)
43+
Write-Host "Cosmos SQL API endpoint listening. Connection successful."
44+
}
45+
catch {
46+
$client.Close()
47+
if($attempt -eq $max) {
48+
throw "Cosmos SQL API endpoint is not listening. Aborting connection."
49+
} else {
50+
[int]$sleepTime = 5 * (++$attempt)
51+
Write-Host "Cosmos SQL API endpoint is not yet listening. Retry after $sleepTime seconds..."
52+
Start-Sleep -Seconds $sleepTime;
53+
}
54+
}
55+
} while(!$client.Connected -and $attempt -lt $max)
56+
}
57+
58+
StartCosmosDbEmulator

0 commit comments

Comments
 (0)