0

Note: This is a self-answered question, which is allowed here. If you have better solutions go ahead and post them.

Hypothetical automation for a web-based cloud school needs to test if students have some minimal competence before proceeding. Students must be able to start a Google Cloud VM, install apache on it, and expose port 80 to the public internet, before proceeding to the next task.

The question is not about how to set up apache. That's obvious.

The question is how to test if its been done.

So the first step is to detect the VM. We tell the students to call it something and we tell them the zone to use.

An early draft of an apache install detector -- executed on another VM inaccessible to the student but within the student's GCP project context -- could start like this:

#!/bin/bash
NAME="required-vm-name"
ZONE="us-central1-a"
IP=$(gcloud compute instances describe ${NAME} --zone ${ZONE} --format json | jq ".networkInterfaces[0].accessConfigs[0].natIP")
echo $IP
curl -v "http://${IP}"

but this produces the output

"1.2.3.4"  
* Rebuilt URL to: http://"1.2.3.4"/
* Could not resolve host: "1.2.3.4"
* Closing connection 0 curl: (6) Could not resolve host: "1.2.3.4"

where I've redacted the address to 1.2.3.4 but kept all other output formatting.

Given the IP address is correct, and the debian apache2 test page displays properly at this address, why can't curl detect it? what's wrong? The correct IP works fine in Chrome.

Paul
  • 1,626
  • 15
  • 19

1 Answers1

1

curl isn't playing well with that double quoted string in the URL.

Add the -r option to the jq command to return a raw string and not a JSON-formatted string. Otherwise jq will output JSON-format by default, which for string results is going to be double-quoted.

#!/bin/bash
NAME="required-vm-name"
ZONE="us-central1-a"
IP=$(gcloud compute instances describe ${NAME} --zone ${ZONE} --format json | jq -r ".networkInterfaces[0].accessConfigs[0].natIP")
echo $IP
curl -v "http://${IP}"

The output is now better. Note the lack of quotes around the extracted public IP address.

1.2.3.4
* Rebuilt URL to: http://1.2.3.4/
*   Trying 1.2.3.4...
* TCP_NODELAY set
* Connected to 1.2.3.4 (1.2.3.4) port 80 (#0)
> GET / HTTP/1.1
> Host: 1.2.3.4
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Sun, 24 Feb 2019 11:46:54 GMT
< Server: Apache/2.4.25 (Debian)
< Last-Modified: Sun, 24 Feb 2019 11:21:58 GMT
< ETag: "c-582a20841f053"
< Accept-Ranges: bytes
< Content-Length: 12
< Content-Type: text/html
<
Hello World
* Curl_http_done: called premature == 0
* Connection #0 to host 1.2.3.4 left intact
Paul
  • 1,626
  • 15
  • 19