install.sh 7.5 KB

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