Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 1 | package main |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 4 | "errors" |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 5 | "fmt" |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 6 | "time" |
| 7 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 8 | "v.io/core/veyron2" |
Matt Rosencrantz | 89445a4 | 2015-01-05 13:32:37 -0800 | [diff] [blame] | 9 | "v.io/core/veyron2/context" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 10 | "v.io/core/veyron2/ipc" |
| 11 | "v.io/core/veyron2/naming" |
| 12 | "v.io/core/veyron2/options" |
Todd Wang | 478fcf9 | 2014-12-26 12:37:37 -0800 | [diff] [blame] | 13 | "v.io/lib/cmdline" |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | var cmdGlob = &cmdline.Command{ |
| 17 | Run: runGlob, |
| 18 | Name: "glob", |
| 19 | Short: "returns all matching entries in the mount table", |
| 20 | Long: "returns all matching entries in the mount table", |
Bogdan Caprita | decd105 | 2014-11-08 16:15:54 -0800 | [diff] [blame] | 21 | ArgsName: "[<mount name>] <pattern>", |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 22 | ArgsLong: ` |
Bogdan Caprita | decd105 | 2014-11-08 16:15:54 -0800 | [diff] [blame] | 23 | <mount name> is a mount name on a mount table. Defaults to namespace root. |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 24 | <pattern> is a glob pattern that is matched against all the entries below the |
| 25 | specified mount name. |
| 26 | `, |
| 27 | } |
| 28 | |
| 29 | func runGlob(cmd *cmdline.Command, args []string) error { |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 30 | ctx, cancel := context.WithTimeout(gctx, time.Minute) |
Matt Rosencrantz | d599e38 | 2015-01-12 11:13:32 -0800 | [diff] [blame] | 31 | defer cancel() |
| 32 | |
Bogdan Caprita | decd105 | 2014-11-08 16:15:54 -0800 | [diff] [blame] | 33 | if len(args) == 1 { |
Matt Rosencrantz | d599e38 | 2015-01-12 11:13:32 -0800 | [diff] [blame] | 34 | roots := veyron2.GetNamespace(ctx).Roots() |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 35 | if len(roots) == 0 { |
| 36 | return errors.New("no namespace root") |
| 37 | } |
| 38 | args = append([]string{roots[0]}, args...) |
Bogdan Caprita | decd105 | 2014-11-08 16:15:54 -0800 | [diff] [blame] | 39 | } |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 40 | if expected, got := 2, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 41 | return cmd.UsageErrorf("glob: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 42 | } |
Matt Rosencrantz | d599e38 | 2015-01-12 11:13:32 -0800 | [diff] [blame] | 43 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 44 | name, pattern := args[0], args[1] |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 45 | client := veyron2.GetClient(ctx) |
| 46 | call, err := client.StartCall(ctx, name, ipc.GlobMethod, []interface{}{pattern}, options.NoResolve{}) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 47 | if err != nil { |
| 48 | return err |
| 49 | } |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 50 | for { |
| 51 | var me naming.VDLMountEntry |
| 52 | if err := call.Recv(&me); err != nil { |
| 53 | break |
| 54 | } |
| 55 | fmt.Fprint(cmd.Stdout(), me.Name) |
| 56 | for _, s := range me.Servers { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 57 | fmt.Fprintf(cmd.Stdout(), " %s (TTL %s)", s.Server, time.Duration(s.TTL)*time.Second) |
| 58 | } |
| 59 | fmt.Fprintln(cmd.Stdout()) |
| 60 | } |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 61 | if ferr := call.Finish(&err); ferr != nil { |
| 62 | err = ferr |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 63 | } |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 64 | return err |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | var cmdMount = &cmdline.Command{ |
| 68 | Run: runMount, |
| 69 | Name: "mount", |
| 70 | Short: "Mounts a server <name> onto a mount table", |
| 71 | Long: "Mounts a server <name> onto a mount table", |
| 72 | ArgsName: "<mount name> <name> <ttl>", |
| 73 | ArgsLong: ` |
| 74 | <mount name> is a mount name on a mount table. |
Bogdan Caprita | d9281a3 | 2014-07-02 14:40:39 -0700 | [diff] [blame] | 75 | <name> is the rooted object name of the server. |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 76 | <ttl> is the TTL of the new entry. It is a decimal number followed by a unit |
| 77 | suffix (s, m, h). A value of 0s represents an infinite duration. |
| 78 | `, |
| 79 | } |
| 80 | |
| 81 | func runMount(cmd *cmdline.Command, args []string) error { |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 82 | got := len(args) |
| 83 | if got < 2 || got > 4 { |
| 84 | return cmd.UsageErrorf("mount: incorrect number of arguments, expected 2, 3, or 4, got %d", got) |
| 85 | } |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 86 | var flags naming.MountFlag |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 87 | var seconds uint32 |
| 88 | if got >= 3 { |
| 89 | ttl, err := time.ParseDuration(args[2]) |
| 90 | if err != nil { |
| 91 | return fmt.Errorf("TTL parse error: %v", err) |
| 92 | } |
| 93 | seconds = uint32(ttl.Seconds()) |
| 94 | } |
| 95 | if got >= 4 { |
| 96 | for _, c := range args[3] { |
| 97 | switch c { |
| 98 | case 'M': |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 99 | flags |= naming.MountFlag(naming.MT) |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 100 | case 'R': |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 101 | flags |= naming.MountFlag(naming.Replace) |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 104 | } |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 105 | ctx, cancel := context.WithTimeout(gctx, time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 106 | defer cancel() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 107 | client := veyron2.GetClient(ctx) |
| 108 | call, err := client.StartCall(ctx, args[0], "Mount", []interface{}{args[1], seconds, 0}, options.NoResolve{}) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 109 | if err != nil { |
| 110 | return err |
| 111 | } |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 112 | if ierr := call.Finish(&err); ierr != nil { |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 113 | err = ierr |
| 114 | } |
| 115 | if err != nil { |
| 116 | return err |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 117 | } |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 118 | |
| 119 | fmt.Fprintln(cmd.Stdout(), "Name mounted successfully.") |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | var cmdUnmount = &cmdline.Command{ |
| 124 | Run: runUnmount, |
| 125 | Name: "unmount", |
| 126 | Short: "removes server <name> from the mount table", |
| 127 | Long: "removes server <name> from the mount table", |
| 128 | ArgsName: "<mount name> <name>", |
| 129 | ArgsLong: ` |
| 130 | <mount name> is a mount name on a mount table. |
Bogdan Caprita | d9281a3 | 2014-07-02 14:40:39 -0700 | [diff] [blame] | 131 | <name> is the rooted object name of the server. |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 132 | `, |
| 133 | } |
| 134 | |
| 135 | func runUnmount(cmd *cmdline.Command, args []string) error { |
| 136 | if expected, got := 2, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 137 | return cmd.UsageErrorf("unmount: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 138 | } |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 139 | ctx, cancel := context.WithTimeout(gctx, time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 140 | defer cancel() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 141 | client := veyron2.GetClient(ctx) |
| 142 | call, err := client.StartCall(ctx, args[0], "Unmount", []interface{}{args[1]}, options.NoResolve{}) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 143 | if err != nil { |
| 144 | return err |
| 145 | } |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 146 | if ierr := call.Finish(&err); ierr != nil { |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 147 | err = ierr |
| 148 | } |
| 149 | if err != nil { |
| 150 | return err |
David Why Use Two When One Will Do Presotto | 59a254c | 2014-10-30 13:09:29 -0700 | [diff] [blame] | 151 | } |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 152 | |
| 153 | fmt.Fprintln(cmd.Stdout(), "Name unmounted successfully.") |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 154 | return err |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | var cmdResolveStep = &cmdline.Command{ |
| 158 | Run: runResolveStep, |
| 159 | Name: "resolvestep", |
| 160 | Short: "takes the next step in resolving a name.", |
| 161 | Long: "takes the next step in resolving a name.", |
| 162 | ArgsName: "<mount name>", |
| 163 | ArgsLong: ` |
| 164 | <mount name> is a mount name on a mount table. |
| 165 | `, |
| 166 | } |
| 167 | |
| 168 | func runResolveStep(cmd *cmdline.Command, args []string) error { |
| 169 | if expected, got := 1, len(args); expected != got { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 170 | return cmd.UsageErrorf("mount: incorrect number of arguments, expected %d, got %d", expected, got) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 171 | } |
Matt Rosencrantz | a5ad272 | 2015-01-22 11:17:47 -0800 | [diff] [blame] | 172 | ctx, cancel := context.WithTimeout(gctx, time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 173 | defer cancel() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 174 | client := veyron2.GetClient(ctx) |
David Why Use Two When One Will Do Presotto | d3aa663 | 2015-01-20 10:15:39 -0800 | [diff] [blame] | 175 | call, err := client.StartCall(ctx, args[0], "ResolveStep", []interface{}{}, options.NoResolve{}) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 176 | if err != nil { |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 177 | return err |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 178 | } |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 179 | var entry naming.VDLMountEntry |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 180 | if ierr := call.Finish(&entry, &err); ierr != nil { |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 181 | err = ierr |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 182 | } |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | |
David Why Use Two When One Will Do Presotto | 6f9f574 | 2014-10-20 16:27:05 -0700 | [diff] [blame] | 187 | fmt.Fprintf(cmd.Stdout(), "Servers: %v Suffix: %q MT: %v\n", entry.Servers, entry.Name, entry.MT) |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 188 | return nil |
| 189 | } |
| 190 | |
Robin Thellend | 18205cf | 2014-10-21 13:53:59 -0700 | [diff] [blame] | 191 | func root() *cmdline.Command { |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 192 | return &cmdline.Command{ |
Todd Wang | fcb72a5 | 2014-10-01 09:53:56 -0700 | [diff] [blame] | 193 | Name: "mounttable", |
| 194 | Short: "Tool for interacting with a Veyron mount table", |
| 195 | Long: ` |
| 196 | The mounttable tool facilitates interaction with a Veyron mount table. |
| 197 | `, |
Robin Thellend | 6b17da7 | 2014-05-14 09:55:14 -0700 | [diff] [blame] | 198 | Children: []*cmdline.Command{cmdGlob, cmdMount, cmdUnmount, cmdResolveStep}, |
| 199 | } |
| 200 | } |