#!/usr/bin/env bash log_msg() { echo -e "[ ${HOSTNAME} ] [ $(date -u +"%Y-%m-%dT%H:%M:%SZ") ] $*" >&2; } log_msg "Installing nomad"; sudo apt-get update; sudo apt-get install --yes hashicorp-nomad; log_msg "nomad installed successfully. Version:"; nomad --version; log_msg "Creating directories"; if [[ ! -d "/etc/nomad" ]]; then sudo mkdir /etc/nomad; sudo chown root:root /etc/nomad; fi if [[ ! -d "/srv/nomad" ]]; then sudo mkdir /srv/nomad; sudo chown root:root /srv/nomad; sudo chmod 0750 /srv/nomad; fi check_cgroups_memory() { log_msg "Checking memory cgroups"; cgroups_enabled="$(awk '/memory/ { print $2 }' < /proc/cgroups)"; if [[ "${cgroups_enabled}" -ne 0 ]]; then log_msg "memory cgroups already enabled"; return 0; fi filepath_cmdline="/boot/cmdline.txt"; if [[ ! -e "${filepath_cmdline}" ]]; then filepath_cmdline="/boot/firmware/cmdline.txt"; fi if [[ ! -e "${filepath_cmdline}" ]]; then log_msg "Failed to find cmdline.txt; can't check for cgroups"; return 1; fi if grep -q cgroup_enable=memory /boot/cmdline.txt; then log_msg "memory cgroups already present in cmdline.txt, a reboot is required to apply the update"; return 0; fi log_msg "memory cgroups not present in cmdline.txt, enabling...."; (tr -d '\n' <"${filepath_cmdline}" && echo " cgroup_enable=memory cgroup_memory=1") | sudo tee "${filepath_cmdline}.new"; sudo mv "${filepath_cmdline}" "${filepath_cmdline}.old-$(date +"%Y-%m-%d")"; sudo mv "${filepath_cmdline}.new" "${filepath_cmdline}"; log_msg "New contents of cmdline.txt:"; cat "${filepath_cmdline}"; log_msg "A reboot is required to apply the changes."; } check_cgroups_memory;