blob: 4aa63c8d1f37e210c394e03ac176f0a2331cdcb5 [file] [log] [blame]
// +build linux
// Package gce provides a profile for Google Compute Engine and should be
// used by binaries that only ever expect to be run on GCE.
package gce
import (
"flag"
"fmt"
"net"
"v.io/core/veyron2"
"v.io/core/veyron2/context"
"v.io/core/veyron2/ipc"
"v.io/core/veyron/lib/appcycle"
"v.io/core/veyron/lib/flags"
"v.io/core/veyron/lib/netstate"
"v.io/core/veyron/profiles/internal/gce"
_ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
_ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
_ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
grt "v.io/core/veyron/runtimes/google/rt"
)
var (
commonFlags *flags.Flags
)
func init() {
commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
veyron2.RegisterProfileInit(Init)
}
func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
if !gce.RunningOnGCE() {
return nil, nil, nil, fmt.Errorf("GCE profile used on a non-GCE system")
}
runtime, ctx, shutdown, err := grt.Init(ctx, nil)
if err != nil {
return nil, nil, shutdown, err
}
runtime.GetLogger(ctx).VI(1).Infof("Initializing GCE profile.")
lf := commonFlags.ListenFlags()
listenSpec := ipc.ListenSpec{
Addrs: ipc.ListenAddrs(lf.Addrs),
Proxy: lf.ListenProxy,
}
if ip, err := gce.ExternalIPAddress(); err != nil {
return nil, nil, shutdown, err
} else {
listenSpec.AddressChooser = func(network string, addrs []ipc.Address) ([]ipc.Address, error) {
return []ipc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
}
}
ctx = runtime.SetListenSpec(ctx, listenSpec)
ac := appcycle.New()
ctx = runtime.SetAppCycle(ctx, ac)
profileShutdown := func() {
shutdown()
ac.Shutdown()
}
return runtime, ctx, profileShutdown, nil
}