blob: ef1837caaf03f235815517f2ecde1664e499df02 [file] [log] [blame]
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -07001// 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 Nicolaou3c8fdff2014-10-20 22:56:30 -07004package static
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -07005
6import (
Jiri Simsa764efb72014-12-25 20:57:03 -08007 "v.io/core/veyron2"
8 "v.io/core/veyron2/config"
9 "v.io/core/veyron2/ipc"
10 "v.io/core/veyron2/rt"
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070011
Jiri Simsa764efb72014-12-25 20:57:03 -080012 "v.io/core/veyron/lib/appcycle"
Jiri Simsa764efb72014-12-25 20:57:03 -080013 "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 Nicolaou3c50ac42014-12-23 07:40:19 -080021
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070022 // TODO(cnicolaou,ashankar): move this into flags.
Jiri Simsa764efb72014-12-25 20:57:03 -080023 sflag "v.io/core/veyron/security/flag"
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070024)
25
26var (
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070027 // ListenSpec is an initialized instance of ipc.ListenSpec that can
28 // be used with ipc.Listen.
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070029 ListenSpec ipc.ListenSpec
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070030)
31
32func init() {
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070033 rt.RegisterProfile(New())
34}
35
36type static struct {
37 gce string
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080038 ac *appcycle.AppCycle
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070039}
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.
43func New() veyron2.Profile {
44 return &static{}
45}
46
47func (p *static) Name() string {
48 return "static" + p.gce
49}
50
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080051func (p *static) Runtime() (string, []veyron2.ROpt) {
52 return "google", nil
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070053}
54
55func (*static) Platform() *veyron2.Platform {
Cosmos Nicolaou87c0a552014-12-02 23:05:49 -080056 pstr, _ := platform.Platform()
57 return pstr
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070058}
59
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080060func (p *static) Init(rt veyron2.Runtime, _ *config.Publisher) (veyron2.AppCycle, error) {
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070061 log := rt.Logger()
62
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080063 rt.ConfigureReservedName(debug.NewDispatcher(log.LogDir(), sflag.NewAuthorizerOrDie(), rt.VtraceStore()))
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070064
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070065 lf := commonFlags.ListenFlags()
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070066 ListenSpec = ipc.ListenSpec{
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080067 Addrs: ipc.ListenAddrs(lf.Addrs),
68 Proxy: lf.ListenProxy,
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070069 }
70
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080071 p.ac = appcycle.New()
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080072
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070073 // 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 Nicolaou39e3ae52014-11-14 13:30:01 -080082 return p.ac, nil
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070083 }
84 }
85 ListenSpec.AddressChooser = internal.IPAddressChooser
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080086 return p.ac, nil
87}
88
89func (p *static) Cleanup() {
90 if p.ac != nil {
91 p.ac.Shutdown()
92 }
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070093}
94
95func (p *static) String() string {
96 return "static profile on " + p.Platform().String()
97}