init-ocb.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. #
  3. set -e # Exit immediately if any command exits with a non-zero status
  4. check_venv() { # Check if the venv module is available
  5. if python3 -c "import venv" &> /dev/null; then
  6. return 0
  7. else
  8. echo "The 'venv' module is not available. Please ensure you are using Python 3.3 or later."
  9. return 1
  10. fi
  11. }
  12. create_venv() {
  13. python3 -m venv env
  14. }
  15. activate_venv() {
  16. source env/bin/activate
  17. }
  18. install_dependencies() {
  19. pip install --upgrade pip || { echo "Failed to upgrade pip"; exit 1; }
  20. pip install gunicorn || { echo "Failed to install Gunicorn"; exit 1; }
  21. pip install -e . || { echo "Failed to install dependencies"; exit 1; }
  22. }
  23. initialize_db() { # Create the database. This also populates it with some goals and votes from the default rewards.py
  24. export FLASK_APP=ownchatbot
  25. if python -m flask init-db; then
  26. echo "Database initialized successfully."
  27. else
  28. echo "Failed to initialize the database. Please check for errors."
  29. exit 1 # Exit the script with a non-zero status
  30. fi
  31. }
  32. generate_key() {
  33. KEY=$(< /dev/urandom tr -dc 'A-Za-z0-9' | head -c 32)
  34. echo "$KEY"
  35. }
  36. update_config() { # Generate keys for SECRET_KEY and MGMT_AUTH
  37. local key_name="$1"
  38. local key_value="$2"
  39. local bak_file="instance/config.py.bak"
  40. # Update the config file
  41. if sed -i.bak "s|$key_name = ''|$key_name = '$key_value'|" "instance/config.py"; then
  42. echo "Updated $key_name successfully."
  43. rm "$bak_file" # Remove the .bak file if the update was successful
  44. else
  45. echo "Failed to update $key_name. Backup file remains."
  46. exit 1 # Exit the script with a non-zero status
  47. fi
  48. }
  49. if check_venv; then
  50. create_venv
  51. else
  52. exit 1
  53. fi
  54. activate_venv
  55. install_dependencies
  56. initialize_db
  57. deactivate
  58. cp ownchatbot/defaults/*py instance/ # Copy the default config files into the instance folder
  59. SECRET_KEY=$(generate_key)
  60. update_config "SECRET_KEY" "$SECRET_KEY"
  61. MGMT_AUTH=$(generate_key)
  62. update_config "MGMT_AUTH" "$MGMT_AUTH"
  63. read -p "Please enter the port number you would like OwnchatBot to listen on: " OCB_PORT
  64. echo "
  65. You're ready to start OwnchatBot! Run:
  66. env/bin/python -m gunicorn --error-logfile ownchatbot.log -b 0.0.0.0:$OCB_PORT -w 1 'ownchatbot:create_app()'
  67. To configure your bot, go to:
  68. http://localhost:$OCB_PORT/mgmt?auth=$MGMT_AUTH
  69. "