centos7.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #!/bin/bash
  2. # Reset
  3. Color_Off='\033[0m' # Text Reset
  4. # Regular Colors
  5. Black='\033[0;30m' # Black
  6. Red='\033[0;31m' # Red
  7. Green='\033[0;32m' # Green
  8. Yellow='\033[0;33m' # Yellow
  9. Blue='\033[0;34m' # Blue
  10. Purple='\033[0;35m' # Purple
  11. Cyan='\033[0;36m' # Cyan
  12. White='\033[0;37m' # White
  13. # Underline
  14. UBlack='\033[4;30m' # Black
  15. URed='\033[4;31m' # Red
  16. UGreen='\033[4;32m' # Green
  17. UYellow='\033[4;33m' # Yellow
  18. UBlue='\033[4;34m' # Blue
  19. UPurple='\033[4;35m' # Purple
  20. UCyan='\033[4;36m' # Cyan
  21. UWhite='\033[4;37m' # White
  22. # Background
  23. On_Black='\033[40m' # Black
  24. On_Red='\033[41m' # Red
  25. On_Green='\033[42m' # Green
  26. On_Yellow='\033[43m' # Yellow
  27. On_Blue='\033[44m' # Blue
  28. On_Purple='\033[45m' # Purple
  29. On_Cyan='\033[46m' # Cyan
  30. On_White='\033[47m' # White
  31. #show banner
  32. clear
  33. echo -e "${Red}"
  34. echo ""
  35. echo " ███████ ██████ ████████ ███████ ██ ██ █████ ██████ ";
  36. sleep 0.1
  37. echo " ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ";
  38. sleep 0.1
  39. echo " █████ ██████ ██ █████ █████ ███████ ██████ ";
  40. sleep 0.1
  41. echo " ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ";
  42. sleep 0.1
  43. echo " ███████ ██████ ██ ███████ ██ ██ ██ ██ ██ ██ ";
  44. echo ""
  45. echo -e "${Color_Off}"
  46. script_path="/root/voip_server_initial_setup/"
  47. # Set the path to the voip_server application
  48. voip_server_path="/root/voip_managment_server/"
  49. # Define an array of symbols to use for the loading indicator
  50. spinner=( 'Loading /' 'Loading -' 'Loading \' 'Loading |' )
  51. # Define a function to display the loading indicator
  52. spin() {
  53. # Run an infinite loop
  54. while true; do
  55. # Display each symbol of the loading indicator in order
  56. for i in "${spinner[@]}"; do
  57. # Clear the previous line and display the new symbol
  58. echo -ne "\r$i"
  59. # Wait for 0.1 seconds
  60. sleep 0.1
  61. done
  62. done
  63. }
  64. loding_start(){
  65. spinner=( "$1 -" "$1 /" "$1 |" "$1 \ " )
  66. spin &
  67. # Save the process ID of the loading indicator
  68. SPIN_PID=$!
  69. }
  70. loding_stop(){
  71. # Clear the line
  72. echo -ne "\r\033[K"
  73. kill -s SIGTERM $SPIN_PID
  74. sleep 0.1
  75. # Unset the SPIN_PID variable
  76. unset SPIN_PID
  77. }
  78. function update()
  79. {
  80. printf "${Cyan}update centos\n${Color_Off}"
  81. yum update -y
  82. }
  83. function install_packages()
  84. {
  85. cd packages
  86. loding_start "install packages"
  87. sudo rpm -ivh --force *.rpm > /dev/null 2>&1
  88. loding_stop 2>/dev/null
  89. cd ../
  90. printf "${Green}packages installed\n${Color_Off}"
  91. }
  92. function install_node()
  93. {
  94. version=$(node -v 2>/dev/null)
  95. if [ -z "$version" -o "$version" != "v14.18.1" ]; then
  96. loding_start "install node js"
  97. tar xzf node-v14.18.1-linux-x64.tar.gz > /dev/null 2>&1
  98. cd node-v14.18.1-linux-x64
  99. for dir in bin include lib share; do cp -par ${dir}/* /usr/local/${dir}/; done > /dev/null 2>&1
  100. cd ../
  101. loding_stop 2>/dev/null
  102. printf "${Green}node.js 14.18.1 installed\n${Color_Off}"
  103. else
  104. printf "${UGreen}node.js 14.18.1 already installed\n${Color_Off}"
  105. fi
  106. }
  107. function install_pm2()
  108. {
  109. version=$(pm2 --version 2>/dev/null)
  110. if [ "$version" != "5.1.2" ]; then
  111. loding_start "install pm2"
  112. tar -xvf pm2-5.1.2.tgz > /dev/null 2>&1
  113. cd pm2
  114. npm install -g > /dev/null 2>&1
  115. cd ../
  116. loding_stop 2>/dev/null
  117. printf "${Green}pm2 5.1.2 installed\n${Color_Off}"
  118. else
  119. printf "${UGreen}pm2 5.1.2 already installed\n${Color_Off}"
  120. fi
  121. }
  122. function install_git_v2()
  123. {
  124. version=$(git --version 2>/dev/null)
  125. if [ "$version" != "git version 2.9.5" ]; then
  126. loding_start "install git"
  127. tar -xvf git-2.9.5.tar.gz > /dev/null 2>&1
  128. cd git-2.9.5
  129. make configure > /dev/null 2>&1
  130. sudo ./configure --prefix=/usr > /dev/null 2>&1
  131. sudo make > /dev/null 2>&1
  132. sudo make install > /dev/null 2>&1
  133. config_git
  134. cd ../
  135. loding_stop 2>/dev/null
  136. printf "${Green}git 2.9.5 installed\n${Color_Off}"
  137. else
  138. printf "${UGreen}git 2.9.5 already installed\n${Color_Off}"
  139. fi
  140. }
  141. function config_git(){
  142. git config --global user.name "Mohamad Rezaie"
  143. git config --global user.email m.rezaie.aval@gmail.com
  144. }
  145. function create_mysql_db()
  146. {
  147. password='69603AaB@'
  148. result=$(mysql --user=root --password=$password -Bse "CREATE DATABASE ebtekar; GRANT ALL PRIVILEGES ON *.* To 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;" 2>&1 )
  149. while [[ "$result" == "ERROR 1045"* ]]
  150. do
  151. printf "${Red}password is not correct\n${Color_Off}"
  152. read -s -p "Please enter your mysql password: " password
  153. result=$(mysql --user=root --password=$password -Bse "create database ebtekar; GRANT ALL PRIVILEGES ON *.* To 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;" 2>&1 )
  154. printf "\n"
  155. done
  156. if [[ "$result" == *"ERROR 1007"* ]]; then
  157. printf "${UGreen}ebtekar database already created\n${Color_Off}"
  158. else
  159. printf "${UGreen}Databse Created\n${Color_Off}"
  160. fi
  161. if [ $(mysql --user=root --password=$password -e "use ebtekar; show tables like 'usermap';" | wc -l) -eq 0 ]
  162. then
  163. echo "restore database"
  164. result=$(mysql --user=root --password=$password ebtekar < ebtekar.sql)2>&1
  165. echo = $result
  166. if [[ "$result" == *"ERROR 1007"* ]]; then
  167. printf "${UGreen}restore failed\n${Color_Off}"
  168. return 1
  169. else
  170. printf "${UGreen}Databse restored\n${Color_Off}"
  171. fi
  172. fi
  173. }
  174. function setup_voip_server()
  175. {
  176. if [ ! -d "$voip_server_path" ]; then
  177. echo "The project is not cloned. Please choose one of the following options:"
  178. echo "1. Clone the project from http://192.168.2.221"
  179. echo "2. Clone the project from http://efspco.ir"
  180. echo "3. Clone the project from another URL"
  181. echo "4. Exit"
  182. read -p "Enter your choice: " choice
  183. case $choice in
  184. 1)
  185. git clone http://192.168.2.221:3000/rezaie/voip_managment_server.git $voip_server_path
  186. tar xf $voip_server_path/node_modules.tar.gz -C $voip_server_path
  187. ;;
  188. 2)
  189. git clone http://efspco.ir:9696/rezaie/voip_managment_server.git $voip_server_path
  190. tar xf $voip_server_path/node_modules.tar.gz -C $voip_server_path
  191. ;;
  192. 3)
  193. read -p "Enter the URL of the project: " url
  194. git clone $url $voip_server_path
  195. tar xf $voip_server_path/node_modules.tar.gz -C $voip_server_path
  196. ;;
  197. 4)
  198. exit 0
  199. ;;
  200. *)
  201. echo "Invalid choice"
  202. exit 1
  203. ;;
  204. esac
  205. else
  206. PS3="Please choose one of the following options:"
  207. options=("Pull the latest changes" "Edit env" "voip server state" "copy resources" "update scripts" "extract node_modules" "pack node_modules" "Delete the project" "Exit")
  208. select opt in "${options[@]}"
  209. do
  210. case $opt in
  211. "Pull the latest changes")
  212. cd $voip_server_path
  213. git pull
  214. break
  215. ;;
  216. "voip server state")
  217. voip_server_state
  218. break
  219. ;;
  220. "Edit env")
  221. edit_env
  222. break
  223. ;;
  224. "extract node_modules")
  225. tar xf $voip_server_path/node_modules.tar.gz -C $voip_server_path
  226. break
  227. ;;
  228. "pack node_modules")
  229. tar -czvf $voip_server_path/node_modules.tar.gz -C $voip_server_path
  230. break
  231. ;;
  232. "Delete the project")
  233. read -p "Are you sure you want to delete the project? Type CONFIRM to confirm: " confirm
  234. if [[ $confirm =~ ^[Cc][Oo][Nn][Ff][Ii][Rr][Mm]$ ]]; then
  235. rm -rf $voip_server_path
  236. else
  237. echo "Deletion cancelled"
  238. fi
  239. break
  240. ;;
  241. "copy resources")
  242. read -p "Are you sure you want to copy resources(all old resources rewriting!!!)? Type CONFIRM to confirm: " confirm
  243. if [[ $confirm =~ ^[Cc][Oo][Nn][Ff][Ii][Rr][Mm]$ ]]; then
  244. curl -X GET http://localhost:2030/api/managment/copy-resources
  245. else
  246. echo "copy resources cancelled"
  247. fi
  248. break
  249. ;;
  250. "update scripts")
  251. read -p "Are you sure you want to update scripts(Changes made cannot be reversed!!!)? Type CONFIRM to confirm: " confirm
  252. if [[ $confirm =~ ^[Cc][Oo][Nn][Ff][Ii][Rr][Mm]$ ]]; then
  253. #TODO: add backup option && option for change port
  254. curl -X GET http://localhost:2030/api/managment/update-scripts
  255. else
  256. echo "update scripts cancelled"
  257. fi
  258. break
  259. ;;
  260. "Exit")
  261. exit 0
  262. ;;
  263. *) echo "Invalid option $REPLY";;
  264. esac
  265. done
  266. fi
  267. read
  268. }
  269. function voip_server_state {
  270. # Check if pm2 is installed
  271. if ! command -v pm2 &> /dev/null; then
  272. echo "pm2 is not installed."
  273. echo "Do you want to setup pm2? (y/n)"
  274. read -r setup_pm2
  275. if [[ "$setup_pm2" =~ ^[Yy]$ ]]; then
  276. install_pm2
  277. echo "pm2 has been installed."
  278. else
  279. echo "pm2 is required to run the voip_server application. Please install pm2 and try again."
  280. exit 1
  281. fi
  282. fi
  283. # Check if voip_server is running in pm2
  284. if pm2 list | grep -q "voip_server"; then
  285. echo "voip_server is already running in pm2"
  286. echo "Do you want to restart voip_server? (y/n)"
  287. read -r restart_voip_server
  288. if [[ "$restart_voip_server" =~ ^[Yy]$ ]]; then
  289. pm2 restart voip_server
  290. echo "voip_server has been restarted in pm2"
  291. fi
  292. else
  293. cd "$voip_server_path"
  294. pm2 start server.js --name voip_server
  295. pm2 startup > /dev/null 2>&1
  296. pm2 save > /dev/null 2>&1
  297. echo "voip_server has been started in pm2"
  298. fi
  299. }
  300. function edit_env {
  301. # Check if .env file exists
  302. if [ -f $voip_server_path.env ]; then
  303. echo ".env file exists"
  304. else
  305. if [ -f $voip_server_path.envSample ]; then
  306. # Copy .envSample to .env
  307. cp $voip_server_path.envSample $voip_server_path.env
  308. else
  309. echo ".envSample file does not exist"
  310. exit 1
  311. fi
  312. fi
  313. source $voip_server_path.env
  314. sed -i 's/ *= */=/g' $voip_server_path.env
  315. declare -A env=()
  316. while true; do
  317. clear
  318. n=0
  319. select line in $(grep '^[[:alnum:]]' $voip_server_path.env); do # Use grep to filter out lines starting with # or empty
  320. if [ -n "$line" ]; then
  321. key=$(echo "$line" | cut -d '=' -f1)
  322. value=$(echo "$line" | cut -d '=' -f2)
  323. echo "Current $key: $value"
  324. if [[ $value == *"'"* ]] || [[ $value == *"\""* ]]; then
  325. read -p "Enter new $key or press enter to keep it: " new_value
  326. if [ -n "$new_value" ]; then
  327. if [[ $new_value != *"'"* ]] && [[ $new_value != *"\""* ]] ; then
  328. sed -i "s/^$key=.*/$key='$new_value'/" $voip_server_path.env
  329. else
  330. sed -i "s/^$key=.*/$key=$new_value/" $voip_server_path.env
  331. fi
  332. env[$key]=$new_value
  333. source $voip_server_path.env
  334. echo "Saved changes."
  335. else
  336. echo "Keeping old value."
  337. fi
  338. else
  339. read -p "Enter new $key or press enter to keep it: " new_value
  340. if [ -n "$new_value" ]; then
  341. sed -i "s/^$key=.*/$key=$new_value/" $voip_server_path.env
  342. env[$key]=$new_value
  343. source $voip_server_path.env
  344. echo "Saved changes."
  345. else
  346. echo "Keeping old value."
  347. fi
  348. fi
  349. break
  350. else
  351. echo "Exiting."
  352. break 2
  353. fi
  354. done
  355. clear
  356. done
  357. echo "Done."
  358. }
  359. setup_network() {
  360. find /etc/sysconfig/network-scripts/ -name "ifcfg-*" -type f -exec sed -i 's/ONBOOT=no/ONBOOT=yes/g' {} \;
  361. printf "${Green}network boot active\n${Color_Off}"
  362. }
  363. correct_timezone(){
  364. zic asia > /dev/null 2>&1
  365. zic -l Asia/Tehran > /dev/null 2>&1
  366. sudo timedatectl set-timezone Asia/Tehran
  367. printf "${Green}time zone has been corrected\n${Color_Off}"
  368. }
  369. ########################################################
  370. setup_new_project() {
  371. install_dependencies
  372. create_db
  373. }
  374. update_centos(){
  375. update
  376. }
  377. install_dependencies() {
  378. install_packages
  379. install_node
  380. install_pm2
  381. install_git_v2
  382. }
  383. create_db() {
  384. create_mysql_db
  385. }
  386. remove_setup_folder(){
  387. cd script_path
  388. cd ../
  389. rm -rf voip_server_initial_setup
  390. }
  391. update_script(){
  392. cd $script_path
  393. git pull
  394. }
  395. cd $script_path
  396. ########################################################
  397. printf "${Green}\n=========================START==============================\n${Color_Off}"
  398. while true; do
  399. printf "${Cyan}"
  400. echo "Menu:"
  401. echo "1) setup new"
  402. echo "2) update centos(need internet)"
  403. echo "3) setup dependencies"
  404. echo "4) create ebtekar db"
  405. echo "5) setup voip server"
  406. echo "6) set network boot active"
  407. echo "7) correct timezone"
  408. echo "8) install git"
  409. echo "9) check update"
  410. echo "0) Exit"
  411. read -p "Enter your choice: " choice
  412. tput cuu 8 && tput ed && printf "${Color_Off}\n"
  413. case $choice in
  414. 1) printf "${Cyan}setup new${Color_Off}\n" && setup_new_project ;;
  415. 2) printf "${Cyan}update centos(need internet)${Color_Off}\n" && update_centos ;;
  416. 3) printf "${Cyan}setup dependencies${Color_Off}\n" && install_dependencies ;;
  417. 4) printf "${Cyan}create ebtekar db${Color_Off}\n" && create_db ;;
  418. 5) printf "${Cyan}restore database${Color_Off}\n" && setup_voip_server ;;
  419. 6) printf "${Cyan}setup network${Color_Off}\n" && setup_network ;;
  420. 7) printf "${Cyan}correct timezone${Color_Off}\n" && correct_timezone ;;
  421. 8) printf "${Cyan}install git${Color_Off}\n" && install_git_v2 ;;
  422. 9) printf "${Cyan}check update${Color_Off}\n" && update_script ;;
  423. 0) break ;;
  424. *) echo "Invalid choice";;
  425. esac
  426. printf "\n"
  427. done
  428. printf "${Green}\n=========================DONE==============================\n${Color_Off}"