66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os/exec"
|
|
"runtime"
|
|
)
|
|
|
|
type podmanMachine struct {
|
|
Name string `json:"name"`
|
|
Running bool `json:"running"`
|
|
VMType string `json:"vm_type"`
|
|
}
|
|
|
|
// ensurePodmanMachineDNS configures public DNS resolvers on running Podman
|
|
// machines on Windows and macOS. This is required because the Podman VM does
|
|
// not always inherit a working DNS from the host, which prevents pulling
|
|
// images and reaching api.wordpress.org from containers.
|
|
func ensurePodmanMachineDNS() {
|
|
if runtime.GOOS != "windows" && runtime.GOOS != "darwin" {
|
|
return
|
|
}
|
|
if getContainerEngine() != "podman" {
|
|
return
|
|
}
|
|
|
|
out, err := exec.Command("podman", "machine", "list", "--format", "json").Output()
|
|
if err != nil {
|
|
log.Printf("ensurePodmanMachineDNS: cannot list machines: %v", err)
|
|
return
|
|
}
|
|
|
|
var machines []podmanMachine
|
|
if err := json.Unmarshal(out, &machines); err != nil {
|
|
log.Printf("ensurePodmanMachineDNS: cannot parse machine list: %v", err)
|
|
return
|
|
}
|
|
|
|
for _, m := range machines {
|
|
if !m.Running {
|
|
continue
|
|
}
|
|
if err := configurePodmanMachineDNS(m.Name); err != nil {
|
|
log.Printf("ensurePodmanMachineDNS: failed for %s: %v", m.Name, err)
|
|
} else {
|
|
log.Printf("ensurePodmanMachineDNS: DNS configured for %s", m.Name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func configurePodmanMachineDNS(name string) error {
|
|
cmd := exec.Command(
|
|
"podman", "machine", "ssh", name,
|
|
"sudo", "sh", "-c",
|
|
"echo nameserver 8.8.8.8 > /etc/resolv.conf && echo nameserver 1.1.1.1 >> /etc/resolv.conf",
|
|
)
|
|
hideWindow(cmd)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("%w: %s", err, string(out))
|
|
}
|
|
return nil
|
|
}
|