blob: 90372cca2c2b1d4982ffee595d57eeb488b8ce3d [file] [log] [blame]
Bogdan Caprita282bf382014-12-30 11:17:14 -08001package main
2
3import (
Bogdan Capritac7e72b62015-01-07 19:22:23 -08004 "fmt"
Bogdan Caprita282bf382014-12-30 11:17:14 -08005 "os"
6
7 "v.io/lib/cmdline"
8
9 "v.io/core/veyron/services/mgmt/device/impl"
Matt Rosencrantzfa3082c2015-01-22 21:39:04 -080010 "v.io/core/veyron2"
Bogdan Caprita282bf382014-12-30 11:17:14 -080011 "v.io/core/veyron2/vlog"
12)
13
Bogdan Capritac7e72b62015-01-07 19:22:23 -080014var (
15 installFrom string
16 suidHelper string
17 agent string
Bogdan Caprita29a3b352015-01-16 16:28:49 -080018 initHelper string
Bogdan Capritac7e72b62015-01-07 19:22:23 -080019 singleUser bool
20 sessionMode bool
Bogdan Caprita29a3b352015-01-16 16:28:49 -080021 initMode bool
Bogdan Capritac7e72b62015-01-07 19:22:23 -080022)
23
24const deviceDirEnv = "VANADIUM_DEVICE_DIR"
25
26func installationDir() string {
27 if d := os.Getenv(deviceDirEnv); d != "" {
28 return d
29 }
30 if d, err := os.Getwd(); err != nil {
31 vlog.Errorf("Failed to get current dir: %v", err)
32 return ""
33 } else {
34 return d
35 }
36}
Bogdan Caprita282bf382014-12-30 11:17:14 -080037
38var cmdInstall = &cmdline.Command{
39 Run: runInstall,
40 Name: "install",
41 Short: "Install the device manager.",
Bogdan Capritac7e72b62015-01-07 19:22:23 -080042 Long: fmt.Sprintf("Performs installation of device manager into %s (if the env var set), or into the current dir otherwise", deviceDirEnv),
Bogdan Caprita282bf382014-12-30 11:17:14 -080043 ArgsName: "[-- <arguments for device manager>]",
44 ArgsLong: `
45Arguments to be passed to the installed device manager`,
46}
47
48func init() {
49 cmdInstall.Flags.StringVar(&installFrom, "from", "", "if specified, performs the installation from the provided application envelope object name")
Bogdan Capritac7e72b62015-01-07 19:22:23 -080050 cmdInstall.Flags.StringVar(&suidHelper, "suid_helper", "", "path to suid helper")
51 cmdInstall.Flags.StringVar(&agent, "agent", "", "path to security agent")
Bogdan Caprita29a3b352015-01-16 16:28:49 -080052 cmdInstall.Flags.StringVar(&initHelper, "init_helper", "", "path to sysinit helper")
Bogdan Capritac7e72b62015-01-07 19:22:23 -080053 cmdInstall.Flags.BoolVar(&singleUser, "single_user", false, "if set, performs the installation assuming a single-user system")
54 cmdInstall.Flags.BoolVar(&sessionMode, "session_mode", false, "if set, installs the device manager to run a single session. Otherwise, the device manager is configured to get restarted upon exit")
Bogdan Caprita29a3b352015-01-16 16:28:49 -080055 cmdInstall.Flags.BoolVar(&initMode, "init_mode", false, "if set, installs the device manager with the system init service manager")
Bogdan Caprita282bf382014-12-30 11:17:14 -080056}
57
Bogdan Capritac7e72b62015-01-07 19:22:23 -080058func runInstall(cmd *cmdline.Command, args []string) error {
Bogdan Caprita282bf382014-12-30 11:17:14 -080059 if installFrom != "" {
60 // TODO(caprita): Also pass args into InstallFrom.
61 if err := impl.InstallFrom(installFrom); err != nil {
62 vlog.Errorf("InstallFrom(%v) failed: %v", installFrom, err)
63 return err
64 }
65 return nil
66 }
Bogdan Capritac7e72b62015-01-07 19:22:23 -080067 if suidHelper == "" {
68 return cmd.UsageErrorf("--suid_helper must be set")
69 }
70 if agent == "" {
71 return cmd.UsageErrorf("--agent must be set")
72 }
Bogdan Caprita29a3b352015-01-16 16:28:49 -080073 if initMode && initHelper == "" {
74 return cmd.UsageErrorf("--init_helper must be set")
75 }
76 if err := impl.SelfInstall(installationDir(), suidHelper, agent, initHelper, singleUser, sessionMode, initMode, args, os.Environ(), cmd.Stderr(), cmd.Stdout()); err != nil {
Bogdan Caprita282bf382014-12-30 11:17:14 -080077 vlog.Errorf("SelfInstall failed: %v", err)
78 return err
79 }
80 return nil
81}
82
83var cmdUninstall = &cmdline.Command{
84 Run: runUninstall,
85 Name: "uninstall",
86 Short: "Uninstall the device manager.",
Bogdan Capritac7e72b62015-01-07 19:22:23 -080087 Long: fmt.Sprintf("Removes the device manager installation from %s (if the env var set), or the current dir otherwise", deviceDirEnv),
Bogdan Caprita282bf382014-12-30 11:17:14 -080088}
89
Bogdan Caprita29a3b352015-01-16 16:28:49 -080090func runUninstall(cmd *cmdline.Command, _ []string) error {
91 if err := impl.Uninstall(installationDir(), cmd.Stderr(), cmd.Stdout()); err != nil {
Bogdan Caprita282bf382014-12-30 11:17:14 -080092 vlog.Errorf("Uninstall failed: %v", err)
93 return err
94 }
95 return nil
96}
Bogdan Capritac7e72b62015-01-07 19:22:23 -080097
98var cmdStart = &cmdline.Command{
99 Run: runStart,
100 Name: "start",
101 Short: "Start the device manager.",
102 Long: fmt.Sprintf("Starts the device manager installed under from %s (if the env var set), or the current dir otherwise", deviceDirEnv),
103}
104
Bogdan Caprita29a3b352015-01-16 16:28:49 -0800105func runStart(cmd *cmdline.Command, _ []string) error {
106 if err := impl.Start(installationDir(), cmd.Stderr(), cmd.Stdout()); err != nil {
Bogdan Capritac7e72b62015-01-07 19:22:23 -0800107 vlog.Errorf("Start failed: %v", err)
108 return err
109 }
110 return nil
111}
112
113var cmdStop = &cmdline.Command{
114 Run: runStop,
115 Name: "stop",
116 Short: "Stop the device manager.",
117 Long: fmt.Sprintf("Stops the device manager installed under from %s (if the env var set), or the current dir otherwise", deviceDirEnv),
118}
119
Bogdan Caprita29a3b352015-01-16 16:28:49 -0800120func runStop(cmd *cmdline.Command, _ []string) error {
Matt Rosencrantzfa3082c2015-01-22 21:39:04 -0800121 ctx, shutdown := veyron2.Init()
122 defer shutdown()
123 if err := impl.Stop(ctx, installationDir(), cmd.Stderr(), cmd.Stdout()); err != nil {
Bogdan Capritac7e72b62015-01-07 19:22:23 -0800124 vlog.Errorf("Stop failed: %v", err)
125 return err
126 }
127 return nil
128}
Bogdan Caprita54ae80e2015-01-20 13:37:52 -0800129
130var cmdProfile = &cmdline.Command{
131 Run: runProfile,
132 Name: "profile",
133 Short: "Dumps profile for the device manager.",
134 Long: "Prints the internal profile description for the device manager.",
135}
136
137func runProfile(cmd *cmdline.Command, _ []string) error {
138 spec, err := impl.ComputeDeviceProfile()
139 if err != nil {
140 vlog.Errorf("ComputeDeviceProfile failed: %v", err)
141 return err
142 }
143 fmt.Fprintf(cmd.Stdout(), "Profile: %#v\n", spec)
Robin Thellend753de652015-01-26 13:50:56 -0800144 desc, err := impl.Describe()
145 if err != nil {
146 vlog.Errorf("Describe failed: %v", err)
147 return err
148 }
149 fmt.Fprintf(cmd.Stdout(), "Description: %#v\n", desc)
Bogdan Caprita54ae80e2015-01-20 13:37:52 -0800150 return nil
151}