INSTALL.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #!/usr/bin/env bash
  2. #
  3. # JimV-C
  4. #
  5. # Copyright (C) 2017 JimV <james.iter.cn@gmail.com>
  6. #
  7. # Author: James Iter <james.iter.cn@gmail.com>
  8. #
  9. # This script will help you to automation install JimV-C.
  10. #
  11. # Example:
  12. # Pass arguments to the installer.
  13. # curl https://raw.githubusercontent.com/jamesiter/JimV-C/master/INSTALL.sh | bash -s -- --version dev
  14. export PYPI='https://mirrors.aliyun.com/pypi/simple/'
  15. export JIMVC_REPOSITORY_URL='https://github.com/jamesiter/JimV-C.git'
  16. export JIMVC_REPOSITORY_URL_CN='https://gitee.com/jimit/JimV-C.git'
  17. export JIMVC_REPOSITORY_RAW_URL='https://raw.githubusercontent.com/jamesiter/JimV-C'
  18. export COUNTRY=`curl http://iit.im/ip/country`
  19. export GENERATE_PASSWORD_SCRIPT_TMP_PATH='/tmp/gen_pswd.sh'
  20. export SMTP_HOST=''
  21. export SMTP_USER=''
  22. export SMTP_PASSWORD=''
  23. if [ ${COUNTRY} = 'CN' ]; then
  24. export JIMVC_REPOSITORY_URL=${JIMVC_REPOSITORY_URL_CN}
  25. fi
  26. ARGS=`getopt -o h --long rdb_root_password:,rdb_jimv_password:,redis_password:,jwt_secret:,secret_key:,version:,help -n 'INSTALL.sh' -- "$@"`
  27. eval set -- "${ARGS}"
  28. while true
  29. do
  30. case "$1" in
  31. --rdb_root_password)
  32. export RDB_ROOT_PSWD=$2
  33. shift 2
  34. ;;
  35. --rdb_jimv_password)
  36. export RDB_JIMV_PSWD=$2
  37. shift 2
  38. ;;
  39. --redis_password)
  40. export REDIS_PSWD=$2
  41. shift 2
  42. ;;
  43. --jwt_secret)
  44. export JWT_SECRET=$2
  45. shift 2
  46. ;;
  47. --secret_key)
  48. export SECRET_KEY=$2
  49. shift 2
  50. ;;
  51. --version)
  52. export JIMV_VERSION=$2
  53. shift 2
  54. ;;
  55. -h|--help)
  56. echo 'INSTALL.sh [-h|--help|--rdb_root_password|--rdb_jimv_password|--redis_password|--jwt_secret|--secret_key|--version]'
  57. exit 0
  58. ;;
  59. --)
  60. shift
  61. break
  62. ;;
  63. *)
  64. echo "Internal error!"
  65. exit 1
  66. ;;
  67. esac
  68. done
  69. function check_precondition() {
  70. source /etc/os-release
  71. case ${ID} in
  72. centos|fedora|rhel)
  73. if [ ${VERSION_ID} -lt 7 ]; then
  74. echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
  75. exit 1
  76. fi
  77. ;;
  78. *)
  79. echo "${ID} is unknown, Please to be installed manually."
  80. exit 1
  81. ;;
  82. esac
  83. }
  84. function ensure_hosts_layout() {
  85. echo "______________________________________________________________________________"
  86. /bin/cat /etc/hosts
  87. echo
  88. read -p "您已经布局好 /etc/hosts 了吗 [Y/N]? " answer
  89. if [ "x_"${answer} != "x_Y" ] && [ "x_"${answer} != "x_y" ]; then
  90. echo "OK, good bye!";
  91. exit 0
  92. fi
  93. }
  94. function sync_ssh_key_pair() {
  95. sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config
  96. rm -f ~/.ssh/id_rsa ~/.ssh/id_rsa.pub; echo -e "\n\n\n" | ssh-keygen -N ""
  97. echo >> ~/.ssh/authorized_keys
  98. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  99. chmod 0600 ~/.ssh/authorized_keys
  100. DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
  101. for dhost in ${DHOSTS}
  102. do
  103. scp -r ~/.ssh ${dhost}:~/
  104. # 关闭 SSH 服务器端 Key 校验
  105. ssh ${dhost} "sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config"
  106. done
  107. }
  108. function sync_hosts_file() {
  109. DHOSTS=`egrep -v '^$|localhost|jimvc' /etc/hosts | awk '{ print $1; }'`
  110. for dhost in ${DHOSTS}
  111. do
  112. scp /etc/hosts ${dhost}:/etc/hosts
  113. done
  114. }
  115. function prepare() {
  116. if [ ! ${JIMV_VERSION} ] || [ ${#JIMV_VERSION} -eq 0 ]; then
  117. export JIMV_VERSION='master'
  118. fi
  119. if [ ! -e ${GENERATE_PASSWORD_SCRIPT_TMP_PATH} ]; then
  120. curl ${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/gen_pswd.sh' -o ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  121. chmod +x ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  122. fi
  123. if [ ! ${RDB_ROOT_PSWD} ] || [ ${#RDB_ROOT_PSWD} -eq 0 ]; then
  124. export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
  125. fi
  126. if [ ! ${RDB_JIMV_PSWD} ] || [ ${#RDB_JIMV_PSWD} -eq 0 ]; then
  127. export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
  128. fi
  129. if [ ! ${REDIS_PSWD} ] || [ ${#REDIS_PSWD} -eq 0 ]; then
  130. export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  131. fi
  132. if [ ! ${JWT_SECRET} ] || [ ${#JWT_SECRET} -eq 0 ]; then
  133. export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  134. fi
  135. if [ ! ${SECRET_KEY} ] || [ ${#SECRET_KEY} -eq 0 ]; then
  136. export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
  137. fi
  138. rm -f ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
  139. yum install epel-release -y
  140. yum install python2-pip git psmisc -y
  141. pip install --upgrade pip -i ${PYPI}
  142. pip install virtualenv -i ${PYPI}
  143. }
  144. function set_ntp() {
  145. yum install ntp -y
  146. systemctl start ntpd
  147. systemctl enable ntpd
  148. timedatectl set-timezone Asia/Shanghai
  149. timedatectl set-ntp true
  150. timedatectl status
  151. }
  152. function clear_up_environment() {
  153. systemctl stop firewalld
  154. systemctl disable firewalld
  155. systemctl stop NetworkManager
  156. systemctl disable NetworkManager
  157. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
  158. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
  159. setenforce 0
  160. }
  161. function install_MariaDB() {
  162. # 安装 MariaDB
  163. yum install mariadb mariadb-server -y
  164. # 配置 MariaDB
  165. mkdir -p /etc/systemd/system/mariadb.service.d
  166. echo '[Service]' > /etc/systemd/system/mariadb.service.d/limits.conf
  167. echo 'LimitNOFILE=65535' >> /etc/systemd/system/mariadb.service.d/limits.conf
  168. systemctl --system daemon-reload
  169. sed -i '/\[mysqld\]/a\bind-address = 127.0.0.1' /etc/my.cnf
  170. sed -i '/\[mysqld\]/a\log-bin' /etc/my.cnf
  171. sed -i '/\[mysqld\]/a\expire_logs_days = 1' /etc/my.cnf
  172. sed -i '/\[mysqld\]/a\innodb_file_per_table' /etc/my.cnf
  173. sed -i '/\[mysqld\]/a\max_connections = 10000' /etc/my.cnf
  174. # 启动并使其随机启动
  175. systemctl enable mariadb.service
  176. systemctl start mariadb.service
  177. # 初始化 MariaDB
  178. mysql_secure_installation << EOF
  179. Y
  180. ${RDB_ROOT_PSWD}
  181. ${RDB_ROOT_PSWD}
  182. Y
  183. Y
  184. Y
  185. Y
  186. EOF
  187. # 测试是否部署成功
  188. mysql -u root -p${RDB_ROOT_PSWD} -e 'show databases'
  189. }
  190. function install_Redis() {
  191. # 安装 Redis
  192. yum install redis -y
  193. # 配置 Redis
  194. echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
  195. sysctl -p
  196. sed -i 's@^daemonize no@daemonize yes@g' /etc/redis.conf
  197. sed -i 's@^bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
  198. sed -i 's@^appendonly no@appendonly yes@g' /etc/redis.conf
  199. echo "requirepass ${REDIS_PSWD}" >> /etc/redis.conf
  200. # 启动并使其随机启动
  201. systemctl enable redis.service
  202. systemctl start redis.service
  203. }
  204. function install_Nginx() {
  205. export NGINX_JIMV_URL=${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/nginx_jimv.conf'
  206. # 安装 Nginx
  207. yum install nginx -y
  208. # 配置 Nginx
  209. mkdir -p /etc/jimv/keys
  210. chown -R www.www /var/lib/nginx
  211. sed -i 's@user nginx.*$@user www;@' /etc/nginx/nginx.conf
  212. sed -i '/^.*server {/,$d' /etc/nginx/nginx.conf
  213. curl ${NGINX_JIMV_URL} >> /etc/nginx/nginx.conf
  214. # 启动并使其随机启动
  215. systemctl enable nginx.service
  216. systemctl start nginx.service
  217. }
  218. function create_web_user() {
  219. useradd -m www
  220. }
  221. function create_web_sites_directory() {
  222. su - www -c "mkdir ~/sites"
  223. }
  224. function clone_and_checkout_JimVC() {
  225. su - www -c "git clone ${JIMVC_REPOSITORY_URL} ~/sites/JimV-C"
  226. su - www -c "cd ~/sites/JimV-C && git checkout ${JIMV_VERSION}"
  227. }
  228. function install_dependencies_library() {
  229. # 创建 python 虚拟环境
  230. su - www -c "virtualenv --system-site-packages ~/venv"
  231. # 导入 python 虚拟环境
  232. su - www -c "source ~/venv/bin/activate"
  233. # 使切入 www 用户时自动导入 python 虚拟环境
  234. su - www -c "echo '. ~/venv/bin/activate' >> .bashrc"
  235. # 安装 JimV-C 所需扩展库
  236. su - www -c "pip install -r ~/sites/JimV-C/requirements.txt -i ${PYPI}"
  237. }
  238. function fit_www_user_permission() {
  239. mkdir -p /var/log/jimv
  240. chown www.www /var/log/jimv
  241. mkdir -p /run/jimv
  242. chown www.www /run/jimv
  243. }
  244. function initialization_db() {
  245. # 建立 JimV 数据库专属用户
  246. mysql -u root -p${RDB_ROOT_PSWD} -e "grant all on jimv.* to jimv@localhost identified by \"${RDB_JIMV_PSWD}\"; flush privileges"
  247. # 初始化数据库
  248. su - www -c "mysql -u jimv -p${RDB_JIMV_PSWD} < ~/sites/JimV-C/misc/init.sql"
  249. # 确认是否初始化成功
  250. mysql -u jimv -p${RDB_JIMV_PSWD} -e 'show databases'
  251. }
  252. function generate_config_file() {
  253. cp -v /home/www/sites/JimV-C/jimvc.conf /etc/jimvc.conf
  254. sed -i "s/\"db_password\".*$/\"db_password\": \"${RDB_JIMV_PSWD}\",/" /etc/jimvc.conf
  255. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvc.conf
  256. sed -i "s/\"jwt_secret\".*$/\"jwt_secret\": \"${JWT_SECRET}\",/" /etc/jimvc.conf
  257. sed -i "s/\"SECRET_KEY\".*$/\"SECRET_KEY\": \"${SECRET_KEY}\",/" /etc/jimvc.conf
  258. sed -i "s/\"smtp_host\".*$/\"smtp_host\": \"${SMTP_HOST}\",/" /etc/jimvc.conf
  259. sed -i "s/\"smtp_user\".*$/\"smtp_user\": \"${SMTP_USER}\",/" /etc/jimvc.conf
  260. sed -i "s/\"smtp_password\".*$/\"smtp_password\": \"${SMTP_PASSWORD}\",/" /etc/jimvc.conf
  261. }
  262. function generate_pid_directory_ad_reboot() {
  263. chmod +x /etc/rc.d/rc.local
  264. echo '' >> /etc/rc.d/rc.local
  265. echo 'mkdir /run/jimv' >> /etc/rc.d/rc.local
  266. echo 'chown www.www /run/jimv' >> /etc/rc.d/rc.local
  267. }
  268. function display_summary_information() {
  269. echo
  270. echo "=== 信息汇总"
  271. echo "MariaDB root 密码: [${RDB_ROOT_PSWD}]"
  272. echo "MariaDB jimv 密码: [${RDB_JIMV_PSWD}]"
  273. echo "Redis 端口: [6379]"
  274. echo "Redis 密码: [${REDIS_PSWD}]"
  275. echo "======================="
  276. echo
  277. echo "记录下上面信息,安装 JimV-N 时需要用到。"
  278. echo "现在可以通过命令 '/home/www/sites/JimV-C/startup.sh' 启动 JimV-C。"
  279. echo "======================="
  280. echo
  281. echo "通过 Web 页面完成 JimV-C 的初始化操作。然后到 [计算节点] 执行如下命令,进行 JimV-N 的部署。"
  282. echo "curl https://raw.githubusercontent.com/jamesiter/JimV-N/master/INSTALL.sh | bash -s -- --redis_host `hostname -I` --redis_password ${REDIS_PSWD} --redis_port 6379"
  283. }
  284. function deploy() {
  285. check_precondition
  286. clear_up_environment
  287. ensure_hosts_layout
  288. sync_ssh_key_pair
  289. sync_hosts_file
  290. prepare
  291. set_ntp
  292. create_web_user
  293. create_web_sites_directory
  294. clone_and_checkout_JimVC
  295. fit_www_user_permission
  296. install_MariaDB
  297. install_Redis
  298. install_Nginx
  299. install_dependencies_library
  300. initialization_db
  301. generate_pid_directory_ad_reboot
  302. generate_config_file
  303. display_summary_information
  304. }
  305. deploy