Nomad Python Batch Job

I have a python batch job that has two tasks. The first task pulls the github code and runs a pip install, and the second task runs the app. How can I find the correct path for the python app to run. Here is my example job config.

job "example-python" {
  periodic {
    cron             = "0 9 * * 1-5"
    prohibit_overlap = true
  }
  datacenters = ["dc1"]

  type = "batch"

  group "app" {
    count = 1

    task "setup" {
      driver = "raw_exec"

      lifecycle {
        hook    = "prestart"
      }

      config {
        command = "/usr/bin/pip"
        args    = ["install", "-r", "local/repo/requirements.txt"]
      }


      artifact {
        source      = "git@github.com:example/daily_quotes.git"
        destination = "local/repo"
        options {
          sshkey = "xxxx"
        }
      }
    }

    task "run" {
      driver = "raw_exec"

      config {
        command = "/usr/bin/python"
        args    = ["local/repo/app.py"]
      }

    }
  }
}
1 Like

Hi @dusts66!

There are two issues here. The first is that if you were to nomad exec into the run task, you’d see that the Nomad task dir is separate for each task:

$ nomad alloc exec  -task run 49e /bin/sh
# echo $NOMAD_TASK_DIR
/var/nomad/alloc/49e613d4-9d66-d504-f68e-387532f09b1b/run/local

The second is that the task dir isn’t found at /local for raw_exec tasks, but the relative path local used in your template and artifact destination is relative to the task working directory .

If you want to want to have a prestart task install software, I’d have it do something like:

job "example-python" {
  periodic {
    cron             = "0 9 * * 1-5"
    prohibit_overlap = true
  }
  datacenters = ["dc1"]

  type = "batch"

  group "app" {
    count = 1

    task "setup" {
      driver = "raw_exec"

      lifecycle {
        hook    = "prestart"
      }

      config {
        command = "/bin/sh"
        args = ["$NOMAD_TASK_DIR}/prestart.sh"]
      }

      # you could inline this into the args for the command above,
      # but it's a little nicer to read and change in the future
      # if you do it in a template
      template {
        data = <-EOT
#!/bin/sh
/usr/bin/pip install -r ${NOMAD_TASK_DIR}/repo/requirements.txt
mv ${NOMAD_TASK_DIR}/repo ${NOMAD_ALLOC_DIR}/repo
        EOT

        destination = "local/prestart.sh"
      }


      artifact {
        source      = "git@github.com:example/daily_quotes.git"
        destination = "local/repo"
        options {
          sshkey = "xxxx"
        }
      }
    }

    task "run" {
      driver = "raw_exec"

      config {
        command = "/usr/bin/python"
        args    = ["${NOMAD_ALLOC_DIR}/repo/app.py"]
      }

    }
  }
}
3 Likes