| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #!/bin/bash
- # Script to install SlackPkg hooks for snap-slack
- # Configuration
- SLACKPKG_HOOKS_DIR="/etc/slackpkg/hooks"
- PRE_INSTALL_HOOK="$SLACKPKG_HOOKS_DIR/pre-install.sh"
- POST_INSTALL_HOOK="$SLACKPKG_HOOKS_DIR/post-install.sh"
- # Check if running as root
- if [ "$(id -u)" != "0" ]; then
- echo "Error: This script must be run as root"
- exit 1
- fi
- # Create hooks directory if it doesn't exist
- if [ ! -d "$SLACKPKG_HOOKS_DIR" ]; then
- echo "Creating hooks directory: $SLACKPKG_HOOKS_DIR"
- mkdir -p "$SLACKPKG_HOOKS_DIR"
- fi
- # Install pre-install hook
- echo "Installing pre-install hook..."
- cat > "$PRE_INSTALL_HOOK" << 'EOF'
- #!/bin/bash
- # SlackPkg Pre-install Hook for snap-slack
- # This file was automatically installed by snap-slack
- # Configuration
- SNAP_SLACK=/usr/bin/snap-slack
- ENABLE_AUTO_SNAPSHOTS=1 # Set to 0 to disable automatic snapshots
- MAX_PACKAGE_COUNT=100 # Maximum number of packages to include in snapshot name
- # Check if snap-slack is installed
- if [ ! -x "$SNAP_SLACK" ]; then
- echo "WARNING: snap-slack not found at $SNAP_SLACK, skipping pre-install snapshot"
- exit 0
- fi
- # Check if auto snapshots are enabled
- if [ "$ENABLE_AUTO_SNAPSHOTS" != "1" ]; then
- echo "INFO: Automatic snapshots are disabled in slackpkg hook"
- exit 0
- fi
- # Function to create a snapshot
- create_snapshot() {
- local desc="$1"
- echo "Creating snapshot before package operations: $desc"
- $SNAP_SLACK create --description "$desc"
- }
- # Get the operation being performed
- OPERATION="$1"
- shift
- PACKAGES="$@"
- # Create appropriate snapshot based on operation
- case "$OPERATION" in
- upgrade-all)
- create_snapshot "pre-upgrade-all"
- ;;
- install|upgrade)
- # Limit the number of packages in the snapshot name for readability
- if [ "$(echo $PACKAGES | wc -w)" -gt $MAX_PACKAGE_COUNT ]; then
- PKG_COUNT=$(echo $PACKAGES | wc -w)
- create_snapshot "pre-$OPERATION-$PKG_COUNT-packages"
- else
- create_snapshot "pre-$OPERATION-$(echo $PACKAGES | tr ' ' '-')"
- fi
- ;;
- remove)
- if [ "$(echo $PACKAGES | wc -w)" -gt $MAX_PACKAGE_COUNT ]; then
- PKG_COUNT=$(echo $PACKAGES | wc -w)
- create_snapshot "pre-remove-$PKG_COUNT-packages"
- else
- create_snapshot "pre-remove-$(echo $PACKAGES | tr ' ' '-')"
- fi
- ;;
- *)
- # For other operations, create a generic snapshot
- create_snapshot "pre-slackpkg-operation"
- ;;
- esac
- exit 0
- EOF
- # Make pre-install hook executable
- chmod +x "$PRE_INSTALL_HOOK"
- # Install post-install hook
- echo "Installing post-install hook..."
- cat > "$POST_INSTALL_HOOK" << 'EOF'
- #!/bin/bash
- # SlackPkg Post-install Hook for snap-slack
- # This file was automatically installed by snap-slack
- # Configuration
- SNAP_SLACK=/usr/bin/snap-slack
- AUTO_CLEANUP=1 # Set to 0 to disable automatic snapshot cleanup
- # Check if snap-slack is installed
- if [ ! -x "$SNAP_SLACK" ]; then
- echo "WARNING: snap-slack not found at $SNAP_SLACK, skipping post-install operations"
- exit 0
- fi
- # Check if auto cleanup is enabled
- if [ "$AUTO_CLEANUP" = "1" ]; then
- echo "Running snapshot management to clean up old snapshots"
- $SNAP_SLACK manage
- fi
- # Log the successful completion
- echo "Package operation completed successfully."
- echo "If you encounter issues, you can rollback using:"
- echo " snap-slack list # to see available snapshots"
- echo " snap-slack adopt --snapshot <snapshot-name> # to rollback"
- exit 0
- EOF
- # Make post-install hook executable
- chmod +x "$POST_INSTALL_HOOK"
- echo "Hooks installed successfully."
- echo "The hooks will create snapshots before package operations and clean up old snapshots afterward."
- echo "To disable automatic snapshots, edit $PRE_INSTALL_HOOK and set ENABLE_AUTO_SNAPSHOTS=0"
- echo "To disable automatic cleanup, edit $POST_INSTALL_HOOK and set AUTO_CLEANUP=0"
- exit 0
|