Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Bogdan Caprita | 858ec16 | 2015-01-21 15:09:22 -0800 | [diff] [blame] | 4 | "encoding/json" |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 5 | "fmt" |
| 6 | |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 7 | "v.io/core/veyron2" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 8 | "v.io/core/veyron2/ipc" |
| 9 | "v.io/core/veyron2/naming" |
| 10 | "v.io/core/veyron2/security" |
| 11 | "v.io/core/veyron2/services/mgmt/device" |
Todd Wang | 478fcf9 | 2014-12-26 12:37:37 -0800 | [diff] [blame] | 12 | "v.io/lib/cmdline" |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | var cmdInstall = &cmdline.Command{ |
| 16 | Run: runInstall, |
| 17 | Name: "install", |
| 18 | Short: "Install the given application.", |
| 19 | Long: "Install the given application.", |
Bogdan Caprita | 858ec16 | 2015-01-21 15:09:22 -0800 | [diff] [blame] | 20 | ArgsName: "<device> <application> [<config override>]", |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 21 | ArgsLong: ` |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 22 | <device> is the veyron object name of the device manager's app service. |
Bogdan Caprita | 858ec16 | 2015-01-21 15:09:22 -0800 | [diff] [blame] | 23 | |
| 24 | <application> is the veyron object name of the application. |
| 25 | |
| 26 | <config override> is an optional JSON-encoded device.Config object, of the form: |
| 27 | '{"flag1":"value1","flag2":"value2"}'.`, |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | func runInstall(cmd *cmdline.Command, args []string) error { |
Bogdan Caprita | 858ec16 | 2015-01-21 15:09:22 -0800 | [diff] [blame] | 31 | if expectedMin, expectedMax, got := 2, 3, len(args); expectedMin > got || expectedMax < got { |
| 32 | return cmd.UsageErrorf("install: incorrect number of arguments, expected between %d and %d, got %d", expectedMin, expectedMax, got) |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 33 | } |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 34 | deviceName, appName := args[0], args[1] |
Bogdan Caprita | 858ec16 | 2015-01-21 15:09:22 -0800 | [diff] [blame] | 35 | var cfg device.Config |
| 36 | if len(args) > 2 { |
| 37 | jsonConfig := args[2] |
| 38 | if err := json.Unmarshal([]byte(jsonConfig), &cfg); err != nil { |
| 39 | return fmt.Errorf("Unmarshal(%v) failed: %v", jsonConfig, err) |
| 40 | } |
| 41 | } |
| 42 | appID, err := device.ApplicationClient(deviceName).Install(gctx, appName, cfg) |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 43 | if err != nil { |
| 44 | return fmt.Errorf("Install failed: %v", err) |
| 45 | } |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 46 | fmt.Fprintf(cmd.Stdout(), "Successfully installed: %q\n", naming.Join(deviceName, appID)) |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 47 | return nil |
| 48 | } |
| 49 | |
| 50 | var cmdStart = &cmdline.Command{ |
| 51 | Run: runStart, |
| 52 | Name: "start", |
| 53 | Short: "Start an instance of the given application.", |
| 54 | Long: "Start an instance of the given application.", |
| 55 | ArgsName: "<application installation> <grant extension>", |
| 56 | ArgsLong: ` |
| 57 | <application installation> is the veyron object name of the |
| 58 | application installation from which to start an instance. |
Bogdan Caprita | 7b0f2f4 | 2014-11-10 17:17:02 -0800 | [diff] [blame] | 59 | |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 60 | <grant extension> is used to extend the default blessing of the |
| 61 | current principal when blessing the app instance.`, |
| 62 | } |
| 63 | |
| 64 | type granter struct { |
| 65 | ipc.CallOpt |
| 66 | p security.Principal |
| 67 | extension string |
| 68 | } |
| 69 | |
| 70 | func (g *granter) Grant(other security.Blessings) (security.Blessings, error) { |
| 71 | return g.p.Bless(other.PublicKey(), g.p.BlessingStore().Default(), g.extension, security.UnconstrainedUse()) |
| 72 | } |
| 73 | |
| 74 | func runStart(cmd *cmdline.Command, args []string) error { |
| 75 | if expected, got := 2, len(args); expected != got { |
| 76 | return cmd.UsageErrorf("start: incorrect number of arguments, expected %d, got %d", expected, got) |
| 77 | } |
| 78 | appInstallation, grant := args[0], args[1] |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 79 | principal := veyron2.GetPrincipal(gctx) |
| 80 | appInstanceIDs, err := device.ApplicationClient(appInstallation).Start(gctx, &granter{p: principal, extension: grant}) |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 81 | if err != nil { |
| 82 | return fmt.Errorf("Start failed: %v", err) |
| 83 | } |
| 84 | for _, id := range appInstanceIDs { |
| 85 | fmt.Fprintf(cmd.Stdout(), "Successfully started: %q\n", naming.Join(appInstallation, id)) |
| 86 | } |
| 87 | return nil |
| 88 | } |
| 89 | |
Bogdan Caprita | 7b0f2f4 | 2014-11-10 17:17:02 -0800 | [diff] [blame] | 90 | var cmdClaim = &cmdline.Command{ |
| 91 | Run: runClaim, |
| 92 | Name: "claim", |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 93 | Short: "Claim the device.", |
| 94 | Long: "Claim the device.", |
| 95 | ArgsName: "<device> <grant extension>", |
Bogdan Caprita | 7b0f2f4 | 2014-11-10 17:17:02 -0800 | [diff] [blame] | 96 | ArgsLong: ` |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 97 | <device> is the veyron object name of the device manager's app service. |
Bogdan Caprita | 7b0f2f4 | 2014-11-10 17:17:02 -0800 | [diff] [blame] | 98 | |
| 99 | <grant extension> is used to extend the default blessing of the |
| 100 | current principal when blessing the app instance.`, |
| 101 | } |
| 102 | |
| 103 | func runClaim(cmd *cmdline.Command, args []string) error { |
| 104 | if expected, got := 2, len(args); expected != got { |
| 105 | return cmd.UsageErrorf("claim: incorrect number of arguments, expected %d, got %d", expected, got) |
| 106 | } |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 107 | deviceName, grant := args[0], args[1] |
Matt Rosencrantz | f1c3b44 | 2015-01-12 17:53:08 -0800 | [diff] [blame] | 108 | principal := veyron2.GetPrincipal(gctx) |
| 109 | if err := device.DeviceClient(deviceName).Claim(gctx, &granter{p: principal, extension: grant}); err != nil { |
Bogdan Caprita | 7b0f2f4 | 2014-11-10 17:17:02 -0800 | [diff] [blame] | 110 | return fmt.Errorf("Claim failed: %v", err) |
| 111 | } |
| 112 | fmt.Fprintln(cmd.Stdout(), "Successfully claimed.") |
| 113 | return nil |
| 114 | } |
| 115 | |
Bogdan Caprita | 54ae80e | 2015-01-20 13:37:52 -0800 | [diff] [blame] | 116 | var cmdDescribe = &cmdline.Command{ |
| 117 | Run: runDescribe, |
| 118 | Name: "describe", |
| 119 | Short: "Describe the device.", |
| 120 | Long: "Describe the device.", |
| 121 | ArgsName: "<device>", |
| 122 | ArgsLong: ` |
| 123 | <device> is the veyron object name of the device manager's app service.`, |
| 124 | } |
| 125 | |
| 126 | func runDescribe(cmd *cmdline.Command, args []string) error { |
| 127 | if expected, got := 1, len(args); expected != got { |
| 128 | return cmd.UsageErrorf("describe: incorrect number of arguments, expected %d, got %d", expected, got) |
| 129 | } |
| 130 | deviceName := args[0] |
| 131 | if description, err := device.DeviceClient(deviceName).Describe(gctx); err != nil { |
| 132 | return fmt.Errorf("Describe failed: %v", err) |
| 133 | } else { |
| 134 | fmt.Fprintf(cmd.Stdout(), "%+v\n", description) |
| 135 | } |
| 136 | return nil |
| 137 | } |
| 138 | |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 139 | func root() *cmdline.Command { |
| 140 | return &cmdline.Command{ |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 141 | Name: "device", |
Bogdan Caprita | 2b21936 | 2014-12-09 17:03:33 -0800 | [diff] [blame] | 142 | Short: "Tool for interacting with the veyron device manager", |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 143 | Long: ` |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 144 | The device tool facilitates interaction with the veyron device manager. |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 145 | `, |
Bogdan Caprita | 54ae80e | 2015-01-20 13:37:52 -0800 | [diff] [blame] | 146 | Children: []*cmdline.Command{cmdInstall, cmdStart, associateRoot(), cmdDescribe, cmdClaim, cmdStop, cmdSuspend, cmdResume, aclRoot()}, |
Bogdan Caprita | 9b1a59a | 2014-11-09 18:33:47 -0800 | [diff] [blame] | 147 | } |
| 148 | } |