From f31595c13101a56578482198f4b6fe50c2e0cb13 Mon Sep 17 00:00:00 2001 From: Aleksandr Maus Date: Fri, 29 Jul 2022 11:30:10 -0400 Subject: [PATCH 1/3] Fix RPM/DEB clean install --- .../templates/linux/postinstall.sh.tmpl | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl index 083ebb91060..a115aa48c3d 100644 --- a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl +++ b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl @@ -1,18 +1,27 @@ #!/usr/bin/env bash -set -e - symlink="/usr/share/elastic-agent/bin/elastic-agent" -old_agent_dir="$( dirname "$(readlink -f -- "$symlink")" )" +old_agent_dir="" + +# check if $symlink exists for the previous install +if test -f "$symlink"; then + resolved_symlink="$(readlink -f -e -- "$symlink")" + # check if it is resolved to non empty string + if ! [ -z "$resolved_symlink" ]; then + old_agent_dir="$( dirname "$resolved_symlink" )" + fi +fi -commit_hash="{{ commit_short }}" +set -e -yml_path="$old_agent_dir/state.yml" -enc_path="$old_agent_dir/state.enc" +commit_hash="{{ commit_short }}" -new_agent_dir="$( dirname "$old_agent_dir")/elastic-agent-$commit_hash" +new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$commit_hash" -if ! [[ "$old_agent_dir" -ef "$new_agent_dir" ]]; then +# copy the state files if there was a previous agent install +if ! [ -z "$old_agent_dir" ]; then + yml_path="$old_agent_dir/state.yml" + enc_path="$old_agent_dir/state.enc" echo "migrate state from $old_agent_dir to $new_agent_dir" if test -f "$yml_path"; then @@ -24,15 +33,17 @@ if ! [[ "$old_agent_dir" -ef "$new_agent_dir" ]]; then echo "found "$enc_path", copy to "$new_agent_dir"." cp "$enc_path" "$new_agent_dir" fi +fi - if test -f "$symlink"; then - echo "found symlink $symlink, unlink" - unlink "$symlink" - fi - - echo "create symlink "$symlink" to "$new_agent_dir/elastic-agent"" - ln -s "$new_agent_dir/elastic-agent" "$symlink" +# delete symlink if exists +if test -f "$symlink"; then + echo "found symlink $symlink, unlink" + unlink "$symlink" fi +# create symlink to the new agent +echo "create symlink "$symlink" to "$new_agent_dir/elastic-agent"" +ln -s "$new_agent_dir/elastic-agent" "$symlink" + systemctl daemon-reload 2> /dev/null exit 0 From a1c04e54f66c875ac9ac8550ce9f6a058878af04 Mon Sep 17 00:00:00 2001 From: Aleksandr Maus Date: Sun, 31 Jul 2022 22:55:19 -0400 Subject: [PATCH 2/3] Improve the post install script * Do not try to copy the state files if the agent directory is the same, this causes the error. * Check the existance of symlink instead of the file it is pointing to for the state file migration. --- .../packaging/templates/linux/postinstall.sh.tmpl | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl index a115aa48c3d..8852a655a63 100644 --- a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl +++ b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl @@ -1,25 +1,26 @@ #!/usr/bin/env bash +set -e + symlink="/usr/share/elastic-agent/bin/elastic-agent" old_agent_dir="" # check if $symlink exists for the previous install -if test -f "$symlink"; then - resolved_symlink="$(readlink -f -e -- "$symlink")" +# and derive the old agent directory +if test -L "$symlink"; then + resolved_symlink="$(readlink -f -- "$symlink")" # check if it is resolved to non empty string if ! [ -z "$resolved_symlink" ]; then old_agent_dir="$( dirname "$resolved_symlink" )" fi fi -set -e - commit_hash="{{ commit_short }}" new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$commit_hash" # copy the state files if there was a previous agent install -if ! [ -z "$old_agent_dir" ]; then +if ! [ -z "$old_agent_dir" ] && ! [ "$old_agent_dir" -ef "$new_agent_dir" ]; then yml_path="$old_agent_dir/state.yml" enc_path="$old_agent_dir/state.enc" echo "migrate state from $old_agent_dir to $new_agent_dir" From 924da80d4fbbce298813d4ff101e82a0ecb82317 Mon Sep 17 00:00:00 2001 From: Aleksandr Maus Date: Mon, 1 Aug 2022 13:53:52 -0400 Subject: [PATCH 3/3] Update check for symlink existance for the cases where the symlink points to non-existent file --- dev-tools/packaging/templates/linux/postinstall.sh.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl index 8852a655a63..d96f21a8629 100644 --- a/dev-tools/packaging/templates/linux/postinstall.sh.tmpl +++ b/dev-tools/packaging/templates/linux/postinstall.sh.tmpl @@ -37,7 +37,7 @@ if ! [ -z "$old_agent_dir" ] && ! [ "$old_agent_dir" -ef "$new_agent_dir" ]; the fi # delete symlink if exists -if test -f "$symlink"; then +if test -L "$symlink"; then echo "found symlink $symlink, unlink" unlink "$symlink" fi