From 8d99ad771c2be0d6df33abf080b74e47ba04e1db Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Fri, 27 Oct 2023 18:02:11 +0200 Subject: [PATCH] Imprs --- internal/client/client.go | 10 ++++++- internal/provider/instance_client.go | 41 +++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index b0fa255..5d8ec15 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -132,7 +132,7 @@ func (c Client) DirEnsure(path string) error { func (c Client) FileExists(path string) (bool, error) { out, err := c.RunShell(fmt.Sprintf("test -f %s && echo '0' || echo '1'", path)) if err != nil { - return false, err + return false, fmt.Errorf("cannot check if file exists '%s': %w", path, err) } return strings.TrimSpace(string(out)) == "0", nil } @@ -147,6 +147,14 @@ func (c Client) FileMove(oldPath string, newPath string) error { return nil } +func (c Client) DirExists(path string) (bool, error) { + out, err := c.RunShell(fmt.Sprintf("test -d %s && echo '0' || echo '1'", path)) + if err != nil { + return false, fmt.Errorf("cannot check if directory exists '%s': %w", path, err) + } + return strings.TrimSpace(string(out)) == "0", nil +} + func (c Client) DirCopy(localPath string, remotePath string, override bool) error { if err := c.DirEnsure(remotePath); err != nil { return err diff --git a/internal/provider/instance_client.go b/internal/provider/instance_client.go index e011bde..3748a24 100644 --- a/internal/provider/instance_client.go +++ b/internal/provider/instance_client.go @@ -3,6 +3,7 @@ package provider import ( "fmt" "github.com/hashicorp/terraform-plugin-log/tflog" + "gopkg.in/yaml.v3" ) type InstanceClient ClientContext[InstanceResourceModel] @@ -28,11 +29,17 @@ func (ic *InstanceClient) prepareDataDir() error { return nil } -func (ic *InstanceClient) installCompose() error { // TODO do not rely on github script here maybe - out, err := ic.cl.RunShellWithEnv(fmt.Sprintf("cd %s && curl -s https://raw.githubusercontent.com/wttech/aemc/main/project-init.sh?token=1 | sh", ic.DataDir())) - tflog.Info(ic.ctx, string(out)) +func (ic *InstanceClient) installComposeWrapper() error { + exists, err := ic.cl.FileExists(fmt.Sprintf("%s/aemw", ic.DataDir())) if err != nil { - return fmt.Errorf("cannot install AEM Compose CLI: %w", err) + return fmt.Errorf("cannot check if AEM Compose CLI wrapper is installed: %w", err) + } + if !exists { + out, err := ic.cl.RunShellWithEnv(fmt.Sprintf("cd %s && curl -s 'https://raw.githubusercontent.com/wttech/aemc/main/pkg/project/common/aemw' -o 'aemw'", ic.DataDir())) + tflog.Info(ic.ctx, string(out)) + if err != nil { + return fmt.Errorf("cannot download AEM Compose CLI wrapper: %w", err) + } } return nil } @@ -84,3 +91,29 @@ func (ic *InstanceClient) launch() error { return nil } + +type InstanceStatus struct { + Data struct { + Instances []struct { + ID string `yaml:"id"` + URL string `yaml:"url"` + AemVersion string `yaml:"aem_version"` + Attributes []string `yaml:"attributes"` + RunModes []string `yaml:"run_modes"` + HealthChecks []string `yaml:"health_checks"` + Dir string `yaml:"dir"` + } `yaml:"instances"` + } +} + +func (ic *InstanceClient) ReadStatus() (InstanceStatus, error) { + var status InstanceStatus + yamlBytes, err := ic.cl.RunShellWithEnv(fmt.Sprintf("cd %s && sh aemw instance status --output-format yaml", ic.DataDir())) + if err != nil { + return status, err + } + if err := yaml.Unmarshal(yamlBytes, &status); err != nil { + return status, fmt.Errorf("unable to parse AEM instance status: %w", err) + } + return status, nil +}