Skip to content

Commit 959e3ca

Browse files
committed
scripts: add gceworker.sh update-hosts
This adds a line like this to /etc/hosts: 34.107.119.188 gceworker-tbg.local AFAIK we don't have a stable DNS entry for gceworkers, so this helps set up remote development in Goland, for example. Epic: none Release note: none
1 parent 4bc4946 commit 959e3ca

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

scripts/gceworker.sh

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ if [[ "${cmd}" ]]; then
3030
shift
3131
fi
3232

33+
function get_ip() {
34+
gcloud compute instances describe --format="value(networkInterfaces[0].accessConfigs[0].natIP)" "${NAME}"
35+
}
36+
3337
function user_domain_suffix() {
3438
gcloud auth list --limit 1 --filter="status:ACTIVE account:@cockroachlabs.com" --format="value(account)" | sed 's/[@\.\-]/_/g'
3539
}
@@ -57,7 +61,7 @@ EOF
5761
}
5862

5963
function refresh_ssh_config() {
60-
IP=$($0 ip)
64+
IP=$(get_ip)
6165
if ! grep -q "${FQNAME}" ~/.ssh/config; then
6266
USER_DOMAIN_SUFFIX="$(user_domain_suffix)"
6367
echo "No alias found for ${FQNAME} in ~/.ssh/config. Creating one for ${USER_DOMAIN_SUFFIX} now with the instance external ip."
@@ -249,7 +253,7 @@ put)
249253
gcloud compute scp --recurse ${lpath} "${to}"
250254
;;
251255
ip)
252-
gcloud compute instances describe --format="value(networkInterfaces[0].accessConfigs[0].natIP)" "${NAME}"
256+
echo "$(get_ip)"
253257
;;
254258
sync)
255259
if ! hash unison 2>/dev/null; then
@@ -300,8 +304,17 @@ vscode)
300304
status)
301305
gcloud compute instances describe ${NAME} --format="table(name,status,lastStartTimestamp,lastStopTimestamp)"
302306
;;
307+
update-hosts)
308+
NEW_IP="$(get_ip)"
309+
HOSTS_FILE="/etc/hosts"
310+
311+
# Step 1: Remove any existing gceworker line.
312+
sudo sed -i '' "/${NAME}\.local/d" "${HOSTS_FILE}"
313+
# Step 2: Insert the new line at the end unconditionally
314+
echo "${NEW_IP} ${NAME}.local" | sudo tee -a ${HOSTS_FILE} > /dev/null
315+
;;
303316
*)
304-
echo "$0: unknown command: ${cmd}, use one of create, start, stop, resume, suspend, delete, status, ssh, get, put, or sync"
317+
echo "$0: unknown command: ${cmd}, use one of create, start, stop, resume, suspend, delete, status, ssh, get, put, sync, or update-hosts"
305318
exit 1
306319
;;
307320
esac

0 commit comments

Comments
 (0)