|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + log "github.com/Sirupsen/logrus" |
| 8 | + "github.com/docker/infrakit/pkg/discovery" |
| 9 | + metadata_plugin "github.com/docker/infrakit/pkg/plugin/metadata" |
| 10 | + "github.com/docker/infrakit/pkg/rpc/client" |
| 11 | + metadata_rpc "github.com/docker/infrakit/pkg/rpc/metadata" |
| 12 | + "github.com/docker/infrakit/pkg/spi/metadata" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +func getPlugin(plugins func() discovery.Plugins, name string) (found metadata.Plugin, err error) { |
| 17 | + err = forPlugin(plugins, func(n string, p metadata.Plugin) error { |
| 18 | + if n == name { |
| 19 | + found = p |
| 20 | + } |
| 21 | + return nil |
| 22 | + }) |
| 23 | + return |
| 24 | +} |
| 25 | + |
| 26 | +func forPlugin(plugins func() discovery.Plugins, do func(string, metadata.Plugin) error) error { |
| 27 | + all, err := plugins().List() |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + for name, endpoint := range all { |
| 32 | + rpcClient, err := client.New(endpoint.Address, metadata.InterfaceSpec) |
| 33 | + if err != nil { |
| 34 | + continue |
| 35 | + } |
| 36 | + if err := do(name, metadata_rpc.Adapt(rpcClient)); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func listAll(m metadata.Plugin, path metadata.Path) ([]metadata.Path, error) { |
| 44 | + if m == nil { |
| 45 | + return nil, fmt.Errorf("no plugin") |
| 46 | + } |
| 47 | + result := []metadata.Path{} |
| 48 | + nodes, err := m.List(path) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + for _, n := range nodes { |
| 53 | + c := path.Join(n) |
| 54 | + more, err := listAll(m, c) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + if len(more) == 0 { |
| 59 | + result = append(result, c) |
| 60 | + } |
| 61 | + for _, pp := range more { |
| 62 | + result = append(result, pp) |
| 63 | + } |
| 64 | + } |
| 65 | + return result, nil |
| 66 | +} |
| 67 | + |
| 68 | +func metadataCommand(plugins func() discovery.Plugins) *cobra.Command { |
| 69 | + |
| 70 | + cmd := &cobra.Command{ |
| 71 | + Use: "metadata", |
| 72 | + Short: "Access metadata exposed by infrakit plugins", |
| 73 | + } |
| 74 | + |
| 75 | + ls := &cobra.Command{ |
| 76 | + Use: "ls", |
| 77 | + Short: "List all metadata entries", |
| 78 | + } |
| 79 | + |
| 80 | + long := ls.Flags().BoolP("long", "l", false, "Print full path") |
| 81 | + all := ls.Flags().BoolP("all", "a", false, "Find all under the paths given") |
| 82 | + |
| 83 | + ls.RunE = func(c *cobra.Command, args []string) error { |
| 84 | + paths := []string{"."} |
| 85 | + |
| 86 | + // All implies long |
| 87 | + if *all { |
| 88 | + *long = true |
| 89 | + } |
| 90 | + |
| 91 | + if len(args) > 0 { |
| 92 | + paths = args |
| 93 | + } |
| 94 | + |
| 95 | + for _, p := range paths { |
| 96 | + |
| 97 | + if p == "/" { |
| 98 | + // TODO(chungers) -- this is a 'local' infrakit ensemble. |
| 99 | + // Absolute paths will come in a multi-cluster / federated model. |
| 100 | + return fmt.Errorf("No absolute path") |
| 101 | + } |
| 102 | + |
| 103 | + path := metadata_plugin.Path(p) |
| 104 | + first := path.Index(0) |
| 105 | + |
| 106 | + targets := []string{} // target plugins to query |
| 107 | + |
| 108 | + // Check all the plugins -- scanning via discovery |
| 109 | + if err := forPlugin(plugins, |
| 110 | + func(name string, mp metadata.Plugin) error { |
| 111 | + if p == "." || (first != nil && name == *first) { |
| 112 | + targets = append(targets, name) |
| 113 | + } |
| 114 | + return nil |
| 115 | + }); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + for _, target := range targets { |
| 120 | + |
| 121 | + nodes := []metadata.Path{} // the result set to print |
| 122 | + |
| 123 | + match, err := getPlugin(plugins, target) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if p == "." { |
| 129 | + if *all { |
| 130 | + allPaths, err := listAll(match, path.Shift(1)) |
| 131 | + if err != nil { |
| 132 | + log.Warningln("Cannot metadata ls on plugin", target, "err=", err) |
| 133 | + } |
| 134 | + for _, c := range allPaths { |
| 135 | + nodes = append(nodes, metadata_plugin.Path(target).Sub(c)) |
| 136 | + } |
| 137 | + } else { |
| 138 | + for _, t := range targets { |
| 139 | + nodes = append(nodes, metadata_plugin.Path(t)) |
| 140 | + } |
| 141 | + } |
| 142 | + } else { |
| 143 | + if *all { |
| 144 | + allPaths, err := listAll(match, path.Shift(1)) |
| 145 | + if err != nil { |
| 146 | + log.Warningln("Cannot metadata ls on plugin", target, "err=", err) |
| 147 | + } |
| 148 | + for _, c := range allPaths { |
| 149 | + nodes = append(nodes, metadata_plugin.Path(target).Sub(c)) |
| 150 | + } |
| 151 | + } else { |
| 152 | + children, err := match.List(path.Shift(1)) |
| 153 | + if err != nil { |
| 154 | + log.Warningln("Cannot metadata ls on plugin", target, "err=", err) |
| 155 | + } |
| 156 | + for _, c := range children { |
| 157 | + nodes = append(nodes, path.Join(c)) |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if len(targets) > 1 { |
| 163 | + fmt.Printf("%s:\n", target) |
| 164 | + } |
| 165 | + if *long { |
| 166 | + fmt.Printf("total %d:\n", len(nodes)) |
| 167 | + } |
| 168 | + for _, l := range nodes { |
| 169 | + if *long { |
| 170 | + fmt.Println(metadata_plugin.String(l)) |
| 171 | + } else { |
| 172 | + fmt.Println(metadata_plugin.String(l.Rel(path))) |
| 173 | + } |
| 174 | + } |
| 175 | + fmt.Println() |
| 176 | + } |
| 177 | + |
| 178 | + } |
| 179 | + return nil |
| 180 | + } |
| 181 | + |
| 182 | + cat := &cobra.Command{ |
| 183 | + Use: "cat", |
| 184 | + Short: "Get metadata entry by path", |
| 185 | + RunE: func(c *cobra.Command, args []string) error { |
| 186 | + |
| 187 | + for _, p := range args { |
| 188 | + |
| 189 | + path := metadata_plugin.Path(p) |
| 190 | + first := path.Index(0) |
| 191 | + if first != nil { |
| 192 | + match, err := getPlugin(plugins, *first) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + value, err := match.Get(path.Shift(1)) |
| 198 | + if err == nil { |
| 199 | + if value != nil { |
| 200 | + str := value.String() |
| 201 | + if s, err := strconv.Unquote(value.String()); err == nil { |
| 202 | + str = s |
| 203 | + } |
| 204 | + fmt.Println(str) |
| 205 | + } |
| 206 | + |
| 207 | + } else { |
| 208 | + log.Warningln("Cannot metadata cat on plugin", *first, "err=", err) |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + return nil |
| 213 | + }, |
| 214 | + } |
| 215 | + |
| 216 | + cmd.AddCommand(ls, cat) |
| 217 | + |
| 218 | + return cmd |
| 219 | +} |
0 commit comments