apiVersion: batch/v1
kind: Job
metadata:
  name: guacamole-mysql-init
  labels:
    job: guacamole-mysql-init
spec:
  template:
    spec:
      restartPolicy: OnFailure
      imagePullSecrets:
        - name: registry.internal.aleemhaji.com
      initContainers:
        - name: guacamole-mysql-init-script-creator
          image: registry.internal.aleemhaji.com/guacamole:1.3.0
          command:
            - sh
            - -c
            - /opt/guacamole/bin/initdb.sh --mysql > /scripts/init.sql
          volumeMounts:
            - name: init-scripts
              mountPath: /scripts
      containers:
        - name: guacamole-mysql-init
          image: registry.internal.aleemhaji.com/mysql:5.7.26
          command:
            - sh
            - -ce
            - |
              mysql -h ${MYSQL_HOST} -u root -e " \
                CREATE USER IF NOT EXISTS ${GUACAMOLE_DB_USER}@'10.244.%.%'; \
                SET PASSWORD FOR ${GUACAMOLE_DB_USER}@'10.244.%.%' = PASSWORD('${GUACAMOLE_DB_PASSWORD}'); \
                CREATE DATABASE IF NOT EXISTS ${GUACAMOLE_DB_NAME}; \
                GRANT ALL ON $${GUACAMOLE_DB_NAME}.* TO $${GUACAMOLE_DB_USER}@'10.244.%.%';"
              # The Guacamole init job isn't idempotent, so check for the
              #   presence of the first table it errors out on and exit if it
              #   already exists.
              if mysql -h ${MYSQL_HOST} -u root guacamole -e 'select 1 from guacamole_connection_group'; then
                echo >&2 "Guacamole MySQL database it probably already initialized."
                exit 0
              fi
              mysql -h ${MYSQL_HOST} -u root guacamole < /scripts/init.sql
          env:
            - name: MYSQL_PWD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: root_password
            - name: MYSQL_HOST
              value: mysql
            - name: GUACAMOLE_DB_USER
              valueFrom:
                configMapKeyRef:
                  name: guacamole-config
                  key: mysql_user
            - name: GUACAMOLE_DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: guacamole-config
                  key: mysql_database
            - name: GUACAMOLE_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: guacamole-secrets
                  key: mysql_password
          volumeMounts:
            - name: init-scripts
              mountPath: /scripts
      volumes:
        - name: init-scripts
          emptyDir: {}
