-
Notifications
You must be signed in to change notification settings - Fork 142
/
smap.go
111 lines (104 loc) · 2.79 KB
/
smap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Package cli provides easy-to-use commands to manage, monitor, and utilize AIS clusters.
// This file contains util functions and types.
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
*/
package cli
import (
"fmt"
"os"
"strings"
"github.com/NVIDIA/aistore/api"
"github.com/NVIDIA/aistore/api/env"
"github.com/NVIDIA/aistore/cmd/cli/teb"
"github.com/NVIDIA/aistore/core/meta"
"github.com/urfave/cli"
)
// In this file:
// utility functions, wrappers, and helpers to work with cluster map (Smap) and clustered nodes.
var curSmap *meta.Smap
// the implementation may look simplified, even naive, but in fact
// within-a-single-command lifecycle and singlethreaded-ness eliminate
// all scenarios that'd be otherwise commonly expected
func getClusterMap(c *cli.Context) (*meta.Smap, error) {
// when "long-running" refresh Smap as well
// (see setLongRunParams)
if curSmap != nil && !flagIsSet(c, refreshFlag) {
return curSmap, nil
}
smap, err := api.GetClusterMap(apiBP)
if err != nil {
return nil, V(err)
}
curSmap = smap
if smap.Primary.PubNet.URL != apiBP.URL {
if cliConfVerbose() {
what := env.AIS.Endpoint
if os.Getenv(env.AIS.Endpoint) == "" {
what = "CLI config URL"
}
warn := fmt.Sprintf("changing %s from %q to %q", what, apiBP.URL, smap.Primary.PubNet.URL)
actionWarn(c, warn)
}
apiBP.URL = smap.Primary.PubNet.URL
}
return curSmap, nil
}
func getNode(c *cli.Context, arg string) (node *meta.Snode, sname string, err error) {
var (
sid string
smap *meta.Smap
)
if arg == "" {
// NOTE: use arg0Node() when the node arg is optional
err = missingArgumentsError(c, c.Command.ArgsUsage)
return
}
if smap, err = getClusterMap(c); err != nil {
return
}
if strings.HasPrefix(arg, meta.TnamePrefix) || strings.HasPrefix(arg, meta.PnamePrefix) {
sid = meta.N2ID(arg)
} else {
sid = arg
}
if node = smap.GetNode(sid); node == nil {
err = &errDoesNotExist{
what: "node",
name: arg,
suffix: " (" + tabHelpOpt + ", or see 'ais show cluster')",
}
return
}
sname = node.StringEx()
return
}
// Gets Smap from a given node (`daemonID`) and displays it
func smapFromNode(c *cli.Context, primarySmap *meta.Smap, sid string, usejs bool) error {
var (
smap = primarySmap
err error
extendedURLs bool
)
if sid != "" {
smap, err = api.GetNodeClusterMap(apiBP, sid)
if err != nil {
return err
}
}
for _, m := range []meta.NodeMap{smap.Tmap, smap.Pmap} {
for _, v := range m {
if v.PubNet != v.ControlNet || v.PubNet != v.DataNet {
extendedURLs = true
}
}
}
body := teb.SmapHelper{
Smap: smap,
ExtendedURLs: extendedURLs,
}
if flagIsSet(c, noHeaderFlag) {
return teb.Print(body, teb.SmapTmplNoHdr, teb.Jopts(usejs))
}
return teb.Print(body, teb.SmapTmpl, teb.Jopts(usejs))
}