diff --git a/salt/elasticfleet/tools/sbin/so-elastic-agent-install b/salt/elasticfleet/tools/sbin/so-elastic-agent-install index fd6bceeff..920b17137 100644 --- a/salt/elasticfleet/tools/sbin/so-elastic-agent-install +++ b/salt/elasticfleet/tools/sbin/so-elastic-agent-install @@ -27,9 +27,13 @@ check_agent_health() { return 1 } -if command -v elastic-agent >/dev/null 2>&1; then - elastic-agent uninstall -f -fi +uninstall_agent() { + if command -v elastic-agent >/dev/null 2>&1; then + elastic-agent uninstall -f + fi +} + +uninstall_agent if [[ -z "$GRIDNODETOKEN" ]]; then noderole=$(so-yaml.py get -r /etc/salt/grains role) @@ -40,6 +44,12 @@ if [[ -z "$GRIDNODETOKEN" ]]; then fi fi +if [[ -z "$GRIDNODETOKEN" ]]; then + echo "Unable to determine Elastic Fleet enrollment token. Exiting." + + exit 1 +fi + if [[ ! -x /opt/so/so-elastic-agent_linux_amd64 ]]; then echo "Downloading so-elastic-agent installer... This could take a while if another Salt job is running." salt-call state.sls_id pull_agent_installer elasticfleet.install_agent_grid queue=True @@ -50,7 +60,7 @@ if [[ -x /opt/so/so-elastic-agent_linux_amd64 ]]; then cd /opt/so/ || exit 1 while [[ $attempts -lt 3 ]]; do - if ./so-elastic-agent_linux_amd64 -token="$GRIDNODETOKEN" -force; echo "Verifying Elastic Agent health..." && check_agent_health; then + if ./so-elastic-agent_linux_amd64 -token="$GRIDNODETOKEN" -force && echo "Verifying Elastic Agent health..." && check_agent_health; then rm -f /opt/so/so-elastic-agent_linux_amd64 elastic-agent status @@ -65,6 +75,8 @@ if [[ -x /opt/so/so-elastic-agent_linux_amd64 ]]; then fi done + uninstall_agent + rm -f /opt/so/so-elastic-agent_linux_amd64 echo "The so-elastic-agent installer failed after 3 attempts. Exiting." exit 1