blob: b34495590da23ce75c0e746fb1c32c436b7819fe [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"
"os"
"v.io/core/veyron2"
"v.io/core/veyron2/context"
"v.io/core/veyron2/ipc"
"v.io/core/veyron2/ipc/stream"
"v.io/core/veyron2/vlog"
"v.io/core/veyron/lib/appcycle"
"v.io/core/veyron/lib/flags"
"v.io/core/veyron/lib/netstate"
"v.io/core/veyron/lib/websocket"
"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)
stream.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
}
func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
vlog.Log.VI(1).Infof("Initializing GCE profile.")
if !gce.RunningOnGCE() {
return nil, nil, nil, fmt.Errorf("GCE profile used on a non-GCE system")
}
ac := appcycle.New()
commonFlags.Parse(os.Args[1:], nil)
lf := commonFlags.ListenFlags()
listenSpec := ipc.ListenSpec{
Addrs: ipc.ListenAddrs(lf.Addrs),
Proxy: lf.ListenProxy,
}
if ip, err := gce.ExternalIPAddress(); err != nil {
return nil, nil, nil, 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
}
}
runtime, ctx, shutdown, err := grt.Init(ctx, ac, nil, &listenSpec, nil)
if err != nil {
return nil, nil, shutdown, err
}
profileShutdown := func() {
ac.Shutdown()
shutdown()
}
return runtime, ctx, profileShutdown, nil
}