#
# Copyright SecureKey Technologies Inc. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#

---
kind: Job
apiVersion: batch/v1
metadata:
  name: aries-add-contexts
  labels:
    component: aries-framework
    group: services
spec:
  template:
    spec:
      volumes:
        - name: script
          configMap:
            name: aries-add-contexts-config
      restartPolicy: Never
      initContainers:
        - name: adapters-ready
          image: fedora
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args:
            - "-c"
            - |
              ENDPOINTS=(adapter-issuer adapter-rp);
              for endpoint in "${ENDPOINTS[@]}";
              do while [[ "$(curl -o /dev/null -s -w '%{http_code}' --insecure --connect-timeout 5 https://${endpoint}.||DOMAIN||/healthcheck)" != "200" ]];
              do echo "waiting for ${endpoint} endpoint";
              sleep 5;
              done;
              done;
        - name: vcs-components-ready
          image: fedora
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args:
            - "-c"
            - |
              ENDPOINTS=(issuer-vcs verifier-vcs);
              for endpoint in "${ENDPOINTS[@]}";
              do while [[ "$(curl -o /dev/null -s -w '%{http_code}' --insecure --connect-timeout 5 https://${endpoint}.||DOMAIN||/healthcheck)" != "200" ]];
              do echo "waiting for ${endpoint} endpoint";
              sleep 5;
              done;
              done;
        - name: demo-ready
          image: fedora
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args:
            - "-c"
            - |
              ENDPOINTS=(demo-issuer ucis-rp cbp-rp benefits-dept-rp);
              for endpoint in "${ENDPOINTS[@]}";
              do while [[ "$(curl -o /dev/null -s -w '%{http_code}' --insecure --connect-timeout 5 https://${endpoint}.||DOMAIN||/healthcheck)" != "200" ]];
              do echo "waiting for ${endpoint} endpoint";
              sleep 5;
              done;
              done;
      containers:
        - name: aries-add-contexts
          image: "alpine:latest"
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args: ["/opt/add_contexts.sh"]
          volumeMounts:
            - name: script
              mountPath: /opt
