Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kjobctl] Move unmask_filename to templates. #3180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions cmd/experimental/kjobctl/pkg/builder/slurm_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,6 @@ const (
slurmEnvsPath = "/slurm/env"
slurmSbatchEnvFilename = "sbatch.env"
slurmSlurmEnvFilename = "slurm.env"

//# \\ - Do not process any of the replacement symbols.
//# %% - The character "%".
//# %A - Job array's master job allocation number (for now it is equivalent to SLURM_JOB_ID).
//# %a - Job array ID (index) number (SLURM_ARRAY_TASK_ID).
//# %j - job id of the running job (SLURM_JOB_ID).
//# %N - short hostname (pod name).
//# %n - node(pod) identifier relative to current job - index from K8S index job.
//# %t - task identifier (rank) relative to current job - It is array id position.
//# %u - username (from the client machine).
//# %x - job name.
unmaskFilenameFunction = `unmask_filename () {
replaced="$1"

if [[ "$replaced" == "\\"* ]]; then
replaced="${replaced//\\/}"
echo "${replaced}"
return 0
fi

replaced=$(echo "$replaced" | sed -E "s/(%)(%A)/\1\n\2/g;:a s/(^|[^\n])%A/\1$SLURM_ARRAY_JOB_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%a)/\1\n\2/g;:a s/(^|[^\n])%a/\1$SLURM_ARRAY_TASK_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%j)/\1\n\2/g;:a s/(^|[^\n])%j/\1$SLURM_JOB_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%N)/\1\n\2/g;:a s/(^|[^\n])%N/\1$HOSTNAME/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%n)/\1\n\2/g;:a s/(^|[^\n])%n/\1$JOB_COMPLETION_INDEX/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%t)/\1\n\2/g;:a s/(^|[^\n])%t/\1$SLURM_ARRAY_TASK_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%u)/\1\n\2/g;:a s/(^|[^\n])%u/\1$USER_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%x)/\1\n\2/g;:a s/(^|[^\n])%x/\1$SBATCH_JOB_NAME/;ta;s/\n//g")

replaced="${replaced//%%/%}"

echo "$replaced"
}`
)

var (
Expand Down Expand Up @@ -570,7 +537,6 @@ type slurmEntrypointScript struct {
EnvsPath string
SbatchEnvFilename string
SlurmEnvFilename string
UnmaskFilenameFunction string
ChangeDir string
BuildEntrypointCommand string
}
Expand All @@ -580,7 +546,6 @@ func (b *slurmBuilder) buildEntrypointScript() (string, error) {
EnvsPath: slurmEnvsPath,
SbatchEnvFilename: slurmSbatchEnvFilename,
SlurmEnvFilename: slurmSlurmEnvFilename,
UnmaskFilenameFunction: unmaskFilenameFunction,
ChangeDir: b.changeDir,
BuildEntrypointCommand: b.buildEntrypointCommand(),
}
Expand Down
15 changes: 14 additions & 1 deletion cmd/experimental/kjobctl/pkg/builder/slurm_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ limitations under the License.
package builder

import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
"testing"
"text/template"
"time"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -84,6 +86,17 @@ func TestUnmaskFilenameFunction(t *testing.T) {
return
}

tmpl, err := template.ParseFS(slurmTemplates, "templates/*")
if err != nil {
t.Fatal(err)
}

var unmaskFilenameFunction bytes.Buffer

if err := tmpl.ExecuteTemplate(&unmaskFilenameFunction, "slurm_unmask_filename.sh.tmpl", nil); err != nil {
t.Fatal(err)
}

mbobrovskyi marked this conversation as resolved.
Show resolved Hide resolved
script := fmt.Sprintf(`#!/usr/bin/bash

set -o errexit
Expand All @@ -94,7 +107,7 @@ set -o pipefail

unmask_filename "%s"

`, unmaskFilenameFunction, tc.input)
`, unmaskFilenameFunction.String(), tc.input)

if _, err := file.WriteString(script); err != nil {
t.Error(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ source {{.EnvsPath}}/$JOB_CONTAINER_INDEX/{{.SbatchEnvFilename}}

export $(cat {{.EnvsPath}}/$JOB_CONTAINER_INDEX/{{.SlurmEnvFilename}} | xargs)

{{.UnmaskFilenameFunction}}
{{template "slurm_unmask_filename.sh.tmpl" .}}

input_file=$(unmask_filename "$SBATCH_INPUT")
output_file=$(unmask_filename "$SBATCH_OUTPUT")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{{- /*
\\ - Do not process any of the replacement symbols.
%% - The character "%".
%A - Job array's master job allocation number (for now it is equivalent to SLURM_JOB_ID).
%a - Job array ID (index) number (SLURM_ARRAY_TASK_ID).
%j - job id of the running job (SLURM_JOB_ID).
%N - short hostname (pod name).
%n - node(pod) identifier relative to current job - index from K8S index job.
%t - task identifier (rank) relative to current job - It is array id position.
%u - username (from the client machine).
%x - job name.
*/ -}}
unmask_filename () {
replaced="$1"

if [[ "$replaced" == "\\"* ]]; then
replaced="${replaced//\\/}"
echo "${replaced}"
return 0
fi

replaced=$(echo "$replaced" | sed -E "s/(%)(%A)/\1\n\2/g;:a s/(^|[^\n])%A/\1$SLURM_ARRAY_JOB_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%a)/\1\n\2/g;:a s/(^|[^\n])%a/\1$SLURM_ARRAY_TASK_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%j)/\1\n\2/g;:a s/(^|[^\n])%j/\1$SLURM_JOB_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%N)/\1\n\2/g;:a s/(^|[^\n])%N/\1$HOSTNAME/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%n)/\1\n\2/g;:a s/(^|[^\n])%n/\1$JOB_COMPLETION_INDEX/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%t)/\1\n\2/g;:a s/(^|[^\n])%t/\1$SLURM_ARRAY_TASK_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%u)/\1\n\2/g;:a s/(^|[^\n])%u/\1$USER_ID/;ta;s/\n//g")
replaced=$(echo "$replaced" | sed -E "s/(%)(%x)/\1\n\2/g;:a s/(^|[^\n])%x/\1$SBATCH_JOB_NAME/;ta;s/\n//g")

replaced="${replaced//%%/%}"

echo "$replaced"
}