Skip to content

Commit

Permalink
Fix RPM/DEB clean install (#816) (#839)
Browse files Browse the repository at this point in the history
* Fix RPM/DEB clean install

* 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.

* Update check for symlink existance for the cases where the symlink points to non-existent file

(cherry picked from commit 12c7d15)

Co-authored-by: Aleksandr Maus <[email protected]>
  • Loading branch information
mergify[bot] and aleksmaus authored Aug 3, 2022
1 parent be10686 commit a515191
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions dev-tools/packaging/templates/linux/postinstall.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
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
# 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

commit_hash="{{ commit_short }}"

yml_path="$old_agent_dir/state.yml"
enc_path="$old_agent_dir/state.enc"
new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$commit_hash"

new_agent_dir="$( dirname "$old_agent_dir")/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" ] && ! [ "$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"

if test -f "$yml_path"; then
Expand All @@ -24,15 +34,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 -L "$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

0 comments on commit a515191

Please sign in to comment.