Console: l'identifiant du noeud est ajoute automatiquement a GARAGE_RPC_HOST (GetNodeInfo)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
17b98b6cf3
commit
ca1330434e
4 files changed
+122
-7
No files matched your search
+74
-3
@@ -28,6 +28,7 @@ import (
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/vLab-BZH/garage-ui/web"
|
||||
@@ -52,10 +53,14 @@ type config struct {
|
||||
s3Region string
|
||||
cliBin string
|
||||
cliConfig string
|
||||
rpcHost string // GARAGE_RPC_HOST : [identifiant@]hôte:port
|
||||
publicURL string
|
||||
sessionTTL time.Duration
|
||||
cliTimeout time.Duration
|
||||
transport http.RoundTripper // nil : transport par défaut (les tests y mettent un faux Garage)
|
||||
|
||||
rpcMu sync.Mutex
|
||||
rpcResolved string // rpcHost complété par l'identifiant du nœud, une fois connu
|
||||
}
|
||||
|
||||
func env(k, def string) string {
|
||||
@@ -76,6 +81,7 @@ func loadConfig() (*config, error) {
|
||||
s3Region: env("GARAGE_S3_REGION", "garage"),
|
||||
cliBin: env("GARAGE_BIN", ""),
|
||||
cliConfig: env("GARAGE_CONFIG", ""),
|
||||
rpcHost: env("GARAGE_RPC_HOST", ""),
|
||||
publicURL: env("PUBLIC_URL", ""),
|
||||
sessionTTL: 24 * time.Hour,
|
||||
cliTimeout: 120 * time.Second,
|
||||
@@ -270,6 +276,66 @@ func (b *limitedBuf) Write(p []byte) (int, error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// selfNodeID demande à l'API d'administration l'identifiant complet du nœud qui répond.
|
||||
func (c *config) selfNodeID(ctx context.Context) (string, error) {
|
||||
u := *c.adminURL
|
||||
u.Path = strings.TrimRight(u.Path, "/") + "/v2/GetNodeInfo"
|
||||
u.RawQuery = "node=self"
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+c.adminToken)
|
||||
cl := &http.Client{Transport: c.transport, Timeout: 10 * time.Second}
|
||||
resp, err := cl.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("GetNodeInfo : HTTP %d", resp.StatusCode)
|
||||
}
|
||||
var out struct {
|
||||
Success map[string]struct {
|
||||
NodeID string `json:"nodeId"`
|
||||
} `json:"success"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&out); err != nil {
|
||||
return "", err
|
||||
}
|
||||
for id, n := range out.Success {
|
||||
if n.NodeID != "" {
|
||||
return n.NodeID, nil
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
return "", errors.New("GetNodeInfo : aucun nœud dans la réponse")
|
||||
}
|
||||
|
||||
// resolveRPCHost renvoie l'adresse RPC au format exigé par la CLI : <identifiant complet>@<hôte>:<port>.
|
||||
// GARAGE_RPC_HOST peut donner seulement hôte:port (l'identifiant est alors demandé à Garage, une fois),
|
||||
// ou rester vide (hôte de l'API d'administration, port 3901).
|
||||
func (c *config) resolveRPCHost(ctx context.Context) (string, error) {
|
||||
host := c.rpcHost
|
||||
if host == "" {
|
||||
host = c.adminURL.Hostname() + ":3901"
|
||||
}
|
||||
if strings.Contains(host, "@") {
|
||||
return host, nil
|
||||
}
|
||||
c.rpcMu.Lock()
|
||||
defer c.rpcMu.Unlock()
|
||||
if c.rpcResolved != "" {
|
||||
return c.rpcResolved, nil
|
||||
}
|
||||
id, err := c.selfNodeID(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
c.rpcResolved = id + "@" + host
|
||||
return c.rpcResolved, nil
|
||||
}
|
||||
|
||||
func (c *config) handleCLI(w http.ResponseWriter, r *http.Request) {
|
||||
if c.cliBin == "" {
|
||||
writeErr(w, http.StatusNotImplemented, "Console CLI non configurée (GARAGE_BIN)")
|
||||
@@ -299,17 +365,22 @@ func (c *config) handleCLI(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), c.cliTimeout)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, c.cliBin, args...)
|
||||
rpcHost, err := c.resolveRPCHost(ctx)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadGateway, "Impossible de déterminer l'identifiant du nœud Garage : "+err.Error())
|
||||
return
|
||||
}
|
||||
var env []string
|
||||
for _, e := range os.Environ() {
|
||||
if !strings.HasPrefix(e, "UI_") && !strings.HasPrefix(e, "SESSION_") {
|
||||
if !strings.HasPrefix(e, "UI_") && !strings.HasPrefix(e, "SESSION_") && !strings.HasPrefix(e, "GARAGE_RPC_HOST=") {
|
||||
env = append(env, e)
|
||||
}
|
||||
}
|
||||
cmd.Env = env
|
||||
cmd.Env = append(env, "GARAGE_RPC_HOST="+rpcHost)
|
||||
out := &limitedBuf{}
|
||||
cmd.Stdout, cmd.Stderr = out, out
|
||||
start := time.Now()
|
||||
err := cmd.Run()
|
||||
err = cmd.Run()
|
||||
code := 0
|
||||
var ee *exec.ExitError
|
||||
switch {
|
||||
|
||||
Reference in new issue
Block a user