blob: 2882a08295e54205e48eb5f4fa9f37083bed00ab [file] [log] [blame]
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -07001// +build linux
2
3// Package gce provides a Profile for Google Compute Engine and should be
4// used by binaries that only ever expect to be run on GCE.
5package gce
6
7import (
Cosmos Nicolaou682d7fd2014-09-24 22:54:16 -07008 "fmt"
Cosmos Nicolaouef323db2014-09-07 22:13:28 -07009 "net"
Cosmos Nicolaou682d7fd2014-09-24 22:54:16 -070010
Jiri Simsa764efb72014-12-25 20:57:03 -080011 "v.io/core/veyron2"
12 "v.io/core/veyron2/config"
13 "v.io/core/veyron2/ipc"
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070014
Jiri Simsa764efb72014-12-25 20:57:03 -080015 "v.io/core/veyron/lib/appcycle"
Jiri Simsa764efb72014-12-25 20:57:03 -080016 "v.io/core/veyron/lib/netstate"
17 "v.io/core/veyron/profiles/internal/gce"
18 "v.io/core/veyron/profiles/internal/platform"
19 _ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
20 _ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
21 _ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
22 _ "v.io/core/veyron/runtimes/google/rt"
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070023)
24
Cosmos Nicolaouc0e4b792014-09-25 10:57:52 -070025var (
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080026 // ListenSpec is an initialized instance of ipc.ListenSpec that can
27 // be used with ipc.Listen.
28 ListenSpec ipc.ListenSpec
Cosmos Nicolaouc0e4b792014-09-25 10:57:52 -070029)
Cosmos Nicolaou767b62d2014-09-19 13:58:40 -070030
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080031type profile struct {
32 ac *appcycle.AppCycle
33}
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070034
35func (p *profile) Name() string {
36 return "GCE"
37}
38
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080039func (p *profile) Runtime() (string, []veyron2.ROpt) {
40 return "", nil
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070041}
42
43func (p *profile) Platform() *veyron2.Platform {
Cosmos Nicolaou87c0a552014-12-02 23:05:49 -080044 pstr, _ := platform.Platform()
45 return pstr
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070046}
47
48func (p *profile) String() string {
49 return "net " + p.Platform().String()
50}
51
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080052func (p *profile) Init(veyron2.Runtime, *config.Publisher) (veyron2.AppCycle, error) {
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070053 if !gce.RunningOnGCE() {
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080054 return nil, fmt.Errorf("GCE profile used on a non-GCE system")
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070055 }
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080056
57 lf := commonFlags.ListenFlags()
58 ListenSpec = ipc.ListenSpec{
59 Addrs: ipc.ListenAddrs(lf.Addrs),
60 Proxy: lf.ListenProxy,
61 }
62
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080063 p.ac = appcycle.New()
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080064
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070065 if ip, err := gce.ExternalIPAddress(); err != nil {
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080066 return p.ac, err
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070067 } else {
Cosmos Nicolaou66bc1202014-09-30 20:42:43 -070068 ListenSpec.AddressChooser = func(network string, addrs []ipc.Address) ([]ipc.Address, error) {
69 return []ipc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
Cosmos Nicolaou767b62d2014-09-19 13:58:40 -070070 }
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070071 }
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080072 return p.ac, nil
73}
74
75func (p *profile) Cleanup() {
76 p.ac.Shutdown()
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070077}