Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of waits and retries to have the instance vnc URL ready #489

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion cmd/instance/instance_vnc.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package instance

import (
"fmt"
"net/http"
"os"
"time"

"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/pkg/browser"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
"os"
)

// maxAttempts represents the max number of attempts (each one every 7s) to connect to the vnc URL
const maxAttempts = 5

var instanceVncCmd = &cobra.Command{
Use: "vnc",
Example: "civo instance vnc INSTANCE-ID/NAME",
Expand Down Expand Up @@ -46,7 +53,16 @@ var instanceVncCmd = &cobra.Command{
// Display VNC details
utility.Info("VNC has been successfully enabled for instance: %s", instance.Hostname)
utility.Info("VNC URL: %s", vnc.URI)
utility.Info("We're preparing the VNC Console Access. This may take a while...")

err = waitEndpointReady(vnc.URI)
if err != nil {
utility.Error("VNC Console URL is not reachable: %s", err)
os.Exit(1)
}

utility.Info("Opening VNC in your default browser...")
time.Sleep(3 * time.Second)

// Open VNC in the browser
err = browser.OpenInBrowser(vnc.URI)
Expand All @@ -57,3 +73,32 @@ var instanceVncCmd = &cobra.Command{
}
},
}

// endpointReady checks if the given URL endpoint is ready by sending a GET request
// and returning true if the HTTP status code is not 503 or 40x
func endpointReady(url string) bool {
utility.Info("New attempt to reach the VNC Console URI...")
resp, err := http.Get(url)
if err != nil {
return false
}
defer resp.Body.Close()

return resp.StatusCode != http.StatusServiceUnavailable
}

// waitEndpointReady continuously checks the given URL endpoint every 5 seconds
// until it becomes ready (i.e., it does not return an HTTP 503 status).
func waitEndpointReady(url string) error {
var attempt int
for {
attempt++
if endpointReady(url) {
return nil
}
if attempt == maxAttempts {
return fmt.Errorf("max num of attempts reached: VNC endpoint not ready - please contact the support")
}
time.Sleep(7 * time.Second) // Wait for 7 seconds before the next attempt
}
}
Loading