install-hooks.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # Script to install SlackPkg hooks for snap-slack
  3. # Configuration
  4. SLACKPKG_HOOKS_DIR="/etc/slackpkg/hooks"
  5. PRE_INSTALL_HOOK="$SLACKPKG_HOOKS_DIR/pre-install.sh"
  6. POST_INSTALL_HOOK="$SLACKPKG_HOOKS_DIR/post-install.sh"
  7. # Check if running as root
  8. if [ "$(id -u)" != "0" ]; then
  9. echo "Error: This script must be run as root"
  10. exit 1
  11. fi
  12. # Create hooks directory if it doesn't exist
  13. if [ ! -d "$SLACKPKG_HOOKS_DIR" ]; then
  14. echo "Creating hooks directory: $SLACKPKG_HOOKS_DIR"
  15. mkdir -p "$SLACKPKG_HOOKS_DIR"
  16. fi
  17. # Install pre-install hook
  18. echo "Installing pre-install hook..."
  19. cat > "$PRE_INSTALL_HOOK" << 'EOF'
  20. #!/bin/bash
  21. # SlackPkg Pre-install Hook for snap-slack
  22. # This file was automatically installed by snap-slack
  23. # Configuration
  24. SNAP_SLACK=/usr/bin/snap-slack
  25. ENABLE_AUTO_SNAPSHOTS=1 # Set to 0 to disable automatic snapshots
  26. MAX_PACKAGE_COUNT=100 # Maximum number of packages to include in snapshot name
  27. # Check if snap-slack is installed
  28. if [ ! -x "$SNAP_SLACK" ]; then
  29. echo "WARNING: snap-slack not found at $SNAP_SLACK, skipping pre-install snapshot"
  30. exit 0
  31. fi
  32. # Check if auto snapshots are enabled
  33. if [ "$ENABLE_AUTO_SNAPSHOTS" != "1" ]; then
  34. echo "INFO: Automatic snapshots are disabled in slackpkg hook"
  35. exit 0
  36. fi
  37. # Function to create a snapshot
  38. create_snapshot() {
  39. local desc="$1"
  40. echo "Creating snapshot before package operations: $desc"
  41. $SNAP_SLACK create --description "$desc"
  42. }
  43. # Get the operation being performed
  44. OPERATION="$1"
  45. shift
  46. PACKAGES="$@"
  47. # Create appropriate snapshot based on operation
  48. case "$OPERATION" in
  49. upgrade-all)
  50. create_snapshot "pre-upgrade-all"
  51. ;;
  52. install|upgrade)
  53. # Limit the number of packages in the snapshot name for readability
  54. if [ "$(echo $PACKAGES | wc -w)" -gt $MAX_PACKAGE_COUNT ]; then
  55. PKG_COUNT=$(echo $PACKAGES | wc -w)
  56. create_snapshot "pre-$OPERATION-$PKG_COUNT-packages"
  57. else
  58. create_snapshot "pre-$OPERATION-$(echo $PACKAGES | tr ' ' '-')"
  59. fi
  60. ;;
  61. remove)
  62. if [ "$(echo $PACKAGES | wc -w)" -gt $MAX_PACKAGE_COUNT ]; then
  63. PKG_COUNT=$(echo $PACKAGES | wc -w)
  64. create_snapshot "pre-remove-$PKG_COUNT-packages"
  65. else
  66. create_snapshot "pre-remove-$(echo $PACKAGES | tr ' ' '-')"
  67. fi
  68. ;;
  69. *)
  70. # For other operations, create a generic snapshot
  71. create_snapshot "pre-slackpkg-operation"
  72. ;;
  73. esac
  74. exit 0
  75. EOF
  76. # Make pre-install hook executable
  77. chmod +x "$PRE_INSTALL_HOOK"
  78. # Install post-install hook
  79. echo "Installing post-install hook..."
  80. cat > "$POST_INSTALL_HOOK" << 'EOF'
  81. #!/bin/bash
  82. # SlackPkg Post-install Hook for snap-slack
  83. # This file was automatically installed by snap-slack
  84. # Configuration
  85. SNAP_SLACK=/usr/bin/snap-slack
  86. AUTO_CLEANUP=1 # Set to 0 to disable automatic snapshot cleanup
  87. # Check if snap-slack is installed
  88. if [ ! -x "$SNAP_SLACK" ]; then
  89. echo "WARNING: snap-slack not found at $SNAP_SLACK, skipping post-install operations"
  90. exit 0
  91. fi
  92. # Check if auto cleanup is enabled
  93. if [ "$AUTO_CLEANUP" = "1" ]; then
  94. echo "Running snapshot management to clean up old snapshots"
  95. $SNAP_SLACK manage
  96. fi
  97. # Log the successful completion
  98. echo "Package operation completed successfully."
  99. echo "If you encounter issues, you can rollback using:"
  100. echo " snap-slack list # to see available snapshots"
  101. echo " snap-slack adopt --snapshot <snapshot-name> # to rollback"
  102. exit 0
  103. EOF
  104. # Make post-install hook executable
  105. chmod +x "$POST_INSTALL_HOOK"
  106. echo "Hooks installed successfully."
  107. echo "The hooks will create snapshots before package operations and clean up old snapshots afterward."
  108. echo "To disable automatic snapshots, edit $PRE_INSTALL_HOOK and set ENABLE_AUTO_SNAPSHOTS=0"
  109. echo "To disable automatic cleanup, edit $POST_INSTALL_HOOK and set AUTO_CLEANUP=0"
  110. exit 0