blob: 4e72975db37b724cbb7b6acfa24f28cbd2335bdc [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 Nicolaouc0e4b792014-09-25 10:57:52 -07008 "flag"
Cosmos Nicolaou682d7fd2014-09-24 22:54:16 -07009 "fmt"
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070010 "net"
Cosmos Nicolaou682d7fd2014-09-24 22:54:16 -070011
Jiri Simsa764efb72014-12-25 20:57:03 -080012 "v.io/core/veyron2"
13 "v.io/core/veyron2/config"
14 "v.io/core/veyron2/ipc"
15 "v.io/core/veyron2/rt"
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070016
Jiri Simsa764efb72014-12-25 20:57:03 -080017 "v.io/core/veyron/lib/appcycle"
18 "v.io/core/veyron/lib/flags"
19 "v.io/core/veyron/lib/netstate"
20 "v.io/core/veyron/profiles/internal/gce"
21 "v.io/core/veyron/profiles/internal/platform"
22 _ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
23 _ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
24 _ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
25 _ "v.io/core/veyron/runtimes/google/rt"
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070026)
27
Cosmos Nicolaouc0e4b792014-09-25 10:57:52 -070028var (
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080029 commonFlags *flags.Flags
Cosmos Nicolaouc0e4b792014-09-25 10:57:52 -070030
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080031 // ListenSpec is an initialized instance of ipc.ListenSpec that can
32 // be used with ipc.Listen.
33 ListenSpec ipc.ListenSpec
Cosmos Nicolaouc0e4b792014-09-25 10:57:52 -070034)
Cosmos Nicolaou767b62d2014-09-19 13:58:40 -070035
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070036func init() {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080037 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070038 rt.RegisterProfile(&profile{})
39}
40
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080041type profile struct {
42 ac *appcycle.AppCycle
43}
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070044
45func (p *profile) Name() string {
46 return "GCE"
47}
48
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080049func (p *profile) Runtime() (string, []veyron2.ROpt) {
50 return "", nil
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070051}
52
53func (p *profile) Platform() *veyron2.Platform {
Cosmos Nicolaou87c0a552014-12-02 23:05:49 -080054 pstr, _ := platform.Platform()
55 return pstr
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070056}
57
58func (p *profile) String() string {
59 return "net " + p.Platform().String()
60}
61
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080062func (p *profile) Init(veyron2.Runtime, *config.Publisher) (veyron2.AppCycle, error) {
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070063 if !gce.RunningOnGCE() {
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080064 return nil, fmt.Errorf("GCE profile used on a non-GCE system")
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070065 }
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080066
67 lf := commonFlags.ListenFlags()
68 ListenSpec = ipc.ListenSpec{
69 Addrs: ipc.ListenAddrs(lf.Addrs),
70 Proxy: lf.ListenProxy,
71 }
72
Bogdan Caprita3e8f9642014-12-05 14:29:40 -080073 p.ac = appcycle.New()
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080074
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070075 if ip, err := gce.ExternalIPAddress(); err != nil {
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080076 return p.ac, err
Cosmos Nicolaouef323db2014-09-07 22:13:28 -070077 } else {
Cosmos Nicolaou66bc1202014-09-30 20:42:43 -070078 ListenSpec.AddressChooser = func(network string, addrs []ipc.Address) ([]ipc.Address, error) {
79 return []ipc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
Cosmos Nicolaou767b62d2014-09-19 13:58:40 -070080 }
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070081 }
Cosmos Nicolaou39e3ae52014-11-14 13:30:01 -080082 return p.ac, nil
83}
84
85func (p *profile) Cleanup() {
86 p.ac.Shutdown()
Cosmos Nicolaou6c6fa112014-08-19 13:22:33 -070087}