blob: ac9d9f16b2b508b7da76fbed7a2ed744630f5264 [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"
11
Jiri Simsa6ac95222015-02-23 16:11:49 -080012 "v.io/v23"
13 "v.io/v23/context"
14 "v.io/v23/ipc"
Jiri Simsa337af232015-02-27 14:36:46 -080015 "v.io/x/lib/vlog"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080016
Jiri Simsaffceefa2015-02-28 11:03:34 -080017 "v.io/x/ref/lib/appcycle"
18 "v.io/x/ref/lib/flags"
19 "v.io/x/ref/lib/netstate"
20 "v.io/x/ref/lib/websocket"
21 "v.io/x/ref/profiles/internal"
22 "v.io/x/ref/profiles/internal/gce"
Matt Rosencrantzdbc1be22015-02-28 15:15:49 -080023 _ "v.io/x/ref/profiles/internal/ipc/protocols/tcp"
24 _ "v.io/x/ref/profiles/internal/ipc/protocols/ws"
25 _ "v.io/x/ref/profiles/internal/ipc/protocols/wsh"
26 grt "v.io/x/ref/profiles/internal/rt"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080027)
28
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080029var commonFlags *flags.Flags
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080030
31func init() {
Jiri Simsa6ac95222015-02-23 16:11:49 -080032 v23.RegisterProfileInit(Init)
Suharsh Sivakumaraf862a52015-02-04 13:50:47 -080033 ipc.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080034 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Runtime, flags.Listen)
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080035}
36
Jiri Simsa6ac95222015-02-23 16:11:49 -080037func Init(ctx *context.T) (v23.Runtime, *context.T, v23.Shutdown, error) {
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080038 if !gce.RunningOnGCE() {
39 return nil, nil, nil, fmt.Errorf("GCE profile used on a non-GCE system")
40 }
41
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080042 if err := internal.ParseFlags(commonFlags); err != nil {
43 return nil, nil, nil, err
44 }
45
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080046 ac := appcycle.New()
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080047
48 lf := commonFlags.ListenFlags()
49 listenSpec := ipc.ListenSpec{
50 Addrs: ipc.ListenAddrs(lf.Addrs),
51 Proxy: lf.ListenProxy,
52 }
53
54 if ip, err := gce.ExternalIPAddress(); err != nil {
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080055 return nil, nil, nil, err
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080056 } else {
57 listenSpec.AddressChooser = func(network string, addrs []ipc.Address) ([]ipc.Address, error) {
58 return []ipc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
59 }
60 }
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080061
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080062 runtime, ctx, shutdown, err := grt.Init(ctx, ac, nil, &listenSpec, commonFlags.RuntimeFlags(), nil)
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080063 if err != nil {
64 return nil, nil, shutdown, err
65 }
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080066
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080067 vlog.Log.VI(1).Infof("Initializing GCE profile.")
68
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080069 profileShutdown := func() {
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080070 ac.Shutdown()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080071 shutdown()
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080072 }
73
74 return runtime, ctx, profileShutdown, nil
75}