diff --git a/deployment/terraform/agent.go b/deployment/terraform/agent.go index ecbd58b90..1484ad3ea 100644 --- a/deployment/terraform/agent.go +++ b/deployment/terraform/agent.go @@ -63,7 +63,10 @@ func (t *Terraform) configureAndRunAgents(extAgent *ssh.ExtAgent) error { } commands := []string{ - "rm -rf mattermost-load-test-ng*", + // Stop and remove are joined with ; so the rm still runs on the first deploy when the services don't exist yet. + // The services must be stopped before removing the directory to avoid a race condition with ltbrowserapi + // writing to browser/node_modules, which causes rm to fail with "Directory not empty". + "sudo systemctl stop ltapi ltbrowserapi 2>/dev/null; sudo rm -rf mattermost-load-test-ng*", "tar xzf tmp.tar.gz", "mv mattermost-load-test-ng* mattermost-load-test-ng", "rm tmp.tar.gz", diff --git a/deployment/terraform/assets/user_data.sh.tpl b/deployment/terraform/assets/user_data.sh.tpl index 627c2ed55..99e5e6065 100644 --- a/deployment/terraform/assets/user_data.sh.tpl +++ b/deployment/terraform/assets/user_data.sh.tpl @@ -12,14 +12,11 @@ PROVEOF chmod +x /tmp/provisioner.sh # Cloud-init runs user_data as root with a minimal environment. -# Set HOME to the AMI user's home directory since provisioner scripts -# (e.g. nvm) install to $HOME and other code expects files there. -export HOME="/home/${ami_user}" -export USER="${ami_user}" - +# Use runuser -l to execute the provisioner as the AMI user with a full +# login shell so that tools like nvm (sourced via ~/.bashrc) are available. cd /tmp rc=0 -/tmp/provisioner.sh || rc=$? +runuser -l "${ami_user}" -c /tmp/provisioner.sh || rc=$? if [ $rc -eq 0 ]; then touch /var/lib/cloud/instance/provisioning-done else