blob: 2720f1d62b40af8c2e7f3ad0b145aafa5c17972b [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"
17 "veyron.io/veyron/veyron/profiles"
18 "veyron.io/veyron/veyron/profiles/internal"
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070019 "veyron.io/veyron/veyron/services/mgmt/debug"
20 // TODO(cnicolaou,ashankar): move this into flags.
21 sflag "veyron.io/veyron/veyron/security/flag"
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070022)
23
24var (
Cosmos Nicolaoubdc917c2014-10-24 12:41:47 -070025 commonFlags *flags.Flags
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070026 // ListenSpec is an initialized instance of ipc.ListenSpec that can
27 // be used with ipc.Listen.
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070028 ListenSpec ipc.ListenSpec
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070029)
30
31func init() {
Cosmos Nicolaoud811b072014-10-28 17:46:27 -070032 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
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 {
56 p, _ := profiles.Platform()
57 return p
58}
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 Nicolaoud811b072014-10-28 17:46:27 -070067 Protocol: lf.ListenProtocol.Protocol,
68 Address: lf.ListenAddress.String(),
69 Proxy: lf.ListenProxy,
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070070 }
71
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080072 p.ac = appcycle.New(rt)
73
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070074 // Our address is private, so we test for running on GCE and for its
75 // 1:1 NAT configuration. GCEPublicAddress returns a non-nil addr
76 // if we are indeed running on GCE.
77 if !internal.HasPublicIP(log) {
78 if addr := internal.GCEPublicAddress(log); addr != nil {
79 ListenSpec.AddressChooser = func(string, []ipc.Address) ([]ipc.Address, error) {
80 return []ipc.Address{&netstate.AddrIfc{addr, "nat", nil}}, nil
81 }
82 p.gce = "+gce"
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080083 return p.ac, nil
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070084 }
85 }
86 ListenSpec.AddressChooser = internal.IPAddressChooser
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080087 return p.ac, nil
88}
89
90func (p *static) Cleanup() {
91 if p.ac != nil {
92 p.ac.Shutdown()
93 }
Cosmos Nicolaouc9c41fb2014-10-16 23:53:10 -070094}
95
96func (p *static) String() string {
97 return "static profile on " + p.Platform().String()
98}