blob: b34495590da23ce75c0e746fb1c32c436b7819fe [file] [log] [blame]
Suharsh Sivakumarb8a8fe82015-01-14 16:54:32 -08001// +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.
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -08005package gce
6
7import (
8 "flag"
9 "fmt"
10 "net"
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080011 "os"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080012
13 "v.io/core/veyron2"
14 "v.io/core/veyron2/context"
15 "v.io/core/veyron2/ipc"
Nicolas LaCasse0a22b452015-01-22 09:57:15 -080016 "v.io/core/veyron2/ipc/stream"
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080017 "v.io/core/veyron2/vlog"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080018
19 "v.io/core/veyron/lib/appcycle"
20 "v.io/core/veyron/lib/flags"
21 "v.io/core/veyron/lib/netstate"
Nicolas LaCasse0a22b452015-01-22 09:57:15 -080022 "v.io/core/veyron/lib/websocket"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080023 "v.io/core/veyron/profiles/internal/gce"
24 _ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
25 _ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
26 _ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
27 grt "v.io/core/veyron/runtimes/google/rt"
28)
29
30var (
31 commonFlags *flags.Flags
32)
33
34func init() {
35 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
36 veyron2.RegisterProfileInit(Init)
Nicolas LaCasse0a22b452015-01-22 09:57:15 -080037 stream.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080038}
39
40func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080041 vlog.Log.VI(1).Infof("Initializing GCE profile.")
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080042 if !gce.RunningOnGCE() {
43 return nil, nil, nil, fmt.Errorf("GCE profile used on a non-GCE system")
44 }
45
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080046 ac := appcycle.New()
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080047
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080048 commonFlags.Parse(os.Args[1:], nil)
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080049 lf := commonFlags.ListenFlags()
50 listenSpec := ipc.ListenSpec{
51 Addrs: ipc.ListenAddrs(lf.Addrs),
52 Proxy: lf.ListenProxy,
53 }
54
55 if ip, err := gce.ExternalIPAddress(); err != nil {
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080056 return nil, nil, nil, err
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080057 } else {
58 listenSpec.AddressChooser = func(network string, addrs []ipc.Address) ([]ipc.Address, error) {
59 return []ipc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
60 }
61 }
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080062
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080063 runtime, ctx, shutdown, err := grt.Init(ctx, ac, nil, &listenSpec, nil)
64 if err != nil {
65 return nil, nil, shutdown, err
66 }
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080067
68 profileShutdown := func() {
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080069 ac.Shutdown()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080070 shutdown()
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080071 }
72
73 return runtime, ctx, profileShutdown, nil
74}