---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "nim-llm.fullname" . }}-scripts-configmap
  labels:
    {{- include "nim-llm.labels" . | nindent 4 }}
data:
  ngc_pull.sh: |-
{{ .Files.Get "files/ngc_pull.sh" | indent 4 }}
{{ if .Values.multiNode.enabled }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "nim-llm.fullname" . }}-lws-mpi-config
  labels:
    {{- include "nim-llm.labels" . | nindent 4 }}
data:
  {{- range $i := untilStep 0 (int (include "nim-llm.totalMaxReplicas" .)) 1 }}
  hostfile-{{ $i }}: |
    localhost slots={{ $.Values.multiNode.gpusPerNode }}
    {{- range $j := untilStep 1 (int $.Values.multiNode.workers) 1 }}
    {{ include "nim-llm.fullname.multinode" $ }}-{{ toString $i }}-{{ toString $j }}.{{ include "nim-llm.fullname.multinode" $ }}.{{ $.Release.Namespace }}.svc slots={{ $.Values.multiNode.gpusPerNode }}
    {{- end }}
  {{- end }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "nim-llm.fullname" . }}-mpi-start-script
  labels:
    {{- include "nim-llm.labels" . | nindent 4 }}
data:
  start-mpi-cluster.sh: |-
    #!/bin/bash

    NIM_JSONL_LOGGING="${NIM_JSONL_LOGGING:-0}"
    if [ "${NIM_JSONL_LOGGING}" -eq "1" ] && /opt/nim/llm/.venv/bin/python3 -c "import nim_llm_sdk.logging.pack_all_logs_into_json" 2> /dev/null; then
      /opt/nim/llm/.venv/bin/python3 -m nim_llm_sdk.entrypoints.openai.api_server |& /opt/nim/llm/.venv/bin/python3 -m nim_llm_sdk.logging.pack_all_logs_into_json
    else
      /opt/nim/llm/.venv/bin/python3 -m nim_llm_sdk.entrypoints.openai.api_server
    fi

{{ end }}
