Last active
October 24, 2021 20:06
-
-
Save shantanugadgil/ea4ee80a1dd5baa9f9594f7ec622e57a to your computer and use it in GitHub Desktop.
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 | |
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 | |
set -u | |
set -e | |
CONSUL_VERSION=${CONSUL_VERSION:-"1.9.4"} | |
NOMAD_VERSION=${NOMAD_VERSION:-"1.0.4"} | |
CNI_PLUGINS_VERSION=${CNI_PLUGINS_VERSION:-"0.9.1"} | |
consul_systemd_file="/etc/systemd/system/consul.service" | |
consul_upstart_file="/etc/init/consul.conf" | |
consul_config_dir="/etc/consul.d" | |
consul_client_config="${consul_config_dir}/client.hcl" | |
nomad_systemd_file="/etc/systemd/system/nomad.service" | |
nomad_upstart_file="/etc/init/nomad.conf" | |
nomad_config_dir="/etc/nomad.d" | |
nomad_client_config="${nomad_config_dir}/client.hcl" | |
vault_client_config="${nomad_config_dir}/vault.hcl" | |
install_consul () | |
{ | |
# binary | |
curl -L -o consul.zip https://releases.hashicorp.com/consul/${CONSUL_VERSION}/consul_${CONSUL_VERSION}_linux_${OS_ARCH}.zip | |
unzip -o consul.zip | |
chown root:root consul | |
chmod 0755 consul | |
mv -fv consul /usr/sbin/ | |
rm -fv consul.zip | |
# service file | |
case "$INIT_SYSTEM" in | |
'systemd') | |
curl -L -o ${consul_systemd_file} https://raw.githubusercontent.com/shantanugadgil/hashistack/master/systemd/consul.service | |
chown root:root ${consul_systemd_file} | |
chmod 0644 ${consul_systemd_file} | |
;; | |
'upstart') | |
curl -L -o ${consul_upstart_file} https://raw.githubusercontent.com/shantanugadgil/hashistack/master/upstart/consul.conf | |
chown root:root ${consul_upstart_file} | |
chmod 0644 ${consul_upstart_file} | |
#initctl reload-configuration | |
#telinit 2 | |
;; | |
esac | |
return 0 | |
} | |
configure_consul_client () | |
{ | |
# client config | |
mkdir -p ${consul_config_dir} | |
curl -L -o ${consul_client_config} https://raw.githubusercontent.com/shantanugadgil/hashistack/master/config/consul/client.hcl | |
chown root:root ${consul_client_config} | |
chmod 0644 ${consul_client_config} | |
sed -i "s/@@NODE_NAME@@/${NODE_NAME}/g" ${consul_client_config} | |
sed -i "s|@@CONSUL_KEY@@|${CONSUL_KEY}|g" ${consul_client_config} | |
sed -i "s/eth0/${NETWORK_INTERFACE}/g" ${consul_client_config} | |
sed -i "s|@@SRV_IP_ADDRESS@@|${SRV_IP_ADDRESS}|g" ${consul_client_config} | |
return 0 | |
} | |
install_nomad () | |
{ | |
# binary | |
curl -L -o nomad.zip https://releases.hashicorp.com/nomad/${NOMAD_VERSION}/nomad_${NOMAD_VERSION}_linux_${OS_ARCH}.zip | |
unzip -o nomad.zip | |
chown root:root nomad | |
chmod 0755 nomad | |
mv -fv nomad /usr/sbin/ | |
rm -fv nomad.zip | |
mkdir -p /var/lib/nomad | |
chmod 0755 /var/lib/nomad | |
# service file | |
case "$INIT_SYSTEM" in | |
'systemd') | |
curl -L https://raw.githubusercontent.com/shantanugadgil/hashistack/master/systemd/nomad.service -o ${nomad_systemd_file} | |
chown root:root ${nomad_systemd_file} | |
chmod 0644 ${nomad_systemd_file} | |
systemctl daemon-reload | |
systemctl enable consul nomad | |
#systemctl restart consul nomad | |
;; | |
'upstart') | |
curl -L https://raw.githubusercontent.com/shantanugadgil/hashistack/master/upstart/nomad.conf -o ${nomad_upstart_file} | |
chown root:root ${nomad_upstart_file} | |
chmod 0644 ${nomad_upstart_file} | |
initctl reload-configuration | |
telinit 2 | |
;; | |
esac | |
return 0 | |
} | |
configure_nomad_client () | |
{ | |
# client config | |
mkdir -p ${nomad_config_dir} | |
curl -L https://raw.githubusercontent.com/shantanugadgil/hashistack/master/config/nomad/client.hcl -o ${nomad_client_config} | |
chown root:root ${nomad_client_config} | |
chmod 0644 ${nomad_client_config} | |
set -x | |
# update variables | |
sed -i "s/@@NODE_NAME@@/${NODE_NAME}/g" ${nomad_client_config} | |
sed -i "s/@@NODE_CLASS@@/${NODE_CLASS}/g" ${nomad_client_config} | |
sed -i "s|@@SRV_IP_ADDRESS@@|${SRV_IP_ADDRESS}|g" ${nomad_client_config} | |
sed -i "s/eth0/${NETWORK_INTERFACE}/g" ${nomad_client_config} | |
if [[ "${_arch}" == "aarch64" ]]; then | |
sed -i "s/#cpu_total_compute.*/cpu_total_compute = ${cpu_total_compute}/g" ${nomad_client_config} | |
fi | |
set +x | |
return 0 | |
} | |
configure_vault () | |
{ | |
if [[ "$VAULT_SERVER" == "" ]]; then | |
return 0 | |
fi | |
# client config | |
mkdir -p ${nomad_config_dir} | |
curl -L https://raw.githubusercontent.com/shantanugadgil/hashistack/master/config/nomad/vault.hcl -o ${vault_client_config} | |
chown root:root ${vault_client_config} | |
chmod 0644 ${vault_client_config} | |
# update variables | |
set -x | |
sed -i "s/@@VAULT_SERVER@@/${VAULT_SERVER}/g" ${vault_client_config} | |
set +x | |
return 0 | |
} | |
install_cni_plugins () | |
{ | |
curl -L -o cni-plugins.tgz https://github.com/containernetworking/plugins/releases/download/v${CNI_PLUGINS_VERSION}/cni-plugins-linux-${OS_ARCH}-v${CNI_PLUGINS_VERSION}.tgz | |
mkdir -p /opt/cni/bin | |
tar -C /opt/cni/bin -xzf cni-plugins.tgz | |
rm -fv cni-plugins.tgz | |
cat > /etc/sysctl.d/90-consul-connect.conf <<EOF | |
net.bridge.bridge-nf-call-arptables = 1 | |
net.bridge.bridge-nf-call-ip6tables = 1 | |
net.bridge.bridge-nf-call-iptables = 1 | |
EOF | |
return 0 | |
} | |
# script starts here | |
echo "[$0] start" | |
me=$(id -un) | |
if [[ "$me" == "root" ]]; then | |
echo "INFO: running as root, all ok..." | |
else | |
echo "ERROR: not running as root." | |
exit 1 | |
fi | |
SRV_IP_ADDRESS="$1" | |
CONSUL_KEY="$2" | |
NETWORK_INTERFACE=${3:-"default"} | |
NODE_CLASS=${4:-"worker"} | |
VAULT_SERVER=${5:-""} | |
NODE_NAME=$(hostname -s) | |
echo "NODE_NAME [$NODE_NAME]" | |
sleep 3 | |
# detect SystemD vs. Upstart | |
### FIXME: treat non-systemd as upstart (don't detect SysV Init) | |
one_exe=$(readlink -m /proc/1/exe) | |
INIT_SYSTEM="none" | |
case $one_exe in | |
*/systemd) | |
INIT_SYSTEM='systemd' | |
echo "***** SystemD detected. [$one_exe] *****" | |
;; | |
*) | |
INIT_SYSTEM='upstart' | |
echo "***** SystemD not detected [$one_exe]. Assuming Upstart. *****" | |
;; | |
esac | |
if [[ "$NETWORK_INTERFACE" == "" || "$NETWORK_INTERFACE" == "default" ]]; then | |
NETWORK_INTERFACE=$(ip route show | grep '^default' | grep -o 'dev .*' | awk '{print $2}') | |
fi | |
echo "***** using network interface [$NETWORK_INTERFACE] *****" | |
sleep 3 | |
_arch=$(uname -m) | |
case "${_arch}" in | |
'x86_64') | |
OS_ARCH="amd64" | |
;; | |
'aarch64') | |
OS_ARCH="arm64" | |
current_speed=$(dmidecode -t 4 | grep 'Current Speed:' | awk '{print $3}') | |
cpu_count=$(cat /proc/cpuinfo | grep 'processor' | wc -l) | |
cpu_total_compute=$(( $current_speed * $cpu_count )) | |
;; | |
*) | |
echo "***** UNSUPPORTED ARCHITECTURE *****" | |
exit 1 | |
;; | |
esac | |
install_consul | |
configure_consul_client | |
install_nomad | |
configure_nomad_client | |
configure_vault | |
install_cni_plugins | |
service consul restart | |
service nomad restart | |
echo "[$0] Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment