-
Notifications
You must be signed in to change notification settings - Fork 1
/
hostname-resolution.sh
109 lines (89 loc) · 3.53 KB
/
hostname-resolution.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
# get script source
SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
ROOT_DIR="${SOURCE_DIR}/.."
ENV_FILE="${ENV_FILE:-"${ROOT_DIR}/.env"}"
# source project files
if [ -f "${ENV_FILE}" ]; then
source "${ENV_FILE}"
fi
source "${SOURCE_DIR}/utils.sh"
# variables
SERVICE_USER="${SERVICE_USER:-"$(get_data "service user account")"}"
export SUDO_PASSWD="${SUDO_PASSWD:-"$(get_password "sudo password")"}"
SSH_PORT="${SSH_PORT:-"22"}"
# env variables
env_variables=(
"SERVICE_USER"
"SUDO_PASSWD"
"SSH_PORT"
)
# ================= DO NOT EDIT BEYOND THIS LINE =================
# get IP-hostname pairs of all master nodes
declare -A master_dns_map
get_kv_pairs master_dns_map "IP of master node"
# get IP-hostname pairs of all worker nodes
declare -A worker_dns_map
get_kv_pairs worker_dns_map "IP of worker node"
# get user confirmation
print_title "hostname resolution"
confirm_values "${env_variables[@]}"
confirm="${?}"
if [ "${confirm}" -ne 0 ]; then
exit "${confirm}"
fi
# sort and combine node keys
master_keys=($(echo "${!master_dns_map[@]}" | tr " " "\n" | sort))
worker_keys=($(echo "${!worker_dns_map[@]}" | tr " " "\n" | sort))
node_keys=("${master_keys[@]}" "${worker_keys[@]}")
# update login node
# the login node must have name resolution to all nodes in the cluster
hostname="$(hostname)"
hosts_file="${hostname}-hosts.tmp"
echo "Updating login node: ${hostname}"
# download hosts file
run_with_sudo cat "/etc/hosts" > "${hosts_file}"
# iterate through all nodes
for node_ip in "${node_keys[@]}"; do
node_name="${master_dns_map[${node_ip}]:-${worker_dns_map[${node_ip}]}}"
# modify hosts file
update_hosts "${node_ip}" "${node_name}" "${hosts_file}"
done
# backup and update hosts file on node
run_with_sudo cp -f "/etc/hosts" "/etc/hosts.bak"
run_with_sudo cp -f "${hosts_file}" "/etc/hosts"
# remove temporary hosts file
rm "${hosts_file}"
# update worker nodes
# the worker nodes must have name resolution to the primary master node
for ip in "${worker_keys[@]}"; do
hostname="${worker_dns_map[${ip}]}"
hosts_file="${hostname}-hosts.tmp"
echo "Updating worker: ${hostname} (${ip})"
# download hosts file
ssh "${SERVICE_USER}@${hostname}" -p "${SSH_PORT}" "echo \"${SUDO_PASSWD}\" | sudo -S bash -c 'cat \"/etc/hosts\"'" > "${hosts_file}"
# modify hosts file
update_hosts "${master_keys[0]}" "${master_dns_map[${master_keys[0]}]}" "${hosts_file}"
# update hosts file on node
ssh "${SERVICE_USER}@${hostname}" -p "${SSH_PORT}" "echo \"${SUDO_PASSWD}\" | sudo -S bash -c 'cp \"/etc/hosts\" \"/etc/hosts.bak\" && echo \"$(cat ${hosts_file})\" > \"/etc/hosts\"'"
# remove temporary hosts file
rm "${hosts_file}"
done
# update master nodes
# the master nodes must have name resolution to all master nodes
for ip in "${master_keys[@]}"; do
hostname="${master_dns_map[${ip}]}"
hosts_file="${hostname}-hosts.tmp"
echo "Updating master: ${hostname} (${ip})"
# download hosts file
ssh "${SERVICE_USER}@${hostname}" -p "${SSH_PORT}" "echo \"${SUDO_PASSWD}\" | sudo -S bash -c 'cat \"/etc/hosts\"'" > "${hosts_file}"
# modify hosts file
for i in "${master_keys[@]}"; do
h="${master_dns_map[${i}]}"
update_hosts "${i}" "${h}" "${hosts_file}"
done
# update hosts file on node
ssh "${SERVICE_USER}@${hostname}" -p "${SSH_PORT}" "echo \"${SUDO_PASSWD}\" | sudo -S bash -c 'cp \"/etc/hosts\" \"/etc/hosts.bak\" && echo \"$(cat ${hosts_file})\" > \"/etc/hosts\"'"
# remove temporary hosts file
rm "${hosts_file}"
done