---
kind: Template
apiVersion: v1
metadata:
  name: edivorce
  annotations:
    description: An example Django application with a PostgreSQL database
    tags: edivorce,python,django,postgresql
    iconClass: icon-python
labels:
  template: edivorce-environment-template
objects:
- kind: Service
  apiVersion: v1
  metadata:
    name: "${NAME}"
    annotations:
      description: Exposes and load balances the application pods
  spec:
    ports:
    - name: web
      port: 8080
      targetPort: 8080
    selector:
      name: "${NAME}"
- kind: DeploymentConfig
  apiVersion: v1
  metadata:
    name: "${NAME}"
    annotations:
      description: Defines how to deploy the application server
  spec:
    strategy:
      type: Rolling
    triggers:
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
        - edivorce-django
        from:
          kind: ImageStreamTag
          namespace: "${APP_IMAGE_NAMESPACE}"
          name: "${APP_IMAGE_NAME}:deploy-to-${ENVIRONMENT_TYPE}"
    - type: ConfigChange
    replicas: 1
    selector:
      name: "${NAME}"
    template:
      metadata:
        name: "${NAME}"
        labels:
          name: "${NAME}"
      spec:
        containers:
        - name: edivorce-django
          image: " "
          ports:
          - containerPort: 8080
          readinessProbe:
            timeoutSeconds: 3
            initialDelaySeconds: 3
            httpGet:
              path: "/health"
              port: 8080
          livenessProbe:
            timeoutSeconds: 3
            initialDelaySeconds: 30
            httpGet:
              path: "/health"
              port: 8080
          env:
          - name: DATABASE_SERVICE_NAME
            value: "${DATABASE_SERVICE_NAME}"
          - name: DATABASE_ENGINE
            value: "${DATABASE_ENGINE}"
          - name: DATABASE_NAME
            value: "${DATABASE_NAME}"
          - name: DATABASE_USER
            value: "${DATABASE_USER}"
          - name: DATABASE_PASSWORD
            value: "${DATABASE_PASSWORD}"
          - name: APP_CONFIG
            value: "${APP_CONFIG}"
          - name: DJANGO_SECRET_KEY
            value: "${DJANGO_SECRET_KEY}"
          - name: ENVIRONMENT_TYPE
            value: "${ENVIRONMENT_TYPE}"
          resources:
            limits:
              memory: "${MEMORY_LIMIT}"
- kind: PersistentVolumeClaim
  apiVersion: v1
  metadata:
    name: "${DATABASE_SERVICE_NAME}"
  spec:
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: "${VOLUME_CAPACITY}"
- kind: Service
  apiVersion: v1
  metadata:
    name: "${DATABASE_SERVICE_NAME}"
    annotations:
      description: Exposes the database server
  spec:
    ports:
    - name: postgresql
      port: 5432
      targetPort: 5432
    selector:
      name: "${DATABASE_SERVICE_NAME}"
- kind: DeploymentConfig
  apiVersion: v1
  metadata:
    name: "${DATABASE_SERVICE_NAME}"
    annotations:
      description: Defines how to deploy the database
  spec:
    strategy:
      type: Recreate
    triggers:
    - type: ImageChange
      imageChangeParams:
        automatic: true
        containerNames:
        - postgresql
        from:
          kind: ImageStreamTag
          namespace: "${NAMESPACE}"
          name: postgresql:9.5
    - type: ConfigChange
    replicas: 1
    selector:
      name: "${DATABASE_SERVICE_NAME}"
    template:
      metadata:
        name: "${DATABASE_SERVICE_NAME}"
        labels:
          name: "${DATABASE_SERVICE_NAME}"
      spec:
        volumes:
        - name: "${DATABASE_SERVICE_NAME}-data"
          persistentVolumeClaim:
            claimName: "${DATABASE_SERVICE_NAME}"
        containers:
        - name: postgresql
          image: " "
          ports:
          - containerPort: 5432
          env:
          - name: POSTGRESQL_USER
            value: "${DATABASE_USER}"
          - name: POSTGRESQL_PASSWORD
            value: "${DATABASE_PASSWORD}"
          - name: POSTGRESQL_DATABASE
            value: "${DATABASE_NAME}"
          volumeMounts:
          - name: "${DATABASE_SERVICE_NAME}-data"
            mountPath: "/var/lib/pgsql/data"
          readinessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 5
            exec:
              command:
              - "/bin/sh"
              - "-i"
              - "-c"
              - psql -h 127.0.0.1 -U ${POSTGRESQL_USER} -q -d ${POSTGRESQL_DATABASE} -c 'SELECT 1'
          livenessProbe:
            timeoutSeconds: 1
            initialDelaySeconds: 30
            tcpSocket:
              port: 5432
          resources:
            limits:
              memory: "${MEMORY_POSTGRESQL_LIMIT}"
- kind: ImageStream
  apiVersion: v1
  metadata:
    name: weasyprint
    labels:
      name: weasyprint
  spec:
    tags:
      - name: latest
        annotations:
          openshift.io/imported-from: jag-csb-edivorce-tools/weasyprint
        from:
          kind: DockerImage
          name: jag-csb-edivorce-tools/weasyprint
- kind: Service
  apiVersion: v1
  metadata:
    name: weasyprint
    labels:
      name: weasyprint
  spec:
    ports:
      - name: 5001-tcp
        protocol: TCP
        port: 5001
        targetPort: 5001
    selector:
      name: weasyprint
- kind: DeploymentConfig
  apiVersion: v1
  metadata:
    name: weasyprint
    labels:
      app: weasyprint
    annotations:
      description: Weasyprint microservice using aquavitae/weasyprint
  spec:
    strategy:
      type: Recreate
    triggers:
      - type: ImageChange
        imageChangeParams:
          automatic: true
          containerNames:
            - weasyprint
          from:
            kind: ImageStreamTag
            namespace: jag-csb-edivorce-tools
            name: 'weasyprint:latest'
    replicas: 1
    selector:
      name: weasyprint
    template:
      metadata:
        name: weasyprint
        labels:
          name: weasyprint
        annotations:
          openshift.io/container.weasyprint.image.entrypoint: '["/bin/sh","-c","gunicorn --bind 0.0.0.0:5001 wsgi:app"]'
      spec:
        containers:
          - name: weasyprint
            image: 'jag-csb-edivorce-tools/weasyprint'
            ports:
              - containerPort: 5001
                protocol: TCP
            livenessProbe:
              httpGet:
                path: /health
                port: 5001
                scheme: HTTP
              initialDelaySeconds: 120
              timeoutSeconds: 30
              periodSeconds: 60
              successThreshold: 1
              failureThreshold: 5
parameters:
- name: NAME
  displayName: Name
  description: The name assigned to all of the frontend objects defined in this template.
  required: true
  value: edivorce-django
- name: NAMESPACE
  displayName: Namespace
  required: true
  description: The OpenShift Namespace where the ImageStream resides.
  value: openshift
- name: MEMORY_LIMIT
  displayName: Memory Limit
  required: true
  description: Maximum amount of memory the Django container can use.
  value: 512Mi
- name: MEMORY_POSTGRESQL_LIMIT
  displayName: Memory Limit (PostgreSQL)
  required: true
  description: Maximum amount of memory the PostgreSQL container can use.
  value: 512Mi
- name: VOLUME_CAPACITY
  displayName: Volume Capacity
  description: Volume space available for data, e.g. 512Mi, 2Gi
  value: 1Gi
  required: true
- name: DATABASE_SERVICE_NAME
  displayName: Database Service Name
  required: true
  value: postgresql
- name: DATABASE_ENGINE
  displayName: Database Engine
  required: true
  description: 'Database engine: postgresql, mysql or sqlite (default).'
  value: postgresql
- name: DATABASE_NAME
  displayName: Database Name
  required: true
  value: default
- name: DATABASE_USER
  displayName: Database Username
  required: true
  value: django
- name: DATABASE_PASSWORD
  displayName: Database User Password
  generate: expression
  from: "[a-zA-Z0-9]{16}"
- name: APP_CONFIG
  displayName: Application Configuration File Path
  description: Relative path to Gunicorn configuration file (optional).
- name: DJANGO_SECRET_KEY
  displayName: Django Secret Key
  description: Set this to a long random string.
  generate: expression
  from: "[\\w]{50}"
- name: APP_IMAGE_NAME
  displayName: Application image name.
  value: edivorce-django
  required": true
- name: APP_IMAGE_NAMESPACE
  displayName: Namespace containing application images.
  value: jag-csb-edivorce-tools
  required: true
- name: ENVIRONMENT_TYPE
  displayName: Type of environnment (dev,test or prod).
  required: true
