Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Jan 30, 2026
1 parent f2db57e commit 5915306
Showing 1 changed file with 27 additions and 33 deletions.
60 changes: 27 additions & 33 deletions local-app/python-tools/build-ansible-distribution/create-ansible.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ else
fi

PYTHON_VERSION="3.13" # Can be changed to 3.12 or latest
MAKE_RELOCATABLE=0 # Set to 1 to patch shebangs for any-path portability

# 0. export environment variables needed
export SSL_CERT_FILE=/etc/pki/tls/certs/ca-bundle.crt
Expand Down Expand Up @@ -64,38 +65,31 @@ source .venv/bin/activate
# 5. Sync the environment
uv sync --link-mode copy

# 6. Create a portable wrapper for Ansible
# This avoids shebang issues if the folder is moved
mkdir -p "$APP_DIR/bin"
cat <<EOF > "$APP_DIR/bin/ansible"
#!/bin/bash
export VIRTUAL_ENV="$APP_DIR/.venv"
export PATH="\$VIRTUAL_ENV/bin:\$PATH"
unset PYTHONHOME
exec "\$VIRTUAL_ENV/bin/python" -m ansible "\$@"
EOF
cat <<EOF > "$APP_DIR/bin/ansible-playbook"
#!/bin/bash
export VIRTUAL_ENV="$APP_DIR/.venv"
export PATH="\$VIRTUAL_ENV/bin:\$PATH"
unset PYTHONHOME
exec "\$VIRTUAL_ENV/bin/python" -m ansible-playbook "\$@"
EOF
# 6. Ensure the Python binary itself is a physical copy
# This severs the link to ~/.local/share/uv/python/...
REAL_PYTHON=$(readlink -f .venv/bin/python)
if [ -L ".venv/bin/python" ]; then
echo "Converting Python symlink to physical binary..."
rm .venv/bin/python
cp "$REAL_PYTHON" .venv/bin/python
fi

chmod +x "$APP_DIR/bin/ansible"
chmod +x "$APP_DIR/bin/ansible-playbook"

# 7. Verification
echo "--- Verification ---"
echo "Project Path: $APP_DIR"
echo "Python Path: $(readlink -f .venv/bin/python)"
if [[ $(ls -l .venv/bin/python) == *"->"* ]]; then
echo "Warning: Python is still a symlink. Attempting hard copy..."
# Manual intervention for absolute isolation:
# Replace the symlink with the actual binary
REAL_PYTHON=$(readlink -f .venv/bin/python)
rm .venv/bin/python
cp "$REAL_PYTHON" .venv/bin/python
echo "Python binary physically copied."
# 7. Optional: Make Relocatable
if [ "$MAKE_RELOCATABLE" -eq 1 ]; then
echo "MAKE_RELOCATABLE is active. Patching shebangs..."
# Replaces absolute path shebangs with /usr/bin/env python
# This allows the binaries to work regardless of the parent directory path
find .venv/bin -maxdepth 1 -type f -executable -exec \
sed -i '1s|#!.*python|#!/usr/bin/env python|' {} +
else
echo "Skipping relocation patch. Scripts will use absolute paths: $APP_DIR"
fi
echo "--------------------"

# 8. Link bin to .venv/bin for easy access to all tools
ln -snf .venv/bin bin

echo "------------------------------------------------"
echo "Ansible distribution ready at $APP_DIR"
echo "Relocatable: $([ "$MAKE_RELOCATABLE" -eq 1 ] && echo "YES" || echo "NO")"
echo "Try: $APP_DIR/bin/ansible --version"
echo "------------------------------------------------"

0 comments on commit 5915306

Please sign in to comment.