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

     1  # Copyright (c) HashiCorp, Inc.
     2  # SPDX-License-Identifier: MPL-2.0
     3  
     4  # lifecycle hook test job for service jobs. touches, removes, and tests
     5  # for the existence of files to assert the order of running tasks.
     6  # after stopping, the alloc dir should contain the following files:
     7  # files: ./init-ran, ./sidecar-ran, ./main-ran, ./poststart-run but not
     8  # the ./main-running, ./sidecar-running, or ./poststart-running files
     9  
    10  job "service-lifecycle" {
    11  
    12    datacenters = ["dc1"]
    13  
    14    type = "service"
    15  
    16    constraint {
    17      attribute = "${attr.kernel.name}"
    18      value     = "linux"
    19    }
    20  
    21    group "test" {
    22  
    23      task "init" {
    24  
    25        lifecycle {
    26          hook = "prestart"
    27        }
    28  
    29        driver = "docker"
    30  
    31        config {
    32          image   = "busybox:1"
    33          command = "/bin/sh"
    34          args    = ["local/prestart.sh"]
    35        }
    36  
    37        template {
    38          data = <<EOT
    39  #!/bin/sh
    40  sleep 1
    41  touch ${NOMAD_ALLOC_DIR}/init-ran
    42  touch ${NOMAD_ALLOC_DIR}/init-running
    43  if [ -f ${NOMAD_ALLOC_DIR}/main ]; then exit 7; fi
    44  if [ -f ${NOMAD_ALLOC_DIR}/poststart ]; 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 "sidecar" {
    59  
    60        lifecycle {
    61          hook    = "prestart"
    62          sidecar = true
    63        }
    64  
    65        driver = "docker"
    66  
    67        config {
    68          image   = "busybox:1"
    69          command = "/bin/sh"
    70          args    = ["local/sidecar.sh"]
    71        }
    72  
    73        template {
    74          data = <<EOT
    75  #!/bin/sh
    76  touch ${NOMAD_ALLOC_DIR}/sidecar-ran
    77  touch ${NOMAD_ALLOC_DIR}/sidecar-running
    78  sleep 5
    79  if [ ! -f ${NOMAD_ALLOC_DIR}/main-running ]; then exit 9; fi
    80  if [ -f ${NOMAD_ALLOC_DIR}/poststart-running ]; then exit 10; fi
    81  sleep 300
    82  EOT
    83  
    84          destination = "local/sidecar.sh"
    85  
    86        }
    87  
    88        resources {
    89          cpu    = 64
    90          memory = 64
    91        }
    92      }
    93  
    94      task "main" {
    95  
    96        driver = "docker"
    97  
    98        config {
    99          image   = "busybox:1"
   100          command = "/bin/sh"
   101          args    = ["local/main.sh"]
   102        }
   103  
   104        template {
   105          data = <<EOT
   106  #!/bin/sh
   107  
   108  function cleanup() {
   109    echo stopping
   110    rm ${NOMAD_ALLOC_DIR}/main-running
   111    exit
   112  }
   113  
   114  touch ${NOMAD_ALLOC_DIR}/main-ran
   115  touch ${NOMAD_ALLOC_DIR}/main-running
   116  touch ${NOMAD_ALLOC_DIR}/main-started
   117  if [ ! -f ${NOMAD_ALLOC_DIR}/init-ran ]; then exit 11; fi
   118  if [ -f ${NOMAD_ALLOC_DIR}/init-running ]; then exit 12; fi
   119  if [ ! -f ${NOMAD_ALLOC_DIR}/sidecar-ran ]; then exit 13; fi
   120  if [ ! -f ${NOMAD_ALLOC_DIR}/sidecar-running ]; then exit 14; fi
   121  sleep 2
   122  if [ ! -f ${NOMAD_ALLOC_DIR}/poststart-started ]; then exit 15; fi
   123  touch ${NOMAD_ALLOC_DIR}/main-checked
   124  
   125  echo trap
   126  trap cleanup SIGTERM
   127  
   128  echo sleep
   129  while true
   130  do
   131    sleep 1
   132  done
   133  
   134  
   135  EOT
   136  
   137          destination = "local/main.sh"
   138        }
   139  
   140        resources {
   141          cpu    = 64
   142          memory = 64
   143        }
   144      }
   145  
   146  
   147      task "poststart" {
   148  
   149        lifecycle {
   150          hook = "poststart"
   151        }
   152  
   153        driver = "docker"
   154  
   155        config {
   156          image   = "busybox:1"
   157          command = "/bin/sh"
   158          args    = ["local/poststart.sh"]
   159        }
   160  
   161        template {
   162          data = <<EOT
   163  #!/bin/sh
   164  touch ${NOMAD_ALLOC_DIR}/poststart-ran
   165  touch ${NOMAD_ALLOC_DIR}/poststart-running
   166  touch ${NOMAD_ALLOC_DIR}/poststart-started
   167  if [ ! -f ${NOMAD_ALLOC_DIR}/init-ran ]; then exit 16; fi
   168  if [ -f ${NOMAD_ALLOC_DIR}/init-running ]; then exit 17; fi
   169  if [ ! -f ${NOMAD_ALLOC_DIR}/sidecar-ran ]; then exit 18; fi
   170  if [ ! -f ${NOMAD_ALLOC_DIR}/sidecar-running ]; then exit 19; fi
   171  if [ ! -f ${NOMAD_ALLOC_DIR}/main-started ]; then exit 20; fi
   172  rm ${NOMAD_ALLOC_DIR}/poststart-running
   173  EOT
   174  
   175          destination = "local/poststart.sh"
   176        }
   177  
   178        resources {
   179          cpu    = 64
   180          memory = 64
   181        }
   182      }
   183  
   184      task "poststop" {
   185  
   186        lifecycle {
   187          hook = "poststop"
   188        }
   189  
   190        driver = "docker"
   191  
   192        config {
   193          image   = "busybox:1"
   194          command = "/bin/sh"
   195          args    = ["local/poststop.sh"]
   196        }
   197  
   198        template {
   199          data = <<EOT
   200  #!/bin/sh
   201  sleep 1
   202  touch ${NOMAD_ALLOC_DIR}/poststop-ran
   203  touch ${NOMAD_ALLOC_DIR}/poststop-running
   204  touch ${NOMAD_ALLOC_DIR}/poststop-started
   205  sleep 5
   206  
   207  if [ ! -f ${NOMAD_ALLOC_DIR}/init-ran ]; then exit 12; fi
   208  if [ ! -f ${NOMAD_ALLOC_DIR}/main-started ]; then exit 15; fi
   209  if [ -f ${NOMAD_ALLOC_DIR}/init-running ]; then exit 14; fi
   210  if [ -f ${NOMAD_ALLOC_DIR}/main-running ]; then exit 17; fi
   211  rm ${NOMAD_ALLOC_DIR}/poststop-running
   212  EOT
   213  
   214          destination = "local/poststop.sh"
   215        }
   216  
   217        resources {
   218          cpu    = 64
   219          memory = 64
   220        }
   221      }
   222  
   223    }
   224  }