Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Oct 2, 2023
1 parent 5d48ed1 commit e0f7559
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@ For example: `sh develop.sh examples/ssh plan`.

1. Run command `go run . -debug` from IDEA in debug mode and copy the value of `TF_REATTACH_PROVIDERS` from the output.
2. Set up breakpoint in the code.
3. Run `TF_REATTACH_PROVIDERS=<value_copied_above> TF_CLI_CONFIG_FILE=$(pwd)/dev_overrides.tfrc terraform apply` in one of the examples directory.
3. Run `TF_REATTACH_PROVIDERS=<value_copied_above> TF_CLI_CONFIG_FILE=$(pwd)/../../dev_overrides.tfrc terraform apply` in one of the examples directory.
4. The breakpoint should be hit.
31 changes: 27 additions & 4 deletions internal/client/client.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package client

import (
"context"
"fmt"
"github.com/melbahja/goph"
"os"
"path/filepath"
"strings"
"time"
)

type Client struct {
Expand Down Expand Up @@ -37,6 +39,25 @@ func (c Client) Connect() error {
return c.connection.Connect()
}

func (c Client) ConnectWithRetry(callback func()) error {
timeout := time.Minute * 5
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
for {
select {
case <-ctx.Done():
return fmt.Errorf("cannot connect - awaiting timeout reached '%s'", timeout)
default:
err := c.Connect()
if err == nil {
return nil
}
time.Sleep(time.Second)
callback()
}
}
}

func (c Client) Disconnect() error {
return c.connection.Disconnect()
}
Expand All @@ -50,7 +71,7 @@ func (c Client) Run(cmdLine []string) (*goph.Cmd, error) {
}

func (c Client) RunShell(cmd string) ([]byte, error) {
cmdObj, err := c.Command([]string{"sh", "-c", cmd}) // TODO run pass output env vars
cmdObj, err := c.Command([]string{"sh", "-c", "\"" + cmd + "\""})
if err != nil {
return nil, fmt.Errorf("cannot create command '%s': %w", cmd, err)
}
Expand Down Expand Up @@ -86,7 +107,7 @@ func (c Client) FileExists(path string) (bool, error) {
if err != nil {
return false, err
}
return strings.TrimSpace(string(out)) == "1", nil
return strings.TrimSpace(string(out)) == "0", nil
}

func (c Client) FileMove(oldPath string, newPath string) error {
Expand All @@ -108,12 +129,14 @@ func (c Client) DirCopy(localPath string, remotePath string, override bool) erro
return fmt.Errorf("cannot list files to copy in directory '%s': %w", localPath, err)
}
for _, file := range dir {
localSubPath := filepath.Join(localPath, file.Name())
remoteSubPath := filepath.Join(remotePath, file.Name())
if file.IsDir() {
if err := c.DirCopy(filepath.Join(localPath, file.Name()), filepath.Join(remotePath, file.Name()), override); err != nil {
if err := c.DirCopy(localSubPath, remoteSubPath, override); err != nil {
return err
}
} else {
if err := c.FileCopy(filepath.Join(localPath, file.Name()), filepath.Join(remotePath, file.Name()), override); err != nil {
if err := c.FileCopy(localSubPath, remoteSubPath, override); err != nil {
return err
}
}
Expand Down
4 changes: 1 addition & 3 deletions internal/client/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ func (c ClientManager) Make(typeName string, settings map[string]string) (*Clien
settings: settings,
connection: connection,

Env: []string{
"TERM=xterm",
},
Env: []string{},
}, nil
}

Expand Down
19 changes: 6 additions & 13 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/wttech/terraform-provider-aem/internal/client"
"os"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand Down Expand Up @@ -146,7 +145,7 @@ func (r *InstanceResource) Create(ctx context.Context, req resource.CreateReques
resp.Diagnostics.AddError("Unable to determine AEM instance client", fmt.Sprintf("%s", err))
return
}
if err := cl.Connect(); err != nil {
if err := cl.ConnectWithRetry(func() { tflog.Info(ctx, "Awaiting connection to AEM instance machine") }); err != nil {
resp.Diagnostics.AddError("Unable to connect to AEM instance machine", fmt.Sprintf("%s", err))
return
}
Expand Down Expand Up @@ -224,26 +223,20 @@ func (r *InstanceResource) copyConfigFile(ic InstanceCreateContext) bool {
}

func (r *InstanceResource) copyLibraryDir(ic InstanceCreateContext) bool {
libDir := ic.data.Compose.LibDir.ValueString()
libFiles, err := os.ReadDir(libDir)
if err != nil {
ic.resp.Diagnostics.AddError("Unable to read files in AEM library directory", fmt.Sprintf("%s", err))
localLibDir := ic.data.Compose.LibDir.ValueString()
remoteLibDir := fmt.Sprintf("%s/aem/home/lib", ic.DataDir())
if err := ic.cl.DirCopy(localLibDir, remoteLibDir, false); err != nil {
ic.resp.Diagnostics.AddError("Unable to copy AEM library dir", fmt.Sprintf("%s", err))
return false
}
for _, libFile := range libFiles {
if err := ic.cl.FileCopy(fmt.Sprintf("%s/%s", libDir, libFile.Name()), fmt.Sprintf("%s/aem/home/lib/%s", ic.DataDir(), libFile.Name()), false); err != nil {
ic.resp.Diagnostics.AddError("Unable to copy AEM library file", fmt.Sprintf("file path '%s/%s': %s", libDir, libFile.Name(), err))
return false
}
}
return true
}

func (r *InstanceResource) launch(ic InstanceCreateContext) bool {
tflog.Info(ic.ctx, "Launching AEM instance(s)")

// TODO register systemd service instead and start it
ymlBytes, err := ic.cl.RunShell(fmt.Sprintf("cd %s && aemw instance launch --output-format yml", ic.DataDir()))
ymlBytes, err := ic.cl.RunShell(fmt.Sprintf("cd %s && sh aemw instance launch", ic.DataDir()))

if err != nil {
ic.resp.Diagnostics.AddError("Unable to launch AEM instance", fmt.Sprintf("%s", err))
Expand Down

0 comments on commit e0f7559

Please sign in to comment.