-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-hosts.sh
More file actions
180 lines (147 loc) · 4.38 KB
/
update-hosts.sh
File metadata and controls
180 lines (147 loc) · 4.38 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env bash
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && DOT_SOURCED=true || DOT_SOURCED=false
AWS_PROFILE=dev
if ! aws sts get-caller-identity --profile "$AWS_PROFILE" >/dev/null 2>&1; then
echo "SSO session expired or not logged in. Logging in..."
aws sso login --profile sso-main
fi
DRYRUN=false
[[ "$1" == "--dryrun" ]] && DRYRUN=true
run_update() {
TIMESTAMP=$(date +"%Y%m%dT%H%M%S")
# Backup folders
BACKUP_DIR="$HOME/temp/hosts"
mkdir -p "$BACKUP_DIR"
HOSTS_FILE="/private/etc/hosts"
SSH_CONFIG="$HOME/.ssh/config"
HOSTS_BAK="$BACKUP_DIR/hosts.$TIMESTAMP"
SSH_BAK="$BACKUP_DIR/ssh_config.$TIMESTAMP"
echo "Backing up current files..."
sudo cp "$HOSTS_FILE" "$HOSTS_BAK" || { echo "Failed to back up $HOSTS_FILE"; exit 1; }
if [[ ! -f "$SSH_CONFIG" ]]; then
echo "Warning: $SSH_CONFIG does not exist. Creating an empty one."
touch "$SSH_CONFIG"
fi
cp "$SSH_CONFIG" "$SSH_BAK" || { echo "Failed to back up $SSH_CONFIG"; exit 1; }
# EC2 instance definitions
NAMES=(
"enterprise-demo-javier-primary"
"enterprise-demo-javier-replica"
"enterprise-demo-javier-replica"
"enterprise-demo-javier-replica"
"enterprise-replica-stockholm-peered-javier"
"javier-spx-demo-sender"
"javier-fosdem-demo"
)
ALIASES=(
"enterprise-primary"
"enterprise-replica"
"enterprise-grafana"
"enterprise-jupyter"
"enterprise-replica2"
"spx-sender"
"clickbench"
)
REGIONS=(
"eu-west-1"
"eu-west-1"
"eu-west-1"
"eu-west-1"
"eu-north-1"
"eu-west-1"
"eu-west-1"
)
PEMS=(
"javier-demos.pem"
"javier-demos.pem"
"javier-demos.pem"
"javier-demos.pem"
"javier-stockholm.pem"
"javier-demos.pem"
"javier-demos.pem"
)
TMP_HOSTS=$(mktemp)
TMP_SSH=$(mktemp)
# Build a dynamic regex of all aliases
PATTERN=$(IFS=\|; echo "${ALIASES[*]}")
# Remove all managed aliases from hosts before rewriting
grep -vE "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+(${PATTERN})$" "$HOSTS_FILE" > "$TMP_HOSTS"
# Add marker for managed section
if ! grep -q '# Managed by update-hosts.sh — do not edit below' "$TMP_HOSTS"; then
echo -e "\n# Managed by update-hosts.sh — do not edit below" >> "$TMP_HOSTS"
fi
# Parse existing ssh config and remove *any* block that matches a managed alias
awk -v aliases="$(IFS=\|; echo "${ALIASES[*]}")" '
BEGIN { keep = 1 }
/^Host[ \t]+/ {
for (i = 1; i <= NF; i++) {
if ($i ~ aliases) { keep = 0; break }
else { keep = 1 }
}
}
keep { print }
' "$SSH_CONFIG" > "$TMP_SSH"
# Append SSH managed block marker
if ! grep -q '# Managed by update-hosts.sh — do not edit below manually' "$TMP_SSH"; then
echo -e "\n# Managed by update-hosts.sh — do not edit below manually" >> "$TMP_SSH"
fi
for ((i = 0; i < ${#NAMES[@]}; i++)); do
NAME="${NAMES[$i]}"
ALIAS="${ALIASES[$i]}"
REGION="${REGIONS[$i]}"
PEM="${PEMS[$i]}"
IP=$(aws ec2 describe-instances \
--profile "$AWS_PROFILE" \
--region "$REGION" \
--filters "Name=tag:Name,Values=$NAME" "Name=instance-state-name,Values=running" \
--query "Reservations[].Instances[].PublicIpAddress" \
--output text)
if [[ -z "$IP" ]]; then
IP=$(grep "[[:space:]]$ALIAS\$" "$HOSTS_FILE" | awk '{print $1}')
if [[ -z "$IP" ]]; then
echo "Warning: $ALIAS not running and no previous IP found. Skipping."
continue
fi
echo "Retaining previous IP for $ALIAS: $IP"
else
echo "Resolved $ALIAS to $IP"
fi
# Skip malformed alias or IP
if [[ -z "$ALIAS" || -z "$IP" ]]; then
echo "Skipping invalid alias: $ALIAS (IP: $IP)"
continue
fi
# Don't allow commented-out aliases
case "$ALIAS" in
\#*) echo "Skipping commented alias: $ALIAS"; continue ;;
esac
echo "$IP $ALIAS" >> "$TMP_HOSTS"
DASHED_IP=$(echo "$IP" | sed 's/\./-/g')
cat >> "$TMP_SSH" <<EOF
Host $ALIAS
HostName ec2-$DASHED_IP.$REGION.compute.amazonaws.com
User ubuntu
IdentityFile ~/.ssh/$PEM
EOF
done
if $DRYRUN; then
echo
echo "Dry run mode — no files modified"
echo
echo "----- /etc/hosts preview -----"
cat "$TMP_HOSTS"
echo
echo "----- ~/.ssh/config preview -----"
cat "$TMP_SSH"
else
echo
echo "Applying updates..."
sudo cp "$TMP_HOSTS" "$HOSTS_FILE" || { echo "Failed to update $HOSTS_FILE"; exit 1; }
cp "$TMP_SSH" "$SSH_CONFIG" || { echo "Failed to update $SSH_CONFIG"; exit 1; }
echo "Update complete. Backups saved in:"
echo " $HOSTS_BAK"
echo " $SSH_BAK"
fi
rm -f "$TMP_HOSTS" "$TMP_SSH"
}
run_update || { $DOT_SOURCED && return 1 || exit 1; }