apiVersion: batch/v1
kind: Job
metadata:
  # 
  # A little helper jobs for creating new indexd
  # users with basic-auth access to indexd update
  # endpoints.
  # Loads the list of users from the indexd
  # creds.json
  # More details in README.md
  #
  name: arranger-config
spec:
  template:
    metadata:
      labels:
        app: gen3job
    spec:
      # not yet supported - backOffLimit: 3
      automountServiceAccountToken: false
      volumes:
        - name: arranger-config
          configMap:
            name: arranger-config
      containers:
        - name: awshelper
          image: quay.io/cdis/awshelper:master
          volumeMounts:
            - name: "arranger-config"
              readOnly: true
              mountPath: "/home/ubuntu/arrangerConfig.tar"
              subPath: "arrangerConfig.tar"  
          imagePullPolicy: Always
          command: ["/bin/bash" ]
          args: 
            - "-c"
            # Script always succeeds if it runs (echo exits with 0)
            - |
              tar xvf arrangerConfig.tar
              sourceFolder=arrangerConfig
              ESHOST=esproxy-service:9200
              indexList=$(ls -1 $sourceFolder | grep .json | sed 's/__.*json$//' | sort -u)
              for name in $indexList; do
                echo $name
                elasticdump --output http://$ESHOST/$name --input $sourceFolder/${name}__data.json --type data
                elasticdump --output http://$ESHOST/$name --input $sourceFolder/${name}__mapping.json --type mapping
              done
              echo "Exit code: $?"
      restartPolicy: Never
