-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
477 lines (401 loc) · 14.6 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright 2022, Pulumi Corporation. All rights reserved.
// The implementation is heavily based on pulumi-language-java.
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"time"
"github.com/virtuslab/besom/language-host/executors"
"github.com/virtuslab/besom/language-host/fsys"
pbempty "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"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"
"github.com/pulumi/pulumi/sdk/v3/go/common/version"
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
)
// Launches the language host RPC endpoint, which in turn fires up an RPC server implementing the
// LanguageRuntimeServer RPC endpoint.
func main() {
var tracing string
var root string
var binary string
flag.StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint")
flag.StringVar(&root, "root", "", "Project root path to use")
flag.StringVar(&binary, "binary", "", "JAR file to execute")
// You can use the below flag to request that the language host load a specific executor instead of probing the
// PATH. This can be used during testing to override the default location.
var useExecutor string
flag.StringVar(&useExecutor, "use-executor", "",
"Use the given program as the executor instead of looking for one on PATH")
flag.Parse()
args := flag.Args()
logging.InitLogging(false, 0, false)
cmdutil.InitTracing("pulumi-language-scala", "pulumi-language-scala", tracing)
wd, err := os.Getwd()
if err != nil {
cmdutil.Exit(fmt.Errorf("could not get the working directory: %w", err))
}
execPath, err := os.Executable()
if err != nil {
cmdutil.Exit(fmt.Errorf("could not get the path of the executable: %w", err))
}
bootstrapLibJarPath := filepath.Join(filepath.Dir(execPath), "bootstrap.jar") // TODO: hardocoded jar name
scalaExecOptions := executors.ScalaExecutorOptions{
Binary: binary,
UseExecutor: useExecutor,
WD: fsys.DirFS(wd),
BootstrapLibJarPath: bootstrapLibJarPath,
}
// Optionally pluck out the engine, so we can do logging, etc.
var engineAddress string
if len(args) > 0 {
engineAddress = args[0]
}
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 interrupt handler
close(cancelChannel)
}()
err = rpcutil.Healthcheck(ctx, engineAddress, 5*time.Minute, cancel)
if err != nil {
cmdutil.Exit(fmt.Errorf("could not start health check host RPC server: %w", err))
}
// Fire up a gRPC server, letting the kernel choose a free port.
handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
Cancel: cancelChannel,
Init: func(srv *grpc.Server) error {
host := newLanguageHost(scalaExecOptions, engineAddress, tracing)
pulumirpc.RegisterLanguageRuntimeServer(srv, host)
return nil
},
Options: rpcutil.OpenTracingServerInterceptorOptions(nil),
})
if err != nil {
cmdutil.Exit(fmt.Errorf("could not start language host RPC server: %w", err))
}
// Otherwise, print out the port so that the spawner knows how to reach us.
fmt.Printf("%d\n", handle.Port)
// And finally wait for the server to stop serving.
if err := <-handle.Done; err != nil {
cmdutil.Exit(errors.Wrapf(err, "language host RPC stopped serving"))
}
}
// scalaLanguageHost implements the LanguageRuntimeServer interface
// for use as an API endpoint.
type scalaLanguageHost struct {
pulumirpc.UnimplementedLanguageRuntimeServer
currentExecutor *executors.ScalaExecutor
execOptions executors.ScalaExecutorOptions
engineAddress string
tracing string
}
func newLanguageHost(execOptions executors.ScalaExecutorOptions,
engineAddress, tracing string,
) /* pulumirpc.LanguageRuntimeServer */ *scalaLanguageHost {
return &scalaLanguageHost{
execOptions: execOptions,
engineAddress: engineAddress,
tracing: tracing,
}
}
func (host *scalaLanguageHost) Executor() (*executors.ScalaExecutor, error) {
if host.currentExecutor == nil {
executor, err := executors.NewScalaExecutor(host.execOptions)
if err != nil {
return nil, err
}
host.currentExecutor = executor
}
return host.currentExecutor, nil
}
// GetRequiredPlugins computes the complete set of anticipated plugins required by a program.
func (host *scalaLanguageHost) GetRequiredPlugins(
ctx context.Context,
req *pulumirpc.GetRequiredPluginsRequest,
) (*pulumirpc.GetRequiredPluginsResponse, error) {
logging.V(5).Infof("GetRequiredPlugins: program=%v", req.GetProgram())
// now, introspect the user project to see which pulumi resource packages it references.
pulumiPackages, err := host.determinePulumiPackages(ctx)
if err != nil {
return nil, errors.Wrapf(err, "language host could not determine Pulumi packages")
}
// Now that we know the set of pulumi packages referenced, and we know where packages have been restored to,
// we can examine each package to determine the corresponding resource-plugin for it.
plugins := []*pulumirpc.PluginDependency{}
for _, pulumiPackage := range pulumiPackages {
logging.V(3).Infof(
"GetRequiredPlugins: Determining plugin dependency: %v, %v",
pulumiPackage.Name, pulumiPackage.Version,
)
if !pulumiPackage.Resource {
continue // the package has no associated resource plugin
}
plugins = append(plugins, &pulumirpc.PluginDependency{
Name: pulumiPackage.Name,
Version: pulumiPackage.Version,
Server: pulumiPackage.Server,
Kind: "resource",
})
}
logging.V(5).Infof("GetRequiredPlugins: plugins=%v", plugins)
return &pulumirpc.GetRequiredPluginsResponse{Plugins: plugins}, nil
}
func (host *scalaLanguageHost) determinePulumiPackages(
ctx context.Context,
) ([]plugin.PulumiPluginJSON, error) {
logging.V(3).Infof("GetRequiredPlugins: Determining Pulumi plugins")
exec, err := host.Executor()
if err != nil {
return nil, err
}
// Run our classpath introspection from the SDK and parse the resulting JSON
cmd := exec.Cmd
args := exec.PluginArgs
output, err := host.runHostCommand(ctx, exec.Dir, cmd, args, true, false)
if err != nil {
// Plugin determination is an advisory feature, so it does not need to escalate to an error.
logging.V(3).Infof("language host could not run plugin discovery command successfully, "+
"returning empty plugins; cause: %s", err)
logging.V(3).Infof("%s", output.stdout)
logging.V(3).Infof("%s", output.stderr)
return []plugin.PulumiPluginJSON{}, nil
}
logging.V(5).Infof("GetRequiredPlugins: bootstrap raw output=%v", output)
var plugins []plugin.PulumiPluginJSON
err = json.Unmarshal([]byte(output.stdout), &plugins)
if err != nil {
if e, ok := err.(*json.SyntaxError); ok {
logging.V(5).Infof("JSON syntax error at byte offset %d", e.Offset)
}
// Plugin determination is an advisory feature, so it does not need to escalate to an error.
logging.V(3).Infof("language host could not unmarshall plugin package information, "+
"returning empty plugins; cause: %s", err)
return []plugin.PulumiPluginJSON{}, nil
}
return plugins, nil
}
type hostCommandOutput struct {
stdout string
stderr string
}
func (host *scalaLanguageHost) runHostCommand(
ctx context.Context, dir, name string, args []string, quiet bool, combineOutput bool,
) (hostCommandOutput, error) {
commandStr := strings.Join(args, " ")
if logging.V(5) {
logging.V(5).Infoln("Language host launching process: ", name, " ", commandStr)
}
// Now simply spawn a process to execute the requested program, wiring up stdout/stderr directly.
cmd := exec.CommandContext(ctx, name, args...) // nolint: gas // intentionally running dynamic program name.
if dir != "" {
cmd.Dir = dir
}
stdoutBuffer := &bytes.Buffer{}
stderrBuffer := &bytes.Buffer{}
var stdoutWriter io.Writer = stdoutBuffer
if !quiet {
stdoutWriter = io.MultiWriter(os.Stdout, stdoutWriter)
}
cmd.Stdout = stdoutWriter
if combineOutput {
cmd.Stderr = stdoutWriter // redirect stderr to stdout
} else {
var stderrWriter io.Writer = stderrBuffer
if !quiet {
stderrWriter = io.MultiWriter(os.Stderr, stderrWriter)
}
cmd.Stderr = stderrWriter
}
err := runCommand(cmd)
if err == nil && logging.V(5) {
logging.V(5).Infof("'%v %v' completed successfully\n", name, commandStr)
}
return hostCommandOutput{
stdout: stdoutBuffer.String(),
stderr: stderrBuffer.String(),
}, err
}
// Run is an RPC endpoint for LanguageRuntimeServer::Run
func (host *scalaLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest) (*pulumirpc.RunResponse, error) {
logging.V(5).Infof("Run: program=%v", req.GetProgram())
config, err := host.constructConfig(req)
if err != nil {
err = errors.Wrap(err, "failed to serialize configuration")
return nil, err
}
configSecretKeys, err := host.constructConfigSecretKeys(req)
if err != nil {
err = errors.Wrap(err, "failed to serialize configuration secret keys")
return nil, err
}
executor, err := host.Executor()
if err != nil {
return nil, err
}
// Run from source.
executable := executor.Cmd
args := executor.RunArgs
if logging.V(5) {
commandStr := strings.Join(args, " ")
logging.V(5).Infoln("Language host launching process: ", executable, " ", commandStr)
}
// Now simply spawn a process to execute the requested program, wiring up stdout/stderr directly.
var errResult string
cmd := exec.CommandContext(ctx, executable, args...) // nolint: gas // intentionally running dynamic program name.
if executor.Dir != "" {
cmd.Dir = executor.Dir
}
var stdoutBuf bytes.Buffer
var stderrBuf bytes.Buffer
cmd.Stdout = &stdoutBuf
cmd.Stderr = &stderrBuf
cmd.Env = host.constructEnv(req, config, configSecretKeys)
if err := runCommand(cmd); err != nil {
// The command failed. Dump any data we collected to
// the actual stdout/stderr streams, so they get
// displayed to the user.
os.Stdout.Write(stdoutBuf.Bytes())
os.Stderr.Write(stderrBuf.Bytes())
errResult = err.Error()
}
return &pulumirpc.RunResponse{Error: errResult}, nil
}
// constructEnv constructs an environ for `pulumi-language-scala`
// by enumerating all the optional and non-optional evn vars present
// in a RunRequest.
func (host *scalaLanguageHost) constructEnv(req *pulumirpc.RunRequest, config, configSecretKeys string) []string {
env := os.Environ()
maybeAppendEnv := func(k, v string) {
if v != "" {
env = append(env, strings.ToUpper("PULUMI_"+k)+"="+v)
}
}
maybeAppendEnv("monitor", req.GetMonitorAddress())
maybeAppendEnv("engine", host.engineAddress)
maybeAppendEnv("project", req.GetProject())
maybeAppendEnv("stack", req.GetStack())
maybeAppendEnv("pwd", req.GetPwd())
maybeAppendEnv("dry_run", fmt.Sprintf("%v", req.GetDryRun()))
maybeAppendEnv("query_mode", fmt.Sprint(req.GetQueryMode()))
maybeAppendEnv("parallel", fmt.Sprint(req.GetParallel()))
maybeAppendEnv("tracing", host.tracing)
maybeAppendEnv("config", config)
maybeAppendEnv("config_secret_keys", configSecretKeys)
return env
}
// constructConfig json-serializes the configuration data given as part of a RunRequest.
func (host *scalaLanguageHost) constructConfig(req *pulumirpc.RunRequest) (string, error) {
configMap := req.GetConfig()
if configMap == nil {
return "", nil
}
configJSON, err := json.Marshal(configMap)
if err != nil {
return "", err
}
return string(configJSON), nil
}
// constructConfigSecretKeys JSON-serializes the list of keys that contain secret values given as part of
// a RunRequest.
func (host *scalaLanguageHost) constructConfigSecretKeys(req *pulumirpc.RunRequest) (string, error) {
configSecretKeys := req.GetConfigSecretKeys()
if configSecretKeys == nil {
return "[]", nil
}
configSecretKeysJSON, err := json.Marshal(configSecretKeys)
if err != nil {
return "", err
}
return string(configSecretKeysJSON), nil
}
func (host *scalaLanguageHost) GetPluginInfo(_ context.Context, _ *pbempty.Empty) (*pulumirpc.PluginInfo, error) {
return &pulumirpc.PluginInfo{
Version: version.Version,
}, nil
}
func (host *scalaLanguageHost) InstallDependencies(req *pulumirpc.InstallDependenciesRequest,
server pulumirpc.LanguageRuntime_InstallDependenciesServer,
) error {
executor, err := host.Executor()
if err != nil {
return err
}
// Executor may not support the build command (for example, jar executor).
if executor.BuildArgs == nil {
logging.V(5).Infof("InstallDependencies(Directory=%s): skipping", req.Directory)
return nil
}
logging.V(5).Infof("InstallDependencies(Directory=%s): starting", req.Directory)
closer, stdout, stderr, err := rpcutil.MakeInstallDependenciesStreams(server, req.IsTerminal)
if err != nil {
return err
}
defer closer.Close()
// intentionally running dynamic program name.
cmd := exec.Command(executor.Cmd, executor.BuildArgs...) // nolint: gas
if executor.Dir != "" {
cmd.Dir = executor.Dir
}
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := runCommand(cmd); err != nil {
logging.V(5).Infof("InstallDependencies(Directory=%s): failed", req.Directory)
return err
}
logging.V(5).Infof("InstallDependencies(Directory=%s): done", req.Directory)
return nil
}
func (host *scalaLanguageHost) GetProgramDependencies(
ctx context.Context, req *pulumirpc.GetProgramDependenciesRequest,
) (*pulumirpc.GetProgramDependenciesResponse, error) {
// TODO: Implement dependency fetcher
return &pulumirpc.GetProgramDependenciesResponse{}, nil
}
func (host *scalaLanguageHost) About(ctx context.Context, _ *emptypb.Empty) (*pulumirpc.AboutResponse, error) {
metadata := make(map[string]string)
scalaExec, err := host.Executor()
if err != nil {
return nil, err
}
javaExec, err := executors.NewScalaExecutor(executors.ScalaExecutorOptions{
UseExecutor: "jar",
WD: host.execOptions.WD,
BootstrapLibJarPath: host.execOptions.BootstrapLibJarPath,
Binary: host.execOptions.Binary,
})
if err != nil {
return nil, err
}
if javaVersion, err := host.runHostCommand(
ctx, javaExec.Dir, javaExec.Cmd, javaExec.VersionArgs, true, true,
); err == nil {
metadata["java"] = strings.ReplaceAll(strings.TrimSpace(javaVersion.stdout), "\n", "; ")
}
if executorVersion, err := host.runHostCommand(
ctx, scalaExec.Dir, scalaExec.Cmd, scalaExec.VersionArgs, true, false,
); err == nil {
metadata[scalaExec.Name] = strings.TrimSpace(executorVersion.stdout)
}
return &pulumirpc.AboutResponse{
Executable: scalaExec.Cmd,
Version: version.Version,
Metadata: metadata,
}, nil
}