-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
106 lines (94 loc) · 3.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// pulumi-language-yaml is the "language host" for Pulumi programs written in YAML or JSON. It is responsible for
// evaluating JSON/YAML templates, registering resources, outputs, and so on, with the Pulumi engine.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"time"
"github.com/pkg/errors"
"github.com/pulumi/pulumi-yaml/pkg/server"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"google.golang.org/grpc"
)
// Launches the language host RPC endpoint, which in turn fires up an RPC server implementing the
// LanguageRuntimeServer RPC endpoint.
func main() {
// Parse the flags and initialize some boilerplate.
var tracing string
var root string
var compiler string
flag.StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint")
flag.StringVar(&root, "root", "", "Root of the program execution")
flag.StringVar(&compiler, "compiler", "", "Compiler to use to pre-process YAML")
flag.Parse()
var cancelChannel chan bool
args := flag.Args()
logging.InitLogging(false, 0, false)
cmdutil.InitTracing("pulumi-language-yaml", "pulumi-language-yaml", tracing)
// Fetch the engine address if available so we can do logging, etc.
var engineAddress string
if len(args) > 0 {
engineAddress = args[0]
var err error
cancelChannel, err = setupHealthChecks(engineAddress)
if err != nil {
cmdutil.Exit(errors.Wrapf(err, "could not start health check host RPC server"))
}
}
// Fire up a gRPC server, letting the kernel choose a free port.
port, done, err := rpcutil.Serve(0, cancelChannel, []func(*grpc.Server) error{
func(srv *grpc.Server) error {
host := server.NewLanguageHost(engineAddress, tracing, compiler)
pulumirpc.RegisterLanguageRuntimeServer(srv, host)
return nil
},
}, nil)
if err != nil {
cmdutil.Exit(errors.Wrapf(err, "could not start language host RPC server"))
}
// Otherwise, print out the port so that the spawner knows how to reach us.
fmt.Printf("%d\n", port)
// And finally wait for the server to stop serving.
if err := <-done; err != nil {
cmdutil.Exit(errors.Wrapf(err, "language host RPC stopped serving"))
}
}
func setupHealthChecks(engineAddress string) (chan bool, error) {
// If the health check begins failing or we receive a SIGINT,
// we'll cancel the context.
//
// The returned channel is used to notify the server that it should
// stop serving and exit.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
// map the context Done channel to the rpcutil boolean cancel channel
cancelChannel := make(chan bool)
go func() {
<-ctx.Done()
cancel() // deregister the signal handler
close(cancelChannel)
}()
err := rpcutil.Healthcheck(ctx, engineAddress, 5*time.Minute, cancel)
if err != nil {
return nil, err
}
return cancelChannel, nil
}