apiVersion: apps/v1
kind: Deployment
metadata:
  name: django-deployment
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: django
      run: django
  template:
    metadata:
      labels:
        app: django
        run: django
    spec:
      hostname: django
      volumes:
      - name: geoserver-static-storage
        persistentVolumeClaim:
          claimName: claim-shared-static
      - name: geoserver-data-dir-storage
        persistentVolumeClaim:
          claimName: claim-shared-data-dir
      - name: geoserver-data-storage
        persistentVolumeClaim:
          claimName: claim-shared-data
      - name: geoserver-backup-restore-storage
        persistentVolumeClaim:
          claimName: claim-shared-backup-restore
      - name: geoserver-tmp-storage
        persistentVolumeClaim:
          claimName: claim-shared-tmp
      - name: entrypoint-sh
        configMap:
          name: entrypoint-sh-configmap
          items:
          - key: entrypoint.sh
            path: "entrypoint.sh"
      - name: uwsgi-ini
        configMap:
          name: uwsgi-ini-configmap
          items:
          - key: uwsgi.ini
            path: "uwsgi.ini"
      initContainers:
      - image: curlimages/curl:7.78.0
        name: waiting-for-database
        command:
         - sh
         - "-c"
         - |
          #!/bin/sh
          a=0;
          while [[ $a != 52 ]] ; do  
            curl -v -sL -I http://$GEONODE_POSTGIS_CLUSTER_SERVICE_HOST:$GEONODE_POSTGIS_CLUSTER_SERVICE_PORT/ping
            a=$?
            sleep 1
          done        
          echo "postgis-cluster is up"
          exit 0

          EOF
      containers:
      - image: mwall2bitflow/geonode:3.1.1
        name: genode-django
        command: ["/bin/bash"]
        args: ["/usr/src/geonode/entrypoint.sh","uwsgi", "--ini", "/usr/src/geonode/uwsgi.ini","--log-master"]
        readinessProbe:
          httpGet:
            port: 8001
          initialDelaySeconds: 10
          timeoutSeconds: 2   
        env:
        # GEOSERVER SECRETS
        - name: ADMIN_USERNAME
          valueFrom:
            secretKeyRef:
              name: geoserver-login
              key: admin_user
        - name: ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: geoserver-login
              key: admin_password
        - name: ADMIN_EMAIL
          valueFrom:
            secretKeyRef:
              name: geoserver-login
              key: admin_emailaddr
        
        # Database users
        # Used if database postgis without patroni
        # - name: POSTGRES_USER
        #   valueFrom:
        #     secretKeyRef:
        #       name: geonode-postgis
        #       key: postgres_user
        
        # - name: POSTGRES_PASSWORD
        #   valueFrom:
        #     secretKeyRef:
        #       name: geonode-postgis
        #       key: postgres_password

        # patroni login credentials from
        # {username}.{team}-{clustername}.credentials.postgresql.acid.zalan.do
        # requires to be variables
        # geogenode database
        - name: DATABASE_HOST
          value: "$(GEONODE_POSTGIS_CLUSTER_SERVICE_HOST)"
        - name: DATABASE_PORT
          value: "$(GEONODE_POSTGIS_CLUSTER_SERVICE_PORT)"

        # Postgres superuser
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: postgres.geonode-postgis-cluster.credentials
              key: username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: postgres.geonode-postgis-cluster.credentials
              key: password
        
        # TODO REMOVE THIS FROM wait-for-databases.sh
        # geonode database/user
        - name: GEONODE_DATABASE
          valueFrom:
            secretKeyRef:
              name: geonode.geonode-postgis-cluster.credentials
              key: username
        # geonode user password
        - name: GEONODE_DATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: geonode.geonode-postgis-cluster.credentials
              key: password
        
        # geogeonode database/user
        - name: GEONODE_GEODATABASE
          valueFrom:
            secretKeyRef:
              name: geogeonode.geonode-postgis-cluster.credentials
              key: username
        # geonode user password
        - name: GEONODE_GEODATABASE_PASSWORD
          valueFrom:
            secretKeyRef:
              name: geogeonode.geonode-postgis-cluster.credentials
              key: password
        # NEED TO BE BUILD WITH VARIABLES TODO 
        - name: GEODATABASE_URL
          value: "postgis://$(GEONODE_DATABASE):$(GEONODE_DATABASE_PASSWORD)@$(DATABASE_HOST):$(DATABASE_PORT)/geonode_data"
        
        # whatever that means
        - name: DEFAULT_BACKEND_DATASTORE
          value: datastore
        # Other ENV
        - name: SITEURL
          value: http://geonode
        - name: IS_CELERY
          value: "False"
        envFrom:
        - configMapRef:
            name: django-config
        - configMapRef:
            name: geoserver-config
        - configMapRef:
            name: nginx-config
        # - configMapRef:
        #     name: postgis-config
        - configMapRef:
            name: rabbitmq-config        
        ports:        
        - containerPort: 8000
        - containerPort: 8001
        volumeMounts:
        - name: geoserver-static-storage
          mountPath: /mnt/volumes/statics
        - name: geoserver-data-dir-storage
          mountPath: /geoserver_data/data
        - name: geoserver-backup-restore-storage
          mountPath: /backup_restore
        - name: geoserver-data-storage
          mountPath: /data
        - name: geoserver-tmp-storage
          mountPath: /tmp
        # Files
        - name: entrypoint-sh
          mountPath: "/usr/src/geonode/entrypoint.sh"
          subPath: entrypoint.sh
          readOnly: true
        - name: uwsgi-ini
          mountPath: "/usr/src/geonode/uwsgi.ini"
          subPath: uwsgi.ini
          readOnly: true