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" |
| 17 | "veyron.io/veyron/veyron/profiles" |
| 18 | "veyron.io/veyron/veyron/profiles/internal" |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 19 | "veyron.io/veyron/veyron/services/mgmt/debug" |
| 20 | // TODO(cnicolaou,ashankar): move this into flags. |
| 21 | sflag "veyron.io/veyron/veyron/security/flag" |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | var ( |
Cosmos Nicolaou | bdc917c | 2014-10-24 12:41:47 -0700 | [diff] [blame] | 25 | commonFlags *flags.Flags |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 26 | // ListenSpec is an initialized instance of ipc.ListenSpec that can |
| 27 | // be used with ipc.Listen. |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 28 | ListenSpec ipc.ListenSpec |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func init() { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 32 | commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen) |
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 { |
| 56 | p, _ := profiles.Platform() |
| 57 | return p |
| 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 | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 67 | Protocol: lf.ListenProtocol.Protocol, |
| 68 | Address: lf.ListenAddress.String(), |
| 69 | Proxy: lf.ListenProxy, |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 72 | p.ac = appcycle.New(rt) |
| 73 | |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 74 | // 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 Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 83 | return p.ac, nil |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | ListenSpec.AddressChooser = internal.IPAddressChooser |
Cosmos Nicolaou | 39e3ae5 | 2014-11-14 13:30:01 -0800 | [diff] [blame] | 87 | return p.ac, nil |
| 88 | } |
| 89 | |
| 90 | func (p *static) Cleanup() { |
| 91 | if p.ac != nil { |
| 92 | p.ac.Shutdown() |
| 93 | } |
Cosmos Nicolaou | c9c41fb | 2014-10-16 23:53:10 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func (p *static) String() string { |
| 97 | return "static profile on " + p.Platform().String() |
| 98 | } |