Skip to content

Commit

Permalink
Fix K3S_DATA_DIR when running as non-root user
Browse files Browse the repository at this point in the history
When running the generated kill and uninstall scripts, the environment
variable `K3S_DATA_DIR` got lost, because `sudo` by default doesn't
preserve the environment. This is fixed by adding the `--preserve-env`
flag.

Signed-off-by: Reinhard Nägele <[email protected]>
  • Loading branch information
unguiculus committed Nov 27, 2024
1 parent 55cda22 commit a9f711a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,15 @@ get_pr_artifact_url() {
if [ -z "${commit_id}" ]; then
fatal "Installing PR builds requires GITHUB_TOKEN with k3s-io/k3s repo permissions"
fi

# GET request to the GitHub API to retrieve the Build workflow associated with the commit
wf_raw=$(curl -s -H "Authorization: Bearer ${GITHUB_TOKEN}" "${github_api_url}/commits/${commit_id}/check-runs")
build_workflow=$(printf "%s" "${wf_raw}" | jq -r '.check_runs[] | select(.name == "build / Build")')

# Extract the Run ID from the build workflow and lookup artifacts associated with the run
run_id=$(echo "${build_workflow}" | jq -r ' .details_url' | awk -F'/' '{print $(NF-2)}' | sort -rn | head -1)

# Extract the artifact ID for the "k3s" artifact
# Extract the artifact ID for the "k3s" artifact
artifacts=$(curl -s -H "Authorization: Bearer ${GITHUB_TOKEN}" "${github_api_url}/actions/runs/${run_id}/artifacts")
artifacts_url=$(echo "${artifacts}" | jq -r '.artifacts[] | select(.name == "k3s") | .archive_download_url')
GITHUB_PR_URL="${artifacts_url}"
Expand Down Expand Up @@ -632,7 +632,7 @@ setup_selinux() {
install_selinux_rpm ${rpm_site} ${rpm_channel} ${rpm_target} ${rpm_site_infix}

policy_error=fatal
if [ "$INSTALL_K3S_SELINUX_WARN" = true ] || [ "${ID_LIKE:-}" = coreos ] ||
if [ "$INSTALL_K3S_SELINUX_WARN" = true ] || [ "${ID_LIKE:-}" = coreos ] ||
[ "${VARIANT_ID:-}" = coreos ] || [ "${VARIANT_ID:-}" = iot ]; then
policy_error=warn
fi
Expand All @@ -652,7 +652,7 @@ setup_selinux() {
}

install_selinux_rpm() {
if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ] || [ -r /etc/oracle-release ] ||
if [ -r /etc/redhat-release ] || [ -r /etc/centos-release ] || [ -r /etc/oracle-release ] ||
[ -r /etc/fedora-release ] || [ -r /etc/system-release ] || [ "${ID_LIKE%%[ ]*}" = "suse" ]; then
repodir=/etc/yum.repos.d
if [ -d /etc/zypp/repos.d ]; then
Expand Down Expand Up @@ -781,7 +781,7 @@ create_killall() {
info "Creating killall script ${KILLALL_K3S_SH}"
$SUDO tee ${KILLALL_K3S_SH} >/dev/null << \EOF
#!/bin/sh
[ $(id -u) -eq 0 ] || exec sudo $0 $@
[ $(id -u) -eq 0 ] || exec sudo --preserve-env $0 $@
K3S_DATA_DIR=${K3S_DATA_DIR:-/var/lib/rancher/k3s}
Expand Down Expand Up @@ -883,7 +883,7 @@ create_uninstall() {
$SUDO tee ${UNINSTALL_K3S_SH} >/dev/null << EOF
#!/bin/sh
set -x
[ \$(id -u) -eq 0 ] || exec sudo \$0 \$@
[ \$(id -u) -eq 0 ] || exec sudo --preserve-env \$0 \$@
K3S_DATA_DIR=\${K3S_DATA_DIR:-/var/lib/rancher/k3s}
Expand Down
2 changes: 1 addition & 1 deletion install.sh.sha256sum
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f49035378daf603b0dbd458efb4d50608d3499aab3b2e724384dfb5801153669 install.sh
3f41ed73265cee4cb0eb27dd1cdcf0691f5c3e2ccb7a206e2897c88d54293d8c install.sh

0 comments on commit a9f711a

Please sign in to comment.