Files
Olares/cli/pkg/terminus/natgateway.go
Eliott van Nuffel df38148149
Some checks failed
Native Verification / Build Docs (pull_request) Successful in 1m12s
Native Verification / Build App-Service Native (pull_request) Successful in 1m27s
Native Verification / Build Daemon Native (pull_request) Successful in 42s
Lint and Test Charts / lint-test (pull_request_target) Failing after 19s
Lint and Test Charts / test-version (pull_request_target) Successful in 0s
Lint and Test Charts / push-image (pull_request_target) Failing after 15s
Lint and Test Charts / upload-cli (pull_request_target) Failing after 1m15s
Lint and Test Charts / upload-daemon (pull_request_target) Failing after 1m12s
Lint and Test Charts / push-deps (pull_request_target) Has been skipped
Lint and Test Charts / push-deps-arm64 (pull_request_target) Has been skipped
Lint and Test Charts / push-image-arm64 (pull_request_target) Has been cancelled
Lint and Test Charts / upload-package (pull_request_target) Has been cancelled
Lint and Test Charts / install-test (pull_request_target) Has been cancelled
continue beOS rebrand and add native verification
2026-03-10 13:48:45 +01:00

150 lines
4.0 KiB
Go

package terminus
import (
"fmt"
"net"
"os"
"os/exec"
"strings"
"github.com/beclab/beos/cli/pkg/utils"
"github.com/pkg/errors"
"github.com/beclab/beos/cli/pkg/common"
"github.com/beclab/beos/cli/pkg/core/connector"
"github.com/beclab/beos/cli/pkg/core/logger"
"github.com/beclab/beos/cli/pkg/core/util"
)
type GetNATGatewayIP struct {
common.KubeAction
}
func (s *GetNATGatewayIP) Execute(runtime connector.Runtime) error {
var prompt string
var input string
var retry bool
var systemInfo = runtime.GetSystemInfo()
var hostIP = s.KubeConf.Arg.HostIP
disableHostIPPrompt := os.Getenv(common.ENV_DISABLE_HOST_IP_PROMPT)
switch {
case systemInfo.IsWsl() || systemInfo.IsWindows():
if strings.EqualFold(disableHostIPPrompt, "") || !util.IsValidIPv4Addr(net.ParseIP(hostIP)) {
prompt = "the NAT gateway(the Windows host)'s IP is " + hostIP + ", Confirm[Y] or ReEnter [R]: "
} else {
input = hostIP
}
case systemInfo.IsDarwin():
if strings.EqualFold(disableHostIPPrompt, "") || !util.IsValidIPv4Addr(net.ParseIP(hostIP)) {
if hostIP == "" {
hostIP = systemInfo.GetLocalIp()
}
prompt = "the NAT gateway(the MacOS host)'s IP is " + hostIP + ", Confirm[Y] or ReEnter [R]: "
} else {
input = hostIP
}
case s.KubeConf.Arg.IsBeOSInContainer:
if hostIP == "" {
return errors.Errorf("host ip not found")
}
input = hostIP
default:
return nil
}
if prompt != "" {
reader, err := utils.GetBufIOReaderOfTerminalInput()
if err != nil {
return errors.Wrap(err, "failed to get terminal input reader")
}
LOOP:
if !retry {
fmt.Printf(prompt)
} else {
fmt.Printf("\nEnter the NAT gateway IP: ")
}
input, err = reader.ReadString('\n')
if input == "" {
if err != nil && err.Error() != "EOF" {
return err
}
}
if retry {
input = strings.TrimSpace(input)
if !util.IsValidIPv4Addr(net.ParseIP(input)) {
fmt.Printf("\nsorry, invalid IP, please try again.\n")
goto LOOP
}
} else {
input = strings.TrimSpace(input)
switch input {
case "Y":
input = hostIP
break
case "R":
retry = true
fallthrough
default:
goto LOOP
}
}
if !util.IsValidIPv4Addr(net.ParseIP(input)) {
fmt.Printf("\nsorry, invalid IP, please try again.\n")
goto LOOP
}
}
logger.Infof("Nat Gateway IP: %s", input)
runtime.GetSystemInfo().SetNATGateway(input)
return nil
}
type UpdateNATGatewayForUser struct {
common.KubeAction
}
func (a *UpdateNATGatewayForUser) Execute(runtime connector.Runtime) error {
hostIP := runtime.GetSystemInfo().GetNATGateway()
if hostIP == "" {
return errors.New("NAT gateway is not set")
}
si := runtime.GetSystemInfo()
var kubectlCMD string
var kubectlCMDDefaultArgs []string
var err error
if si.IsDarwin() {
kubectlCMD, err = util.GetCommand(common.CommandKubectl)
if err != nil {
return errors.Wrap(errors.WithStack(err), "kubectl not found")
}
} else if si.IsWindows() {
kubectlCMD = "cmd"
kubectlCMDDefaultArgs = []string{"/C", "wsl", "-d", a.KubeConf.Arg.WSLDistribution, "-u", "root", common.CommandKubectl}
}
getUserArgs := []string{"get", "user", "-o", "jsonpath='{.items[0].metadata.name}'"}
getUserCMD := exec.Command(kubectlCMD, append(kubectlCMDDefaultArgs, getUserArgs...)...)
usernameBytes, err := getUserCMD.Output()
if err != nil {
return errors.Wrap(err, "failed to get user for updating")
}
username := strings.TrimSpace(string(usernameBytes))
username = strings.TrimPrefix(username, "'")
username = strings.TrimSuffix(username, "'")
if len(username) == 0 {
return errors.New("failed to get user for updating: got empty username")
}
logger.Infof("updating user: %s", username)
jsonPatch := fmt.Sprintf(`{"metadata":{"annotations":{"bytetrade.io/nat-gateway-ip":"%s"}}}`, hostIP)
patchUserArgs := []string{"patch", "user", username, "-p", jsonPatch, "--type=merge"}
patchUserCMD := exec.Command(kubectlCMD, append(kubectlCMDDefaultArgs, patchUserArgs...)...)
output, err := patchUserCMD.CombinedOutput()
if err != nil {
return errors.Wrapf(err, "failed to update nat gateway for user, output: %s", output)
}
return nil
}