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