apiVersion: batch/v1
kind: Job
metadata:
  name: bootstrap-dataverse
  labels:
    app.kubernetes.io/name: bootstrap
    app.kubernetes.io/version: "1.1"
    app.kubernetes.io/component: job
    app.kubernetes.io/part-of: dataverse
    app.kubernetes.io/managed-by: kubectl
spec:
  template:
    metadata:
      labels:
        app.kubernetes.io/name: bootstrap
        app.kubernetes.io/part-of: dataverse
    spec:
      containers:
        - name: bootstrap
          image: iqss/dataverse-k8s 
          command: ["scripts/bootstrap-job.sh"]
          envFrom:
            - configMapRef:
                name: dataverse
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: dataverse-postgresql
                  key: username
                  optional: true
            - name: POSTGRES_DATABASE
              valueFrom:
                secretKeyRef:
                  name: dataverse-postgresql
                  key: database
                  optional: true
          volumeMounts:
            - name: db-secret
              mountPath: "/secrets/db"
              readOnly: true
            - name: api-secret
              mountPath: "/secrets/api"
              readOnly: true
            - name: admin-secret
              mountPath: "/secrets/admin"
              readOnly: true
            - name: provider-secrets
              mountPath: "/secrets/providers"
              readOnly: true
      initContainers:
        - name: check-db-ready
          image: postgres:9.6
          command: ['sh', '-c',
            'until pg_isready -h "${DATAVERSE_POSTGRES_POSTGRESQL_SERVICE_HOST}" -p "${DATAVERSE_POSTGRES_POSTGRESQL_SERVICE_PORT}";
            do env; echo waiting for database; sleep 3; done;']
        - name: check-solr-ready
          image: giantswarm/tiny-tools
          command: ['sh', '-c',
            'until curl -sS -f "http://${SOLR_SERVICE_HOST}:${SOLR_SERVICE_PORT}/solr/collection1/admin/ping" 2>&1 | grep "status.:.OK";
            do echo waiting for solr; sleep 3; done;']
        - name: check-dataverse-ready
          image: giantswarm/tiny-tools
          command: ['sh', '-c',
            'until curl -sS -f "http://${DATAVERSE_SERVICE_HOST}:${DATAVERSE_SERVICE_PORT_HTTP}/robots.txt" -m 2 2>&1 > /dev/null;
            do echo waiting for dataverse; sleep 15; done;']
      volumes:
        - name: db-secret
          secret:
            secretName: dataverse-postgresql
        - name: api-secret
          secret:
            secretName: dataverse-api
        - name: admin-secret
          secret:
            secretName: dataverse-admin
            optional: true
        - name: provider-secrets
          secret:
            secretName: dataverse-providers
            optional: true
      restartPolicy: Never
  backoffLimit: 4
