Skip to content

Commit

Permalink
Hooks imprs
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Nov 6, 2023
1 parent 7c4d6ae commit c9f63e9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/ssh/aem.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ resource "aem_instance" "single" {
#!/bin/sh
sudo yum install -y unzip && \
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
unzip awscliv2.zip && \
sudo ./aws/install && \
unzip -q awscliv2.zip && \
sudo ./aws/install --update && \
mkdir -p "/home/ec2-user/aemc/aem/home/lib" && \
aws s3 cp --recursive --no-progress "s3://aemc/instance/classic/" "/home/ec2-user/aemc/aem/home/lib"
EOF
Expand Down
2 changes: 1 addition & 1 deletion examples/ssh/aem/default/etc/aem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@ output:
# File path of logs written especially when output format is different than 'text'
file: aem/home/var/log/aem.log
# Controls where outputs and logs should be written to when format is 'text' (console|file|both)
mode: console
mode: both
4 changes: 2 additions & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (c Client) RunShellWithEnv(cmd string) ([]byte, error) {
return c.RunShell(fmt.Sprintf("source %s && %s", c.envScriptPath(), cmd))
}

func (c Client) RunShellScriptWithEnv(cmdScript string) ([]byte, error) {
func (c Client) RunShellScriptWithEnv(dir string, cmdScript string) ([]byte, error) {
file, err := os.CreateTemp(os.TempDir(), "tf-provider-aem-script-*.sh")
path := file.Name()
defer func() { _ = file.Close(); _ = os.Remove(path) }()
Expand All @@ -120,7 +120,7 @@ func (c Client) RunShellScriptWithEnv(cmdScript string) ([]byte, error) {
if err := c.FileCopy(path, remotePath, true); err != nil {
return nil, err
}
return c.RunShellWithEnv(fmt.Sprintf("sh %s", remotePath))
return c.RunShellWithEnv(fmt.Sprintf("cd %s && sh %s", dir, remotePath))
}

func (c Client) RunShell(cmd string) ([]byte, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/instance_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ func (ic *InstanceClient) Close() error {

// TODO chown data dir to ssh user or 'aem' user (create him maybe)
func (ic *InstanceClient) prepareDataDir() error {
/* TODO to avoid re-uploading library files (probably temporary)
if _, err := ic.cl.RunShell(fmt.Sprintf("rm -fr %s", ic.DataDir())); err != nil {
return fmt.Errorf("cannot clean up AEM data directory: %w", err)
}
*/
if _, err := ic.cl.RunShell(fmt.Sprintf("mkdir -p %s", ic.DataDir())); err != nil {
return fmt.Errorf("cannot create AEM data directory: %w", err)
}
Expand Down Expand Up @@ -142,10 +140,12 @@ func (ic *InstanceClient) ReadStatus() (InstanceStatus, error) {
return status, nil
}

// TODO when create fails this could be run twice; how about protecting it with lock?
func (ic *InstanceClient) bootstrap() error {
return ic.runHook("bootstrap", ic.data.Hook.Bootstrap.ValueString())
}

// TODO when create fails this could be run twice; how about protecting it with lock?
func (ic *InstanceClient) initialize() error {
return ic.runHook("initialize", ic.data.Hook.Initialize.ValueString())
}
Expand All @@ -161,7 +161,7 @@ func (ic *InstanceClient) runHook(name string, cmdScript string) error {

tflog.Info(ic.ctx, fmt.Sprintf("Hook '%s' started", name))

textOut, err := ic.cl.RunShellScriptWithEnv(cmdScript)
textOut, err := ic.cl.RunShellScriptWithEnv(ic.DataDir(), cmdScript)
if err != nil {
return fmt.Errorf("unable to run hook '%s' properly: %w", name, err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ func (r *InstanceResource) createOrUpdate(ctx context.Context, plan *tfsdk.Plan,
}
}(ic)

if err := ic.prepareDataDir(); err != nil {
diags.AddError("Unable to prepare AEM data directory", fmt.Sprintf("%s", err))
return
}
if create {
if err := ic.bootstrap(); err != nil {
diags.AddError("Unable to bootstrap AEM machine", fmt.Sprintf("%s", err))
return
}
}
if err := ic.prepareDataDir(); err != nil {
diags.AddError("Unable to prepare AEM data directory", fmt.Sprintf("%s", err))
return
}
if err := ic.installComposeWrapper(); err != nil {
diags.AddError("Unable to install AEM Compose CLI", fmt.Sprintf("%s", err))
return
Expand Down

0 comments on commit c9f63e9

Please sign in to comment.