Skip to content

Commit fa30f57

Browse files
Tt 265 cosmosdb emulator (#300)
* feat: TT-265 Support for docker-compose and cosmosdb emulator Co-authored-by: lenshinoda <[email protected]>
1 parent 4cc598d commit fa30f57

File tree

9 files changed

+212
-1
lines changed

9 files changed

+212
-1
lines changed

.env.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ export MS_CLIENT_ID=
2020
export MS_SCOPE=
2121
export MS_SECRET=
2222
export MS_ENDPOINT=
23-
export AZURE_APP_CONFIGURATION_CONNECTION_STRING=
23+
export AZURE_APP_CONFIGURATION_CONNECTION_STRING=
24+
25+
## If you are running on Docker compose:
26+
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ migration_status.csv
3939

4040
# windows env variables
4141
env.*
42+
43+
# SSL certificate for cosmos emulator
44+
emulatorcert.crt

Dockerfile.dev

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM python:3.9-alpine
2+
ARG buildDeps='g++ gnupg libffi-dev openssl-dev musl-dev cargo'
3+
WORKDIR /usr/src/app
4+
COPY . .
5+
RUN apk update \
6+
&& apk add --no-cache $buildDeps gcc unixodbc-dev ca-certificates curl \
7+
&& pip3 install --no-cache-dir -r requirements/time_tracker_api/dev.txt \
8+
&& curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.2.1-1_amd64.apk \
9+
&& curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.2.1-1_amd64.apk \
10+
&& curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.2.1-1_amd64.sig \
11+
&& curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.2.1-1_amd64.sig \
12+
&& curl https://packages.microsoft.com/keys/microsoft.asc | gpg --import - \
13+
&& gpg --verify msodbcsql17_17.5.2.1-1_amd64.sig msodbcsql17_17.5.2.1-1_amd64.apk \
14+
&& gpg --verify mssql-tools_17.5.2.1-1_amd64.sig mssql-tools_17.5.2.1-1_amd64.apk \
15+
&& apk add --no-cache --allow-untrusted msodbcsql17_17.5.2.1-1_amd64.apk \
16+
&& apk add --no-cache --allow-untrusted mssql-tools_17.5.2.1-1_amd64.apk \
17+
&& rm msodbcsql17_17.5.2.1-1_amd64.apk mssql-tools_17.5.2.1-1_amd64.apk msodbcsql17_17.5.2.1-1_amd64.sig mssql-tools_17.5.2.1-1_amd64.sig \
18+
&& apk del $buildDeps \
19+
&& rm -rfv /root/.cache/pip/* && \
20+
find /usr/local \( -type d -a -name test -o -name tests \) -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) -exec rm -rfv '{}' \+
21+
RUN apk add --no-cache --upgrade bash
22+
ENV FLASK_APP time_tracker_api
23+
ENV FLASK_RUN_HOST 0.0.0.0
24+
25+
EXPOSE 5000
26+
27+
CMD ["flask","run"]
28+

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ They will be automatically run during the Continuous Deployment process.
438438
Shared file with all the Feature Toggles we create, so we can have a history of them
439439
[Feature Toggles dictionary](https://github.com/ioet/time-tracker-ui/wiki/Feature-Toggles-dictionary)
440440

441+
## Support for docker-compose and cosmosdb emulator
442+
443+
To run the dev enviroment in docker-compose:
444+
```bash
445+
docker-compose up
446+
```
447+
441448
## More information about the project
442449

443450
[Starting in Time Tracker](https://github.com/ioet/time-tracker-ui/wiki/Time-tracker)

cosmosdb-emulator/entrypoint.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
until curl -ksf "${DATABASE_ACCOUNT_URI}/_explorer/emulator.pem" -o 'cosmosdb-emulator/emulatorcert.crt'; do
4+
echo "Waiting for Cosmosdb to start..."
5+
sleep 10
6+
done
7+
8+
echo "Container cosmosemulator started."
9+
10+
echo "Importing SSL..."
11+
cp cosmosdb-emulator/emulatorcert.crt /usr/local/share/ca-certificates/
12+
cp cosmosdb-emulator/emulatorcert.crt /usr/share/ca-certificates/
13+
update-ca-certificates --fresh
14+
echo "Importing Containers..."
15+
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/
16+
python3 ./cosmosdb-emulator/init_emulator_db.py
17+
echo "Installation succeed!!"
18+
19+
echo "Starting Flask!!"
20+
flask run

cosmosdb-emulator/init_emulator.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
3+
containerId=$(docker ps --all | grep 'Time-Tracker-Cosmos-Db' | awk '{print $1}')
4+
if [ -z "$containerId" ]; then
5+
ipaddr="`ifconfig | grep "inet " | grep -Fv 127.0.0.1 | awk '{print $2}' | head -n 1`"
6+
containerId=$(docker create -p 8081:8081 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254 -m 3g --cpus=2.0 --name=Time-Tracker-Cosmos-Db -e AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10 -e AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true -e AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr -it mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator)
7+
echo "##vso[task.setvariable variable=cosmosDbContainerId]$containerId"> /dev/tty
8+
fi
9+
docker start $containerId
10+
11+
until curl -ksf "127.0.0.1:8081/_explorer/emulator.pem" -o 'cosmosdb-emulator/emulatorcert.crt'; do
12+
echo "Waiting for Cosmosdb to start..."
13+
sleep 10
14+
done
15+
16+
echo "Container cosmosemulator started."
17+
18+
echo "Checking SSL"
19+
isInstalled=$( awk -v cmd='openssl x509 -noout -subject' '/BEGIN/{close(cmd)};{print | cmd}' < /etc/ssl/certs/ca-certificates.crt | grep host ) || :
20+
21+
echo "ps"
22+
echo "$isInstalled"
23+
24+
if [ -z "$isInstalled" ]; then
25+
echo "Importing SSL..."
26+
cp cosmosdb-emulator/emulatorcert.crt /usr/local/share/ca-certificates/
27+
cp cosmosdb-emulator/emulatorcert.crt /usr/share/ca-certificates/
28+
update-ca-certificates --fresh
29+
echo "Importing Containers..."
30+
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/
31+
python3 ./cosmosdb-emulator/init_emulator_db.py
32+
echo "Installation succeed!!"
33+
fi
34+
35+
echo "Starting Flask!!"
36+
flask run

cosmosdb-emulator/init_emulator_db.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from azure.cosmos import exceptions, CosmosClient, PartitionKey
2+
import os, sys
3+
4+
sys.path.append("/usr/src/app")
5+
6+
DATABASE_ACCOUNT_URI = os.environ.get('DATABASE_ACCOUNT_URI')
7+
DATABASE_MASTER_KEY = os.environ.get('DATABASE_MASTER_KEY')
8+
9+
endpoint = DATABASE_ACCOUNT_URI
10+
key = DATABASE_MASTER_KEY
11+
12+
# <create_cosmos_client>
13+
client = CosmosClient(endpoint, key)
14+
# <create_database_if_not_exists>
15+
database_name = 'time-tracker-db'
16+
database = client.create_database_if_not_exists(id=database_name)
17+
# </create_containers_if_not_exists>
18+
19+
print("Creating TimeTracker initial initial database schema...")
20+
21+
try:
22+
print('- Project')
23+
from time_tracker_api.projects.projects_model import container_definition as project_definition
24+
database.create_container_if_not_exists(**project_definition)
25+
26+
print('- Project type')
27+
from time_tracker_api.project_types.project_types_model import container_definition as project_type_definition
28+
database.create_container_if_not_exists(**project_type_definition)
29+
30+
print('- Activity')
31+
from time_tracker_api.activities.activities_model import container_definition as activity_definition
32+
database.create_container_if_not_exists(**activity_definition)
33+
34+
print('- Customer')
35+
from time_tracker_api.customers.customers_model import container_definition as customer_definition
36+
database.create_container_if_not_exists(**customer_definition)
37+
38+
print('- Time entry')
39+
from time_tracker_api.time_entries.time_entries_model import container_definition as time_entry_definition
40+
database.create_container_if_not_exists(**time_entry_definition)
41+
42+
print('- Technology')
43+
from time_tracker_api.technologies.technologies_model import container_definition as technologies_definition
44+
database.create_container_if_not_exists(**technologies_definition)
45+
except exceptions.CosmosResourceExistsError as e:
46+
print("Unexpected error while creating initial database schema: %s" % e.message)
47+
48+
print("Done!")
49+

docker-compose.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
version: '3.8'
2+
3+
services:
4+
api:
5+
container_name: "time-tracker-backend_api"
6+
build:
7+
context: .
8+
dockerfile: ./Dockerfile.dev
9+
ports:
10+
- 5000:5000
11+
env_file:
12+
- .env
13+
volumes:
14+
- .:/usr/src/app
15+
# depends_on:
16+
# - cosmosdb
17+
entrypoint:
18+
- /bin/sh
19+
- ./cosmosdb-emulator/entrypoint.sh
20+
# networks:
21+
# services_net:
22+
# ipv4_address: 172.20.0.77
23+
24+
cosmosdb:
25+
container_name: "azurecosmosemulator"
26+
hostname: "azurecosmosemulator"
27+
image: 'mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator'
28+
tty: true
29+
mem_limit: 3GB
30+
ports:
31+
- '8081:8081'
32+
- '10251:10251'
33+
- '10252:10252'
34+
- '10253:10253'
35+
- '10254:10254'
36+
environment:
37+
- AZURE_COSMOS_EMULATOR_PARTITION_COUNT=7
38+
- AZURE_COSMOS_EMULATOR_ARGS=/alternativenames=azurecosmosemulator
39+
40+
# networks:
41+
# services_net:
42+
# ipv4_address: 172.20.0.78
43+
44+
45+
# networks:
46+
# services_net:
47+
# ipam:
48+
# driver: default
49+
# config:
50+
# - subnet: 172.20.0.0/16

run-test.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
PYTHON_COMMAND="pip install azure-functions"
3+
PYTHON_TEST="python3 -m pytest -v --ignore=tests/commons/data_access_layer/azure/sql_repository_test.py"
4+
API_CONTAINER_NAME="time-tracker-backend_api"
5+
execute(){
6+
7+
docker exec -ti $API_CONTAINER_NAME sh -c "$PYTHON_COMMAND"
8+
docker exec -ti $API_CONTAINER_NAME sh -c "awk -v cmd='openssl x509 -noout -subject' '/BEGIN/{close(cmd)};{print | cmd}' < /etc/ssl/certs/ca-certificates.crt | grep host"
9+
docker exec -ti $API_CONTAINER_NAME sh -c "env"
10+
docker exec -ti $API_CONTAINER_NAME sh -c "$PYTHON_TEST"
11+
12+
}
13+
14+
15+
execute

0 commit comments

Comments
 (0)