Search code examples
bashgoogle-cloud-platformgcloud

suppress extra o/p generated by gcloud command


I am using gcloud commands to deploy simple VMs. I am using startup script to configure all the required packages on the machine. Our packages are fetched from nexus. Sometimes our changes or nexus network issues result in failure of startup script. I configured a block of code to validate startup script return code. I run gcloud create command and provide startup script, the code block to validate startup script output looks like this..

echo "Step 2: sleep till startup script finishes"
## block to check return code of the VM startup script
MAX_WAIT_TIME=1200
WAIT_TIME=0
RC_CODE=""
echo "[INFO] waiting for startup-script return code"
while [ -z $RC_CODE ] && [ $WAIT_TIME -le $MAX_WAIT_TIME ]
do
  RC_CODE=$(gcloud compute instances get-serial-port-output gce-$GCP_ZONE-d-$APP-dev \
  --project=$GCP_PROJECT_ID \
  --zone=$GCP_ZONE | awk '/^[0-9]{4}/ && /google_metadata_script_runner/ && /startup-script-url exit status/ {sub("\r", "", $NF); print $NF}')
  if [[ -z $RC_CODE ]] ; then
    echo -n "."
    WAIT_TIME=$((WAIT_TIME+10))
    sleep 10
  else
    if [[ $RC_CODE -eq 0 ]] ; then
      echo -e "\n[INFO] startup script completed with return code $RC_CODE."
      break
    else
      echo -e "\n[INFO] Startup script completed with return code $RC_CODE."
      exit $RC_CODE
    fi
  fi
done

# to check timeout scenario
if [[ -z $RC_CODE ]]
then
  echo "[INFO] Startup script timed out after $((MAX_WAIT_TIME/60))."
  echo "[INFO] Startup script completed with return code 1."
  exit 1
fi

My output looks like this,

+ ./sh/create-dev-vm-app.sh
Deleted [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].
Created [https://www.googleapis.com/compute/v1/projects/project-name/zones/europe-west1-c/instances/gce-europe-west1-c-d-myapp-dev].

waiting for startup-script return code
Specify --start=2473 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=32157 in the next get-serial-port-output invocation to get only the new output starting from here.
.
Specify --start=37602 in the next get-serial-port-output invocation to get only the new output starting from here.```
.
Startup script completed with return code 0.

How can I suppress the these 'Specify' lines from appearing on o/p screen? AND/OR print all startup-script messages received from get-serial-port-output output after I receive the return code.


Solution

  • Swallow standard error to avoid the extraneous output:

    gcloud ... 2>/dev/null | awk ...