/
main.go
812 lines (684 loc) · 25.7 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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// Copyright 2016-2021, 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.
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/blang/semver"
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/executable"
"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"
)
var (
// A exit-code we recognize when the nodejs process exits. If we see this error, there's no
// need for us to print any additional error messages since the user already got a a good
// one they can handle.
dotnetProcessExitedAfterShowingUserActionableMessage = 32
)
// 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 binary string
var root string
flag.StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint")
flag.StringVar(&binary, "binary", "", "A relative or an absolute path to a precompiled .NET assembly to execute")
flag.StringVar(&root, "root", "", "Project root path to use")
// 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 givenExecutor string
flag.StringVar(&givenExecutor, "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-dotnet", "pulumi-language-dotnet", tracing)
var dotnetExec string
switch {
case givenExecutor != "":
logging.V(3).Infof("language host asked to use specific executor: `%s`", givenExecutor)
dotnetExec = givenExecutor
case binary != "" && !strings.HasSuffix(binary, ".dll"):
logging.V(3).Info("language host requires no .NET SDK for a self-contained binary")
default:
pathExec, err := exec.LookPath("dotnet")
if err != nil {
err = errors.Wrap(err, "could not find `dotnet` on the $PATH")
cmdutil.Exit(err)
}
logging.V(3).Infof("language host identified executor from path: `%s`", pathExec)
dotnetExec = pathExec
}
// Optionally pluck out the engine so we can do logging, etc.
var engineAddress string
if len(args) > 0 {
engineAddress = args[0]
}
ctx, cancel := context.WithCancel(context.Background())
// map the context Done channel to the rpcutil boolean cancel channel
cancelChannel := make(chan bool)
go func() {
<-ctx.Done()
close(cancelChannel)
}()
err := rpcutil.Healthcheck(ctx, engineAddress, 5*time.Minute, cancel)
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 := newLanguageHost(dotnetExec, engineAddress, tracing, binary)
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"))
}
}
// dotnetLanguageHost implements the LanguageRuntimeServer interface
// for use as an API endpoint.
type dotnetLanguageHost struct {
exec string
engineAddress string
tracing string
binary string
dotnetBuildSucceeded bool
}
func newLanguageHost(exec, engineAddress, tracing string, binary string) pulumirpc.LanguageRuntimeServer {
return &dotnetLanguageHost{
exec: exec,
engineAddress: engineAddress,
tracing: tracing,
binary: binary,
}
}
// GetRequiredPlugins computes the complete set of anticipated plugins required by a program.
func (host *dotnetLanguageHost) GetRequiredPlugins(
ctx context.Context,
req *pulumirpc.GetRequiredPluginsRequest) (*pulumirpc.GetRequiredPluginsResponse, error) {
logging.V(5).Infof("GetRequiredPlugins: %v", req.GetProgram())
if host.binary != "" {
logging.V(5).Infof("GetRequiredPlugins: no plugins can be listed when a binary is specified")
return &pulumirpc.GetRequiredPluginsResponse{}, nil
}
// Make a connection to the real engine that we will log messages to.
conn, err := grpc.Dial(
host.engineAddress,
grpc.WithInsecure(),
rpcutil.GrpcChannelOptions(),
)
if err != nil {
return nil, errors.Wrapf(err, "language host could not make connection to engine")
}
// Make a client around that connection. We can then make our own server that will act as a
// monitor for the sdk and forward to the real monitor.
engineClient := pulumirpc.NewEngineClient(conn)
// First do a `dotnet build`. This will ensure that all the nuget dependencies of the project
// are restored and locally available for us.
if err := host.DotnetBuild(ctx, req, engineClient); err != nil {
return nil, err
}
// now, introspect the user project to see which pulumi resource packages it references.
possiblePulumiPackages, err := host.DeterminePossiblePulumiPackages(ctx, engineClient)
if err != nil {
return nil, err
}
// Ensure we know where the local nuget package cache directory is. User can specify where that
// is located, so this makes sure we respect any custom location they may have.
packageDir, err := host.DetermineDotnetPackageDirectory(ctx, engineClient)
if err != nil {
return nil, err
}
// 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{}
packageToVersion := make(map[string]string)
for _, parts := range possiblePulumiPackages {
packageName := parts[0]
packageVersion := parts[1]
if existingVersion := packageToVersion[packageName]; existingVersion == packageVersion {
// only include distinct dependencies.
continue
}
packageToVersion[packageName] = packageVersion
plugin, err := DeterminePluginDependency(packageDir, packageName, packageVersion)
if err != nil {
return nil, err
}
if plugin != nil {
plugins = append(plugins, plugin)
}
}
return &pulumirpc.GetRequiredPluginsResponse{Plugins: plugins}, nil
}
func (host *dotnetLanguageHost) DeterminePossiblePulumiPackages(
ctx context.Context, engineClient pulumirpc.EngineClient) ([][]string, error) {
logging.V(5).Infof("GetRequiredPlugins: Determining pulumi packages")
// Run the `dotnet list package --include-transitive` command. Importantly, do not clutter the
// stream with the extra steps we're performing. This is just so we can determine the required
// plugins. And, after the first time we do this, subsequent runs will see that the plugin is
// installed locally and not need to do anything.
args := []string{"list", "package", "--include-transitive"}
commandStr := strings.Join(args, " ")
commandOutput, err := host.RunDotnetCommand(ctx, engineClient, args, false /*logToUser*/)
if err != nil {
return nil, err
}
// expected output should be like so:
//
// Project 'Aliases' has the following package references
// [netcoreapp3.1]:
// Top-level Package Requested Resolved
// > Pulumi 1.5.0-preview-alpha.1572911568 1.5.0-preview-alpha.1572911568
//
// Transitive Package Resolved
// > Google.Protobuf 3.10.0
// > Grpc 2.24.0
outputLines := strings.Split(strings.Replace(commandOutput, "\r\n", "\n", -1), "\n")
sawPulumi := false
packages := [][]string{}
for _, line := range outputLines {
fields := strings.Fields(line)
if len(fields) < 3 {
continue
}
// Has to start with `>` and have at least 3 chunks:
//
// > name requested_ver? resolved_ver
if fields[0] != ">" {
continue
}
// We only care about `Pulumi.` packages
packageName := fields[1]
if packageName == "Pulumi" {
sawPulumi = true
continue
}
version := fields[len(fields)-1]
packages = append(packages, []string{packageName, version})
}
if !sawPulumi && len(packages) == 0 {
return nil, errors.Errorf(
"unexpected output from 'dotnet %v'. Program does not appear to reference any 'Pulumi.*' packages",
commandStr)
}
logging.V(5).Infof("GetRequiredPlugins: Pulumi packages: %#v", packages)
return packages, nil
}
func (host *dotnetLanguageHost) DetermineDotnetPackageDirectory(
ctx context.Context, engineClient pulumirpc.EngineClient) (string, error) {
logging.V(5).Infof("GetRequiredPlugins: Determining package directory")
// Run the `dotnet nuget locals global-packages --list` command. Importantly, do not clutter
// the stream with the extra steps we're performing. This is just so we can determine the
// required plugins. And, after the first time we do this, subsequent runs will see that the
// plugin is installed locally and not need to do anything.
args := []string{"nuget", "locals", "global-packages", "--list"}
commandStr := strings.Join(args, " ")
commandOutput, err := host.RunDotnetCommand(ctx, engineClient, args, false /*logToUser*/)
if err != nil {
return "", err
}
// expected output should be like so: "info : global-packages: /home/cyrusn/.nuget/packages/"
// so grab the portion after "global-packages:"
index := strings.Index(commandOutput, "global-packages:")
if index < 0 {
return "", errors.Errorf("Unexpected output from 'dotnet %v': %v", commandStr, commandOutput)
}
dir := strings.TrimSpace(commandOutput[index+len("global-packages:"):])
logging.V(5).Infof("GetRequiredPlugins: Package directory: %v", dir)
return dir, nil
}
type versionFile struct {
name string
version string
}
func newVersionFile(b []byte, packageName string) *versionFile {
var name string
version := strings.TrimSpace(string(b))
parts := strings.SplitN(version, "\n", 2)
if len(parts) == 2 {
// version.txt may contain two lines, in which case it's "plugin name\nversion"
name = strings.TrimSpace(parts[0])
version = strings.TrimSpace(parts[1])
}
if !strings.HasPrefix(version, "v") {
// Version file has stripped off the "v" that we need. So add it back here.
version = fmt.Sprintf("v%v", version)
}
return &versionFile{
name: name,
version: version,
}
}
func DeterminePluginDependency(packageDir, packageName, packageVersion string) (*pulumirpc.PluginDependency, error) {
logging.V(5).Infof("GetRequiredPlugins: Determining plugin dependency: %v, %v, %v",
packageDir, packageName, packageVersion)
// Check for a `~/.nuget/packages/package_name/package_version/content/{pulumi-plugin.json,version.txt}` file.
artifactPath := filepath.Join(packageDir, strings.ToLower(packageName), packageVersion, "content")
pulumiPluginFilePath := filepath.Join(artifactPath, "pulumi-plugin.json")
versionFilePath := filepath.Join(artifactPath, "version.txt")
logging.V(5).Infof("GetRequiredPlugins: plugin file path: %v", versionFilePath)
logging.V(5).Infof("GetRequiredPlugins: version file path: %v", versionFilePath)
pulumiPlugin, err := plugin.LoadPulumiPluginJSON(pulumiPluginFilePath)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
// Explicitly not a resource
if pulumiPlugin != nil && !pulumiPlugin.Resource {
return nil, nil
}
var vf *versionFile
b, err := ioutil.ReadFile(versionFilePath)
switch {
case err == nil:
vf = newVersionFile(b, packageName)
break
case os.IsNotExist(err):
break
case err != nil:
return nil, fmt.Errorf("Failed to read version file: %w", err)
}
defaultName := strings.ToLower(strings.TrimPrefix(packageName, "Pulumi."))
// No pulumi-plugin.json or version.txt
// That means this is not a resource.
if pulumiPlugin == nil && vf == nil {
return nil, nil
}
// Create stubs to avoid dereferencing a null
if pulumiPlugin == nil {
pulumiPlugin = &plugin.PulumiPluginJSON{}
} else if vf == nil {
vf = &versionFile{}
}
or := func(o ...string) string {
for _, s := range o {
if s != "" {
return s
}
}
return ""
}
name := or(pulumiPlugin.Name, vf.name, defaultName)
version := or(pulumiPlugin.Version, vf.version, packageVersion)
_, err = semver.ParseTolerant(version)
if err != nil {
return nil, fmt.Errorf("Invalid package version: %w", err)
}
result := &pulumirpc.PluginDependency{
Name: name,
Version: version,
Server: pulumiPlugin.Server,
Kind: "resource",
}
logging.V(5).Infof("GetRequiredPlugins: Determining plugin dependency: %#v", result)
return result, nil
}
func (host *dotnetLanguageHost) DotnetBuild(
ctx context.Context, req *pulumirpc.GetRequiredPluginsRequest, engineClient pulumirpc.EngineClient) error {
args := []string{"build", "-nologo"}
if req.GetProgram() != "" {
args = append(args, req.GetProgram())
}
// Run the `dotnet build` command. Importantly, report the output of this to the user
// (ephemerally) as it is happening so they're aware of what's going on and can see the progress
// of things.
_, err := host.RunDotnetCommand(ctx, engineClient, args, true /*logToUser*/)
if err != nil {
return err
}
host.dotnetBuildSucceeded = true
return nil
}
func (host *dotnetLanguageHost) RunDotnetCommand(
ctx context.Context, engineClient pulumirpc.EngineClient, args []string, logToUser bool) (string, error) {
commandStr := strings.Join(args, " ")
if logging.V(5) {
logging.V(5).Infoln("Language host launching process: ", host.exec, commandStr)
}
// Buffer the writes we see from dotnet from its stdout and stderr streams. We will display
// these ephemerally as `dotnet build` runs. If the build does fail though, we will dump
// messages back to our own stdout/stderr so they get picked up and displayed to the user.
streamID := rand.Int31() //nolint:gosec
infoBuffer := &bytes.Buffer{}
errorBuffer := &bytes.Buffer{}
infoWriter := &logWriter{
ctx: ctx,
logToUser: logToUser,
engineClient: engineClient,
streamID: streamID,
buffer: infoBuffer,
severity: pulumirpc.LogSeverity_INFO,
}
errorWriter := &logWriter{
ctx: ctx,
logToUser: logToUser,
engineClient: engineClient,
streamID: streamID,
buffer: errorBuffer,
severity: pulumirpc.LogSeverity_ERROR,
}
// Now simply spawn a process to execute the requested program, wiring up stdout/stderr directly.
cmd := exec.Command(host.exec, args...) // nolint: gas // intentionally running dynamic program name.
cmd.Stdout = infoWriter
cmd.Stderr = errorWriter
_, err := infoWriter.LogToUser(fmt.Sprintf("running 'dotnet %v'", commandStr))
if err != nil {
return "", err
}
if err := cmd.Run(); 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(infoBuffer.Bytes())
os.Stderr.Write(errorBuffer.Bytes())
if exiterr, ok := err.(*exec.ExitError); ok {
// If the program ran, but exited with a non-zero error code. This will happen often, since user
// errors will trigger this. So, the error message should look as nice as possible.
if status, stok := exiterr.Sys().(syscall.WaitStatus); stok {
return "", errors.Errorf(
"'dotnet %v' exited with non-zero exit code: %d", commandStr, status.ExitStatus())
}
return "", errors.Wrapf(exiterr, "'dotnet %v' exited unexpectedly", commandStr)
}
// Otherwise, we didn't even get to run the program. This ought to never happen unless there's
// a bug or system condition that prevented us from running the language exec. Issue a scarier error.
return "", errors.Wrapf(err, "Problem executing 'dotnet %v'", commandStr)
}
_, err = infoWriter.LogToUser(fmt.Sprintf("'dotnet %v' completed successfully", commandStr))
return infoBuffer.String(), err
}
type logWriter struct {
ctx context.Context
logToUser bool
engineClient pulumirpc.EngineClient
streamID int32
severity pulumirpc.LogSeverity
buffer *bytes.Buffer
}
func (w *logWriter) Write(p []byte) (n int, err error) {
n, err = w.buffer.Write(p)
if err != nil {
return
}
return w.LogToUser(string(p))
}
func (w *logWriter) LogToUser(val string) (int, error) {
if w.logToUser {
_, err := w.engineClient.Log(w.ctx, &pulumirpc.LogRequest{
Message: strings.ToValidUTF8(val, "�"),
Urn: "",
Ephemeral: true,
StreamId: w.streamID,
Severity: w.severity,
})
if err != nil {
return 0, err
}
}
return len(val), nil
}
// RPC endpoint for LanguageRuntimeServer::Run
func (host *dotnetLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest) (*pulumirpc.RunResponse, error) {
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
}
executable := host.exec
args := []string{}
switch {
case host.binary != "" && strings.HasSuffix(host.binary, ".dll"):
// Portable pre-compiled dll: run `dotnet <name>.dll`
args = append(args, host.binary)
case host.binary != "":
// Self-contained executable: run it directly.
executable = host.binary
default:
// Run from source.
args = append(args, "run")
// If we are certain the project has been built,
// passing a --no-build flag to dotnet run results in
// up to 1s time savings.
if host.dotnetBuildSucceeded {
args = append(args, "--no-build")
}
if req.GetProgram() != "" {
args = append(args, req.GetProgram())
}
}
if logging.V(5) {
commandStr := strings.Join(args, " ")
logging.V(5).Infoln("Language host launching process: ", host.exec, commandStr)
}
// Now simply spawn a process to execute the requested program, wiring up stdout/stderr directly.
var errResult string
cmd := exec.Command(executable, args...) // nolint: gas // intentionally running dynamic program name.
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = host.constructEnv(req, config, configSecretKeys)
if err := cmd.Run(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// If the program ran, but exited with a non-zero error code. This will happen often, since user
// errors will trigger this. So, the error message should look as nice as possible.
if status, stok := exiterr.Sys().(syscall.WaitStatus); stok {
// Check if we got special exit code that means "we already gave the user an
// actionable message". In that case, we can simply bail out and terminate `pulumi`
// without showing any more messages.
if status.ExitStatus() == dotnetProcessExitedAfterShowingUserActionableMessage {
return &pulumirpc.RunResponse{Error: "", Bail: true}, nil
}
err = errors.Errorf("Program exited with non-zero exit code: %d", status.ExitStatus())
} else {
err = errors.Wrapf(exiterr, "Program exited unexpectedly")
}
} else {
// Otherwise, we didn't even get to run the program. This ought to never happen unless there's
// a bug or system condition that prevented us from running the language exec. Issue a scarier error.
err = errors.Wrapf(err, "Problem executing program (could not run language executor)")
}
errResult = err.Error()
}
return &pulumirpc.RunResponse{Error: errResult}, nil
}
func (host *dotnetLanguageHost) 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("organization", req.GetOrganization())
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 *dotnetLanguageHost) 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 *dotnetLanguageHost) 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 *dotnetLanguageHost) GetPluginInfo(ctx context.Context, req *pbempty.Empty) (*pulumirpc.PluginInfo, error) {
return &pulumirpc.PluginInfo{
Version: version.Version,
}, nil
}
func (host *dotnetLanguageHost) InstallDependencies(
req *pulumirpc.InstallDependenciesRequest, server pulumirpc.LanguageRuntime_InstallDependenciesServer) error {
closer, stdout, stderr, err := rpcutil.MakeInstallDependenciesStreams(server, req.IsTerminal)
if err != nil {
return err
}
// best effort close, but we try an explicit close and error check at the end as well
defer closer.Close()
stdout.Write([]byte("Installing dependencies...\n\n"))
dotnetbin, err := executable.FindExecutable("dotnet")
if err != nil {
return err
}
cmd := exec.Command(dotnetbin, "build")
cmd.Dir = req.Directory
cmd.Env = os.Environ()
cmd.Stdout, cmd.Stderr = stdout, stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("`dotnet build` failed to install dependencies: %w", err)
}
stdout.Write([]byte("Finished installing dependencies\n\n"))
if err := closer.Close(); err != nil {
return err
}
return nil
}
func (host *dotnetLanguageHost) About(ctx context.Context, req *pbempty.Empty) (*pulumirpc.AboutResponse, error) {
getResponse := func(execString string, args ...string) (string, string, error) {
ex, err := executable.FindExecutable(execString)
if err != nil {
return "", "", fmt.Errorf("could not find executable '%s': %w", execString, err)
}
cmd := exec.Command(ex, args...)
var out []byte
if out, err = cmd.Output(); err != nil {
cmd := ex
if len(args) != 0 {
cmd += " " + strings.Join(args, " ")
}
return "", "", fmt.Errorf("failed to execute '%s'", cmd)
}
return ex, strings.TrimSpace(string(out)), nil
}
dotnet, version, err := getResponse("dotnet", "--version")
if err != nil {
return nil, err
}
return &pulumirpc.AboutResponse{
Executable: dotnet,
Version: version,
}, nil
}
func (host *dotnetLanguageHost) GetProgramDependencies(
ctx context.Context, req *pulumirpc.GetProgramDependenciesRequest) (*pulumirpc.GetProgramDependenciesResponse, error) {
// dotnet list package
var err error
if host.binary != "" {
return nil, errors.New("Could not get dependencies because pulumi specifies a binary")
}
var ex string
var out []byte
ex, err = executable.FindExecutable("dotnet")
if err != nil {
return nil, err
}
cmdArgs := []string{"list", "package"}
if req.TransitiveDependencies {
cmdArgs = append(cmdArgs, "--include-transitive")
}
cmd := exec.Command(ex, cmdArgs...)
if out, err = cmd.Output(); err != nil {
return nil, fmt.Errorf("Failed to call \"%s\": %w", ex, err)
}
lines := strings.Split(strings.ReplaceAll(string(out), "\r\n", "\n"), "\n")
var packages []*pulumirpc.DependencyInfo
for _, p := range lines {
p := strings.TrimSpace(p)
if strings.HasPrefix(p, ">") {
p = strings.TrimPrefix(p, "> ")
segments := strings.Split(p, " ")
var nameRequiredVersion []string
for _, s := range segments {
if s != "" {
nameRequiredVersion = append(nameRequiredVersion, s)
}
}
var version int
if len(nameRequiredVersion) == 3 {
// Top level package => name required version
version = 2
} else if len(nameRequiredVersion) == 2 {
// Transitive package => name version
version = 1
} else {
return nil, fmt.Errorf("Failed to parse \"%s\"", p)
}
packages = append(packages, &pulumirpc.DependencyInfo{
Name: nameRequiredVersion[0],
Version: nameRequiredVersion[version],
})
}
}
return &pulumirpc.GetProgramDependenciesResponse{
Dependencies: packages,
}, nil
}
func (host *dotnetLanguageHost) RunPlugin(
req *pulumirpc.RunPluginRequest, server pulumirpc.LanguageRuntime_RunPluginServer) error {
return errors.New("not supported")
}