#79 using .env file for vars and refactoring install.sh

Merged
Alexandra merged 12 commits from blezz-tech/unstable into ShariX_Open/unstable 6 hours ago
9 changed files with 243 additions and 131 deletions
  1. 67 0
      .env.example
  2. 2 2
      .gitignore
  3. 0 22
      bin/functions.sh
  4. 8 44
      bin/install.sh
  5. 34 0
      bin/install_handlers.sh
  6. 17 0
      bin/install_service.sh
  7. 42 0
      bin/utils/functions.sh
  8. 0 63
      core/_settings_vars.py
  9. 73 0
      core/settings_vars.py

+ 67 - 0
.env.example

@@ -0,0 +1,67 @@
+########
+#
+# Django
+#
+########
+
+DEBUG=True
+SECRET_KEY='django-insecure-$eodx2ilnb*djkahfguiwegbwelgbnajl*u$qkt7j-b)7t1iq'
+CSRF_TRUSTED_ORIGINS=
+ALLOWED_HOSTS=127.0.0.1
+INTERNAL_IPS=127.0.0.1
+NAME_SYSTEM=ShariX
+API_URL=http://127.0.0.1:8000
+
+########
+#
+# Database
+#
+########
+
+DB_NAME=
+DB_USER=
+DB_PASSWORD=
+DB_HOST=
+DB_PORT=5432
+
+########
+#
+# Email
+#
+########
+
+EMAIL_HOST=
+EMAIL_PORT=587
+EMAIL_USE_TLS=
+EMAIL_HOST_USER=
+EMAIL_HOST_PASSWORD=
+DEFAULT_FROM_EMAIL=
+
+########
+#
+# Gunicorn
+#
+########
+
+BIND=127.0.0.1:8000
+
+########
+#
+# ejabber
+#
+########
+
+EJ_PROTOCOL=http
+EJ_IP=10.0.20.9
+EJ_PORT=5280
+EJ_SERVICE=chat.ej.sharix-app.org
+EJ_HOST=ej.sharix-app.org
+
+########
+#
+# metaservice
+#
+########
+
+METASERVICE_NAME=open
+METASERVICE_ID=1

+ 2 - 2
.gitignore

@@ -35,5 +35,5 @@ media/
 *.*.swp
 *.*.swp
 .DS_Store
 .DS_Store
 
 
-# Project configuration variables
-core/settings_vars.py
+# env vars
+.env

+ 0 - 22
bin/functions.sh

@@ -1,22 +0,0 @@
-
-# Colors
-
-RED="\e[31m"
-GREEN="\e[32m"
-YELLOW="\e[33m"
-RESET="\e[0m"
-
-
-# Functions
-
-echo_error() {
-    echo -e "${RED}$1${RESET}"
-}
-
-echo_warning() {
-    echo -e "${YELLOW}$1${RESET}"
-}
-
-echo_success() {
-    echo -e "${GREEN}$1${RESET}"
-}

+ 8 - 44
bin/install.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 #!/bin/bash
 
 
-source bin/functions.sh
+source bin/utils/functions.sh
 
 
 if [ -f bin/install.cfg ]; then
 if [ -f bin/install.cfg ]; then
     echo "File install.cfg already exists"
     echo "File install.cfg already exists"
@@ -34,26 +34,6 @@ fi
 
 
 source bin/install.cfg
 source bin/install.cfg
 
 
-# Function to check if a repository exists and perform git pull or git clone
-update_repository() {
-    local repo_url="$1"
-    local repo_dir="$2"
-    local repo_branch="$3"
-
-    if [ -d "$repo_dir" ]; then
-        # If the directory exists, then do a git pull
-        echo_warning "Updating $repo_dir..."
-        cd "$repo_dir" || exit
-        git checkout $repo_branch
-        git pull
-        cd - || exit
-    else
-        # If the directory does not exist, then do a git clone
-        echo_warning "Cloning $repo_url into $repo_dir..."
-        git clone -b "$repo_branch" "$repo_url" "$repo_dir"
-    fi
-}
-
 # Update repositories
 # Update repositories
 update_repository "$TICKETS"            "tickets"            "$TICKETS_BRANCH"
 update_repository "$TICKETS"            "tickets"            "$TICKETS_BRANCH"
 update_repository "$BACKEND"            "dbsynce"            "$BACKEND_BRANCH"
 update_repository "$BACKEND"            "dbsynce"            "$BACKEND_BRANCH"
@@ -72,14 +52,14 @@ source venv/bin/activate
 pip install --upgrade pip
 pip install --upgrade pip
 pip install -r requirements.txt
 pip install -r requirements.txt
 
 
-# Checking for core/settings_vars.py
-if [ -f core/settings_vars.py ]; then
-    echo "File settings_vars.py already exists"
+# Checking for .env
+if [ -f .env ]; then
+    echo "File .env already exists"
 else
 else
-    cp core/_settings_vars.py core/settings_vars.py
-    echo "File settings_vars.py was successfully created"
-    echo "Write the connection settings for the PostgreSQL database."
-    echo "Restart install.sh script after editing."
+    cp .env.example .env
+    echo_success "File .env was successfully created"
+    echo_warning "Write the connection settings for the PostgreSQL database."
+    echo_warning "Restart install.sh script after editing."
     exit
     exit
 fi
 fi
 
 
@@ -95,19 +75,6 @@ if (($GENERATE_TEST_USERS)); then
     python manage.py create_metaservice_test_users
     python manage.py create_metaservice_test_users
 fi
 fi
 
 
-echo_warning "Recreating sharix_open.service"
-\cp conf/sharix_open.service.example conf/sharix_open.service
-sed -i "s|WORKING_DIRECTORY|$(pwd)|g" conf/sharix_open.service
-echo_success "Recreated sharix_open.service"
-
-echo_warning "Copying sharix_open.service to /etc/systemd/system/ folder"
-\cp conf/sharix_open.service /etc/systemd/system/
-echo_success "Copied sharix_open.service to /etc/systemd/system/ folder"
-
-echo_warning "Reloading systemd daemon"
-systemctl daemon-reload
-echo_success "Reloaded systemd daemon"
-
 # Checking for webservice_running/handlers/.env
 # Checking for webservice_running/handlers/.env
 if [ -f webservice_running/handlers/.env ]; then
 if [ -f webservice_running/handlers/.env ]; then
     echo "env file for handlers already exists. "
     echo "env file for handlers already exists. "
@@ -116,6 +83,3 @@ else
     cp webservice_running/handlers/.env.example webservice_running/handlers/.env
     cp webservice_running/handlers/.env.example webservice_running/handlers/.env
     echo_success "Created env file for handlers."
     echo_success "Created env file for handlers."
 fi
 fi
-
-echo_warning "Installation handlers"
-webservice_running/handlers/conf/bin/install.sh

+ 34 - 0
bin/install_handlers.sh

@@ -0,0 +1,34 @@
+source ./bin/utils/functions.sh
+source .env
+source webservice_running/handlers/config.env
+
+(
+  cd webservice_running/handlers
+  python -m venv .venv
+  source .venv/bin/activate
+  pip install -r requirements.txt
+)
+
+echo_warning "Installation handlers"
+
+echo_warning "Recreating sharix.service"
+\cp conf/handler@.service.example conf/handler@.service
+sed -i "s|WORKING_DIRECTORY|$(pwd)|g" conf/handler@.service
+echo_success "Recreated sharix.service"
+
+echo_warning "Copying handler@.service to /etc/systemd/system/ folder"
+\cp conf/handler@.service /etc/systemd/system/handler@.service
+echo_success "Copied handler@.service to /etc/systemd/system/ folder"
+
+echo_warning "Reloading systemd daemon"
+systemctl daemon-reload
+echo_success "Reloaded systemd daemon"
+
+for service in "${handlers[@]}"; do
+  echo "Enabling open_handler@${service}.service"
+  systemctl enable handler@"${service}".service
+done
+
+echo_warning "Reloading systemd daemon"
+systemctl daemon-reload
+echo_success "Reloaded systemd daemon"

+ 17 - 0
bin/install_service.sh

@@ -0,0 +1,17 @@
+source ./bin/utils/functions.sh
+source .env
+
+echo_warning "Recreating sharix.service"
+\cp conf/sharix.service.example conf/sharix.service
+sed -i "s|WORKING_DIRECTORY|$(pwd)|g" conf/sharix.service
+echo_success "Recreated sharix.service"
+
+echo_warning "Copying sharix_open.service to /etc/systemd/system/ folder"
+\cp conf/sharix.service /etc/systemd/system/sharix_${METASERVICE_NAME}.service
+echo_success "Copied sharix_open.service to /etc/systemd/system/ folder"
+
+echo_warning "Reloading systemd daemon"
+systemctl daemon-reload
+echo_success "Reloaded systemd daemon"
+
+systemctl enable sharix_${METASERVICE_NAME}.service

+ 42 - 0
bin/utils/functions.sh

@@ -0,0 +1,42 @@
+
+# Colors
+
+RED="\e[31m"
+GREEN="\e[32m"
+YELLOW="\e[33m"
+RESET="\e[0m"
+
+
+# Functions colors echo text
+echo_error() {
+    echo -e "${RED}$1${RESET}"
+}
+
+echo_warning() {
+    echo -e "${YELLOW}$1${RESET}"
+}
+
+echo_success() {
+    echo -e "${GREEN}$1${RESET}"
+}
+
+
+# Function to check if a repository exists and perform git pull or git clone
+update_repository() {
+    local repo_url="$1"
+    local repo_dir="$2"
+    local repo_branch="$3"
+
+    if [ -d "$repo_dir" ]; then
+        # If the directory exists, then do a git pull
+        echo_warning "Updating $repo_dir..."
+        cd "$repo_dir" || exit
+        git checkout $repo_branch
+        git pull
+        cd - || exit
+    else
+        # If the directory does not exist, then do a git clone
+        echo_warning "Cloning $repo_url into $repo_dir..."
+        git clone -b "$repo_branch" "$repo_url" "$repo_dir"
+    fi
+}

+ 0 - 63
core/_settings_vars.py

@@ -1,63 +0,0 @@
-########
-#
-# Django
-#
-########
-
-DEBUG = True
-SECRET_KEY = 'django-insecure-$eodx2ilnb*djkahfguiwegbwelgbnajl*u$qkt7j-b)7t1iq'
-CSRF_TRUSTED_ORIGINS = []
-ALLOWED_HOSTS = ["127.0.0.1"]
-INTERNAL_IPS = ["127.0.0.1"]
-NAME_SYSTEM = "ShariX"
-API_URL = 'http://127.0.0.1:8000'
-
-########
-#
-# Database
-#
-########
-
-DB_NAME = None
-DB_USER = None
-DB_PASSWORD = None
-DB_HOST = None
-DB_PORT = 5432
-
-########
-#
-# Email
-#
-########
-
-EMAIL_HOST = None
-EMAIL_PORT = 587
-EMAIL_USE_TLS = True
-EMAIL_HOST_USER = None
-EMAIL_HOST_PASSWORD = None
-DEFAULT_FROM_EMAIL = None
-
-########
-#
-# Gunicorn
-#
-########
-
-BIND = "127.0.0.1:8000"
-
-########
-#
-# ejabber
-#
-########
-
-EJ_PROTOCOL = "http"
-EJ_IP = "10.0.20.9"
-EJ_PORT = "5280"
-EJ_URL = EJ_PROTOCOL + "://" + EJ_IP + ":" + EJ_PORT + "/" + "api/"
-EJ_SERVICE = "chat.ej.sharix-app.org"
-EJ_HOST = "ej.sharix-app.org"
-
-# Service name that is used in generating ejabber rooms
-METASERVICE_NAME = "open"
-METASERVICE_ID = 1

+ 73 - 0
core/settings_vars.py

@@ -0,0 +1,73 @@
+from dotenv import load_dotenv
+import os
+
+load_dotenv()
+
+########
+#
+# Django
+#
+########
+
+DEBUG = os.getenv("DEBUG")
+SECRET_KEY = os.getenv("SECRET_KEY")
+CSRF_TRUSTED_ORIGINS = os.getenv("CSRF_TRUSTED_ORIGINS").split(',')
+ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS").split(',')
+INTERNAL_IPS = os.getenv("INTERNAL_IPS").split(',')
+NAME_SYSTEM = os.getenv("NAME_SYSTEM")
+API_URL = os.getenv("API_URL")
+
+########
+#
+# Database
+#
+########
+
+DB_NAME = os.getenv("DB_NAME")
+DB_USER = os.getenv("DB_USER")
+DB_PASSWORD = os.getenv("DB_PASSWORD")
+DB_HOST = os.getenv("DB_HOST")
+DB_PORT = os.getenv("DB_PORT")
+
+########
+#
+# Email
+#
+########
+
+EMAIL_HOST = os.getenv("EMAIL_HOST")
+EMAIL_PORT = os.getenv("EMAIL_PORT")
+EMAIL_USE_TLS = os.getenv("EMAIL_USE_TLS")
+EMAIL_HOST_USER = os.getenv("EMAIL_HOST_USER")
+EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")
+DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL")
+
+########
+#
+# Gunicorn
+#
+########
+
+BIND = os.getenv("BIND")
+
+########
+#
+# ejabber
+#
+########
+
+EJ_PROTOCOL = os.getenv("EJ_PROTOCOL")
+EJ_IP = os.getenv("EJ_IP")
+EJ_PORT = os.getenv("EJ_PORT")
+EJ_URL = EJ_PROTOCOL + "://" + EJ_IP + ":" + EJ_PORT + "/" + "api/"
+EJ_SERVICE = os.getenv("EJ_SERVICE")
+EJ_HOST = os.getenv("EJ_HOST")
+
+########
+#
+# ejabber
+#
+########
+
+METASERVICE_NAME = os.getenv("METASERVICE_NAME")
+METASERVICE_ID = os.getenv("METASERVICE_ID")