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