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

Fix debian packaging for upgrades #5260

Merged
merged 14 commits into from
Aug 13, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: bug-fix

# Change summary; a 80ish characters long description of the change.
summary: Fix loss of state.enc on upgrade with debian packaging

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component:

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/5260

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
issue: https://github.com/elastic/elastic-agent/issues/5101
1 change: 1 addition & 0 deletions dev-tools/packaging/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ shared:
# Deb/RPM spec for community beats.
- &deb_rpm_agent_spec
<<: *common
pre_install_script: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/preinstall.sh.tmpl'
post_install_script: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/postinstall.sh.tmpl'
post_rm_script: '{{ elastic_beats_dir }}/dev-tools/packaging/templates/linux/postrm.sh.tmpl'
files:
Expand Down
38 changes: 6 additions & 32 deletions dev-tools/packaging/templates/linux/postinstall.sh.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,12 @@

set -e

symlink="/usr/share/elastic-agent/bin/elastic-agent"
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 }}"
version_dir="{{agent_package_version}}{{snapshot_suffix}}"

symlink="/usr/share/elastic-agent/bin/elastic-agent"
new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$version_dir-$commit_hash"

# 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
echo "found "$yml_path", copy to "$new_agent_dir"."
cp "$yml_path" "$new_agent_dir"
fi

if test -f "$enc_path"; then
echo "found "$enc_path", copy to "$new_agent_dir"."
cp "$enc_path" "$new_agent_dir"
fi
fi

# delete symlink if exists
# delete $symlink if exists
if test -L "$symlink"; then
echo "found symlink $symlink, unlink"
unlink "$symlink"
Expand All @@ -47,5 +17,9 @@ fi
echo "create symlink "$symlink" to "$new_agent_dir/elastic-agent""
ln -s "$new_agent_dir/elastic-agent" "$symlink"

# reload systemctl and then restart service
echo "systemd enable/restart elastic-agent"
systemctl daemon-reload 2> /dev/null
systemctl enable elatic-agent 2> /dev/null || true
systemctl restart elastic-agent 2> /dev/null || true
exit 0
46 changes: 46 additions & 0 deletions dev-tools/packaging/templates/linux/preinstall.sh.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

set -e

commit_hash="{{ commit_short }}"
version_dir="{{agent_package_version}}{{snapshot_suffix}}"
symlink="/usr/share/elastic-agent/bin/elastic-agent"
new_agent_dir="/var/lib/elastic-agent/data/elastic-agent-$version_dir-$commit_hash"
old_agent_dir=""

# upon upgrade we migrate the current symlink to an upgrade symlink as the previous
# installed version will remove the symlink
if test -L "$symlink"; then
resolved_symlink="$(readlink -- "$symlink")"
if ! [ -z "$resolved_symlink" ]; then
old_agent_dir="$( dirname "$resolved_symlink" )"
echo "previous installation directory $old_agent_dir"
else
echo "unable to read existing symlink"
fi

# 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
echo "found "$yml_path", copy to "$new_agent_dir"."
mkdir -p "$new_agent_dir"
cp "$yml_path" "$new_agent_dir"
else
echo "didn't find $yml_path"
fi

if test -f "$enc_path"; then
echo "found "$enc_path", copy to "$new_agent_dir"."
mkdir -p "$new_agent_dir"
cp "$enc_path" "$new_agent_dir"
else
echo "didn't find $enc_path"
fi
fi
else
echo "no previous installation found"
fi