From 44032f6eebf365232e30225b3b8d3b26a9fb8835 Mon Sep 17 00:00:00 2001 From: Tim Holm Date: Thu, 25 Jul 2024 12:17:46 +1000 Subject: [PATCH] fix: Add dependency check for pulumi providers. (#650) --- cloud/common/deploy/provider/dependencies.go | 64 ++++++++++++++++++++ cloud/common/deploy/provider/pulumi.go | 12 ++++ 2 files changed, 76 insertions(+) create mode 100644 cloud/common/deploy/provider/dependencies.go diff --git a/cloud/common/deploy/provider/dependencies.go b/cloud/common/deploy/provider/dependencies.go new file mode 100644 index 000000000..1a641cde7 --- /dev/null +++ b/cloud/common/deploy/provider/dependencies.go @@ -0,0 +1,64 @@ +// Copyright 2021 Nitric Technologies Pty Ltd. +// +// 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 provider + +import ( + "fmt" + "os/exec" +) + +type DependencyCheck func() error + +func checkPulumiAvailable() error { + _, err := exec.LookPath("pulumi") + if err != nil { + return fmt.Errorf("pulumi is required to use this provider, please install pulumi and try again") + } + + return nil +} + +func checkDockerAvailable() error { + cmd := exec.Command("docker", "info") + err := cmd.Run() + if err != nil { + return fmt.Errorf("docker is required to use this provider, please install docker and try again") + } + + return nil +} + +func checkDependencies(checks ...DependencyCheck) error { + errs := []error{} + + for _, check := range checks { + err := check() + if err != nil { + errs = append(errs, err) + } + } + + if len(errs) > 0 { + errMsg := "The following dependencies are missing:" + for _, e := range errs { + errMsg += fmt.Sprintf("\n - %s", e.Error()) + } + + // combine the errors in a list + return fmt.Errorf(errMsg) + } + + return nil +} diff --git a/cloud/common/deploy/provider/pulumi.go b/cloud/common/deploy/provider/pulumi.go index b5ce8eb1e..9cd6602cc 100644 --- a/cloud/common/deploy/provider/pulumi.go +++ b/cloud/common/deploy/provider/pulumi.go @@ -31,6 +31,8 @@ import ( "github.com/pulumi/pulumi/sdk/v3/go/auto/optup" "github.com/pulumi/pulumi/sdk/v3/go/pulumi" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) type PulumiProviderServer struct { @@ -196,6 +198,11 @@ func parsePulumiError(err error) error { // Up - automatically called by the Nitric CLI via the `up` command func (s *PulumiProviderServer) Up(req *deploymentspb.DeploymentUpRequest, stream deploymentspb.Deployment_UpServer) error { + // Verify if dependencies are available + if err := checkDependencies(checkPulumiAvailable, checkDockerAvailable); err != nil { + return status.Error(codes.FailedPrecondition, err.Error()) + } + projectName, stackName, err := stackAndProjectFromAttributes(req.Attributes.AsMap()) if err != nil { return err @@ -284,6 +291,11 @@ func (s *PulumiProviderServer) Up(req *deploymentspb.DeploymentUpRequest, stream // Down - automatically called by the Nitric CLI via the `down` command func (s *PulumiProviderServer) Down(req *deploymentspb.DeploymentDownRequest, stream deploymentspb.Deployment_DownServer) error { + // Verify if dependencies are available + if err := checkDependencies(checkPulumiAvailable, checkDockerAvailable); err != nil { + return status.Error(codes.FailedPrecondition, err.Error()) + } + projectName, stackName, err := stackAndProjectFromAttributes(req.Attributes.AsMap()) if err != nil { return err