install.sh 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/bin/bash
  2. # Emergency Access Server Installation Script
  3. # Run as root or with sudo
  4. set -euo pipefail
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. NC='\033[0m' # No Color
  10. # Configuration
  11. SERVICE_USER="emergency-access"
  12. SERVICE_GROUP="emergency-access"
  13. INSTALL_DIR="/opt/emergency-access"
  14. CONFIG_DIR="/etc/emergency-access"
  15. LOG_FILE="/var/log/emergency-access.log"
  16. SERVICE_FILE="/etc/systemd/system/emergency-access.service"
  17. print_status() {
  18. echo -e "${GREEN}[INFO]${NC} $1"
  19. }
  20. print_warning() {
  21. echo -e "${YELLOW}[WARNING]${NC} $1"
  22. }
  23. print_error() {
  24. echo -e "${RED}[ERROR]${NC} $1"
  25. }
  26. check_root() {
  27. if [[ $EUID -ne 0 ]]; then
  28. print_error "This script must be run as root"
  29. exit 1
  30. fi
  31. }
  32. install_dependencies() {
  33. print_status "Installing system dependencies..."
  34. # Detect package manager
  35. if command -v apt-get &> /dev/null; then
  36. apt-get update
  37. apt-get install -y python3 python3-pip python3-venv
  38. elif command -v yum &> /dev/null; then
  39. yum install -y python3 python3-pip python3-venv
  40. elif command -v dnf &> /dev/null; then
  41. dnf install -y python3 python3-pip python3-venv
  42. elif command -v pacman &> /dev/null; then
  43. pacman -S --noconfirm python python-pip python-virtualenv
  44. else
  45. print_error "Unsupported package manager. Please install Python 3, pip, and venv manually."
  46. exit 1
  47. fi
  48. }
  49. create_user() {
  50. print_status "Creating service user and group..."
  51. if ! getent group "$SERVICE_GROUP" > /dev/null 2>&1; then
  52. groupadd --system "$SERVICE_GROUP"
  53. print_status "Created group: $SERVICE_GROUP"
  54. else
  55. print_warning "Group $SERVICE_GROUP already exists"
  56. fi
  57. if ! getent passwd "$SERVICE_USER" > /dev/null 2>&1; then
  58. useradd --system --gid "$SERVICE_GROUP" --home-dir "$INSTALL_DIR" \
  59. --shell /bin/false --comment "Emergency Access Service" "$SERVICE_USER"
  60. print_status "Created user: $SERVICE_USER"
  61. else
  62. print_warning "User $SERVICE_USER already exists"
  63. fi
  64. }
  65. setup_directories() {
  66. print_status "Setting up directories..."
  67. # Create installation directory
  68. mkdir -p "$INSTALL_DIR"
  69. mkdir -p "$CONFIG_DIR"
  70. # Set ownership
  71. chown "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
  72. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR"
  73. # Set permissions
  74. chmod 755 "$INSTALL_DIR"
  75. chmod 750 "$CONFIG_DIR" # More restrictive for config
  76. print_status "Created directories with proper permissions"
  77. }
  78. install_application() {
  79. print_status "Installing application files..."
  80. # Copy application files
  81. cp main.py "$INSTALL_DIR/"
  82. cp config.py "$INSTALL_DIR/"
  83. cp requirements.txt "$INSTALL_DIR/"
  84. # Copy example config if config doesn't exist
  85. if [[ ! -f "$CONFIG_DIR/config.json" ]]; then
  86. cp config.json "$CONFIG_DIR/"
  87. print_status "Copied example configuration to $CONFIG_DIR/config.json"
  88. print_warning "Please edit $CONFIG_DIR/config.json with your backend names"
  89. else
  90. print_warning "Configuration file already exists, skipping copy"
  91. fi
  92. # Set permissions
  93. chown -R "$SERVICE_USER:$SERVICE_GROUP" "$INSTALL_DIR"
  94. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/config.json"
  95. chmod 644 "$INSTALL_DIR"/*.py
  96. chmod 644 "$INSTALL_DIR/requirements.txt"
  97. chmod 640 "$CONFIG_DIR/config.json" # Restrictive permissions for config
  98. }
  99. setup_python_environment() {
  100. print_status "Setting up Python virtual environment..."
  101. # Create virtual environment
  102. sudo -u "$SERVICE_USER" python3 -m venv "$INSTALL_DIR/venv"
  103. # Install dependencies
  104. sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install --upgrade pip
  105. sudo -u "$SERVICE_USER" "$INSTALL_DIR/venv/bin/pip" install -r "$INSTALL_DIR/requirements.txt"
  106. print_status "Python environment setup complete"
  107. }
  108. setup_logging() {
  109. print_status "Setting up logging..."
  110. # Create log file
  111. touch "$LOG_FILE"
  112. chown "$SERVICE_USER:$SERVICE_GROUP" "$LOG_FILE"
  113. chmod 644 "$LOG_FILE"
  114. # Setup log rotation
  115. cat > /etc/logrotate.d/emergency-access << EOF
  116. $LOG_FILE {
  117. daily
  118. rotate 30
  119. compress
  120. delaycompress
  121. missingok
  122. notifempty
  123. create 644 $SERVICE_USER $SERVICE_GROUP
  124. postrotate
  125. systemctl reload emergency-access.service > /dev/null 2>&1 || true
  126. endscript
  127. }
  128. EOF
  129. print_status "Logging configuration complete"
  130. }
  131. install_systemd_service() {
  132. print_status "Installing systemd service..."
  133. # Copy service file
  134. cp emergency-access.service "$SERVICE_FILE"
  135. # Reload systemd
  136. systemctl daemon-reload
  137. print_status "Systemd service installed"
  138. }
  139. create_example_files() {
  140. print_status "Creating example key and dummy files..."
  141. # Create example key file
  142. if [[ ! -f "$CONFIG_DIR/key-part.txt" ]]; then
  143. echo "EXAMPLE_KEY_PART_$(openssl rand -hex 16)" > "$CONFIG_DIR/key-part.txt"
  144. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/key-part.txt"
  145. chmod 600 "$CONFIG_DIR/key-part.txt"
  146. print_status "Created example key file: $CONFIG_DIR/key-part.txt"
  147. print_warning "Replace this with your actual key part!"
  148. fi
  149. # Create dummy file
  150. if [[ ! -f "$CONFIG_DIR/dummy.txt" ]]; then
  151. echo "system_healthy_$(date +%s)" > "$CONFIG_DIR/dummy.txt"
  152. chown "$SERVICE_USER:$SERVICE_GROUP" "$CONFIG_DIR/dummy.txt"
  153. chmod 644 "$CONFIG_DIR/dummy.txt"
  154. print_status "Created dummy file: $CONFIG_DIR/dummy.txt"
  155. fi
  156. }
  157. print_final_instructions() {
  158. print_status "Installation complete!"
  159. echo
  160. print_warning "IMPORTANT: Before starting the service:"
  161. echo "1. Edit $CONFIG_DIR/config.json with your backend names from global ntfy config"
  162. echo "2. Replace $CONFIG_DIR/key-part.txt with your actual key part"
  163. echo "3. Ensure your global ntfy configuration (~/.ntfy.yml) has the required backends"
  164. echo "4. Test the configuration"
  165. echo
  166. print_status "Service management commands:"
  167. echo " Start service: sudo systemctl start emergency-access"
  168. echo " Enable at boot: sudo systemctl enable emergency-access"
  169. echo " Check status: sudo systemctl status emergency-access"
  170. echo " View logs: sudo journalctl -u emergency-access -f"
  171. echo " View log file: sudo tail -f $LOG_FILE"
  172. echo
  173. print_status "Configuration files:"
  174. echo " Service config: $CONFIG_DIR/config.json"
  175. echo " Key file: $CONFIG_DIR/key-part.txt"
  176. echo " Dummy file: $CONFIG_DIR/dummy.txt"
  177. echo " Log file: $LOG_FILE"
  178. echo
  179. print_warning "Security note: This server provides access to sensitive key material."
  180. print_warning "Ensure proper network security and monitoring are in place."
  181. }
  182. main() {
  183. print_status "Starting Emergency Access Server installation..."
  184. check_root
  185. install_dependencies
  186. create_user
  187. setup_directories
  188. install_application
  189. setup_python_environment
  190. setup_logging
  191. install_systemd_service
  192. create_example_files
  193. print_final_instructions
  194. print_status "Installation completed successfully!"
  195. }
  196. # Run main function
  197. main "$@"