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 11KB

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