Skip to content
Snippets Groups Projects
run.sh 2.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • David Trattnig's avatar
    David Trattnig committed
    #!/bin/bash
    # Default mode
    
    David Trattnig's avatar
    David Trattnig committed
    docker="false"
    
    #
    # Run Script for AURA Engine API
    #
    # Call with one of these parameters:
    #
    
    David Trattnig's avatar
    David Trattnig committed
    # - prod
    
    David Trattnig's avatar
    David Trattnig committed
    # - test
    
    David Trattnig's avatar
    David Trattnig committed
    # - recreate-database
    
    # - docker:recreate-database
    # - docker:build
    # - docker:push
    
    David Trattnig's avatar
    David Trattnig committed
    # - docker:dev
    
    David Trattnig's avatar
    David Trattnig committed
    
    
    David Trattnig's avatar
    David Trattnig committed
    if [[ $* =~ ^(dev|api-test-0|api-test-1|api-test-2|prod|test)$ ]]; then
    
    David Trattnig's avatar
    David Trattnig committed
    fi
    
    if [[ "$1" == *"docker:"* ]]; then
    	docker="true"
    	mode=${1#*:}
    fi
    
    
    echo "[ Run mode=$mode ]"
    echo "[ Docker=$docker ]"
    
    
    # Check for the correct Python version (3.8+)
    PYTHON_EXEC="python3"
    
    echo "[ Using $("$PYTHON_EXEC" -V) ]"
    
    David Trattnig's avatar
    David Trattnig committed
    
    
    # +++ DEFAULT COMMANDS +++ #
    
    if [[ $docker == "false" ]]; then
    
    	### Runs the API Server (Development) ###
    
    
    	if [[ $mode == "dev" ]]; then
    		source python/bin/activate
    
    		echo "Running Engine API in Python Environment ($("$PYTHON_EXEC" -V))"
    
    David Trattnig's avatar
    David Trattnig committed
    		echo "Starting API Server"
    
    		"$PYTHON_EXEC" src/app.py
    
    David Trattnig's avatar
    David Trattnig committed
    	fi
    
    
    	### Runs the API Server (Test) ###
    
    	if [[ $mode == "api-test-0" ]]; then
    		echo "Starting API Server 0"
    
    		"$PYTHON_EXEC" src/app.py config=tests/config/engine-0-api.ini
    
    	fi
    	if [[ $mode == "api-test-1" ]]; then
    		echo "Starting API Server 1"
    
    		"$PYTHON_EXEC" src/app.py config=tests/config/engine-1-api.ini
    
    	fi
    	if [[ $mode == "api-test-2" ]]; then
    		echo "Starting API Server 2"
    
    		"$PYTHON_EXEC" src/app.py config=tests/config/engine-2-api.ini
    
    David Trattnig's avatar
    David Trattnig committed
    	### Runs the API Server using Gunicorn without a system daemon (Production) ###
    
    
    David Trattnig's avatar
    David Trattnig committed
    	if [[ $mode == "prod" ]]; then
    
    David Trattnig's avatar
    David Trattnig committed
    		echo "Starting API Server"
    
    		gunicorn -c config/gunicorn.conf.py src.app:app
    
    David Trattnig's avatar
    David Trattnig committed
    	fi
    
    
    David Trattnig's avatar
    David Trattnig committed
    	if [[ $mode == "test" ]]; then
    		echo "Testing API Server"
    		tox
    	fi
    
    
    David Trattnig's avatar
    David Trattnig committed
    	### CAUTION: This deletes everything in your database ###
    
    	if [[ $mode == "recreate-database" ]]; then
    
    		"$PYTHON_EXEC" src/app.py --recreate-database
    
    David Trattnig's avatar
    David Trattnig committed
    	fi
    
    fi
    
    
    # +++ DOCKER COMMANDS +++ #
    
    if [[ $docker == "true" ]]; then
    	BASE_D=$(realpath "${BASH_SOURCE%/*}/")
    
    	### Runs Engine API using Gunicorn ###
    
    
    David Trattnig's avatar
    David Trattnig committed
    	if [[ $mode == "dev" ]]; then
    
    		exec sudo docker run \
    			--network="host" \
    
    			--name aura-engine-api \
    
    David Trattnig's avatar
    David Trattnig committed
    			-u $UID:$GID \
    			-v "$BASE_D":/srv \
    
    			-v "$BASE_D/config/docker":/srv/config \
    
    			--tmpfs /var/log/aura/ \
    			-e TZ=Europe/Vienna \
    			autoradio/engine-api
    
    David Trattnig's avatar
    David Trattnig committed
    	fi
    
    	### Create Docker Image from local project ###
    
    	if [[ $mode == "build" ]]; then
    		exec sudo docker build -t autoradio/engine-api .
    	fi
    
    	### Pushes the latest Docker Image to Docker Hub ###
    
    	if [[ $mode == "push" ]]; then
    		exec sudo docker push autoradio/engine-api
    	fi