You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

i.lin.sh 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/bin/bash
  2. # Get user input for username and script to run
  3. # read -p "Enter the username to create: " username
  4. # read -p "Enter the full path to the shell script to run: " script_to_run
  5. username=elxrprod
  6. script_to_run=i.lin.sh
  7. # Get the current user (who is running this script)
  8. current_user=$(whoami)
  9. # 1. Check if the current user is the same as the username to create
  10. if [ "$current_user" == "$username" ]; then
  11. echo "Script launched from same user!"
  12. else
  13. if id "$username" &>/dev/null; then
  14. echo "User $username already exists, skipping user creation and password set."
  15. else
  16. # 1. Add user
  17. echo "Adding user $username..."
  18. useradd -m -s /bin/bash "$username"
  19. read -sp "Enter password for the new user: " user_password
  20. echo
  21. echo "Setting password for user $username..."
  22. echo "$username:$user_password" | sudo chpasswd
  23. # 2. Add user to the sudoers group (usually 'wheel' on CentOS)
  24. echo "Adding $username to the 'wheel' group for sudo access..."
  25. usermod -aG wheel "$username"
  26. fi
  27. # cd /home/$username
  28. #wget -q -O - http://git.bbh.org.in/chess/elxr/raw/branch/master/$script_to_run > $script_to_run
  29. sudo cp ./$script_to_run /home/$username
  30. sudo chmod u+x /home/$username/i.lin.sh
  31. # 3. Switch to the user and run the script
  32. echo "Switching to user $username and executing the script..."
  33. sudo -u "$username" bash -c "sh /home/$username/$script_to_run"
  34. exit 0
  35. fi
  36. # Default values
  37. DEFAULT_REPOSERVER="http://git.bbh"
  38. DEFAULT_DEFAULTREPOOWNER="chess"
  39. DEFAULT_INSTANCENAME="cihsr"
  40. DEFAULT_INSTANCETYPE="development"
  41. DEFAULT_GITUSER="pb"
  42. DEFAULT_GITEMAIL="chess@bbh.org.in"
  43. # Prompt for user input with default values
  44. echo "Enter repo server URL (default: $DEFAULT_REPOSERVER):"
  45. read reposervername
  46. REPOSERVER=${reposervername:-$DEFAULT_REPOSERVER}
  47. echo "Enter default repo owner (default: $DEFAULT_DEFAULTREPOOWNER):"
  48. read defaultrepoownername
  49. DEFAULTREPOOWNER=${defaultrepoownername:-$DEFAULT_DEFAULTREPOOWNER}
  50. REPOOWNER=$DEFAULTREPOOWNER
  51. echo "Enter instance name (default: $DEFAULT_INSTANCENAME):"
  52. read instancename
  53. INSTANCENAME=${instancename:-$DEFAULT_INSTANCENAME}
  54. echo "Enter instance type (default: $DEFAULT_INSTANCETYPE):"
  55. read instancetypename
  56. INSTANCETYPE=${instancetypename:-$DEFAULT_INSTANCETYPE}
  57. echo "Enter Git user name (default: $DEFAULT_GITUSER):"
  58. read GITUSERID
  59. GITUSER=${GITUSERID:-$DEFAULT_GITUSER}
  60. echo "Enter Git user email (default: $DEFAULT_GITEMAIL):"
  61. read GITEMAILID
  62. GITEMAIL=${GITEMAILID:-$DEFAULT_GITEMAIL}
  63. # Prompt for skipping prerequisites
  64. echo "Do you want to skip prerequisite installations? (yes/no, default: no)"
  65. read skipprereqs
  66. SKIPPREREQS=${skipprereqs:-"no"}
  67. # Prompt for skipping dev prerequisites
  68. echo "Do you want to skip development prerequisite installations? (yes/no, default: no)"
  69. read skipdevprereqs
  70. SKIPDEVPREREQS=${skipdevprereqs:-"no"}
  71. # Prompt for ROOT folder, with two options: ./chess/production_A or /var/chess/$INSTANCENAME/$INSTANCETYPE
  72. echo "Enter the ROOT folder location (default: ./chess/production_A):"
  73. echo "1) ./chess/production_A"
  74. echo "2) /var/chess/$INSTANCENAME/$INSTANCETYPE"
  75. read rootfolder
  76. if [ "$rootfolder" == "2" ]; then
  77. ROOT=/var/chess/$INSTANCENAME/$INSTANCETYPE
  78. else
  79. ROOT=$(pwd) # Default to current directory if user chooses 1
  80. fi
  81. mkdir -p $ROOT
  82. cd $ROOT
  83. wget -q -O - http://git.bbh/chess/elxr/raw/branch/master/centosinit.sh > $ROOT/centosinit.sh
  84. sudo chmod u+x $ROOT/centosinit.sh
  85. sh $ROOT/centosinit.sh
  86. # If ROOT is not the current directory, create the necessary directories and change to ROOT
  87. if [ "$ROOT" != "$(pwd)" ]; then
  88. sudo mkdir -p /var/chess
  89. sudo chown $USER:$USER /var/chess
  90. mkdir -p /var/chess/$INSTANCENAME
  91. sudo chown $USER:$USER /var/chess/$INSTANCENAME
  92. mkdir -p $ROOT
  93. sudo chown $USER:$USER $ROOT
  94. # Change to ROOT directory
  95. cd $ROOT || { echo "Failed to change to ROOT directory $ROOT"; exit 1; }
  96. fi
  97. # Platform-specific installation
  98. if [ -f /etc/os-release ]; then
  99. . /etc/os-release
  100. OS_NAME=$NAME
  101. OS_VERSION=$VERSION_ID
  102. fi
  103. # Function to check if a package is installed
  104. is_package_installed() {
  105. local package=$1
  106. if command -v $package &>/dev/null; then
  107. return 0 # Package is installed
  108. else
  109. return 1 # Package is not installed
  110. fi
  111. }
  112. # Function to install packages based on the package manager
  113. install_package() {
  114. local package=$1
  115. if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then
  116. sudo apt update
  117. sudo apt install -y $package || { echo "Failed to install $package"; exit 1; }
  118. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  119. # Prefer dnf for CentOS 8 and above
  120. if command -v dnf &>/dev/null; then
  121. sudo dnf install -y $package || { echo "Failed to install $package"; exit 1; }
  122. else
  123. sudo yum install -y $package || { echo "Failed to install $package"; exit 1; }
  124. fi
  125. else
  126. echo "Unsupported OS $OS_NAME"
  127. exit 1
  128. fi
  129. }
  130. # Install prerequisites
  131. if [ "$SKIPPREREQS" = "yes" ]; then
  132. echo "Skipping prerequisites for first run"
  133. else
  134. # Create group and add user (skip failure)
  135. sudo groupadd chessprod || echo "Group 'chessprod' already exists or failed to create, proceeding..."
  136. sudo usermod -a -G chessprod $USER || echo "Failed to add user $USER to chessprod group or already a member, proceeding..."
  137. # Install Git if not already installed
  138. if ! is_package_installed "git"; then
  139. install_package "git"
  140. fi
  141. git config --global user.name "$GITUSER"
  142. git config --global user.email "$GITEMAIL"
  143. git config --global credential.helper store
  144. # Install Python 2 if not installed
  145. if ! is_package_installed "python2"; then
  146. install_package "python2"
  147. fi
  148. # Install build tools based on OS type
  149. if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then
  150. install_package "build-essential"
  151. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  152. install_package "gcc"
  153. install_package "make"
  154. install_package "glibc-devel"
  155. install_package "gcc-c++"
  156. fi
  157. # Installing Node.js and NVM
  158. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  159. sleep 2
  160. # Add NVM configuration to .bash_profile
  161. echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bash_profile
  162. echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bash_profile
  163. echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bash_profile
  164. # Source .bash_profile to apply NVM setup
  165. source ~/.bash_profile
  166. # Install Node.js
  167. nvm install v16.19.1
  168. node --version
  169. npm --version
  170. # Install code editor (VSCode)
  171. if [[ "$OS_NAME" == "Ubuntu" ]] || [[ "$OS_NAME" == "Debian" ]]; then
  172. # VSCode installation for Debian-based systems (Ubuntu/Debian)
  173. install_package "wget"
  174. wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
  175. sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
  176. 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'
  177. rm -f packages.microsoft.gpg
  178. sudo apt install apt-transport-https
  179. sudo apt update
  180. sudo apt install code -y
  181. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  182. # VSCode installation for CentOS-based systems (CentOS 8 / RHEL)
  183. sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
  184. 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'
  185. sudo dnf check-update
  186. sudo dnf install code -y
  187. fi
  188. fi
  189. # Install OpenJDK 11 (Java) for both Debian/Ubuntu and CentOS/RHEL systems
  190. if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then
  191. install_package "openjdk-11-jre-headless"
  192. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  193. # Install Java 11 on CentOS/RHEL-based systems
  194. sudo dnf install java-11-openjdk -y || { echo "Failed to install Java 11"; exit 1; }
  195. fi
  196. # MySQL Installation for CentOS (without debconf-set-selections)
  197. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  198. sudo dnf install mysql-server -y
  199. sudo systemctl start mysqld
  200. sudo systemctl enable mysqld
  201. MYSQL_ROOT_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log | tail -n 1 | awk '{print $NF}')
  202. echo "MySQL root temporary password: $MYSQL_ROOT_PASSWORD"
  203. # Secure MySQL installation
  204. sudo mysql_secure_installation
  205. else
  206. # If on Ubuntu/Debian, you can use debconf-set-selections
  207. sudo debconf-set-selections <<EOF
  208. mysql-server mysql-server/lowercase-table-names select Enabled
  209. EOF
  210. sudo apt install mysql-server -y
  211. sudo systemctl start mysql
  212. fi
  213. git clone $REPOSERVER/$REPOOWNER/bbhverse
  214. cd bbhverse
  215. npm i
  216. cd ..
  217. git clone $REPOSERVER/$REPOOWNER/serververse
  218. git clone $REPOSERVER/$REPOOWNER/global-this
  219. git clone $REPOSERVER/$REPOOWNER/elxr.git
  220. # Install npm dependencies and link
  221. cd elxr
  222. npm i
  223. npm link
  224. cd ..
  225. sudo ln -s $ROOT/elxr/bin/elxr $(npm bin -g)/elxr
  226. elxr use $INSTANCENAME
  227. elxr install
  228. echo "Module configuration generated for: $INSTANCENAME"
  229. echo "module.exports = { instanceName : '$INSTANCENAME', reposerver: '$REPOSERVER', gitUser: '$GITUSER', gitEmail: '$GITEMAIL' }" > installchoices.js
  230. echo "Setup completed successfully!"
  231. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  232. sudo dnf install redis -y
  233. else
  234. sudo apt install redis -y
  235. fi
  236. sudo systemctl start redis
  237. sudo systemctl enable redis
  238. sudo systemctl status redis
  239. echo "Setup completed successfully!"
  240. # USE mysql;
  241. # ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'cihsr';
  242. # FLUSH PRIVILEGES;
  243. mysql -u root -p cihsr < cihsr_prod.sql