# pod to test the build of docker and containerd with the docker and s3 secrets
apiVersion: v1
kind: Pod
metadata:
  name: pod-dind-docker-build
spec:
  automountServiceAccountToken: false
  containers:
  - name: test
    command:
    - /bin/bash
    args:
    - -c
    - |
      # path to the github repository
      DIR_GITHUB="docker_ce_build_ppc64"
      PATH_GITHUB="https://github.com/florencepascual/${DIR_GITHUB}.git"
      
      # path to the image for building and testing
      NAME_IMAGE="dind-docker-build"
      PATH_IMAGE_BUILD="/workspace/${DIR_GITHUB}/images/docker-in-docker/${NAME_IMAGE}"

      # path to the scripts 
      PATH_SCRIPTS="/workspace/${DIR_GITHUB}/hack/docker-in-docker"
      DATE=`date +%d%m%y-%H%S`
      PATH_LOGS="/workspace/logs-${DATE}"
      NAME_LOG_PROWJOB="prow-job.log"
      NAME_LOG_BUILD_IMAGE="build_dind.log"

      export PATH_SCRIPTS
      export PATH_LOGS
      export NAME_LOG_PROWJOB

      # Create the log file
      echo "Create the log dir"
      mkdir ${PATH_LOGS}
      echo "Create the prow job log file"
      touch ${PATH_LOGS}/${NAME_LOG_PROWJOB}
      if ! test -f ${PATH_LOGS}/${NAME_LOG_PROWJOB}
      then
        echo "The log file ${PATH_LOGS}/${NAME_LOG_PROWJOB} was not created."
        exit 1
      fi

      # clone the directory where the scripts are
      echo "* Git clone and get the dockerd-entrypoint script*" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
      git clone ${PATH_GITHUB}
      if ! test -d ${DIR_GITHUB}
      then
        echo "The directory from ${PATH_GITHUB} was not cloned." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
        exit 1
      fi
      wget -O ${PATH_SCRIPTS}/dockerd-entrypoint.sh https://raw.githubusercontent.com/docker-library/docker/master/dockerd-entrypoint.sh 
      if ! test -f ${PATH_SCRIPTS}/dockerd-entrypoint.sh
      then
        echo "The dockerd-entrypoint file was not downloaded." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
        exit 1
      fi
      chmod a+x ${PATH_SCRIPTS}/*.sh

      # start the dockerd
      echo "** Dockerd **" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
      # bash ${PATH_SCRIPTS}/dockerd-entrypoint.sh &
      source ${PATH_SCRIPTS}/dockerd-starting.sh


      if [ -z "$pid" ]
      then
        echo "There is no docker daemon." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
        exit 1
      else
        if ! test -d /root/.docker 
        then
          # docker login
          echo "*** Docker login ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          mkdir /root/.docker
          echo "$DOCKER_SECRET_AUTH" > /root/.docker/config.json
        fi
        if grep -Fq "index.docker.io" /root/.docker/config.json
        then
          # get the env file and the dockertest repo and the latest built of containerd if we don't want to build containerd
          echo "*** * COS Bucket * ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          CONT_NAME=docker_s3_env
          docker run --env S3_SECRET_AUTH --env PATH_LOGS --env NAME_LOG_PROWJOB -d -v /workspace:/workspace --privileged --name $CONT_NAME debian:bullseye /bin/bash -c "${PATH_SCRIPTS}/get_env.sh"
          status_code="$(docker container wait $CONT_NAME)"
          echo $status_code 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}

          if [[ ${status_code} -ne 0 ]]
          then
            echo "The docker to get the env.list and the dockertest has failed." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            docker logs $CONT_NAME 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 1
          fi

          ls /workspace 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          
          if [[ -f env.list ]]
          then
            # check there are 3 env variables in env.list
            cat env.list 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            if grep -Fq "DOCKER_VERS" env.list && grep -Fq "CONTAINERD_VERS" env.list && grep -Fq "PACKAGING_REF" env.list
            then 
              echo "DOCKER_VERS, CONTAINERD_VERS, PACKAGING_REF are in env.list" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            else 
              echo "DOCKER_VERS, CONTAINERD_VERS and/or PACKAGING_REF are not in env.list" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
              exit 1
            fi
            set -o allexport
            source env.list
            echo "&&& DEBUG ENV.LIST"
            echo $DOCKER_VERS
            echo $CONTAINERD_VERS
            echo $PACKAGING_REF
          else
            echo "There is no env.list" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          fi
          
          # generate the env-distrib.list
          echo "*** ** env-distrib.list ** ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          mkdir docker-ce-packaging
          pushd docker-ce-packaging
          git init
          git remote add origin  https://github.com/docker/docker-ce-packaging.git
          git fetch --depth 1 origin ${PACKAGING_REF}
          git checkout FETCH_HEAD

          make REF=${DOCKER_VERS} checkout
          popd

          # get the packages list in the env_distrib.list
          echo DEBS=\"`cd docker-ce-packaging/deb && ls -1d debian-* ubuntu-*`\" > env-distrib.list
          echo RPMS=\"`cd docker-ce-packaging/rpm && ls -1d centos-* fedora-*`\" >> env-distrib.list

          rm -rf docker-ce-packaging

          # check the env.list (versions of docker-ce, containerd and list of packages)
          if [[ -f env-distrib.list ]]
          # if there is env.list and env-distrib.list
          then
            cat env-distrib.list 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            # check if there are two variables in env-distrib.list
            if grep -Fq "DEBS" env-distrib.list && grep -Fq "RPMS" env-distrib.list
            then 
              echo "DEBS and RPMS are in env-distrib.list" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            else 
              echo "DEBS and/or RPMS are not in env-distrib.list" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
              exit 1
            fi
            source env-distrib.list
            echo "&&& DEBUG ENV-DISTRIB.LIST"
            echo $DEBS
            echo $RPMS
          else
            echo "There is no env-distrib.list" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 1
          fi

          # build the dind-docker-build image
          echo "*** ** BUILD IMAGE ** ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          touch ${PATH_LOGS}/${NAME_LOG_BUILD_IMAGE}
          if ! test -f ${PATH_LOGS}/${NAME_LOG_BUILD_IMAGE}
          then
            echo "The log file ${PATH_LOGS}/${NAME_LOG_BUILD_IMAGE} was not created." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 1
          fi
          
          docker build -t ${NAME_IMAGE} ${PATH_IMAGE_BUILD} 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_BUILD_IMAGE}
          echo "See ${PATH_LOGS}/${NAME_LOG_BUILD_IMAGE} for the log." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_BUILD_IMAGE}

          # build docker_ce and containerd
          echo "*** *** BUILD *** ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          CONT_NAME=docker-build
          docker run --env DOCKER_SECRET_AUTH --env PATH_SCRIPTS --env PATH_LOGS --env NAME_LOG_PROWJOB -d -v /workspace:/workspace --privileged --name $CONT_NAME ${NAME_IMAGE} ${PATH_SCRIPTS}/build.sh 
          docker logs -f $CONT_NAME
          status_code="$(docker container wait $CONT_NAME)"
          if [[ ${status_code} -ne 0 ]]
          then
            echo "The docker supposed to build the packages has failed." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 1
          fi

          # change the containerd environment variable if CONTAINERD_VERS=0
          if [[ ${CONTAINERD_VERS} -eq 0 ]]
          then
            echo "Change CONTAINERD_VERS from 0 to the last version we got from the COS Bucket" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            ls -d /workspace/containerd-* 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            if [[ $? -ne 0 ]]
            then
              echo "There is no containerd package." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
              exit 1
            fi
            CONTAINERD_VERS=$(eval "ls -d /workspace/containerd-* | cut -d'-' -f2")
            echo ${CONTAINER_VERS} 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            sed -i 's/CONTAINERD_VERS=0/CONTAINERD_VERS='${CONTAINERD_VERS}'/g' env.list
            cat env.list 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            ls /workspace 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          fi

          echo "PATH_LOGS : ${PATH_LOGS}" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          echo "NAME LOG JOB : ${NAME_LOG_PROWJOB}" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}

          # test the packages
          echo "*** *** * TEST * *** ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          CONT_NAME=docker-test
          docker run --env DOCKER_SECRET_AUTH --env PATH_SCRIPTS --env PATH_LOGS --env NAME_LOG_PROWJOB -d -v /workspace:/workspace --privileged --name $CONT_NAME ${NAME_IMAGE} ${PATH_SCRIPTS}/test.sh
          status_code="$(docker container wait $CONT_NAME)"
          if [[ ${status_code} -ne 0 ]]
          then
            echo "We have not been able to check the tests." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            docker logs $CONT_NAME 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 1
          else
            docker logs $CONT_NAME 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            echo "*** *** ** TESTS CHECK ** *** ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            source ${PATH_SCRIPTS}/check_tests.sh
            echo ${CHECK_TESTS_BOOL} 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          fi

          # push to the COS Bucket
          echo "*** *** ** COS Bucket ** *** ***" 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
          CONT_NAME=docker_s3_push
          docker run --env S3_SECRET_AUTH --env PATH_LOGS --env NAME_LOG_PROWJOB -d -v /workspace:/workspace --privileged --name $CONT_NAME debian:bullseye /bin/bash -c "${PATH_SCRIPTS}/push_COS.sh ${CHECK_TESTS_BOOL}"
          status_code="$(docker container wait $CONT_NAME)"
          echo $status_code 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}

          if [[ ${status_code} -ne 0 ]]
          then
            echo "The docker to push the packages, the log and/or the tests has failed." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            docker logs $CONT_NAME 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 1
          else 
            echo "The packages, the log and/or the tests have been pushed." 2>&1 | tee -a ${PATH_LOGS}/${NAME_LOG_PROWJOB}
            exit 0
          fi
        fi
      fi
    image: quay.io/florencepascual/docker_ce_build
    resources: {}
    terminationMessagePolicy: FallbackToLogsOnError
    securityContext:
      privileged: true
    volumeMounts:
    - name: docker-graph-storage
      mountPath: /var/lib/docker
    env:
      - name: DOCKER_SECRET_AUTH
        valueFrom:
          secretKeyRef:
            name: docker-token
            key: .dockerconfigjson
      - name: S3_SECRET_AUTH
        valueFrom:
          secretKeyRef:
            name: secret-s3
            key: password
  restartPolicy: Never
  terminationGracePeriodSeconds: 18
  volumes:
  - name: docker-graph-storage
    emptyDir: {}
status: {}  
