blob: e192b7264af3e7df09e108f81dba566a2522bc5e [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Suharsh Sivakumarb8a8fe82015-01-14 16:54:32 -08005// +build linux
6
7// Package gce provides a profile for Google Compute Engine and should be
8// used by binaries that only ever expect to be run on GCE.
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -08009package gce
10
11import (
12 "flag"
13 "fmt"
14 "net"
15
Jiri Simsa6ac95222015-02-23 16:11:49 -080016 "v.io/v23"
17 "v.io/v23/context"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070018 "v.io/v23/rpc"
Jiri Simsa337af232015-02-27 14:36:46 -080019 "v.io/x/lib/vlog"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080020
Matt Rosencrantz9d3278a2015-03-11 14:58:34 -070021 "v.io/x/lib/netstate"
Jiri Simsaffceefa2015-02-28 11:03:34 -080022 "v.io/x/ref/lib/flags"
Jiri Simsaffceefa2015-02-28 11:03:34 -080023 "v.io/x/ref/profiles/internal"
24 "v.io/x/ref/profiles/internal/gce"
Matt Rosencrantz86ba1a12015-03-09 13:19:02 -070025 "v.io/x/ref/profiles/internal/lib/appcycle"
26 "v.io/x/ref/profiles/internal/lib/websocket"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070027 _ "v.io/x/ref/profiles/internal/rpc/protocols/tcp"
28 _ "v.io/x/ref/profiles/internal/rpc/protocols/ws"
29 _ "v.io/x/ref/profiles/internal/rpc/protocols/wsh"
Matt Rosencrantzdbc1be22015-02-28 15:15:49 -080030 grt "v.io/x/ref/profiles/internal/rt"
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080031)
32
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080033var commonFlags *flags.Flags
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080034
35func init() {
Suharsh Sivakumarc6f38e92015-03-31 13:04:06 -070036 v23.RegisterProfile(Init)
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070037 rpc.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080038 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Runtime, flags.Listen)
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080039}
40
Jiri Simsa6ac95222015-02-23 16:11:49 -080041func Init(ctx *context.T) (v23.Runtime, *context.T, v23.Shutdown, error) {
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 Sivakumard68949c2015-01-26 10:32:23 -080046 if err := internal.ParseFlags(commonFlags); err != nil {
47 return nil, nil, nil, err
48 }
49
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080050 ac := appcycle.New()
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080051
52 lf := commonFlags.ListenFlags()
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070053 listenSpec := rpc.ListenSpec{
54 Addrs: rpc.ListenAddrs(lf.Addrs),
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080055 Proxy: lf.ListenProxy,
56 }
57
58 if ip, err := gce.ExternalIPAddress(); err != nil {
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080059 return nil, nil, nil, err
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080060 } else {
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070061 listenSpec.AddressChooser = func(network string, addrs []rpc.Address) ([]rpc.Address, error) {
62 return []rpc.Address{&netstate.AddrIfc{&net.IPAddr{IP: ip}, "gce-nat", nil}}, nil
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080063 }
64 }
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080065
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080066 runtime, ctx, shutdown, err := grt.Init(ctx, ac, nil, &listenSpec, commonFlags.RuntimeFlags(), nil)
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080067 if err != nil {
68 return nil, nil, shutdown, err
69 }
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080070
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080071 vlog.Log.VI(1).Infof("Initializing GCE profile.")
72
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080073 profileShutdown := func() {
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080074 ac.Shutdown()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080075 shutdown()
Suharsh Sivakumar98aa48c2015-01-14 16:11:30 -080076 }
77
78 return runtime, ctx, profileShutdown, nil
79}