---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: media
  name: plex-meta-manager
spec:
  suspend: false
  # Run once a day, every day, starting at 3AM
  schedule: "0 4 * * *"
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 1
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          dnsPolicy: None
          dnsConfig:
            nameservers:
            - "${ROUTER_IP}"
          initContainers:
          - name: git-config
            image: alpine/git:v2.32.0
            command:
            - "/bin/sh"
            - "-c"
            - |
              set -o nounset
              set -o errexit
              cp -R -L /root/ssh /root/.ssh
              chmod -R 600 /root/.ssh
              # sleep 600000000m
              git clone --depth 1 "${SECRET_CONFIGS_URL}" /shared/Configs
              echo "Clone Complete"
              echo "Setting Perms"
              chown -R 1000:1000 /shared/Configs
              chmod a+x /shared/Configs/Plex-Meta-Manager/pushGit.sh
              exit 0
            volumeMounts:
            - name: shared
              # Path will be /shared/Configs/Plex-Meta-Manager/config.yml
              mountPath: /shared
            - name: secret-volume
              readOnly: true
              mountPath: "/root/ssh"
          containers:
          - name: plex-meta-manager
            image: meisnate12/plex-meta-manager:latest
            imagePullPolicy: Always
            lifecycle:
              postStart:
                exec:
                  command: ["/bin/sh", "-c", "/shared/Configs/Plex-Meta-Manager/setupGit.sh"]
              preStop:
                exec:
                  command: ["/bin/sh", "-c", "/shared/Configs/Plex-Meta-Manager/pushGit.sh"]
            env:
            - name: PUID
              value: "1000"
            - name: GUID
              value: "1000"
            - name: TZ
              value: "America/Los_Angeles"
            # - name: PMM_DEBUG
            - name: PMM_TEST
              value: "false"
            - name: PMM_RUN
              value: "true"
            # - name: PMM_COLLECTIONS
            - name: PMM_TIME
              value: "14:00,05:18"
            - name: PMM_CONFIG
              value: "/shared/Configs/Plex-Meta-Manager/config.yml"
            volumeMounts:
            - mountPath: /volume1/Media
              name: media-pvc
            - mountPath: /shared
              name: shared
            - name: secret-volume
              readOnly: true
              mountPath: "/root/ssh"
            resources:
              requests:
                memory: 500Mi
                cpu: 100m
              limits:
                memory: 2000Mi
                cpu: 500m
          volumes:
          - name: media-pvc
            nfs:
              server: "${NAS_ADDR}"
              path: /mnt/NFS/Merged-Media
          - name: shared
            emptyDir: {}
          - name: secret-volume
            secret:
              secretName: plex-meta-manager-sshkey