
apiVersion: v1
kind: ConfigMap
metadata:
  name: entrypoint-sh-configmap
data:
  entrypoint.sh: |
    #!/bin/bash -x

    # Exit script in case of error
    set -e

    # Start cron && memcached services
    service cron restart
    service memcached restart

    echo $"\n\n\n"
    echo "-----------------------------------------------------"
    echo "STARTING DJANGO ENTRYPOINT $(date)"
    echo "-----------------------------------------------------"

    /usr/local/bin/invoke update 

    source $HOME/.bashrc
    echo DOCKER_API_VERSION=$DOCKER_API_VERSION
    echo POSTGRES_USER=$POSTGRES_USER
    echo POSTGRES_PASSWORD=$POSTGRES_PASSWORD
    echo DATABASE_URL=$DATABASE_URL
    echo GEODATABASE_URL=$GEODATABASE_URL
    echo SITEURL=$SITEURL
    echo ALLOWED_HOSTS=$ALLOWED_HOSTS
    echo GEOSERVER_PUBLIC_LOCATION=$GEOSERVER_PUBLIC_LOCATION
    echo MONITORING_ENABLED=$MONITORING_ENABLED
    echo MONITORING_HOST_NAME=$MONITORING_HOST_NAME
    echo MONITORING_SERVICE_NAME=$MONITORING_SERVICE_NAME
    echo MONITORING_DATA_TTL=$MONITORING_DATA_TTL

    echo "running waitfordbs"
    /usr/local/bin/invoke waitfordbs
    echo "waitfordbs task done"

    echo "running migrations"
    /usr/local/bin/invoke migrations 
    echo "migrations task done"

    cmd="$@"

    if [ ${IS_CELERY} = "true" ]  || [ ${IS_CELERY} = "True" ]
    then
        echo "Executing Celery server $cmd for Production"
    else

        /usr/local/bin/invoke prepare 
        echo "prepare task done"

        if [ ${FORCE_REINIT} = "true" ]  || [ ${FORCE_REINIT} = "True" ] || [ ! -e "/mnt/volumes/statics/geonode_init.lock" ]; then
            
            /usr/local/bin/invoke updategeoip 
            echo "updategeoip task done"
            /usr/local/bin/invoke fixtures 
            echo "fixture task done"
            /usr/local/bin/invoke monitoringfixture
            echo "monitoringfixture task done"
            /usr/local/bin/invoke initialized 
            echo "initialized"
        fi

        echo "refresh static data"
        /usr/local/bin/invoke statics 
        echo "static data refreshed"
        /usr/local/bin/invoke waitforgeoserver 
        echo "waitforgeoserver task done"
        /usr/local/bin/invoke geoserverfixture 
        echo "geoserverfixture task done"
        /usr/local/bin/invoke updateadmin 
        echo "updateadmin task done"

        echo "Executing UWSGI server $cmd for Production"
    fi

    echo "-----------------------------------------------------"
    echo "FINISHED DJANGO ENTRYPOINT --------------------------"
    echo "-----------------------------------------------------"

    # Run the CMD 
    echo "got command $cmd"
    exec $cmd
    