Nomad: expose volume paths as variables for task

Created on 24 Jun 2020  路  7Comments  路  Source: hashicorp/nomad

Nomad version

Server and Client at the same version:
Nomad v0.12.0-beta1 (f8e0623f9fa80d2bc9a28bf260d8ace577d5a17c)

Operating system and Environment details

CentOS 7/8, Ubuntu 18.04

Issue

Using the job file below, I want to get access to the value "/sources/workspace" when I am running the raw_exec task.

I tried to see if it is available as an environment variable, but it doesn't seem to be 馃槥

I know raw_exec doesn't support volume_mount for host volumes, I am just using the group volume stanza for correct allocation placement.

Reproduction steps

run the job to see if the host volume's source directory is somehow accessible as an environment variable.

Job file (if appropriate)

On the agent:

file: /etc/nomad/volume_workspace.hcl

client {
  host_volume "workspace" {
    path = "/sources/workspace"
    read_only = true
  }
}

job definition:

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

  type = "batch"

  group "mygroup" {
    volume "myworkspace" {
      type      = "host"
      source    = "workspace"
      read_only = true
    }

    task "mytask" {
      driver = "raw_exec"

      # volume_mount {
      #  volume      = "myworkspace"
      #  destination = "/myworkspace"
      #}

      template {
        data = <<EOH
#!/bin/bash

set -u

id -a

hostname

env | sort

sleep 30
EOH

        destination = "local/runme.bash"
      }

      config {
        command = "/bin/bash"
        args    = ["-x", "local/runme.bash"]
      }
    }
  }
}

If the source directory is set as an interpolation attribute, that would be fine too, though didn't find any docs around that! 馃槩

stagaccepted themstorage typenhancement

All 7 comments

Hi @shantanugadgil! There's no way to do that currently. I'm inclined to call this a dupe of #7877, but I think this is subtly different, so I'm going to change the name of this issue slightly and turn it into a feature request.

Example of using HCL2 to accomplish this is in https://github.com/hashicorp/nomad/pull/9449

Closed by #9449

@tgross without extra variables, how do I get access to the components of the volume as variables? (from the above example)

is there any section of variable interpolation section for volumes and their paths?

to elaborate ... and to to try out if it works, I changed the raw_exec to docker and added the environment variable of what I would like to get at ...

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

  constraint {
    attribute = "${node.class}"
    value     = "git-builder"
  }

  type = "batch"

  reschedule {
    attempts  = 0
    unlimited = false
  }

  group "mygroup" {

    restart {
      attempts = 0
      mode     = "fail"
    }

    volume "myworkspace" {
      type      = "host"
      source    = "workspace"
      read_only = true
    }

    task "mytask" {
      driver = "docker"

      config {
        image   = "busybox:1"
        command = "/bin/sh"
        args    = ["local/runme.sh"]
      }

      volume_mount {
        volume      = "myworkspace"
        destination = "/myworkspace"
      }

      template {
        data = <<EOH
#!/bin/sh

set -u

uname -a

id -a

hostname

env | sort

sleep 10

echo "Done"
exit 0
EOH

        destination = "local/runme.sh"
      }

      env {
        VOLUME_WORKSPACE = "${meta.volume.workspace}"
        SOMETHING        = "${volume_mount.myworkspace.source}"    ### this does not work as expected
      }

      service {
        name = "host-volume-2"
        tags = ["host-volume-2"]

        meta {
          info = "I am host_volume_2"
        }
      }

    }
  }
}


@tgross without extra variables, how do I get access to the components of the volume as variables?

You can't. That's why the workaround demonstrates using the variables block to populate the fields you need. If you're trying to use this to interpolate the source, see https://github.com/hashicorp/nomad/issues/7877#issuecomment-733803231 for an explanation as to why that won't currently work.

@tgross without extra variables, how do I get access to the components of the volume as variables?

You can't. That's why the workaround demonstrates using the variables block to populate the fields you need. If you're trying to use this to interpolate the source, see #7877 (comment) for an explanation as to why that won't currently work.

OK, got it.
So, for now, my previous hack of having the client add an extra meta variable along with the volume definition itself, makes more "togetherness" sense to be.

FWIW, what I am currently doing (as a workaround hack) is the following:

# "path" and the "meta" variable must match, of course

client {
  host_volume "workspace" {
    path = "/some/path/to/workspace"
    read_only = true
  }

  meta {
    "volume.workspace" = "/some/path/to/workspace"
  }
}


Was this page helpful?
0 / 5 - 0 ratings

Related issues

joliver picture joliver  路  3Comments

Gerrrr picture Gerrrr  路  3Comments

hamann picture hamann  路  3Comments

byronwolfman picture byronwolfman  路  3Comments

DanielDent picture DanielDent  路  3Comments