blob: 9758d72ec4e04479b62dfa5ae0abae9bc5d27bb6 [file] [log] [blame]
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -08001package main
2
3import (
4 "fmt"
5
6 "veyron.io/veyron/veyron2/ipc"
7 "veyron.io/veyron/veyron2/naming"
8 "veyron.io/veyron/veyron2/rt"
9 "veyron.io/veyron/veyron2/security"
10 "veyron.io/veyron/veyron2/services/mgmt/node"
11
12 "veyron.io/veyron/veyron/lib/cmdline"
13)
14
15var cmdInstall = &cmdline.Command{
16 Run: runInstall,
17 Name: "install",
18 Short: "Install the given application.",
19 Long: "Install the given application.",
20 ArgsName: "<node> <application>",
21 ArgsLong: `
22<node> is the veyron object name of the node manager's app service.
23<application> is the veyron object name of the application.`,
24}
25
26func runInstall(cmd *cmdline.Command, args []string) error {
27 if expected, got := 2, len(args); expected != got {
28 return cmd.UsageErrorf("install: incorrect number of arguments, expected %d, got %d", expected, got)
29 }
30 nodeName, appName := args[0], args[1]
31 appID, err := node.ApplicationClient(nodeName).Install(rt.R().NewContext(), appName)
32 if err != nil {
33 return fmt.Errorf("Install failed: %v", err)
34 }
35 fmt.Fprintf(cmd.Stdout(), "Successfully installed: %q\n", naming.Join(nodeName, appID))
36 return nil
37}
38
39var cmdStart = &cmdline.Command{
40 Run: runStart,
41 Name: "start",
42 Short: "Start an instance of the given application.",
43 Long: "Start an instance of the given application.",
44 ArgsName: "<application installation> <grant extension>",
45 ArgsLong: `
46<application installation> is the veyron object name of the
47application installation from which to start an instance.
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -080048
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -080049<grant extension> is used to extend the default blessing of the
50current principal when blessing the app instance.`,
51}
52
53type granter struct {
54 ipc.CallOpt
55 p security.Principal
56 extension string
57}
58
59func (g *granter) Grant(other security.Blessings) (security.Blessings, error) {
60 return g.p.Bless(other.PublicKey(), g.p.BlessingStore().Default(), g.extension, security.UnconstrainedUse())
61}
62
63func runStart(cmd *cmdline.Command, args []string) error {
64 if expected, got := 2, len(args); expected != got {
65 return cmd.UsageErrorf("start: incorrect number of arguments, expected %d, got %d", expected, got)
66 }
67 appInstallation, grant := args[0], args[1]
68 appInstanceIDs, err := node.ApplicationClient(appInstallation).Start(rt.R().NewContext(), &granter{p: rt.R().Principal(), extension: grant})
69 if err != nil {
70 return fmt.Errorf("Start failed: %v", err)
71 }
72 for _, id := range appInstanceIDs {
73 fmt.Fprintf(cmd.Stdout(), "Successfully started: %q\n", naming.Join(appInstallation, id))
74 }
75 return nil
76}
77
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -080078var cmdClaim = &cmdline.Command{
79 Run: runClaim,
80 Name: "claim",
81 Short: "Claim the node.",
82 Long: "Claim the node.",
83 ArgsName: "<node> <grant extension>",
84 ArgsLong: `
85<node> is the veyron object name of the node manager's app service.
86
87<grant extension> is used to extend the default blessing of the
88current principal when blessing the app instance.`,
89}
90
91func runClaim(cmd *cmdline.Command, args []string) error {
92 if expected, got := 2, len(args); expected != got {
93 return cmd.UsageErrorf("claim: incorrect number of arguments, expected %d, got %d", expected, got)
94 }
95 nodeName, grant := args[0], args[1]
96 if err := node.NodeClient(nodeName).Claim(rt.R().NewContext(), &granter{p: rt.R().Principal(), extension: grant}); err != nil {
97 return fmt.Errorf("Claim failed: %v", err)
98 }
99 fmt.Fprintln(cmd.Stdout(), "Successfully claimed.")
100 return nil
101}
102
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -0800103func root() *cmdline.Command {
104 return &cmdline.Command{
105 Name: "node",
106 Short: "Tool for interacting with the veyron node manager",
107 Long: `
108The node tool facilitates interaction with the veyron node manager.
109`,
Bogdan Caprita7b0f2f42014-11-10 17:17:02 -0800110 Children: []*cmdline.Command{cmdInstall, cmdStart, cmdClaim},
Bogdan Caprita9b1a59a2014-11-09 18:33:47 -0800111 }
112}