Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions handler/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func HandleHealth() http.HandlerFunc {
checkDuration := getConnectivityCheckDuration(r.URL.Query())
err := checkInternetConnectivity(checkDuration)
if err != nil {
attemptNetworkRecovery()
WriteError(w, err)
return
}
Expand Down
10 changes: 10 additions & 0 deletions handler/network_recovery_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2022 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.

//go:build !windows

package handler

// attemptNetworkRecovery is a no-op on non-Windows platforms.
func attemptNetworkRecovery() {}
53 changes: 53 additions & 0 deletions handler/network_recovery_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2022 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.

//go:build windows

package handler

import (
"fmt"
"os/exec"
"strings"
"time"

"github.com/sirupsen/logrus"
)

// attemptNetworkRecovery tries to restore Windows network connectivity
// after GCE suspend/resume by renewing DHCP, flushing DNS, syncing clock,
// and re-adding DNS servers.
func attemptNetworkRecovery() {
logrus.Infoln("Attempting Windows network recovery after connectivity failure")
start := time.Now()

cmds := []struct {
name string
args []string
}{
{"ipconfig", []string{"/renew"}},
{"ipconfig", []string{"/flushdns"}},
{"w32tm", []string{"/resync", "/nowait"}},
{"netsh", []string{"interface", "ipv4", "add", "dnsserver", "Ethernet", "8.8.8.8", "index=1"}},
{"netsh", []string{"interface", "ipv4", "add", "dnsserver", "Ethernet", "1.1.1.1", "index=2"}},
}

var errors []string
for _, c := range cmds {
out, err := exec.Command(c.name, c.args...).CombinedOutput()
if err != nil {
msg := fmt.Sprintf("%s %s failed: %v (output: %s)", c.name, strings.Join(c.args, " "), err, strings.TrimSpace(string(out)))
errors = append(errors, msg)
logrus.Warnln(msg)
}
}

if len(errors) > 0 {
logrus.WithField("errors", len(errors)).WithField("elapsed", time.Since(start)).
Warnln("Network recovery completed with some errors")
} else {
logrus.WithField("elapsed", time.Since(start)).
Infoln("Network recovery completed successfully")
}
}