blob: 2600b4e7c976035a5db77295361ef6283c2beae7 [file] [log] [blame]
Bogdan Capritad8204052015-01-28 10:40:34 -08001package impl
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -08002
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 Caprita8964d3f2015-02-02 13:47:39 -080020 ArgsName: "<device> <application>",
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.
Bogdan Caprita8964d3f2015-02-02 13:47:39 -080025`,
26}
Bogdan Caprita858ec162015-01-21 15:09:22 -080027
Bogdan Caprita8964d3f2015-02-02 13:47:39 -080028type configFlag device.Config
29
30func (c *configFlag) String() string {
31 jsonConfig, _ := json.Marshal(c)
32 return string(jsonConfig)
33}
34func (c *configFlag) Set(s string) error {
35 if err := json.Unmarshal([]byte(s), c); err != nil {
36 return fmt.Errorf("Unmarshal(%v) failed: %v", s, err)
37 }
38 return nil
39}
40
41var configOverride configFlag = configFlag{}
42
43func init() {
44 cmdInstall.Flags.Var(&configOverride, "config", "JSON-encoded device.Config object, of the form: '{\"flag1\":\"value1\",\"flag2\":\"value2\"}'")
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080045}
46
47func runInstall(cmd *cmdline.Command, args []string) error {
Bogdan Caprita8964d3f2015-02-02 13:47:39 -080048 if expected, got := 2, len(args); expected != got {
49 return cmd.UsageErrorf("install: incorrect number of arguments, expected %d, got %d", expected, got)
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080050 }
Bogdan Caprita2b219362014-12-09 17:03:33 -080051 deviceName, appName := args[0], args[1]
Bogdan Caprita8964d3f2015-02-02 13:47:39 -080052 appID, err := device.ApplicationClient(deviceName).Install(gctx, appName, device.Config(configOverride))
53 // Reset the value for any future invocations of "install" or
54 // "install-local" (we run more than one command per process in unit
55 // tests).
56 configOverride = configFlag{}
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080057 if err != nil {
58 return fmt.Errorf("Install failed: %v", err)
59 }
Bogdan Caprita2b219362014-12-09 17:03:33 -080060 fmt.Fprintf(cmd.Stdout(), "Successfully installed: %q\n", naming.Join(deviceName, appID))
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080061 return nil
62}
63
64var cmdStart = &cmdline.Command{
65 Run: runStart,
66 Name: "start",
67 Short: "Start an instance of the given application.",
68 Long: "Start an instance of the given application.",
69 ArgsName: "<application installation> <grant extension>",
70 ArgsLong: `
71<application installation> is the veyron object name of the
72application installation from which to start an instance.
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -080073
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080074<grant extension> is used to extend the default blessing of the
75current principal when blessing the app instance.`,
76}
77
78type granter struct {
79 ipc.CallOpt
80 p security.Principal
81 extension string
82}
83
84func (g *granter) Grant(other security.Blessings) (security.Blessings, error) {
85 return g.p.Bless(other.PublicKey(), g.p.BlessingStore().Default(), g.extension, security.UnconstrainedUse())
86}
87
88func runStart(cmd *cmdline.Command, args []string) error {
89 if expected, got := 2, len(args); expected != got {
90 return cmd.UsageErrorf("start: incorrect number of arguments, expected %d, got %d", expected, got)
91 }
92 appInstallation, grant := args[0], args[1]
Matt Rosencrantzf1c3b442015-01-12 17:53:08 -080093 principal := veyron2.GetPrincipal(gctx)
94 appInstanceIDs, err := device.ApplicationClient(appInstallation).Start(gctx, &granter{p: principal, extension: grant})
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080095 if err != nil {
96 return fmt.Errorf("Start failed: %v", err)
97 }
98 for _, id := range appInstanceIDs {
99 fmt.Fprintf(cmd.Stdout(), "Successfully started: %q\n", naming.Join(appInstallation, id))
100 }
101 return nil
102}
103
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -0800104var cmdClaim = &cmdline.Command{
105 Run: runClaim,
106 Name: "claim",
Bogdan Caprita2b219362014-12-09 17:03:33 -0800107 Short: "Claim the device.",
108 Long: "Claim the device.",
109 ArgsName: "<device> <grant extension>",
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -0800110 ArgsLong: `
Bogdan Capritad8373a12015-01-28 19:52:37 -0800111<device> is the veyron object name of the device manager's device service.
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -0800112
113<grant extension> is used to extend the default blessing of the
114current principal when blessing the app instance.`,
115}
116
117func runClaim(cmd *cmdline.Command, args []string) error {
118 if expected, got := 2, len(args); expected != got {
119 return cmd.UsageErrorf("claim: incorrect number of arguments, expected %d, got %d", expected, got)
120 }
Bogdan Caprita2b219362014-12-09 17:03:33 -0800121 deviceName, grant := args[0], args[1]
Matt Rosencrantzf1c3b442015-01-12 17:53:08 -0800122 principal := veyron2.GetPrincipal(gctx)
123 if err := device.DeviceClient(deviceName).Claim(gctx, &granter{p: principal, extension: grant}); err != nil {
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -0800124 return fmt.Errorf("Claim failed: %v", err)
125 }
126 fmt.Fprintln(cmd.Stdout(), "Successfully claimed.")
127 return nil
128}
129
Bogdan Caprita54ae80e2015-01-20 13:37:52 -0800130var cmdDescribe = &cmdline.Command{
131 Run: runDescribe,
132 Name: "describe",
133 Short: "Describe the device.",
134 Long: "Describe the device.",
135 ArgsName: "<device>",
136 ArgsLong: `
Bogdan Capritad8373a12015-01-28 19:52:37 -0800137<device> is the veyron object name of the device manager's device service.`,
Bogdan Caprita54ae80e2015-01-20 13:37:52 -0800138}
139
140func runDescribe(cmd *cmdline.Command, args []string) error {
141 if expected, got := 1, len(args); expected != got {
142 return cmd.UsageErrorf("describe: incorrect number of arguments, expected %d, got %d", expected, got)
143 }
144 deviceName := args[0]
145 if description, err := device.DeviceClient(deviceName).Describe(gctx); err != nil {
146 return fmt.Errorf("Describe failed: %v", err)
147 } else {
148 fmt.Fprintf(cmd.Stdout(), "%+v\n", description)
149 }
150 return nil
151}
Bogdan Capritad8373a12015-01-28 19:52:37 -0800152
Robin Thellendd9ffea92015-01-29 09:47:37 -0800153var cmdUpdate = &cmdline.Command{
154 Run: runUpdate,
155 Name: "update",
156 Short: "Update the device manager or application",
157 Long: "Update the device manager or application",
158 ArgsName: "<object>",
159 ArgsLong: `
160<object> is the veyron object name of the device manager or application
161installation to update.`,
162}
163
164func runUpdate(cmd *cmdline.Command, args []string) error {
165 if expected, got := 1, len(args); expected != got {
166 return cmd.UsageErrorf("update: incorrect number of arguments, expected %d, got %d", expected, got)
167 }
168 deviceName := args[0]
169 if err := device.ApplicationClient(deviceName).Update(gctx); err != nil {
170 return err
171 }
172 fmt.Fprintln(cmd.Stdout(), "Update successful.")
173 return nil
174}
175
176var cmdRevert = &cmdline.Command{
177 Run: runRevert,
178 Name: "revert",
179 Short: "Revert the device manager or application",
180 Long: "Revert the device manager or application to its previous version",
181 ArgsName: "<object>",
182 ArgsLong: `
183<object> is the veyron object name of the device manager or application
184installation to revert.`,
185}
186
187func runRevert(cmd *cmdline.Command, args []string) error {
188 if expected, got := 1, len(args); expected != got {
189 return cmd.UsageErrorf("revert: incorrect number of arguments, expected %d, got %d", expected, got)
190 }
191 deviceName := args[0]
192 if err := device.ApplicationClient(deviceName).Revert(gctx); err != nil {
193 return err
194 }
195 fmt.Fprintln(cmd.Stdout(), "Revert successful.")
196 return nil
197}
198
Bogdan Capritad8373a12015-01-28 19:52:37 -0800199var cmdDebug = &cmdline.Command{
200 Run: runDebug,
201 Name: "debug",
202 Short: "Debug the device.",
203 Long: "Debug the device.",
204 ArgsName: "<device>",
205 ArgsLong: `
206<device> is the veyron object name of an app installation or instance.`,
207}
208
209func runDebug(cmd *cmdline.Command, args []string) error {
210 if expected, got := 1, len(args); expected != got {
211 return cmd.UsageErrorf("debug: incorrect number of arguments, expected %d, got %d", expected, got)
212 }
213 deviceName := args[0]
214 if description, err := device.DeviceClient(deviceName).Debug(gctx); err != nil {
215 return fmt.Errorf("Debug failed: %v", err)
216 } else {
217 fmt.Fprintf(cmd.Stdout(), "%v\n", description)
218 }
219 return nil
220}