How to deal with multiple files in Template stanza

Hi,

I am writing a raw_exec task running a program that requires several specific files in a folder named ‘resources’ that must be present in the current working directory.

My task configuration goes:

task "my-task" {
    driver = "raw_exec"
    config {
        command = "/path/to/program/binary"
    }

    template {
        source="/path/to/program/resources/file1"
        destination="resources/file1"
    }

    template {
        source="/path/to/program/resources/file2"
        destination="resources/file2"
    }

    ...

    template {
        source="/path/to/program/resources/fileN"
        destination="resources/fileN"
    }
}

This is working fine (with disable_file_sandbox = true for the client), however the number of files is rather large.

I tried replacing individual template blocks file1…fileN with the following:

template {
    source="/path/to/program/resources/"
    destination="resources/"
}

Which is giving me the error:
Task hook failed template: failed to read template: read /path/to/program/resources: is a directory

Can anyone suggest an approach for what I’m trying to do ?

Thanks,
rema

1 Like

can you try emitting the “command” as a template shell file?
In this shell file you can add all the commands that you want (cp -Rp, etc.).

The last line of the script would be which executes /path/to/program/binary

example code here:

if you ever do need to download and extract files, also keep an eye on:

HTH,
:slight_smile:

1 Like

Thanks Shantanu, this worked beautifully !

In case this helps anyone, I ended up doing:

task "my-task" {
  driver = "raw_exec"

  template {
    data = <<EOH
#!/bin/bash
PACKAGE_DIR=/path/to/program
cp -pr ${PACKAGE_DIR}/resources/ local/resources/
cd local/
${PACKAGE_DIR}/binary
EOH
    destination = "local/runner.bash"
  }

  config {
    command = "/bin/bash"
    args    = ["local/runner.bash"]
  }
}
1 Like

Starting with Nomad 1.0.0, you can also use dynamic blocks to generate these, so something like:

job "example" {
  datacenters = ["dc1"]

  group "group" {
    task "task" {
      driver = "raw_exec"

      config {
        command = "ls"
        args    = ["local"]
      }

      dynamic "template" {
        for_each = range(1, 100)

        content {
          source      = "/path/to/templates/${template.value}"
          destination = "local/${template.value}"
        }
      }
    }
  }
}

Good to know, thanks !

1 Like