|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +export CONTAINERD_VERSION=1.3.4 |
| 6 | + |
| 7 | +main() { |
| 8 | + echo "INFO: Welcome! nomad-driver-containerd setup." |
| 9 | + check_root |
| 10 | + check_os |
| 11 | + check_nomad |
| 12 | + check_golang |
| 13 | + |
| 14 | + echo "WARN: Some installation steps are time consuming. Please be patient!" |
| 15 | + |
| 16 | + # Save present working directory (pwd). |
| 17 | + curr_dir=$(echo $PWD) |
| 18 | + |
| 19 | + if systemctl -q is-active "containerd.service"; then |
| 20 | + echo "WARN: Containerd detected on the system." |
| 21 | + read -p "INFO: Backup existing containerd and deploy containerd-${CONTAINERD_VERSION} (Y/N)? Press Y to continue. " -n 1 -r |
| 22 | + echo |
| 23 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 24 | + echo "INFO: Aborting setup..." |
| 25 | + exit 0 |
| 26 | + fi |
| 27 | + systemctl stop containerd |
| 28 | + if [ -f "/lib/systemd/system/containerd.service" ]; then |
| 29 | + echo "INFO: Backup containerd systemd unit /lib/systemd/system/containerd.service." |
| 30 | + mv /lib/systemd/system/containerd.service /lib/systemd/system/containerd.service.bkp |
| 31 | + echo "WARN: Backup file saved at: /lib/systemd/system/containerd.service.bkp" |
| 32 | + fi |
| 33 | + fi |
| 34 | + setup_containerd |
| 35 | + |
| 36 | + read -p "INFO: Setup nomad server + nomad-driver-containerd (Y/N)? Press Y to continue. " -n 1 -r |
| 37 | + echo |
| 38 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 39 | + echo "INFO: Aborting setup..." |
| 40 | + cleanup |
| 41 | + exit 0 |
| 42 | + fi |
| 43 | + echo "INFO: Cleanup any old binaries." |
| 44 | + make clean >/dev/null 2>&1 |
| 45 | + echo "INFO: Build nomad-driver-containerd binary: containerd-driver." |
| 46 | + make build >/dev/null 2>&1 || (cleanup && exit 1) |
| 47 | + echo "INFO: Create plugin-dir for containerd-driver: /tmp/nomad-driver-containerd." |
| 48 | + mkdir -p /tmp/nomad-driver-containerd || (cleanup && exit 1) |
| 49 | + echo "INFO: Move containerd-driver to /tmp/nomad-driver-containerd." |
| 50 | + mv containerd-driver /tmp/nomad-driver-containerd || (cleanup && exit 1) |
| 51 | + drop_nomad_unit_file $curr_dir |
| 52 | + echo "INFO: Reload nomad.service systemd unit." |
| 53 | + systemctl daemon-reload |
| 54 | + echo "INFO: Starting nomad server + nomad-driver-containerd." |
| 55 | + systemctl start nomad || (cleanup && exit 1) |
| 56 | + if ! systemctl -q is-active "nomad.service"; then |
| 57 | + echo "ERROR: nomad.service didn't come up. journalctl -u nomad.service for more info." |
| 58 | + exit 1 |
| 59 | + fi |
| 60 | + echo "INFO: Setup finished successfully." |
| 61 | +} |
| 62 | + |
| 63 | +cleanup() { |
| 64 | + echo "INFO: Starting cleanup." |
| 65 | + pushd $curr_dir >/dev/null 2>&1 |
| 66 | + if [ -f "/lib/systemd/system/containerd.service.bkp" ]; then |
| 67 | + if systemctl -q is-active "containerd.service"; then |
| 68 | + echo "INFO: Stopping containerd." |
| 69 | + systemctl stop containerd.service |
| 70 | + fi |
| 71 | + |
| 72 | + if [ -f "/tmp/containerd.service" ]; then |
| 73 | + echo "INFO: Cleanup /tmp/containerd.service." |
| 74 | + rm -f /tmp/containerd.service |
| 75 | + fi |
| 76 | + |
| 77 | + if [ -f "/lib/systemd/system/containerd.service" ]; then |
| 78 | + echo "INFO: Cleanup: /lib/systemd/system/containerd.service." |
| 79 | + rm -f /lib/systemd/system/containerd.service |
| 80 | + fi |
| 81 | + |
| 82 | + if [ -f "/tmp/containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz" ]; then |
| 83 | + echo "INFO: Cleanup: /tmp/containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz." |
| 84 | + rm -f /tmp/containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz |
| 85 | + fi |
| 86 | + fi |
| 87 | + |
| 88 | + if systemctl -q is-active "nomad.service"; then |
| 89 | + echo "INFO: Stopping nomad server+nomad-driver-containerd." |
| 90 | + systemctl stop nomad |
| 91 | + fi |
| 92 | + |
| 93 | + if [ -f "$curr_dir/nomad.service" ]; then |
| 94 | + echo "INFO: Cleanup $curr_dir/nomad.service." |
| 95 | + rm -f $curr_dir/nomad.service |
| 96 | + fi |
| 97 | + |
| 98 | + if [ -f "/lib/systemd/system/nomad.service" ]; then |
| 99 | + echo "INFO: Cleanup: /lib/systemd/system/nomad.service." |
| 100 | + rm -f /lib/systemd/system/nomad.service |
| 101 | + fi |
| 102 | + |
| 103 | + echo "INFO: Cleanup /tmp/nomad-driver-containerd." |
| 104 | + rm -rf /tmp/nomad-driver-containerd |
| 105 | + |
| 106 | + echo "INFO: Cleanup containerd-driver binary." |
| 107 | + make clean >/dev/null 2>&1 |
| 108 | + popd >/dev/null 2>&1 |
| 109 | + echo "INFO: Cleanup complete." |
| 110 | +} |
| 111 | + |
| 112 | +setup_containerd() { |
| 113 | + read -p "INFO: Download containerd (Y/N)? Press Y to continue. " -n 1 -r |
| 114 | + echo |
| 115 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 116 | + echo "INFO: Aborting setup..." |
| 117 | + cleanup |
| 118 | + exit 0 |
| 119 | + fi |
| 120 | + pushd /tmp >/dev/null 2>&1 |
| 121 | + curl -L --silent -o containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz https://github.com/containerd/containerd/releases/download/v${CONTAINERD_VERSION}/containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz || (cleanup && exit 1) |
| 122 | + tar -C /usr/local -xzf containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz || (cleanup && exit 1) |
| 123 | + rm -f containerd-${CONTAINERD_VERSION}.linux-amd64.tar.gz |
| 124 | + read -p "INFO: Drop systemd unit containerd.service into /lib/systemd/system/containerd.service (Y/N)? Press Y to continue. " -n 1 -r |
| 125 | + echo |
| 126 | + if [[ ! $REPLY =~ ^[Yy]$ ]]; then |
| 127 | + echo "INFO: Aborting setup..." |
| 128 | + cleanup |
| 129 | + exit 0 |
| 130 | + fi |
| 131 | + drop_containerd_unit_file |
| 132 | + echo "INFO: Reload containerd.service systemd unit." |
| 133 | + systemctl daemon-reload |
| 134 | + echo "INFO: Starting containerd daemon." |
| 135 | + systemctl start containerd || (cleanup && exit 1) |
| 136 | + popd >/dev/null 2>&1 |
| 137 | + if ! systemctl -q is-active "containerd.service"; then |
| 138 | + echo "ERROR: containerd.service didn't come up. journalctl -u containerd.service for more info." |
| 139 | + exit 1 |
| 140 | + fi |
| 141 | +} |
| 142 | + |
| 143 | +drop_nomad_unit_file() { |
| 144 | + local nomad=$(which nomad) |
| 145 | + # Drop nomad server (dev) + nomad-driver-containerd systemd unit file into /lib/systemd/system. |
| 146 | + cat << EOF > nomad.service |
| 147 | +# /lib/systemd/system/nomad.service |
| 148 | +[Unit] |
| 149 | +Description=nomad server (dev) + nomad-driver-containerd |
| 150 | +Documentation=https://nomadproject.io |
| 151 | +After=network.target containerd.service |
| 152 | +
|
| 153 | +[Service] |
| 154 | +ExecStart=$nomad agent -dev -config=$1/example/agent.hcl -plugin-dir=/tmp/nomad-driver-containerd |
| 155 | +KillMode=process |
| 156 | +Delegate=yes |
| 157 | +LimitNOFILE=1048576 |
| 158 | +LimitNPROC=infinity |
| 159 | +LimitCORE=infinity |
| 160 | +TasksMax=infinity |
| 161 | +
|
| 162 | +[Install] |
| 163 | +WantedBy=multi-user.target |
| 164 | +EOF |
| 165 | +mv nomad.service /lib/systemd/system/nomad.service |
| 166 | +} |
| 167 | + |
| 168 | +drop_containerd_unit_file() { |
| 169 | + # Drop containerd systemd unit file into /lib/systemd/system. |
| 170 | + cat << EOF > containerd.service |
| 171 | +# /lib/systemd/system/containerd.service |
| 172 | +[Unit] |
| 173 | +Description=containerd container runtime |
| 174 | +Documentation=https://containerd.io |
| 175 | +After=network.target |
| 176 | +
|
| 177 | +[Service] |
| 178 | +ExecStartPre=-/sbin/modprobe overlay |
| 179 | +ExecStart=/usr/local/bin/containerd |
| 180 | +KillMode=process |
| 181 | +Delegate=yes |
| 182 | +LimitNOFILE=1048576 |
| 183 | +# Having non-zero Limit*s causes performance problems due to accounting overhead |
| 184 | +# in the kernel. We recommend using cgroups to do container-local accounting. |
| 185 | +LimitNPROC=infinity |
| 186 | +LimitCORE=infinity |
| 187 | +TasksMax=infinity |
| 188 | +
|
| 189 | +[Install] |
| 190 | +WantedBy=multi-user.target |
| 191 | +EOF |
| 192 | +mv containerd.service /lib/systemd/system/containerd.service |
| 193 | +} |
| 194 | + |
| 195 | +check_golang() { |
| 196 | + set +e |
| 197 | + go version >/dev/null 2>&1 |
| 198 | + rc=$? |
| 199 | + set -e |
| 200 | + if [ $rc -ne 0 ];then |
| 201 | + echo "ERROR: Golang is missing. Please install golang >=1.11 to continue with the setup." |
| 202 | + exit 1 |
| 203 | + fi |
| 204 | +} |
| 205 | + |
| 206 | +check_nomad() { |
| 207 | + set +e |
| 208 | + which nomad >/dev/null 2>&1 |
| 209 | + rc=$? |
| 210 | + set -e |
| 211 | + if [ $rc -ne 0 ];then |
| 212 | + echo "ERROR: Nomad is missing. Please install nomad >=0.11 to continue with the setup." |
| 213 | + exit 1 |
| 214 | + fi |
| 215 | +} |
| 216 | + |
| 217 | +check_root() { |
| 218 | + if [ $(id -u) != 0 ]; then |
| 219 | + echo "ERROR: Run as root user." |
| 220 | + exit 1 |
| 221 | + fi |
| 222 | +} |
| 223 | + |
| 224 | +check_os() { |
| 225 | + set +e |
| 226 | + cat /etc/os-release|grep -q -i "Ubuntu" |
| 227 | + rc=$? |
| 228 | + set -e |
| 229 | + if [ $rc -ne 0 ];then |
| 230 | + echo "ERROR: Unsupported host OS. Run tests on Ubuntu." |
| 231 | + exit 1 |
| 232 | + fi |
| 233 | +} |
| 234 | + |
| 235 | +main "$@" |
0 commit comments