Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 1 | // Package static provides a network-aware Profile that provides appropriate |
| 2 | // options and configuration for a variety of network configurations, including |
| 3 | // being behind 1-1 NATs, but without the ability to respond to dhcp changes. |
Cosmos Nicolaou | 3c8fdff | 2014-10-20 22:56:30 -0700 | [diff] [blame] | 4 | package static |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 5 | |
| 6 | import ( |
| 7 | "flag" |
| 8 | |
| 9 | "veyron.io/veyron/veyron2" |
| 10 | "veyron.io/veyron/veyron2/config" |
| 11 | "veyron.io/veyron/veyron2/ipc" |
| 12 | "veyron.io/veyron/veyron2/rt" |
| 13 | |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 14 | "veyron.io/veyron/veyron/lib/appcycle" |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron/lib/flags" |
| 16 | "veyron.io/veyron/veyron/lib/netstate" |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame^] | 17 | _ "veyron.io/veyron/veyron/lib/tcp" |
| 18 | _ "veyron.io/veyron/veyron/lib/websocket" |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 19 | "veyron.io/veyron/veyron/profiles/internal" |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame^] | 20 | "veyron.io/veyron/veyron/profiles/internal/platform" |
| 21 | _ "veyron.io/veyron/veyron/runtimes/google/rt" |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 22 | "veyron.io/veyron/veyron/services/mgmt/debug" |
| 23 | // TODO(cnicolaou,ashankar): move this into flags. |
| 24 | sflag "veyron.io/veyron/veyron/security/flag" |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var ( |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 28 | commonFlags *flags.Flags |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 29 | // ListenSpec is an initialized instance of ipc.ListenSpec that can |
| 30 | // be used with ipc.Listen. |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 31 | ListenSpec ipc.ListenSpec |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | func init() { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 35 | commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen) |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 36 | rt.RegisterProfile(New()) |
| 37 | } |
| 38 | |
| 39 | type static struct { |
| 40 | gce string |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 41 | ac *appcycle.AppCycle |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // New returns a new instance of a very static Profile. It can be used |
| 45 | // as a default by Runtime implementations, in unit tests etc. |
| 46 | func New() veyron2.Profile { |
| 47 | return &static{} |
| 48 | } |
| 49 | |
| 50 | func (p *static) Name() string { |
| 51 | return "static" + p.gce |
| 52 | } |
| 53 | |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 54 | func (p *static) Runtime() (string, []veyron2.ROpt) { |
| 55 | return "google", nil |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func (*static) Platform() *veyron2.Platform { |
Cosmos Nicolaou | 87c0a55 | 2014-12-02 23:05:49 -0800 | [diff] [blame^] | 59 | pstr, _ := platform.Platform() |
| 60 | return pstr |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 63 | func (p *static) Init(rt veyron2.Runtime, _ *config.Publisher) (veyron2.AppCycle, error) { |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 64 | log := rt.Logger() |
| 65 | |
Matt Rosencrantz | b30286b | 2014-11-10 14:52:17 -0800 | [diff] [blame] | 66 | rt.ConfigureReservedName(debug.NewDispatcher(log.LogDir(), sflag.NewAuthorizerOrDie(), rt.VtraceStore())) |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 67 | |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 68 | lf := commonFlags.ListenFlags() |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 69 | ListenSpec = ipc.ListenSpec{ |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 70 | Protocol: lf.ListenProtocol.Protocol, |
| 71 | Address: lf.ListenAddress.String(), |
| 72 | Proxy: lf.ListenProxy, |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 75 | p.ac = appcycle.New(rt) |
| 76 | |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 77 | // Our address is private, so we test for running on GCE and for its |
| 78 | // 1:1 NAT configuration. GCEPublicAddress returns a non-nil addr |
| 79 | // if we are indeed running on GCE. |
| 80 | if !internal.HasPublicIP(log) { |
| 81 | if addr := internal.GCEPublicAddress(log); addr != nil { |
| 82 | ListenSpec.AddressChooser = func(string, []ipc.Address) ([]ipc.Address, error) { |
| 83 | return []ipc.Address{&netstate.AddrIfc{addr, "nat", nil}}, nil |
| 84 | } |
| 85 | p.gce = "+gce" |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 86 | return p.ac, nil |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | ListenSpec.AddressChooser = internal.IPAddressChooser |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 90 | return p.ac, nil |
| 91 | } |
| 92 | |
| 93 | func (p *static) Cleanup() { |
| 94 | if p.ac != nil { |
| 95 | p.ac.Shutdown() |
| 96 | } |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func (p *static) String() string { |
| 100 | return "static profile on " + p.Platform().String() |
| 101 | } |