github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/resource/provider/main.go (about)

     1  // Copyright 2016-2018, Pulumi Corporation.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package provider
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"flag"
    21  	"fmt"
    22  	"time"
    23  
    24  	"google.golang.org/grpc"
    25  
    26  	"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
    27  	"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
    28  	"github.com/pulumi/pulumi/sdk/v3/go/common/util/rpcutil"
    29  	pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
    30  )
    31  
    32  // Tracing is the optional command line flag passed to this provider for configuring a  Zipkin-compatible tracing
    33  // endpoint
    34  var tracing string
    35  
    36  // Main is the typical entrypoint for a resource provider plugin.  Using it isn't required but can cut down
    37  // significantly on the amount of boilerplate necessary to fire up a new resource provider.
    38  func Main(name string, provMaker func(*HostClient) (pulumirpc.ResourceProviderServer, error)) error {
    39  	flag.StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint")
    40  	flag.Parse()
    41  
    42  	// Initialize loggers before going any further.
    43  	logging.InitLogging(false, 0, false)
    44  	cmdutil.InitTracing(name, name, tracing)
    45  
    46  	// Read the non-flags args and connect to the engine.
    47  	var cancelChannel chan bool
    48  	args := flag.Args()
    49  	var host *HostClient
    50  	if len(args) == 0 {
    51  		// Start the provider in Attach mode
    52  	} else if len(args) == 1 {
    53  		var err error
    54  		host, err = NewHostClient(args[0])
    55  		if err != nil {
    56  			return fmt.Errorf("fatal: could not connect to host RPC: %v", err)
    57  		}
    58  
    59  		// If we have a host cancel our cancellation context if it fails the healthcheck
    60  		ctx, cancel := context.WithCancel(context.Background())
    61  		// map the context Done channel to the rpcutil boolean cancel channel
    62  		cancelChannel = make(chan bool)
    63  		go func() {
    64  			<-ctx.Done()
    65  			close(cancelChannel)
    66  		}()
    67  		err = rpcutil.Healthcheck(ctx, args[0], 5*time.Minute, cancel)
    68  		if err != nil {
    69  			return fmt.Errorf("could not start health check host RPC server: %w", err)
    70  		}
    71  	} else {
    72  		return errors.New("fatal: could not connect to host RPC; missing argument")
    73  	}
    74  
    75  	// Fire up a gRPC server, letting the kernel choose a free port for us.
    76  	handle, err := rpcutil.ServeWithOptions(rpcutil.ServeOptions{
    77  		Cancel: cancelChannel,
    78  		Init: func(srv *grpc.Server) error {
    79  			prov, proverr := provMaker(host)
    80  			if proverr != nil {
    81  				return fmt.Errorf("failed to create resource provider: %v", proverr)
    82  			}
    83  			pulumirpc.RegisterResourceProviderServer(srv, prov)
    84  			return nil
    85  		},
    86  		Options: rpcutil.OpenTracingServerInterceptorOptions(nil),
    87  	})
    88  	if err != nil {
    89  		return fmt.Errorf("fatal: %v", err)
    90  	}
    91  
    92  	// The resource provider protocol requires that we now write out the port we have chosen to listen on.
    93  	fmt.Printf("%d\n", handle.Port)
    94  
    95  	// Finally, wait for the server to stop serving.
    96  	if err := <-handle.Done; err != nil {
    97  		return fmt.Errorf("fatal: %v", err)
    98  	}
    99  
   100  	return nil
   101  }