use USER instead of whoami (#1134)

* use USER instead of whoami

make gcloud_FigureRemoteUsername robust against unsolicited output
   (that I get on login ;) )

validate --prefix argument

* Update gcloud.sh
This commit is contained in:
Rob Walker 2018-09-07 00:18:05 +09:00 committed by GitHub
parent 6560b0e2cc
commit fdc48d521c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 11 deletions

View File

@ -6,7 +6,7 @@ source "$here"/../scripts/gcloud.sh
# shellcheck source=net/common.sh # shellcheck source=net/common.sh
source "$here"/common.sh source "$here"/common.sh
prefix=testnet-dev-$(id -un | sed -e s/[^a-z0-9].*//) prefix=testnet-dev-${USER//[^A-Za-z0-9]/}
validatorNodeCount=5 validatorNodeCount=5
clientNodeCount=1 clientNodeCount=1
leaderMachineType=n1-standard-16 leaderMachineType=n1-standard-16
@ -37,8 +37,8 @@ Configure a GCE-based testnet
delete - delete the testnet delete - delete the testnet
common options: common options:
-p prefix - Optional common prefix for instance names to avoid collisions -p prefix - Optional common prefix for instance names to avoid
(default: $prefix) collisions (default: $prefix)
create-specific options: create-specific options:
-n [number] - Number of validator nodes (default: $validatorNodeCount) -n [number] - Number of validator nodes (default: $validatorNodeCount)
@ -71,6 +71,7 @@ while getopts "h?p:Pi:n:c:z:ga:" opt; do
usage usage
;; ;;
p) p)
[[ ${OPTARG//[^A-Za-z0-9]/} == "$OPTARG" ]] || usage "Invalid prefix: \"$OPTARG\", alphanumeric only"
prefix=$OPTARG prefix=$OPTARG
;; ;;
P) P)

View File

@ -196,7 +196,6 @@ gcloud_FigureRemoteUsername() {
echo "Detecting remote username using $zone in $zone:" echo "Detecting remote username using $zone in $zone:"
# Figure the gcp ssh username # Figure the gcp ssh username
( (
set -x set -x
@ -205,14 +204,18 @@ gcloud_FigureRemoteUsername() {
# instance is reported as RUNNING and when it's reachable over the network # instance is reported as RUNNING and when it's reachable over the network
timeout 30s bash -c "set -o pipefail; until ping -c 3 $publicIp | tr - _; do echo .; done" timeout 30s bash -c "set -o pipefail; until ping -c 3 $publicIp | tr - _; do echo .; done"
gcloud compute ssh "$name" --zone "$zone" -- "echo whoami \$(id -un)" | tee whoami gcloud compute ssh "$name" --zone "$zone" -- "echo whoami:\$USER:iamwho" | tr -d $'\r '| tee /tmp/whoami-$$
) )
while IFS=: read -r whoami gcloud_username iamwho ; do
[[ $whoami == "whoami" && $iamwho == "iamwho" ]] && break;
done < /tmp/whoami-$$
rm -f /tmp/whoami-$$
if [[ -z $gcloud_username ]]; then
echo Unable to figure remote user name
exit 1
fi
[[ "$(tr -dc '[:print:]' < whoami; rm -f whoami)" =~ ^whoami\ (.*)$ ]] || {
echo Unable to figure remote user name;
exit 1
}
gcloud_username="${BASH_REMATCH[1]}"
echo "Remote username: $gcloud_username" echo "Remote username: $gcloud_username"
} }
@ -303,4 +306,3 @@ gcloud_PrepInstancesForSsh() {
fi fi
done done
} }