Skip to content

Commit

Permalink
Merge branch 'main' into registry
Browse files Browse the repository at this point in the history
  • Loading branch information
eveld committed Nov 22, 2024
2 parents a90cf50 + 134ed58 commit 1f6e5b7
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ generate_mocks:

install_local:
go build -ldflags "-X main.version=${git_commit}" -gcflags=all="-N -l" -o bin/jumppad main.go
sudo mv /usr/bin/jumppad /usr/bin/jumppad-old || true
sudo cp bin/jumppad /usr/bin/jumppad
sudo mv /usr/bin/jumppad /usr/local/bin/jumppad-old || true
sudo cp bin/jumppad /usr/local/bin/jumppad

remove_local:
sudo mv /usr/bin/jumppad-old /usr/bin/jumppad || true
sudo mv /usr/local/bin/jumppad-old /usr/local/bin/jumppad || true
28 changes: 12 additions & 16 deletions examples/functions/container.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ resource "container" "consul" {
}

environment = {
file_dir = dir()
env = env("HOME")
k8s_config = k8s_config("dc1")
k8s_config_docker = k8s_config_docker("dc1")
home = home()
shipyard = shipyard()
file = file("./default.vars")
data = data("mine")
docker_ip = docker_ip()
docker_host = docker_host()
shipyard_ip = shipyard_ip()
cluster_api = cluster_api("nomad_cluster.dc1")
cluster_port = cluster_port("nomad_cluster.dc1")
var_len = len(variable.test_var)
os = system("os")
arch = system("arch")
file_dir = dir()
env = env("HOME")
home = home()
file = file("./default.vars")
data = data("mine")
data_with_permissions = data_with_permissions("mine", 755)
docker_ip = docker_ip()
docker_host = docker_host()
var_len = len(variable.test_var)
os = system("os")
arch = system("arch")
exists = exists("file")
}
}
10 changes: 9 additions & 1 deletion pkg/config/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func customHCLFuncDockerHost() (string, error) {
}

func customHCLFuncDataFolderWithPermissions(name string, permissions int) (string, error) {
if permissions > 0 && permissions < 778 {
if permissions < 0 || permissions > 777 {
return "", fmt.Errorf("permissions must be a three digit number less than 777")
}

Expand All @@ -50,3 +50,11 @@ func customHCLFuncSystem(property string) (string, error) {
return "", fmt.Errorf("unknown system property %s", property)
}
}

func customHCLFuncExists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
return false, nil
}

return true, nil
}
24 changes: 24 additions & 0 deletions pkg/config/functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestExistsFalse(t *testing.T) {
exists, err := customHCLFuncExists("nonexistent")
require.NoError(t, err)
require.Equal(t, false, exists)
}

func TestExistsTrue(t *testing.T) {
file := filepath.Join(t.TempDir(), "testdata")
os.WriteFile(file, []byte("test"), 0644)

exists, err := customHCLFuncExists(file)
require.NoError(t, err)
require.Equal(t, true, exists)
}
83 changes: 58 additions & 25 deletions pkg/config/resources/exec/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import (
"strings"
"time"

"github.com/jumppad-labs/hclconfig/convert"
htypes "github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/clients"
cmdClient "github.com/jumppad-labs/jumppad/pkg/clients/command"
cmdTypes "github.com/jumppad-labs/jumppad/pkg/clients/command/types"
contClient "github.com/jumppad-labs/jumppad/pkg/clients/container"
"github.com/jumppad-labs/jumppad/pkg/clients/container/types"
contTypes "github.com/jumppad-labs/jumppad/pkg/clients/container/types"
"github.com/jumppad-labs/jumppad/pkg/clients/logger"
"github.com/jumppad-labs/jumppad/pkg/utils"
sdk "github.com/jumppad-labs/plugin-sdk"
"github.com/zclconf/go-cty/cty"
)

// checks Provider implements the sdk.Provider interface
Expand Down Expand Up @@ -63,6 +64,13 @@ func (p *Provider) Create(ctx context.Context) error {

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

if _, err := os.Stat(outPath); err != nil {
err := os.WriteFile(outPath, []byte{}, 0755)
if err != nil {
return fmt.Errorf("unable to create output file: %w", err)
}
}

// cleanup the local output file
defer os.Remove(outPath)

Expand All @@ -83,31 +91,11 @@ func (p *Provider) Create(ctx context.Context) error {
p.config.PID = pid
}

// parse any output from the script
if _, err := os.Stat(outPath); err != nil {
p.log.Debug("Output file not found", "ref", p.config.Meta.ID, "path", outPath)
return nil
}

d, err := os.ReadFile(outPath)
err := p.generateOutput()
if err != nil {
return fmt.Errorf("unable to read output file: %w", err)
return fmt.Errorf("unable to generate output: %w", err)
}

output := map[string]string{}

outs := strings.Split(string(d), "\n")
for _, v := range outs {
parts := strings.Split(v, "=")
if len(parts) != 2 {
continue
}

output[parts[0]] = parts[1]
}

p.config.Output = output

return nil
}

Expand Down Expand Up @@ -146,6 +134,11 @@ func (p *Provider) Refresh(ctx context.Context) error {

p.log.Debug("Refresh Exec", "ref", p.config.Meta.Name)

err := p.generateOutput()
if err != nil {
return fmt.Errorf("unable to generate output: %w", err)
}

return nil
}

Expand Down Expand Up @@ -226,9 +219,9 @@ func (p *Provider) createRemoteExecContainer() (string, error) {
// generate the ID for the new container based on the clock time and a string
fqdn := utils.FQDN(p.config.Meta.Name, p.config.Meta.Module, p.config.Meta.Type)

new := contTypes.Container{
new := types.Container{
Name: fqdn,
Image: &contTypes.Image{Name: p.config.Image.Name, Username: p.config.Image.Username, Password: p.config.Image.Password},
Image: &types.Image{Name: p.config.Image.Name, Username: p.config.Image.Username, Password: p.config.Image.Password},
Environment: p.config.Environment,
}

Expand Down Expand Up @@ -331,3 +324,43 @@ func (p *Provider) createLocalExec(outputPath string) (int, error) {

return pid, nil
}

func (p *Provider) generateOutput() error {
outPath := fmt.Sprintf("%s/%s.out", utils.JumppadTemp(), p.config.Meta.ID)

// parse any output from the script
if _, err := os.Stat(outPath); err != nil {
p.log.Debug("Output file not found", "ref", p.config.Meta.ID, "path", outPath)
return nil
}

d, err := os.ReadFile(outPath)
if err != nil {
return fmt.Errorf("unable to read output file: %w", err)
}

output := map[string]string{}
outs := strings.Split(string(d), "\n")
for _, v := range outs {
parts := strings.Split(v, "=")
if len(parts) != 2 {
continue
}

output[parts[0]] = parts[1]
}

values := map[string]cty.Value{}
for k, v := range output {
value, err := convert.GoToCtyValue(v)
if err != nil {
return fmt.Errorf("unable to convert output value to cty: %w", err)
}

values[k] = value
}

p.config.Output = cty.ObjectVal(values)

return nil
}
2 changes: 1 addition & 1 deletion pkg/config/resources/exec/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestParsesOutput(t *testing.T) {
err := p.Create(context.Background())
require.NoError(t, err)

require.True(t, e.Output["FOO"] == "BAR")
require.True(t, e.Output.AsValueMap()["FOO"].AsString() == "BAR")
}

func TestDeletesOutput(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions pkg/config/resources/exec/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/jumppad-labs/jumppad/pkg/config"
ctypes "github.com/jumppad-labs/jumppad/pkg/config/resources/container"
"github.com/jumppad-labs/jumppad/pkg/utils"
"github.com/zclconf/go-cty/cty"
)

// TypeExec is the resource string for an Exec resource
Expand All @@ -33,9 +34,9 @@ type Exec struct {
RunAs *ctypes.User `hcl:"run_as,block" json:"run_as,omitempty"` // User block for mapping the user id and group id inside the container

// 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 exec
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 cty.Value `hcl:"output,optional"` // output values returned from exec
}

func (e *Exec) Process() error {
Expand All @@ -55,6 +56,8 @@ func (e *Exec) Process() error {
}
}

// e.Output = map[string]string{}

// do we have an existing resource in the state?
// if so we need to set any computed resources for dependents
cfg, err := config.LoadState()
Expand Down
1 change: 1 addition & 0 deletions pkg/config/zz_hclparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func NewParser(callback hclconfig.WalkCallback, variables map[string]string, var
p.RegisterFunction("data", customHCLFuncDataFolder)
p.RegisterFunction("data_with_permissions", customHCLFuncDataFolderWithPermissions)
p.RegisterFunction("system", customHCLFuncSystem)
p.RegisterFunction("exists", customHCLFuncExists)

return p
}

0 comments on commit 1f6e5b7

Please sign in to comment.