Skip to content

Commit

Permalink
ensure references to TempDir use JumppadTemp
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Apr 11, 2024
1 parent 18fdc9d commit 157c963
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func newDestroyCmd(cc connector.Connector, l logger.Logger) *cobra.Command {
// clean up the data folders
os.RemoveAll(utils.DataFolder("", os.ModePerm))
os.RemoveAll(utils.LibraryFolder("", os.ModePerm))
os.RemoveAll(utils.JumppadTemp())

// shutdown ingress when we destroy all resources
if cc.IsRunning() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/plugin_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/clients"
"github.com/jumppad-labs/jumppad/pkg/config/resources/build"
"github.com/jumppad-labs/jumppad/pkg/utils"
cp "github.com/otiai10/copy"
"github.com/spf13/cobra"
"golang.org/x/mod/modfile"
Expand Down Expand Up @@ -64,7 +65,7 @@ var pluginCmd = &cobra.Command{
engineClients, _ := clients.GenerateClients(l)

// create a temp output folder
tmp := os.TempDir()
tmp := utils.JumppadTemp()
output := filepath.Join(tmp, "jumppad_build")

os.RemoveAll(output)
Expand Down
4 changes: 2 additions & 2 deletions pkg/clients/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package command

import (
"fmt"
"os"
"path/filepath"
"sync"
"time"

"github.com/jumppad-labs/gohup"
"github.com/jumppad-labs/jumppad/pkg/clients/command/types"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
"github.com/jumppad-labs/jumppad/pkg/utils"
)

var ErrorCommandTimeout = fmt.Errorf("Command timed out before completing")
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *CommandImpl) Execute(config types.CommandConfig) (int, error) {
// Kill a process with the given pid
func (c *CommandImpl) Kill(pid int) error {
lp := gohup.LocalProcess{}
pidPath := filepath.Join(os.TempDir(), fmt.Sprintf("%d.pid", pid))
pidPath := filepath.Join(utils.JumppadTemp(), fmt.Sprintf("%d.pid", pid))

if s, _ := lp.QueryStatus(pidPath); s == gohup.StatusRunning {
return lp.Stop(pidPath)
Expand Down
4 changes: 2 additions & 2 deletions pkg/clients/container/docker_tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ func (d *DockerTasks) CopyFromContainer(id, src, dst string) error {
defer reader.Close()

// untar the file to a temporary folder
dir, err := os.MkdirTemp(os.TempDir(), "")
dir, err := os.MkdirTemp(utils.JumppadTemp(), id)
if err != nil {
return fmt.Errorf("unable to create temporary directory, %s", err)
}
Expand Down Expand Up @@ -968,7 +968,7 @@ func (d *DockerTasks) CopyFilesToVolume(volumeID string, filenames []string, pat
// CreateFileInContainer creates a file with the given contents and name in the container containerID and
// stores it in the container at the directory path.
func (d *DockerTasks) CreateFileInContainer(containerID, contents, filename, path string) error {
tmpFile := filepath.Join(os.TempDir(), filename)
tmpFile := filepath.Join(utils.JumppadTemp(), filename)

err := os.WriteFile(tmpFile, []byte(contents), 0755)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/resources/container/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (c *Provider) internalCreate(ctx context.Context, sidecar bool) error {
func (c *Provider) runExecHealthCheck(ctx context.Context, id string, command []string, script string, exitCode int, timeout time.Duration) error {
if len(script) > 0 {
// write the script to a temp file
dir, err := os.MkdirTemp(os.TempDir(), "script*")
dir, err := os.MkdirTemp(utils.JumppadTemp(), "script*")
if err != nil {
return fmt.Errorf("unable to create temporary directory for script: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/resources/exec/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (p *Provider) Create(ctx context.Context) error {

p.log.Info("executing script", "ref", p.config.Meta.ID, "script", p.config.Script)

outPath := fmt.Sprintf("%s/%s.out", os.TempDir(), p.config.Meta.ID)
outPath := fmt.Sprintf("%s/%s.out", utils.JumppadTemp(), p.config.Meta.ID)

// cleanup the local output file
defer os.Remove(outPath)
Expand Down
10 changes: 6 additions & 4 deletions pkg/config/resources/exec/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
containerMocks "github.com/jumppad-labs/jumppad/pkg/clients/container/mocks"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
"github.com/jumppad-labs/jumppad/pkg/config/resources/container"
"github.com/jumppad-labs/jumppad/pkg/utils"
"github.com/jumppad-labs/jumppad/testutils"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestInjectsOutputEnvIntoLocal(t *testing.T) {

ac := testutils.GetCalls(&cm.Mock, "Execute")[0].Arguments[0].(cmdTypes.CommandConfig)

td := os.TempDir()
td := utils.JumppadTemp()
require.Contains(t, ac.Env, fmt.Sprintf("EXEC_OUTPUT=%s/resource.exec.test.out", td))
}

Expand All @@ -51,7 +52,8 @@ func TestParsesOutput(t *testing.T) {
e.Script = "echo FOO=BAR >> $EXEC_OUTPUT"

// write the output for the test
td := os.TempDir()

td := utils.JumppadTemp()
os.WriteFile(fmt.Sprintf("%s/resource.exec.test.out", td), []byte("FOO=BAR"), 0644)
t.Cleanup(func() {
os.Remove(fmt.Sprintf("%s/resource.exec.test.out", td))
Expand All @@ -68,7 +70,7 @@ func TestDeletesOutput(t *testing.T) {
e.Script = "echo FOO=BAR >> $EXEC_OUTPUT"

// write the output for the test
td := os.TempDir()
td := utils.JumppadTemp()
os.WriteFile(fmt.Sprintf("%s/resource.exec.test.out", td), []byte("FOO=BAR"), 0644)

err := p.Create(context.Background())
Expand All @@ -85,7 +87,7 @@ func TestCopiesOutputInExec(t *testing.T) {
e.Script = "echo FOO=BAR >> $EXEC_OUTPUT"

// write the output for the test
td := os.TempDir()
td := utils.JumppadTemp()
os.WriteFile(fmt.Sprintf("%s/resource.exec.test.out", td), []byte("FOO=BAR"), 0644)
t.Cleanup(func() {
os.Remove(fmt.Sprintf("%s/resource.exec.test.out", td))
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/resources/exec/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Exec struct {
// output
PID int `hcl:"pid,optional" json:"pid,omitempty"` // PID stores the ID of the created connector service if it is a local exec
ExitCode int `hcl:"exit_code,optional" json:"exit_code,omitempty"` // Exit code of the process
Output map[string]string `hcl:"output,optional" json:"output,omitempty"` // output values returned from Terraform
Output map[string]string `hcl:"output,optional" json:"output,omitempty"` // output values returned from exec
}

func (e *Exec) Process() error {
Expand Down

0 comments on commit 157c963

Please sign in to comment.