Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add dependency check for pulumi providers. #650

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
tjholm marked this conversation as resolved.
Show resolved Hide resolved
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
Loading