blob: 91ab924c1e56d45f89b631aa9cd41a3bd9901b47 [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 (
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 Nicolaou39e3ae52014-11-14 13:30:01 -080014 "veyron.io/veyron/veyron/lib/appcycle"
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070015 "veyron.io/veyron/veyron/lib/flags"
16 "veyron.io/veyron/veyron/lib/netstate"
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070017 "veyron.io/veyron/veyron/profiles/internal"
Cosmos Nicolaou87c0a552014-12-02 23:05:49 -080018 "veyron.io/veyron/veyron/profiles/internal/platform"
Cosmos Nicolaou3c50ac42014-12-23 07:40:19 -080019 _ "veyron.io/veyron/veyron/runtimes/google/ipc/protocols/tcp"
20 _ "veyron.io/veyron/veyron/runtimes/google/ipc/protocols/ws"
21 _ "veyron.io/veyron/veyron/runtimes/google/ipc/protocols/wsh"
Cosmos Nicolaou87c0a552014-12-02 23:05:49 -080022 _ "veyron.io/veyron/veyron/runtimes/google/rt"
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070023 "veyron.io/veyron/veyron/services/mgmt/debug"
Cosmos Nicolaou3c50ac42014-12-23 07:40:19 -080024
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070025 // TODO(cnicolaou,ashankar): move this into flags.
26 sflag "veyron.io/veyron/veyron/security/flag"
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070027)
28
29var (
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -070030 commonFlags *flags.Flags
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070031 // ListenSpec is an initialized instance of ipc.ListenSpec that can
32 // be used with ipc.Listen.
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070033 ListenSpec ipc.ListenSpec
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070034)
35
36func init() {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070037 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070038 rt.RegisterProfile(New())
39}
40
41type static struct {
42 gce string
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080043 ac *appcycle.AppCycle
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070044}
45
46// New returns a new instance of a very static Profile. It can be used
47// as a default by Runtime implementations, in unit tests etc.
48func New() veyron2.Profile {
49 return &static{}
50}
51
52func (p *static) Name() string {
53 return "static" + p.gce
54}
55
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080056func (p *static) Runtime() (string, []veyron2.ROpt) {
57 return "google", nil
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070058}
59
60func (*static) Platform() *veyron2.Platform {
Cosmos Nicolaou87c0a552014-12-02 23:05:49 -080061 pstr, _ := platform.Platform()
62 return pstr
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070063}
64
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080065func (p *static) Init(rt veyron2.Runtime, _ *config.Publisher) (veyron2.AppCycle, error) {
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070066 log := rt.Logger()
67
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080068 rt.ConfigureReservedName(debug.NewDispatcher(log.LogDir(), sflag.NewAuthorizerOrDie(), rt.VtraceStore()))
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070069
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070070 lf := commonFlags.ListenFlags()
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070071 ListenSpec = ipc.ListenSpec{
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080072 Addrs: ipc.ListenAddrs(lf.Addrs),
73 Proxy: lf.ListenProxy,
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070074 }
75
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080076 p.ac = appcycle.New()
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080077
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070078 // Our address is private, so we test for running on GCE and for its
79 // 1:1 NAT configuration. GCEPublicAddress returns a non-nil addr
80 // if we are indeed running on GCE.
81 if !internal.HasPublicIP(log) {
82 if addr := internal.GCEPublicAddress(log); addr != nil {
83 ListenSpec.AddressChooser = func(string, []ipc.Address) ([]ipc.Address, error) {
84 return []ipc.Address{&netstate.AddrIfc{addr, "nat", nil}}, nil
85 }
86 p.gce = "+gce"
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080087 return p.ac, nil
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070088 }
89 }
90 ListenSpec.AddressChooser = internal.IPAddressChooser
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080091 return p.ac, nil
92}
93
94func (p *static) Cleanup() {
95 if p.ac != nil {
96 p.ac.Shutdown()
97 }
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070098}
99
100func (p *static) String() string {
101 return "static profile on " + p.Platform().String()
102}