#!/bin/bash # Exit immediately if a command exits with a non-zero status set -e # Function to print messages log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" } # Check if the script is run as root if [ "$(id -u)" -ne 0 ]; then log "Please run this script as root or with sudo." exit 1 fi # Function to create PostgreSQL user and database create_postgres_db() { local project_name=$1 local db_name="${project_name}_db" local db_user="${project_name}_user" local db_password=$(openssl rand -base64 12) log "Creating PostgreSQL user and database" # Change to a directory accessible by the postgres user cd /tmp sudo -u postgres psql < "$credentials_file" } # Function to check and install a package if not installed check_and_install() { local package=$1 local install_cmd=$2 if ! dpkg -l | grep -q "$package"; then log "$package is not installed. Installing $package..." eval "$install_cmd" else log "$package is already installed." fi } # Function to start and enable a service start_and_enable_service() { local service=$1 log "Starting and enabling $service service" sudo systemctl enable --now "$service" } # Check if required packages are installed required_packages=("nginx" "python3.12" "postgresql") missing_packages=() for package in "${required_packages[@]}"; do if ! dpkg -l | grep -q "$package"; then missing_packages+=("$package") fi done # If any packages are missing, prompt the user to install them if [ ${#missing_packages[@]} -ne 0 ]; then log "The following packages are not installed: ${missing_packages[*]}" log "Do you want to install them? (y/n)" read install_choice if [ "$install_choice" == "y" ]; then for package in "${missing_packages[@]}"; do case $package in "nginx") sudo apt-get install -y nginx ;; "python3.12") sudo add-apt-repository -y ppa:deadsnakes/ppa sudo apt-get update sudo apt-get install -y python3.12 ;; "postgresql") sudo apt-get install -y postgresql postgresql-contrib ;; esac done else log "Skipping installation of missing packages." fi else log "All required packages are already installed." fi # Start and enable services if installed if dpkg -l | grep -q "nginx"; then if ! systemctl is-enabled --quiet nginx; then start_and_enable_service "nginx" # Open nginx ports log "Opening nginx ports 80 and 443" sudo ufw allow 'Nginx Full' sudo ufw reload fi fi if dpkg -l | grep -q "postgresql"; then if ! systemctl is-enabled --quiet postgresql; then start_and_enable_service "postgresql" fi fi # Ask if the project is new or existing while true; do log "Is this a new or existing project? (1 for new, 2 for existing)" read project_type if [ "$project_type" == "1" ] || [ "$project_type" == "2" ]; then break else log "Invalid input. Please enter '1' for new or '2' for existing." fi done if [ "$project_type" == "1" ]; then log "Enter the name of the new project:" read project_name log "Enter the GitHub link of the new project:" read github_link log "Cloning the repository from $github_link into /home/sepehr/$project_name" sudo -u sepehr git clone "$github_link" "/home/sepehr/$project_name" create_postgres_db "$project_name" elif [ "$project_type" == "2" ]; then log "Do you want to create a PostgreSQL database? (1 for yes, 2 for no)" read create_db if [ "$create_db" == "1" ]; then log "Enter the name of the existing project:" read project_name create_postgres_db "$project_name" else log "Skipping database creation." fi fi