myxce/src/frontend/xcecli/cmd/hosts/controller.go
Rongsong Shen 94f50ce6e1 fix(scheduler,cli): bug-1453/1454/1455/1456 remove dead CA-cert plumbing, fix scheduler HA config bugs
bug-1453: remove unused client-side CA-certificate plumbing. C-side:
drop ca_cert_path from xce_cluster_config_t, do_connect()'s ca_cert
param, --ca-cert flags on worker/scheduler, and controller's cert
population (TLS is server-side only, SSL_VERIFY_NONE everywhere on
the client side already, verified zero SSL_CTX_load_verify_locations
calls in the C tree). Go-side: delete xcecli's self-signed cert
TOFU-pinning flow (cmd/auth/cert/*), CACert field, SetRootCAs/
loadRootCAs, and every call site.

bug-1454: fix xcecli_ctx_t leak in scheduler.c sched_init() — the
unconditional initial ctx_init was never freed before the HA
candidate loop re-initialized it on the first iteration.

bug-1455: sched_init()'s memset(sched, 0, ...) was silently wiping
sched->cluster_cfg that main.c had populated from --peers just before
calling sched_init(), so --peers was always ignored and the scheduler
fell back to the hardcoded localhost:9990 single-host default.
Restructured sched_init() to take cluster_cfg as an explicit parameter
(assigned after the memset) instead of relying on caller-side
ordering with a separate sched_set_cluster_cfg() setter, which is now
removed.

bug-1456: revert an unsafe stack-local xce_client_config_t introduced
while fixing bug-1455 — xcecli_ctx_init() stores that pointer as
ctx->conf and client_failover.c dereferences it for the ctx's entire
life (runtime reconnect), so a per-loop-iteration stack copy would
dangle the moment sched_init() returned. Back to mutating the
caller-owned, process-lifetime config in place.

Verified: full ASAN build (all components), meson test suite 9/9 OK,
manual scheduler testsuite 15/15, Go build/vet/test all pass.
2026-07-20 12:01:10 +00:00

177 lines
4.5 KiB
Go
Executable file

package hosts
import (
"encoding/json"
"fmt"
"strings"
"xcecli/common/request"
"xcecli/config"
"xcecli/models"
"github.com/spf13/cobra"
)
const (
HOST_OPEN = 1
HOST_CLOSE = 2
)
// HostController host control controller
type HostController struct {
configManager *config.ConfigManager
action string
hostnames []string
comment string
allHosts bool
}
// NewHostController create host control controller
func NewHostController(configManager *config.ConfigManager, action string) *HostController {
return &HostController{
configManager: configManager,
action: action,
hostnames: []string{},
}
}
// Run execute host control
func (c *HostController) RunHostsControl() error {
// get server info and verify authentication
serverInfo, _ := c.configManager.GetServerInfo()
// create API client
apiClient := request.NewAPIClient(serverInfo.URL)
// handle all hosts case
if c.allHosts {
c.hostnames = []string{"all"}
}
// if no hostnames specified, return error
if len(c.hostnames) == 0 {
return fmt.Errorf("must specify hostnames or use --all option")
}
// create request
req := models.HostControlRequest{
ControlAction: c.action,
Hostname: c.hostnames,
Comment: c.comment,
}
// encode request to JSON bytes
reqData, err := json.Marshal(req)
if err != nil {
errMsg := request.ExtractErrorMessage(err)
return fmt.Errorf("failed to encode request data: [%s]", errMsg)
}
var resp models.HostControlResponse
err = apiClient.PostRequestToAPIServer(
serverInfo.Token,
"/xce/v1/hosts/control",
reqData,
&resp,
)
if err != nil {
return err
}
if len(c.hostnames) == 1 && c.hostnames[0] == "all" {
for _, data := range resp.Data.Accepted {
fmt.Printf("host <%s> is being <%s>\n", data.HostName, c.action)
}
for _, host := range resp.Data.Failed {
fmt.Printf("host <%s> cannot be <%s> for reason <%s>\n", host.HostName, c.action, host.Msg)
}
} else {
for _, data := range resp.Data.Accepted {
fmt.Printf("host <%s> is being <%s>\n", data.HostName, c.action)
}
for _, host := range resp.Data.Failed {
fmt.Printf("host <%s> cannot be <%s> for reason <%s>\n", host.HostName, c.action, host.Msg)
}
}
return nil
}
// Parse parse command line arguments
func (c *HostController) ParseCmdArgs(cmd *cobra.Command, args []string) error {
// handle hostnames
if len(args) > 0 {
c.hostnames = append(c.hostnames, args...)
}
return nil
}
// NewOpenHostsCmd create open hosts command
func NewOpenHostsCmd(configManager *config.ConfigManager) *cobra.Command {
controller := NewHostController(configManager, "open")
cmd := &cobra.Command{
Use: "open [hostname1 hostname2...]",
Short: "Open hosts",
Long: `Open hosts by hostname.
Open hosts changes hosts from DOWN, UNREACH, or UNAVAIL status to OK status.
Examples:
xce hosts open host1,host2,... # open specific hosts
xce hosts open --all # open all hosts`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := controller.ParseCmdArgs(cmd, args); err != nil {
fmt.Println(strings.ToLower(err.Error()))
return nil
}
if err := controller.RunHostsControl(); err != nil {
fmt.Println(strings.ToLower(err.Error()))
return nil
}
return nil
},
}
// add control options
flags := cmd.Flags()
flags.BoolVarP(&controller.allHosts, "all", "a", false, "apply to all hosts")
flags.StringVarP(&controller.comment, "comment", "c", "", "add comment for this operation")
return cmd
}
// NewCloseHostsCmd create close hosts command
func NewCloseHostsCmd(configManager *config.ConfigManager) *cobra.Command {
controller := NewHostController(configManager, "close")
cmd := &cobra.Command{
Use: "close [hostname1 hostname2...]",
Short: "Close hosts",
Long: `Close hosts by hostname.
Close hosts changes hosts from OK, BUSY, or IDLE status to UNAVAIL status.
Examples:
xce hosts close host1 host2 # close specific hosts
xce hosts close --all # close all hosts`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := controller.ParseCmdArgs(cmd, args); err != nil {
fmt.Println(strings.ToLower(err.Error()))
return nil
}
if err := controller.RunHostsControl(); err != nil {
fmt.Println(strings.ToLower(err.Error()))
return nil
}
return nil
},
}
// add control options
flags := cmd.Flags()
flags.BoolVarP(&controller.allHosts, "all", "a", false, "apply to all hosts")
flags.StringVarP(&controller.comment, "comment", "c", "", "add comment for this operation")
return cmd
}