blob: b62fb96c7797cf8df35f77cd23ee7622df0d5dc8 [file] [log] [blame]
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -08001package main
2
3import (
Bogdan Caprita858ec162015-01-21 15:09:22 -08004 "encoding/json"
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -08005 "fmt"
6
Matt Rosencrantzf1c3b442015-01-12 17:53:08 -08007 "v.io/core/veyron2"
Jiri Simsa764efb72014-12-25 20:57:03 -08008 "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 Wang478fcf92014-12-26 12:37:37 -080012 "v.io/lib/cmdline"
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080013)
14
15var cmdInstall = &cmdline.Command{
16 Run: runInstall,
17 Name: "install",
18 Short: "Install the given application.",
19 Long: "Install the given application.",
Bogdan Caprita858ec162015-01-21 15:09:22 -080020 ArgsName: "<device> <application> [<config override>]",
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080021 ArgsLong: `
Bogdan Caprita2b219362014-12-09 17:03:33 -080022<device> is the veyron object name of the device manager's app service.
Bogdan Caprita858ec162015-01-21 15:09:22 -080023
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 Caprita9b1a59a2014-11-09 18:33:47 -080028}
29
30func runInstall(cmd *cmdline.Command, args []string) error {
Bogdan Caprita858ec162015-01-21 15:09:22 -080031 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 Caprita9b1a59a2014-11-09 18:33:47 -080033 }
Bogdan Caprita2b219362014-12-09 17:03:33 -080034 deviceName, appName := args[0], args[1]
Bogdan Caprita858ec162015-01-21 15:09:22 -080035 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 Caprita9b1a59a2014-11-09 18:33:47 -080043 if err != nil {
44 return fmt.Errorf("Install failed: %v", err)
45 }
Bogdan Caprita2b219362014-12-09 17:03:33 -080046 fmt.Fprintf(cmd.Stdout(), "Successfully installed: %q\n", naming.Join(deviceName, appID))
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080047 return nil
48}
49
50var 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
58application installation from which to start an instance.
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -080059
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080060<grant extension> is used to extend the default blessing of the
61current principal when blessing the app instance.`,
62}
63
64type granter struct {
65 ipc.CallOpt
66 p security.Principal
67 extension string
68}
69
70func (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
74func 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 Rosencrantzf1c3b442015-01-12 17:53:08 -080079 principal := veyron2.GetPrincipal(gctx)
80 appInstanceIDs, err := device.ApplicationClient(appInstallation).Start(gctx, &granter{p: principal, extension: grant})
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080081 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 Caprita7b0f2f42014-11-10 17:17:02 -080090var cmdClaim = &cmdline.Command{
91 Run: runClaim,
92 Name: "claim",
Bogdan Caprita2b219362014-12-09 17:03:33 -080093 Short: "Claim the device.",
94 Long: "Claim the device.",
95 ArgsName: "<device> <grant extension>",
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -080096 ArgsLong: `
Bogdan Caprita2b219362014-12-09 17:03:33 -080097<device> is the veyron object name of the device manager's app service.
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -080098
99<grant extension> is used to extend the default blessing of the
100current principal when blessing the app instance.`,
101}
102
103func 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 Caprita2b219362014-12-09 17:03:33 -0800107 deviceName, grant := args[0], args[1]
Matt Rosencrantzf1c3b442015-01-12 17:53:08 -0800108 principal := veyron2.GetPrincipal(gctx)
109 if err := device.DeviceClient(deviceName).Claim(gctx, &granter{p: principal, extension: grant}); err != nil {
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -0800110 return fmt.Errorf("Claim failed: %v", err)
111 }
112 fmt.Fprintln(cmd.Stdout(), "Successfully claimed.")
113 return nil
114}
115
Bogdan Caprita54ae80e2015-01-20 13:37:52 -0800116var 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
126func 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 Caprita9b1a59a2014-11-09 18:33:47 -0800139func root() *cmdline.Command {
140 return &cmdline.Command{
Bogdan Capritaa456f472014-12-10 10:18:03 -0800141 Name: "device",
Bogdan Caprita2b219362014-12-09 17:03:33 -0800142 Short: "Tool for interacting with the veyron device manager",
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -0800143 Long: `
Bogdan Capritaa456f472014-12-10 10:18:03 -0800144The device tool facilitates interaction with the veyron device manager.
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -0800145`,
Bogdan Caprita54ae80e2015-01-20 13:37:52 -0800146 Children: []*cmdline.Command{cmdInstall, cmdStart, associateRoot(), cmdDescribe, cmdClaim, cmdStop, cmdSuspend, cmdResume, aclRoot()},
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -0800147 }
148}