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

Can not share the data between tasks: alloc folder is not visible for template section #23416

Closed
EugenKon opened this issue Jun 21, 2024 · 5 comments

Comments

@EugenKon
Copy link

Nomad version

v1.8.0

Issue

I am trying to share the data between tasks. Because first task is exec and second task is docker the alloc folder is mounted directly into container https://developer.hashicorp.com/nomad/docs/runtime/environment#task-directories and is not visible to template block of second task.

Reproduction steps

Create two tasks, first one is 'prestart' and stores some data into alloc folder. The second task is a docker and tries to read data from the 'alloc` folder at template section.

Expected Result

'alloc' should be visible from 'template' section for the task of 'docker' type.

Actual Result

The folder is not visible and data could not be shared.

Job file

Job file
job "postgres" {
  region = "xxx"
  datacenters = ["dc"]
  type = "service"

  group "postgres-db" {
    count = 1

    network {
      port "postgres-db" {
        static = 5432
        to     = 5432
      }
    }

    task "prepare-credentials-task" {
      lifecycle {
        hook = "prestart"
        sidecar = false
      }

      driver = "exec"

      config {
        command = "/bin/bash"
        args    = ["local/script.sh"]
      }

      template {
        destination = "local/script.sh"
        data        = <<-EOH
          set -ex

          cat <<CREDS > $NOMAD_ALLOC_DIR/creds.env;
          INITIAL_PASSWORD=${INITIAL_PASSWORD}
          CREDS
        EOH
      }
    }

    task "postgres-task" {
      driver = "docker"

      config {
        force_pull = false
        image = "xxx/userdb:v15"
        ports = ["postgres-db"]
      }

      template {
        env         = true
        destination = "secrets/file.env"        
        # data        = file("$NOMAD_ALLOC_DIR/creds.env")
        data        = <<-EOH
          {{ $file :=  file "$NOMAD_ALLOC_DIR/creds.env" }}
          {{ range $line := split "\n" $file }}
            {{ $parts := split "=" $line }}
            {{ if eq (len $parts) 2 }}
              {{ env (index $parts 0) (index $parts 1) }}
            {{ end }}
          {{ end }}
        EOH
      }
    }
  }
}
@tgross
Copy link
Member

tgross commented Jun 21, 2024

Hi @EugenKon! The environment variable $NOMAD_ALLOC_DIR doesn't exist in the context of the template runner, which is running in the host's environment. You'll need to do something like the following instead:

task "postgres-task" {
  driver = "docker"

  config {
    force_pull = false
    image      = "xxx/userdb:v15"
    ports      = ["postgres-db"]
    command    = "/local/start.sh"
  }

  template {
    env         = true
    destination = "local/start.sh"
    data        = <<-EOH
source $NOMAD_ALLOC_DIR/creds.env
exec /path/to/postgres --arg1 --arg2
EOH
  }
}

@EugenKon
Copy link
Author

This looks like a hack, but thanks.

@EugenKon
Copy link
Author

@tgross How to deal with this if pre-start task fetches/generates template for nginx and we need to restart nginx if service list was changed?

    task "wi-nginx-task" {
      driver = "docker"

      config {
        force_pull = false
        image = "nginx"

        volumes = [
          "custom/xxx.nginx.conf:/tmp/nginx/sites-enabled/xxx",
        ]
      }

      template {
        source = "$NOMAD_ALLOC_DIR/generated"
        destination = "custom/xxx.nginx.conf"
        change_mode = "restart"
      }
    }

@EugenKon
Copy link
Author

It looks like I need artifact { script = XXX } option.

Copy link

I'm going to lock this issue because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Development

No branches or pull requests

2 participants