@@ -61,6 +61,32 @@ type FlavorResolution struct {
6161 SlurmFlags []string
6262}
6363
64+ func extractHeredoc (content , marker string ) (string , error ) {
65+ // Find the start of the heredoc
66+ startPattern := fmt .Sprintf ("cat <<'%s'" , marker )
67+ startIdx := strings .Index (content , startPattern )
68+ if startIdx == - 1 {
69+ return "" , fmt .Errorf ("heredoc start marker not found" )
70+ }
71+
72+ // Find the line after the cat command (start of actual content)
73+ contentStart := strings .Index (content [startIdx :], "\n " )
74+ if contentStart == - 1 {
75+ return "" , fmt .Errorf ("invalid heredoc format" )
76+ }
77+ contentStart += startIdx + 1
78+
79+ // Find the end marker
80+ endMarker := "\n " + marker
81+ endIdx := strings .Index (content [contentStart :], endMarker )
82+ if endIdx == - 1 {
83+ return "" , fmt .Errorf ("heredoc end marker not found" )
84+ }
85+
86+ // Extract the content between start and end markers
87+ return content [contentStart : contentStart + endIdx ], nil
88+ }
89+
6490// stringToHex encodes the provided str string into a hex string and removes all trailing redundant zeroes to keep the output more compact
6591func stringToHex (str string ) string {
6692 var buffer bytes.Buffer
@@ -955,7 +981,39 @@ func produceSLURMScript(
955981 }
956982
957983 if preExecAnnotations , ok := metadata .Annotations ["slurm-job.vk.io/pre-exec" ]; ok {
958- prefix += "\n " + preExecAnnotations
984+ // Check if pre-exec contains a heredoc that creates mesh.sh
985+ if strings .Contains (preExecAnnotations , "cat <<'EOFMESH' > $TMPDIR/mesh.sh" ) {
986+ // Extract the heredoc content
987+ meshScript , err := extractHeredoc (preExecAnnotations , "EOFMESH" )
988+ if err == nil && meshScript != "" {
989+
990+ meshPath := filepath .Join (path , "mesh.sh" )
991+ err := os .WriteFile (meshPath , []byte (meshScript ), 0755 )
992+ if err != nil {
993+ // Fallback: include the full pre-exec as-is
994+ prefix += "\n " + preExecAnnotations
995+ } else {
996+ // Successfully wrote mesh.sh, just add the execution command
997+ //prefix += "\n" + fmt.Sprintf("source %s", meshPath)
998+ prefix += "\n " + fmt .Sprintf (" %s" , meshPath )
999+ }
1000+
1001+ err = os .Chmod (path + "/mesh.sh" , 0774 )
1002+ if err != nil {
1003+ log .G (Ctx ).Error ("Unable to chmod file " , path , "/job.sh" )
1004+ log .G (Ctx ).Error (err )
1005+ return "" , err
1006+ } else {
1007+ log .G (Ctx ).Debug ("--- Created with correct permission file " , path , "/job.sh" )
1008+ }
1009+ } else {
1010+ // Could not extract heredoc, include as-is
1011+ prefix += "\n " + preExecAnnotations
1012+ }
1013+ } else {
1014+ // No heredoc pattern, include pre-exec as-is
1015+ prefix += "\n " + preExecAnnotations
1016+ }
9591017 }
9601018
9611019 sbatch_macros := "#!" + config .BashPath +
0 commit comments