Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.4](backport #816) Fix RPM/DEB clean install #839

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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