Created
June 30, 2015 11:36
-
-
Save mdaffin/17a22fab2722e506705e to your computer and use it in GitHub Desktop.
consul install script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
function required() { | |
hash "${1}" 2>/dev/null || { echo >&2 "${1} is required but is not installed. Aborting."; exit 1; } | |
} | |
required wget | |
required unzip | |
required mktemp | |
required id | |
required useradd | |
CONSUL_CONFIG=${1:-} | |
VERSION="${VERSION:-0.5.2}" | |
ARCH="$(uname -m)" | |
case "${ARCH}" in | |
"x86_64") | |
ARCH="amd64" | |
;; | |
*) | |
echo >&2 "Unsupported architecture: ${ARCH}" | |
exit 1 | |
;; | |
esac | |
ZIP_NAME="${VERSION}_linux_${ARCH}.zip" | |
URL="https://dl.bintray.com/mitchellh/consul/${ZIP_NAME}" | |
DLDIR=$(mktemp --directory -t tmp.XXXXXXXXXX) | |
function finish { | |
rm -rf "${DLDIR}" | |
} | |
trap finish EXIT | |
ZIP="${DLDIR}/${ZIP_NAME}" | |
wget --quiet -O "${ZIP}" "${URL}" | |
unzip -q "${ZIP}" -d "${DLDIR}" | |
id consul > /dev/null 2>&1 || useradd --no-create-home --system --shell "/bin/nologin" --user-group consul | |
install -D --mode 0755 "${DLDIR}/consul" "/usr/local/bin/consul" | |
install --directory --mode 0755 --owner consul --group consul "/var/lib/consul" | |
install --directory --mode 0755 "/etc/consul.d" | |
if [[ -z "${CONSUL_CONFIG}" ]]; then | |
[[ -f "/etc/consul.conf" ]] || cat <<-EOF > "/etc/consul.conf" | |
{ | |
"server": true, | |
"data_dir": "/var/lib/consul", | |
} | |
EOF | |
else | |
install -D --mode 0644 "${CONSUL_CONFIG}" "/etc/consul.conf" | |
fi | |
# TODO install service file | |
if hash systemctl 2>/dev/null; then | |
cat <<-EOF > /etc/systemd/system/consul.service | |
[Unit] | |
Description=Consul Agent | |
Wants=basic.target | |
After=basic.target network.target | |
[Service] | |
User=consul | |
Group=consul | |
Environment="GOMAXPROCS=2" | |
ExecStart=/usr/local/bin/consul agent -config-file=/etc/consul.conf -config-dir=/etc/consul.d | |
ExecReload=/bin/kill -HUP \$MAINPID | |
KillMode=process | |
Restart=on-failure | |
RestartSec=42s | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
else | |
echo "Unknown init system, service not installed" 1>&2 | |
fi | |
echo "Finished installing consul." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment