#!/bin/bash # Get user input for username and script to run # read -p "Enter the username to create: " username # read -p "Enter the full path to the shell script to run: " script_to_run username=elxrprod script_to_run=i.lin.sh # Get the current user (who is running this script) current_user=$(whoami) # 1. Check if the current user is the same as the username to create if [ "$current_user" == "$username" ]; then echo "Script launched from same user!" else if id "$username" &>/dev/null; then echo "User $username already exists, skipping user creation and password set." else # 1. Add user echo "Adding user $username..." useradd -m -s /bin/bash "$username" read -sp "Enter password for the new user: " user_password echo echo "Setting password for user $username..." echo "$username:$user_password" | sudo chpasswd # 2. Add user to the sudoers group (usually 'wheel' on CentOS) echo "Adding $username to the 'wheel' group for sudo access..." usermod -aG wheel "$username" fi # cd /home/$username wget -q -O - http://git.bbh.org.in/chess/elxr/raw/branch/master/$script_to_run > $script_to_run sudo cp ./$script_to_run /home/$username sudo chmod u+x /home/$username/i.lin.sh # 3. Switch to the user and run the script echo "Switching to user $username and executing the script..." sudo -u "$username" bash -c "sh /home/$username/$script_to_run" exit 0 fi # Default values DEFAULT_REPOSERVER="http://git.bbh" DEFAULT_DEFAULTREPOOWNER="chess" DEFAULT_INSTANCENAME="cihsr" DEFAULT_INSTANCETYPE="development" DEFAULT_GITUSER="pb" DEFAULT_GITEMAIL="chess@bbh.org.in" # Prompt for user input with default values echo "Enter repo server URL (default: $DEFAULT_REPOSERVER):" read reposervername REPOSERVER=${reposervername:-$DEFAULT_REPOSERVER} echo "Enter default repo owner (default: $DEFAULT_DEFAULTREPOOWNER):" read defaultrepoownername DEFAULTREPOOWNER=${defaultrepoownername:-$DEFAULT_DEFAULTREPOOWNER} REPOOWNER=$DEFAULTREPOOWNER echo "Enter instance name (default: $DEFAULT_INSTANCENAME):" read instancename INSTANCENAME=${instancename:-$DEFAULT_INSTANCENAME} echo "Enter instance type (default: $DEFAULT_INSTANCETYPE):" read instancetypename INSTANCETYPE=${instancetypename:-$DEFAULT_INSTANCETYPE} echo "Enter Git user name (default: $DEFAULT_GITUSER):" read GITUSERID GITUSER=${GITUSERID:-$DEFAULT_GITUSER} echo "Enter Git user email (default: $DEFAULT_GITEMAIL):" read GITEMAILID GITEMAIL=${GITEMAILID:-$DEFAULT_GITEMAIL} # Prompt for skipping prerequisites echo "Do you want to skip prerequisite installations? (yes/no, default: no)" read skipprereqs SKIPPREREQS=${skipprereqs:-"no"} # Prompt for skipping dev prerequisites echo "Do you want to skip development prerequisite installations? (yes/no, default: no)" read skipdevprereqs SKIPDEVPREREQS=${skipdevprereqs:-"no"} # Prompt for ROOT folder, with two options: ./chess/production_A or /var/chess/$INSTANCENAME/$INSTANCETYPE echo "Enter the ROOT folder location (default: ./chess/production_A):" echo "1) ./chess/production_A" echo "2) /var/chess/$INSTANCENAME/$INSTANCETYPE" read rootfolder if [ "$rootfolder" == "2" ]; then ROOT=/var/chess/$INSTANCENAME/$INSTANCETYPE else ROOT=$(pwd) # Default to current directory if user chooses 1 fi mkdir -p $ROOT cd $ROOT wget -q -O - http://git.bbh/chess/elxr/raw/branch/master/centosinit.sh > $ROOT/centosinit.sh sudo chmod u+x $ROOT/centosinit.sh sh $ROOT/centosinit.sh # If ROOT is not the current directory, create the necessary directories and change to ROOT if [ "$ROOT" != "$(pwd)" ]; then sudo mkdir -p /var/chess sudo chown $USER:$USER /var/chess mkdir -p /var/chess/$INSTANCENAME sudo chown $USER:$USER /var/chess/$INSTANCENAME mkdir -p $ROOT sudo chown $USER:$USER $ROOT # Change to ROOT directory cd $ROOT || { echo "Failed to change to ROOT directory $ROOT"; exit 1; } fi # Platform-specific installation if [ -f /etc/os-release ]; then . /etc/os-release OS_NAME=$NAME OS_VERSION=$VERSION_ID fi # Function to check if a package is installed is_package_installed() { local package=$1 if command -v $package &>/dev/null; then return 0 # Package is installed else return 1 # Package is not installed fi } # Function to install packages based on the package manager install_package() { local package=$1 if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then sudo apt update sudo apt install -y $package || { echo "Failed to install $package"; exit 1; } elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then # Prefer dnf for CentOS 8 and above if command -v dnf &>/dev/null; then sudo dnf install -y $package || { echo "Failed to install $package"; exit 1; } else sudo yum install -y $package || { echo "Failed to install $package"; exit 1; } fi else echo "Unsupported OS $OS_NAME" exit 1 fi } # Install prerequisites if [ "$SKIPPREREQS" = "yes" ]; then echo "Skipping prerequisites for first run" else # Create group and add user (skip failure) sudo groupadd chessprod || echo "Group 'chessprod' already exists or failed to create, proceeding..." sudo usermod -a -G chessprod $USER || echo "Failed to add user $USER to chessprod group or already a member, proceeding..." # Install Git if not already installed if ! is_package_installed "git"; then install_package "git" fi git config --global user.name "$GITUSER" git config --global user.email "$GITEMAIL" git config --global credential.helper store # Install Python 2 if not installed if ! is_package_installed "python2"; then install_package "python2" fi # Install build tools based on OS type if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then install_package "build-essential" elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then install_package "gcc" install_package "make" install_package "glibc-devel" install_package "gcc-c++" fi # Installing Node.js and NVM curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash sleep 2 # Add NVM configuration to .bash_profile echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bash_profile echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bash_profile echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bash_profile # Source .bash_profile to apply NVM setup source ~/.bash_profile # Install Node.js nvm install v16.19.1 node --version npm --version # Install code editor (VSCode) if [[ "$OS_NAME" == "Ubuntu" ]] || [[ "$OS_NAME" == "Debian" ]]; then # VSCode installation for Debian-based systems (Ubuntu/Debian) install_package "wget" wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg sudo sh -c 'echo "deb [arch=amd64,arm64,armhf signed-by=/etc/apt/keyrings/packages.microsoft.gpg] https://packages.microsoft.com/repos/code stable main" > /etc/apt/sources.list.d/vscode.list' rm -f packages.microsoft.gpg sudo apt install apt-transport-https sudo apt update sudo apt install code -y elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then # VSCode installation for CentOS-based systems (CentOS 8 / RHEL) sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'echo -e "[code]\nname=Visual Studio Code\nbaseurl=https://packages.microsoft.com/yumrepos/vscode\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/vscode.repo' sudo dnf check-update sudo dnf install code -y fi fi # Install OpenJDK 11 (Java) for both Debian/Ubuntu and CentOS/RHEL systems if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then install_package "openjdk-11-jre-headless" elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then # Install Java 11 on CentOS/RHEL-based systems sudo dnf install java-11-openjdk -y || { echo "Failed to install Java 11"; exit 1; } fi # MySQL Installation for CentOS (without debconf-set-selections) if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then sudo dnf install mysql-server -y sudo systemctl start mysqld sudo systemctl enable mysqld MYSQL_ROOT_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log | tail -n 1 | awk '{print $NF}') echo "MySQL root temporary password: $MYSQL_ROOT_PASSWORD" # Secure MySQL installation sudo mysql_secure_installation else # If on Ubuntu/Debian, you can use debconf-set-selections sudo debconf-set-selections < installchoices.js echo "Setup completed successfully!" if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then sudo dnf install redis -y else sudo apt install redis -y fi sudo systemctl start redis sudo systemctl enable redis sudo systemctl status redis echo "Setup completed successfully!" # USE mysql; # ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'cihsr'; # FLUSH PRIVILEGES; mysql -u root -p cihsr < cihsr_prod.sql