Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

i.lin.sh 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. # Determine the group to add the user to based on the OS
  10. if [ -f /etc/os-release ]; then
  11. . /etc/os-release
  12. OS_NAME=$NAME
  13. fi
  14. # Function to check if the OS is CentOS or CentOS Stream
  15. is_centos() {
  16. # Check if the OS is CentOS or CentOS Stream
  17. if [ -f /etc/os-release ]; then
  18. . /etc/os-release
  19. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$NAME" == "CentOS Stream" ]]; then
  20. return 0 # It's CentOS or CentOS Stream
  21. fi
  22. fi
  23. return 1 # Not CentOS or CentOS Stream
  24. }
  25. # Set ROOT directory
  26. ROOT="./chess/production_A"
  27. # Check if the system is Ubuntu/Debian or CentOS/RHEL
  28. if [[ "$OS_NAME" == "Ubuntu" ]] || [[ "$OS_NAME" == "Debian" ]]; then
  29. GROUP_NAME="sudo" # Ubuntu/Debian systems typically use 'sudo'
  30. else
  31. GROUP_NAME="wheel" # CentOS/RHEL typically use 'wheel'
  32. fi
  33. # Trim whitespace from both current_user and username
  34. current_user=$(echo "$current_user" | xargs)
  35. username=$(echo "$username" | xargs)
  36. echo $current_user
  37. echo $username
  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. echo $pwd
  109. ROOT=./chess/production_A
  110. fi
  111. mkdir -p $ROOT
  112. ROOT=$(realpath $ROOT)
  113. sudo chown -R $USER:$USER $ROOT
  114. # If ROOT is not the current directory, create the necessary directories and change to ROOT
  115. # if [ "$ROOT" != "$(pwd)" ]; then
  116. # sudo chown $USER:$USER $ROOT
  117. # fi
  118. # Change to ROOT directory
  119. cd $ROOT || { echo "Failed to change to ROOT directory $ROOT"; exit 1; }
  120. echo $PWD
  121. # Only run this block if it's CentOS or CentOS Stream
  122. # if is_centos; then
  123. if [ -f /etc/os-release ]; then
  124. . /etc/os-release
  125. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$NAME" == "CentOS" ]]; then
  126. echo "This is CentOS Running CentOS specific script..."
  127. # Download and run CentOS-specific script
  128. wget -q -O - http://git.bbh/chess/elxr/raw/branch/master/centosinit.sh > $ROOT/centosinit.sh
  129. sudo chmod u+x $ROOT/centosinit.sh
  130. sh $ROOT/centosinit.sh
  131. fi
  132. fi
  133. # else
  134. # echo "This is not CentOS. Skipping CentOS-specific setup."
  135. # fi
  136. # Function to check if a package is installed
  137. is_package_installed() {
  138. local package=$1
  139. if command -v $package &>/dev/null; then
  140. return 0 # Package is installed
  141. else
  142. return 1 # Package is not installed
  143. fi
  144. }
  145. # Function to install packages based on the package manager
  146. install_package() {
  147. local package=$1
  148. if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then
  149. sudo apt update
  150. sudo apt install -y $package || { echo "Failed to install $package"; exit 1; }
  151. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS Stream" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  152. # Prefer dnf for CentOS Stream
  153. if command -v dnf &>/dev/null; then
  154. sudo dnf install -y $package || { echo "Failed to install $package"; exit 1; }
  155. else
  156. sudo yum install -y $package || { echo "Failed to install $package"; exit 1; }
  157. fi
  158. else
  159. echo "Unsupported OS $OS_NAME"
  160. exit 1
  161. fi
  162. }
  163. # Install prerequisites
  164. if [ "$SKIPPREREQS" = "yes" ]; then
  165. echo "Skipping prerequisites for first run"
  166. else
  167. # Create group and add user (skip failure)
  168. sudo groupadd chessprod || echo "Group 'chessprod' already exists or failed to create, proceeding..."
  169. sudo usermod -a -G chessprod $USER || echo "Failed to add user $USER to chessprod group or already a member, proceeding..."
  170. # Install Git if not already installed
  171. if ! is_package_installed "git"; then
  172. install_package "git"
  173. fi
  174. git config --global user.name "$GITUSER"
  175. git config --global user.email "$GITEMAIL"
  176. git config --global credential.helper store
  177. # Install Python 2 if not installed
  178. if ! is_package_installed "python2"; then
  179. if [ -f /etc/os-release ]; then
  180. . /etc/os-release
  181. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$NAME" == "Ubuntu" ]]; then
  182. install_package "python2"
  183. fi
  184. fi
  185. fi
  186. # Install build tools based on OS type
  187. if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then
  188. install_package "build-essential"
  189. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS Stream" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  190. install_package "gcc"
  191. install_package "make"
  192. install_package "glibc-devel"
  193. install_package "gcc-c++"
  194. fi
  195. fi
  196. # Install NVM (Node Version Manager)
  197. wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  198. sleep 2
  199. # Add NVM configuration to .bash_profile if it's not already there
  200. if ! grep -q 'export NVM_DIR="$HOME/.nvm"' ~/.bash_profile; then
  201. echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bash_profile
  202. echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.bash_profile
  203. echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bash_profile
  204. fi
  205. # Source .bash_profile to apply NVM setup
  206. source ~/.bash_profile
  207. # Verify NVM installation
  208. command -v nvm
  209. # Install Node.js
  210. nvm install v16.19.1
  211. node --version
  212. npm --version
  213. # Install code editor (VSCode)
  214. if [[ "$OS_NAME" == "Ubuntu" ]] || [[ "$OS_NAME" == "Debian" ]]; then
  215. install_package "wget"
  216. wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > packages.microsoft.gpg
  217. sudo install -D -o root -g root -m 644 packages.microsoft.gpg /etc/apt/keyrings/packages.microsoft.gpg
  218. 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'
  219. rm -f packages.microsoft.gpg
  220. sudo apt install apt-transport-https
  221. sudo apt update
  222. sudo apt install code -y
  223. elif [[ "$OS_NAME" == "CentOS Stream" ]] || [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  224. sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
  225. 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'
  226. sudo dnf check-update
  227. sudo dnf install code -y
  228. fi
  229. # Install OpenJDK 11 (Java) for both Debian/Ubuntu and CentOS/RHEL systems
  230. if [ "$OS_NAME" == "Ubuntu" ] || [ "$OS_NAME" == "Debian" ]; then
  231. install_package "openjdk-11-jre-headless"
  232. elif [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  233. # [[ "$OS_NAME" == "CentOS Stream" ]] || -- skip java for centos stream
  234. sudo dnf install java-11-openjdk -y || { echo "Failed to install Java 11"; exit 1; }
  235. fi
  236. # MySQL Installation for CentOS Stream
  237. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS Stream" ]]; then
  238. sudo dnf install mysql-server -y
  239. sudo systemctl start mysqld
  240. sudo systemctl enable mysqld
  241. MYSQL_ROOT_PASSWORD=$(sudo grep 'temporary password' /var/log/mysqld.log | tail -n 1 | awk '{print $NF}')
  242. echo "MySQL root temporary password: $MYSQL_ROOT_PASSWORD"
  243. sudo mysql_secure_installation
  244. else
  245. # If on Ubuntu/Debian, you can use debconf-set-selections
  246. sudo debconf-set-selections <<EOF
  247. mysql-server mysql-server/lowercase-table-names select Enabled
  248. EOF
  249. sudo apt install mysql-server -y
  250. sudo systemctl start mysql
  251. fi
  252. git clone $REPOSERVER/$REPOOWNER/bbhverse
  253. cd bbhverse
  254. npm i
  255. cd ..
  256. git clone $REPOSERVER/$REPOOWNER/serververse
  257. git clone $REPOSERVER/$REPOOWNER/global-this
  258. git clone $REPOSERVER/$REPOOWNER/elxr.git
  259. # Install npm dependencies and link
  260. cd elxr
  261. npm i
  262. npm link --force
  263. cd ..
  264. sudo ln -s $ROOT/elxr/bin/elxr $(npm bin -g)/elxr
  265. elxr use $INSTANCENAME
  266. elxr i $INSTANCENAME
  267. echo "Module configuration generated for: $INSTANCENAME"
  268. echo "module.exports = { instanceName : '$INSTANCENAME', reposerver: '$REPOSERVER', gitUser: '$GITUSER', gitEmail: '$GITEMAIL' }" > installchoices.js
  269. if [[ "$OS_NAME" == "CentOS Linux" ]] || [[ "$OS_NAME" == "CentOS" ]] || [[ "$OS_NAME" == "Red Hat" ]]; then
  270. sudo dnf install redis -y
  271. else
  272. sudo apt install redis -y
  273. fi
  274. sudo systemctl start redis
  275. sudo systemctl enable redis
  276. # sudo systemctl status redis
  277. echo "Setup completed successfully!"
  278. # USE mysql;
  279. # ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'cihsr';
  280. # FLUSH PRIVILEGES;
  281. bash elxr/mysql.init.sh
  282. bash elxr/load.mysql.sh
  283. bash elxr/i.sqlexpress.sh
  284. bash elxr/load.sqlexpress.sh