github.com/hernad/nomad@v1.6.112/e2e/lifecycle/inputs/batch.nomad (about)

     1  # Copyright (c) HashiCorp, Inc.
     2  # SPDX-License-Identifier: MPL-2.0
     3  
     4  # lifecycle hook test job for batch jobs. touches, removes, and tests
     5  # for the existence of files to assert the order of running tasks.
     6  # all tasks should exit 0 and the alloc dir should contain the following
     7  # files: ./init-ran, ./main-ran, ./poststart-run
     8  
     9  job "batch-lifecycle" {
    10  
    11    datacenters = ["dc1"]
    12  
    13    type = "batch"
    14  
    15    constraint {
    16      attribute = "${attr.kernel.name}"
    17      value     = "linux"
    18    }
    19  
    20    group "test" {
    21  
    22      task "init" {
    23  
    24        lifecycle {
    25          hook = "prestart"
    26        }
    27  
    28        driver = "docker"
    29  
    30        config {
    31          image   = "busybox:1"
    32          command = "/bin/sh"
    33          args    = ["local/prestart.sh"]
    34        }
    35  
    36        template {
    37          data = <<EOT
    38  #!/bin/sh
    39  sleep 1
    40  touch ${NOMAD_ALLOC_DIR}/init-ran
    41  touch ${NOMAD_ALLOC_DIR}/init-running
    42  sleep 5
    43  if [ -f ${NOMAD_ALLOC_DIR}/main ]; then exit 7; fi
    44  if [ -f ${NOMAD_ALLOC_DIR}/poststart-running ]; then exit 8; fi
    45  rm ${NOMAD_ALLOC_DIR}/init-running
    46  EOT
    47  
    48          destination = "local/prestart.sh"
    49  
    50        }
    51  
    52        resources {
    53          cpu    = 64
    54          memory = 64
    55        }
    56      }
    57  
    58      task "main" {
    59  
    60        driver = "docker"
    61  
    62        config {
    63          image   = "busybox:1"
    64          command = "/bin/sh"
    65          args    = ["local/main.sh"]
    66        }
    67  
    68        template {
    69          data = <<EOT
    70  #!/bin/sh
    71  sleep 1
    72  touch ${NOMAD_ALLOC_DIR}/main-running
    73  touch ${NOMAD_ALLOC_DIR}/main-started
    74  # NEED TO HANG AROUND TO GIVE POSTSTART TIME TO GET STARTED
    75  sleep 10
    76  if [ ! -f ${NOMAD_ALLOC_DIR}/init-ran ]; then exit 9; fi
    77  if [ -f ${NOMAD_ALLOC_DIR}/init-running ]; then exit 10; fi
    78  
    79  if [ ! -f ${NOMAD_ALLOC_DIR}/poststart-started ]; then exit 11; fi
    80  
    81  touch ${NOMAD_ALLOC_DIR}/main-ran
    82  rm ${NOMAD_ALLOC_DIR}/main-running
    83  EOT
    84  
    85          destination = "local/main.sh"
    86        }
    87  
    88        resources {
    89          cpu    = 64
    90          memory = 64
    91        }
    92      }
    93  
    94  
    95      task "poststart" {
    96  
    97        lifecycle {
    98          hook = "poststart"
    99        }
   100  
   101        driver = "docker"
   102  
   103        config {
   104          image   = "busybox:1"
   105          command = "/bin/sh"
   106          args    = ["local/poststart.sh"]
   107        }
   108  
   109        template {
   110          data = <<EOT
   111  #!/bin/sh
   112  sleep 1
   113  touch ${NOMAD_ALLOC_DIR}/poststart-ran
   114  touch ${NOMAD_ALLOC_DIR}/poststart-running
   115  touch ${NOMAD_ALLOC_DIR}/poststart-started
   116  sleep 10
   117  
   118  if [ ! -f ${NOMAD_ALLOC_DIR}/init-ran ]; then exit 12; fi
   119  if [ ! -f ${NOMAD_ALLOC_DIR}/main-started ]; then exit 15; fi
   120  if [ -f ${NOMAD_ALLOC_DIR}/init-running ]; then exit 14; fi
   121  rm ${NOMAD_ALLOC_DIR}/poststart-running
   122  EOT
   123  
   124          destination = "local/poststart.sh"
   125        }
   126  
   127        resources {
   128          cpu    = 64
   129          memory = 64
   130        }
   131      }
   132  
   133      task "poststop" {
   134  
   135        lifecycle {
   136          hook = "poststop"
   137        }
   138  
   139        driver = "docker"
   140  
   141        config {
   142          image   = "busybox:1"
   143          command = "/bin/sh"
   144          args    = ["local/poststop.sh"]
   145        }
   146  
   147        template {
   148          data = <<EOT
   149  #!/bin/sh
   150  sleep 1
   151  touch ${NOMAD_ALLOC_DIR}/poststop-ran
   152  touch ${NOMAD_ALLOC_DIR}/poststop-running
   153  touch ${NOMAD_ALLOC_DIR}/poststop-started
   154  sleep 5
   155  
   156  if [ ! -f ${NOMAD_ALLOC_DIR}/init-ran ]; then exit 12; fi
   157  if [ ! -f ${NOMAD_ALLOC_DIR}/main-started ]; then exit 15; fi
   158  if [ -f ${NOMAD_ALLOC_DIR}/init-running ]; then exit 14; fi
   159  if [ -f ${NOMAD_ALLOC_DIR}/main-running ]; then exit 17; fi
   160  rm ${NOMAD_ALLOC_DIR}/poststop-running
   161  EOT
   162  
   163          destination = "local/poststop.sh"
   164        }
   165  
   166        resources {
   167          cpu    = 64
   168          memory = 64
   169        }
   170      }
   171    }
   172  }