# XXX This is broken because our process for creating a new db
# seems not to be maintained for new systems.
# Apparently https://github.com/18F/identity-idp/blob/master/bin/setup#L54-L56
# is the way to do it, and I did it by hand and it had errors but it
# worked in the end.
apiVersion: batch/v1
kind: Job
metadata:
  name: idp-dbcreate
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
      - name: idp-dbcreate
        image: gsatspencer/identity-idp
        command: ['/bin/sh']
        args: ['-c', '/usr/local/bin/bundle exec rake db:create ; /usr/local/bin/bundle exec rake db:environment:set ; /usr/local/bin/bundle exec rake db:reset /usr/local/bin/bundle exec rake db:environment:set ; /usr/local/bin/bundle exec rake dev:prime ; /usr/local/bin/bundle exec rake parallel:setup ; /usr/local/bin/bundle exec rake db:migrate']
        env:
        - name: RAILS_ENV
          value: "production"
        - name: NEW_RELIC_AGENT_ENABLED
          value: "false"
        - name: database_host
          valueFrom:
            configMapKeyRef:
              name: idp-config
              key: db_hostname
        - name: domain_name
          valueFrom:
            configMapKeyRef:
              name: idp-config
              key: domain_name
        - name: mailer_domain_name
          valueFrom:
            configMapKeyRef:
              name: idp-config
              key: mailer_domain_name
        - name: asset_host
          valueFrom:
            configMapKeyRef:
              name: idp-config
              key: mailer_domain_name
        - name: MIGRATION_STATEMENT_TIMEOUT
          value: "600000"
        - name: SAFETY_ASSURED
          value: "1"
        volumeMounts:
        - name: appconfig
          mountPath: "/appconfig"
          readOnly: true
        - name: certs
          mountPath: "/upaya/certs"
          readOnly: true
        - name: keys
          mountPath: "/upaya/keys"
          readOnly: true
        lifecycle:
          postStart:
            exec:
              command:
                - /bin/sh
                - -c
                - |
                  /bin/bash <<'EOF'
                  # This copies in all of the secrets/config since k8s can't mount files like docker does
                  # Ideally, config would be passed in as env vars, but that seems like a PITA to translate over.
                  cp /appconfig/application.yml /upaya/config/
                  cp /appconfig/agencies.yml /upaya/config/
                  cp /appconfig/service_providers.yml /upaya/config/
                  cp /appconfig/pwned_passwords.txt /upaya/pwned_passwords/
                  EOF
      volumes:
      - name: appconfig
        secret:
          secretName: appconfig
      - name: certs
        secret:
          secretName: certs
      - name: keys
        secret:
          secretName: keys
