blob: f38466311bd81e9c6b1d4b2be7ce349caa66e866 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package main
2
3import (
4 "flag"
Bogdan Caprita5420f172014-10-10 15:58:14 -07005 "os"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07006
Jiri Simsa519c5072014-09-17 21:37:57 -07007 "veyron.io/veyron/veyron2/naming"
8 "veyron.io/veyron/veyron2/rt"
9 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070010
11 "veyron.io/veyron/veyron/lib/signals"
12 "veyron.io/veyron/veyron/profiles/roaming"
13 "veyron.io/veyron/veyron/services/mgmt/node/config"
14 "veyron.io/veyron/veyron/services/mgmt/node/impl"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070015)
16
Adam Sadovsky5181bdb2014-08-13 10:29:11 -070017var (
Bogdan Caprita5420f172014-10-10 15:58:14 -070018 publishAs = flag.String("name", "", "name to publish the node manager at")
19 installSelf = flag.Bool("install_self", false, "perform installation using environment and command-line flags")
20 installFrom = flag.String("install_from", "", "if not-empty, perform installation from the provided application envelope object name")
Adam Sadovsky5181bdb2014-08-13 10:29:11 -070021)
22
Jiri Simsa5293dcb2014-05-10 09:56:38 -070023func main() {
Jiri Simsa5293dcb2014-05-10 09:56:38 -070024 flag.Parse()
Matt Rosencrantz5180d162014-12-03 13:48:40 -080025 runtime, err := rt.New()
26 if err != nil {
27 vlog.Fatalf("Could not initialize runtime: %v", err)
28 }
Bogdan Caprita4258d882014-07-02 09:15:22 -070029 defer runtime.Cleanup()
Bogdan Caprita5420f172014-10-10 15:58:14 -070030
31 if len(*installFrom) > 0 {
32 if err := impl.InstallFrom(*installFrom); err != nil {
33 vlog.Errorf("InstallFrom failed: %v", err)
34 os.Exit(1)
35 }
36 return
37 }
38
39 if *installSelf {
Bogdan Capritad1748462014-11-08 16:49:46 -080040 // If the user specified a name to publish as, pass that through
41 // to the installed node manager script.
42 // TODO(caprita): Make the flag survive updates.
43 args := append([]string{"--name=" + *publishAs}, flag.Args()...)
44 if err := impl.SelfInstall(args, os.Environ()); err != nil {
Bogdan Caprita5420f172014-10-10 15:58:14 -070045 vlog.Errorf("SelfInstall failed: %v", err)
46 os.Exit(1)
47 }
48 return
49 }
50
Jiri Simsa5293dcb2014-05-10 09:56:38 -070051 server, err := runtime.NewServer()
52 if err != nil {
53 vlog.Fatalf("NewServer() failed: %v", err)
54 }
55 defer server.Stop()
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070056 endpoint, err := server.Listen(roaming.ListenSpec)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070057 if err != nil {
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070058 vlog.Fatalf("Listen(%s) failed: %v", roaming.ListenSpec, err)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070059 }
David Why Use Two When One Will Do Presotto8b4dbbf2014-11-06 10:50:14 -080060 name := naming.JoinAddressName(endpoint.String(), "")
Bogdan Capritac87a9142014-07-21 10:38:13 -070061 vlog.VI(0).Infof("Node manager object name: %v", name)
62 configState, err := config.Load()
63 if err != nil {
64 vlog.Fatalf("Failed to load config passed from parent: %v", err)
65 return
66 }
67 configState.Name = name
68 // TODO(caprita): We need a way to set config fields outside of the
69 // update mechanism (since that should ideally be an opaque
70 // implementation detail).
Matt Rosencrantz5180d162014-12-03 13:48:40 -080071 dispatcher, err := impl.NewDispatcher(runtime.Principal(), configState)
Bogdan Capritac87a9142014-07-21 10:38:13 -070072 if err != nil {
73 vlog.Fatalf("Failed to create dispatcher: %v", err)
74 }
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080075 if err := server.ServeDispatcher(*publishAs, dispatcher); err != nil {
Adam Sadovsky5181bdb2014-08-13 10:29:11 -070076 vlog.Fatalf("Serve(%v) failed: %v", *publishAs, err)
Jiri Simsa24e87aa2014-06-09 09:27:34 -070077 }
Bogdan Capritad1748462014-11-08 16:49:46 -080078 vlog.VI(0).Infof("Node manager published as: %v", *publishAs)
Matt Rosencrantz5180d162014-12-03 13:48:40 -080079 impl.InvokeCallback(runtime.NewContext(), name)
Bogdan Capritac87a9142014-07-21 10:38:13 -070080
Jiri Simsa5293dcb2014-05-10 09:56:38 -070081 // Wait until shutdown.
Matt Rosencrantzc7fecf12014-11-27 19:58:43 -080082 <-signals.ShutdownOnSignals(runtime)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070083}