Skip to content

Commit

Permalink
fix: Add dependency check for pulumi providers. (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm authored Jul 25, 2024
1 parent 8dbddb7 commit 44032f6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
64 changes: 64 additions & 0 deletions cloud/common/deploy/provider/dependencies.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions cloud/common/deploy/provider/pulumi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 44032f6

Please sign in to comment.