apiVersion: batch/v1
kind: Job
metadata:
  name: usagecollector-repayg-precheck
spec:
  template:
    spec:
      containers:
        - name: usagecollector-repayg-precheck
          image: ${repo}/usagecollector-backend.com.automation.usagecollector:${usage_collector_version}
          command: ["/bin/sh", "-c"]
          args:
            - |
              printf '%s' '
              JBOSS_HOME=/opt/jboss/wildfly
              JBOSS_CLI=$JBOSS_HOME/bin/jboss-cli.sh
              JBOSS_MODE="standalone"
              JBOSS_CONFIG="standalone-full.xml"

              function wait_for_server() {
                until `$JBOSS_CLI -c "ls /deployment" &> /dev/null`; do
                  echo "=> Sleeping for 1 second..."
                  sleep 1
                done
                echo "=> Server ready"
              }

              echo "  ROOT: INFO
                com.automation.usagecollector: INFO
                com.ingrammicro.imcp.library.components.trace.Tracer: TRACE
              " > /opt/log.properties

              echo "=> Starting WildFly server"
              $JBOSS_HOME/bin/$JBOSS_MODE.sh -c $JBOSS_CONFIG> /dev/null &

              echo "=> Waiting for the server to boot"
              wait_for_server

              echo "=> Probing..."
              RESPONSE=$(curl -k --write-out '%{http_code}' --silent --output /dev/null https://127.0.0.1:8081/rest/application/startupProbe)
              if [ "$RESPONSE" != "200" ]; then
                if [ "$RESPONSE" = "417" ]; then
                  echo "=> The system is not ready for Usage Collector, please handle problems from the log below:"
                  grep "ejb.MigrationManagerBean" $JBOSS_HOME/standalone/log/server.log
                else
                  echo "=> Problem running precheck, please investigate the job log"
                  echo "=> Fetching server logs..."
                  cat $JBOSS_HOME/standalone/log/server.log
                fi
              else
                  echo "=> Precheck successful, the system is ready for Usage Collector"
              fi

              echo "=> Shutting down WildFly"
              if [ "$JBOSS_MODE" = "standalone" ]; then
                $JBOSS_CLI -c ":shutdown"
              else
                $JBOSS_CLI -c "/host=*:shutdown"
              fi
              ' > /tmp/standalone_startup_probe.sh && cat /tmp/standalone_startup_probe.sh && sh /tmp/standalone_startup_probe.sh && rm /tmp/standalone_startup_probe.sh
          imagePullPolicy: "IfNotPresent"
          envFrom:
            - configMapRef:
                name: a8n-config
                optional: true
          env:
            - name: OAUTH_KEY
              value: "72f4b6e9-3fcb-46b5-8757-1ad62f1a0265"
            - name: OAUTH_SECRET
              value: "a8c360a7-f344-4949-a0f8-e6fdac73fcc1"
            - name: LOGFILE_PATH
              value: "/opt/"
            - name: JAVA_HEAP_SIZE
              value: "-XX:InitialRAMPercentage=30.0 -XX:MaxRAMPercentage=70.0"
            - name: DS_DB_NAME
              valueFrom:
                secretKeyRef:
                  name: ${usage_collector_db_secret}
                  key: dbname
            - name: DS_LOGIN
              valueFrom:
                secretKeyRef:
                  name: ${usage_collector_db_secret}
                  key: username
            - name: DS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${usage_collector_db_secret}
                  key: password
            - name: DS_HOST
              valueFrom:
                secretKeyRef:
                  name: ${usage_collector_db_secret}
                  key: host
            - name: DS_PORT
              value: ${usage_collector_db_port}
            - name: CACHE_SIZE
              value: "5000"
            - name: CHARGES_LIMIT
              value: "5000"
            - name: CHARGE_DATES_VALIDATION
              value: "true"
            - name: MIN_DB_POOL_SIZE
              value: "1"
            - name: MAX_DB_POOL_SIZE
              value: "16"
            - name: JDBC_CONNECTION_PARAMS
              value: "?sslmode=prefer&ApplicationName=usagecollector"
            - name: BSS_DB_NAME
              valueFrom:
                secretKeyRef:
                  name: ${bss_db_secret}
                  key: dbname
            - name: BSS_DB_USER
              valueFrom:
                secretKeyRef:
                  name: ${bss_db_secret}
                  key: username
            - name: BSS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ${bss_db_secret}
                  key: password
            - name: BSS_DB_HOST
              valueFrom:
                secretKeyRef:
                  name: ${bss_db_secret}
                  key: host
            - name: BSS_DB_PORT
              value: ${bss_db_port}
            - name: PLANS_NOT_SWITCHED_TO_RE
              value: ${plans_not_switched_to_re}
          resources:
            requests:
              cpu: "1"
              memory: 1280Mi
            limits:
              cpu: "1"
              memory: 1280Mi

      restartPolicy: Never
      imagePullSecrets:
        - name: a8n-docker-registry
  backoffLimit: 4